Skip to content

Commit 125702d

Browse files
Added support for setting a user's domain (#1756)
1 parent 38c1c9b commit 125702d

File tree

12 files changed

+83
-13
lines changed

12 files changed

+83
-13
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Even when matching on the main class name or on system properties,
6262
* Cassandra instrumentation - {pull}1712[#1712]
6363
* Log correlation supports JBoss Logging - {pull}1737[#1737]
6464
* Update Byte-buddy to `1.11.0` - {pull}1769[#1769]
65+
* Support for user.domain {pull}1756[#1756]
6566
6667
[float]
6768
===== Bug fixes

apm-agent-api/src/main/java/co/elastic/apm/api/NoopTransaction.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ public Transaction setUser(String id, String email, String username) {
116116
return this;
117117
}
118118

119+
@Override
120+
public Transaction setUser(String id, String email, String username, String domain) {
121+
// noop
122+
return this;
123+
}
124+
119125
@Override
120126
public Transaction setResult(String result) {
121127
// noop

apm-agent-api/src/main/java/co/elastic/apm/api/Transaction.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,23 @@ public interface Transaction extends Span {
181181
*/
182182
Transaction setUser(String id, String email, String username);
183183

184+
/**
185+
* Call this to enrich collected performance data and errors with information about the user/client.
186+
* <p>
187+
* This method can be called at any point during the request/response life cycle (i.e. while a transaction is active).
188+
* The given context will be added to the active transaction.
189+
* </p>
190+
* <p>
191+
* If an error is captured, the context from the active transaction is used as context for the captured error.
192+
* </p>
193+
*
194+
* @param id The user's id or {@code null}, if not applicable.
195+
* @param email The user's email address or {@code null}, if not applicable.
196+
* @param username The user's name or {@code null}, if not applicable.
197+
* @param domain The user's domain or {@code null}, if not applicable.
198+
*/
199+
Transaction setUser(String id, String email, String username, String domain);
200+
184201
/**
185202
* A string describing the result of the transaction.
186203
* This is typically the HTTP status code, or e.g. "success" for a background task

apm-agent-api/src/main/java/co/elastic/apm/api/TransactionImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ public Transaction setUser(String id, String email, String username) {
134134
return this;
135135
}
136136

137+
@Override
138+
public Transaction setUser(String id, String email, String username, String domain) {
139+
// co.elastic.apm.agent.pluginapi.TransactionInstrumentation$SetUserInstrumentation.setUser
140+
return this;
141+
}
142+
137143
@Override
138144
public Transaction setResult(String result) {
139145
// co.elastic.apm.agent.pluginapi.TransactionInstrumentation.SetResultInstrumentation

apm-agent-core/src/main/java/co/elastic/apm/agent/impl/context/User.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* the Apache License, Version 2.0 (the "License"); you may
1212
* not use this file except in compliance with the License.
1313
* You may obtain a copy of the License at
14-
*
14+
*
1515
* http://www.apache.org/licenses/LICENSE-2.0
16-
*
16+
*
1717
* Unless required by applicable law or agreed to in writing,
1818
* software distributed under the License is distributed on an
1919
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -37,6 +37,12 @@
3737
*/
3838
public class User implements Recyclable {
3939

40+
/**
41+
* Domain of the logged in user
42+
*/
43+
@Nullable
44+
private String domain;
45+
4046
/**
4147
* Identifier of the logged in user, e.g. the primary key of the user
4248
*/
@@ -53,6 +59,23 @@ public class User implements Recyclable {
5359
@Nullable
5460
private String username;
5561

62+
63+
/**
64+
* Domain of the logged in user
65+
*/
66+
@Nullable
67+
public String getDomain() {
68+
return domain;
69+
}
70+
71+
/**
72+
* Domain of the logged in user
73+
*/
74+
public User withDomain(@Nullable String domain) {
75+
this.domain = domain;
76+
return this;
77+
}
78+
5679
/**
5780
* Identifier of the logged in user, e.g. the primary key of the user
5881
*/
@@ -103,18 +126,20 @@ public User withUsername(@Nullable String username) {
103126

104127
@Override
105128
public void resetState() {
129+
domain = null;
106130
id = null;
107131
email = null;
108132
username = null;
109133
}
110134

111135
public void copyFrom(User other) {
136+
this.domain = other.domain;
112137
this.email = other.email;
113138
this.id = other.id;
114139
this.username = other.username;
115140
}
116141

117142
public boolean hasContent() {
118-
return id != null || email != null || username != null;
143+
return domain != null || id != null || email != null || username != null;
119144
}
120145
}

apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/Transaction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ public Transaction withResult(@Nullable String result) {
207207
return this;
208208
}
209209

210-
public void setUser(String id, String email, String username) {
210+
public void setUser(String id, String email, String username, String domain) {
211211
if (!isSampled()) {
212212
return;
213213
}
214-
getContext().getUser().withId(id).withEmail(email).withUsername(username);
214+
getContext().getUser().withDomain(domain).withId(id).withEmail(email).withUsername(username);
215215
}
216216

217217
@Override

apm-agent-core/src/main/java/co/elastic/apm/agent/report/serialize/DslJsonSerializer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,7 @@ private void serializePotentiallyMultiValuedEntry(String key, @Nullable Object v
12311231
private void serializeUser(final User user) {
12321232
writeFieldName("user");
12331233
jw.writeByte(OBJECT_START);
1234+
writeField("domain", user.getDomain());
12341235
writeField("id", user.getId());
12351236
writeField("email", user.getEmail());
12361237
writeLastField("username", user.getUsername());

apm-agent-core/src/test/java/co/elastic/apm/agent/impl/ElasticApmTracerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ void testSamplingNone() throws IOException {
343343
Transaction transaction = startTestRootTransaction().withType("request");
344344

345345
try (Scope scope = transaction.activateInScope()) {
346-
transaction.setUser("1", "[email protected]", "jondoe");
346+
transaction.setUser("1", "[email protected]", "jondoe", "domain");
347347
Span span = tracerImpl.getActive().createSpan();
348348
try (Scope spanScope = span.activateInScope()) {
349349
span.end();

apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/TransactionInstrumentation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public SetUserInstrumentation() {
6666

6767
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
6868
public static void setUser(@Advice.FieldValue(value = "span", typing = Assigner.Typing.DYNAMIC) Object transaction,
69-
@Advice.Argument(0) String id, @Advice.Argument(1) String email, @Advice.Argument(2) String username) {
69+
@Advice.Argument(0) String id, @Advice.Argument(1) String email, @Advice.Argument(2) String username, @Advice.Argument(value = 3, optional = true) String domain) {
7070
if (transaction instanceof Transaction) {
71-
((Transaction) transaction).setUser(id, email, username);
71+
((Transaction) transaction).setUser(id, email, username, domain);
7272
}
7373
}
7474
}

apm-agent-plugins/apm-api-plugin/src/test/java/co/elastic/apm/agent/pluginapi/TransactionInstrumentationTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,19 @@ void testAddOrSetLabel() {
8989

9090
@Test
9191
void testSetUser() {
92+
transaction.setUser("foo", "bar", "baz", "abc");
93+
endTransaction();
94+
assertThat(reporter.getFirstTransaction().getContext().getUser().getDomain()).isEqualTo("abc");
95+
assertThat(reporter.getFirstTransaction().getContext().getUser().getId()).isEqualTo("foo");
96+
assertThat(reporter.getFirstTransaction().getContext().getUser().getEmail()).isEqualTo("bar");
97+
assertThat(reporter.getFirstTransaction().getContext().getUser().getUsername()).isEqualTo("baz");
98+
}
99+
100+
@Test
101+
void testSetUserWithoutDomain() {
92102
transaction.setUser("foo", "bar", "baz");
93103
endTransaction();
104+
assertThat(reporter.getFirstTransaction().getContext().getUser().getDomain()).isNull();
94105
assertThat(reporter.getFirstTransaction().getContext().getUser().getId()).isEqualTo("foo");
95106
assertThat(reporter.getFirstTransaction().getContext().getUser().getEmail()).isEqualTo("bar");
96107
assertThat(reporter.getFirstTransaction().getContext().getUser().getUsername()).isEqualTo("baz");
@@ -137,12 +148,13 @@ void testChaining() {
137148
.setLabel("stringKey", randomString)
138149
.setLabel("numberKey", randomInt)
139150
.setLabel("booleanKey", randomBoolean)
140-
.setUser("foo", "bar", "baz")
151+
.setUser("foo", "bar", "baz", "abc")
141152
.setResult("foo");
142153
endTransaction();
143154
assertThat(reporter.getFirstTransaction().getNameAsString()).isEqualTo("foo");
144155
assertThat(reporter.getFirstTransaction().getType()).isEqualTo("foo");
145156
assertThat(reporter.getFirstTransaction().getContext().getLabel("foo")).isEqualTo("bar");
157+
assertThat(reporter.getFirstTransaction().getContext().getUser().getDomain()).isEqualTo("abc");
146158
assertThat(reporter.getFirstTransaction().getContext().getUser().getId()).isEqualTo("foo");
147159
assertThat(reporter.getFirstTransaction().getContext().getUser().getEmail()).isEqualTo("bar");
148160
assertThat(reporter.getFirstTransaction().getContext().getUser().getUsername()).isEqualTo("baz");

0 commit comments

Comments
 (0)