Skip to content

Commit d48e1d6

Browse files
Added support for setting service name and version for a transaction via the public api (#2451)
1 parent 5ec1442 commit d48e1d6

File tree

25 files changed

+274
-50
lines changed

25 files changed

+274
-50
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ endif::[]
2525
2626
[float]
2727
===== Features
28+
* Added support for setting service name and version for a transaction via the public api - {pull}2451[#2451]
2829
2930
[[release-notes-1.x]]
3031
=== Java Agent version 1.x

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,21 @@ public static void captureException(@Nullable Throwable e) {
232232
// co.elastic.apm.api.ElasticApmInstrumentation.CaptureExceptionInstrumentation.captureException
233233
}
234234

235+
/**
236+
* Associates a class loader with a service name and version.
237+
* <p>
238+
* The association is used to overwrite the autodetected service name and version when a transaction is started.
239+
* </p>
240+
* <p>
241+
* NOTE: If the class loader already is associated with a service name and version,
242+
* the existing information will not be overwritten.
243+
* </p>
244+
*
245+
* @param classLoader the class loader which should be associated with the given service name and version
246+
* @param serviceName the service name
247+
* @param serviceVersion the service version
248+
*/
249+
public static void setServiceInfoForClassLoader(@Nullable ClassLoader classLoader, @Nullable String serviceName, @Nullable String serviceVersion) {
250+
// co.elastic.apm.api.ElasticApmInstrumentation.SetServiceInfoForClassLoader.setServiceInfoForClassLoader
251+
}
235252
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ public Transaction setFrameworkName(String frameworkName) {
4848
return this;
4949
}
5050

51+
@Nonnull
52+
@Override
53+
public Transaction setServiceInfo(String serviceName, String serviceVersion) {
54+
// noop
55+
return this;
56+
}
57+
58+
@Nonnull
59+
@Override
60+
public Transaction useServiceInfoForClassLoader(ClassLoader classLoader) {
61+
// noop
62+
return this;
63+
}
64+
5165
@Nonnull
5266
@Deprecated
5367
@Override

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,33 @@ public interface Transaction extends Span {
6767
@Nonnull
6868
Transaction setFrameworkName(String frameworkName);
6969

70+
/**
71+
* Sets the service name and version for this transaction and its child spans.
72+
* <p>
73+
* NOTE: If this method is called after child spans are already created,
74+
* they may have the wrong service name and version.
75+
* </p>
76+
*
77+
* @param serviceName the service name
78+
* @param serviceVersion the service version
79+
*/
80+
@Nonnull
81+
Transaction setServiceInfo(String serviceName, String serviceVersion);
82+
83+
/**
84+
* Sets the service name and version, that are associated with the given class loader
85+
* (see: {@link ElasticApm#setServiceInfoForClassLoader(ClassLoader, String, String)}),
86+
* for this transaction and its child spans.
87+
* <p>
88+
* NOTE: If this method is called after child spans are already created,
89+
* they may have the wrong service name and version.
90+
* </p>
91+
*
92+
* @param classLoader the class loader that should be used to set the service name and version
93+
*/
94+
@Nonnull
95+
Transaction useServiceInfoForClassLoader(ClassLoader classLoader);
96+
7097
/**
7198
* {@inheritDoc}
7299
*

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ public Transaction setType(String type) {
5656
return this;
5757
}
5858

59+
@Nonnull
60+
@Override
61+
public Transaction setServiceInfo(String serviceName, String serviceVersion) {
62+
// co.elastic.apm.agent.pluginapi.TransactionInstrumentation$SetServiceInfoInstrumentation
63+
return this;
64+
}
65+
66+
@Nonnull
67+
@Override
68+
public Transaction useServiceInfoForClassLoader(ClassLoader classLoader) {
69+
// co.elastic.apm.agent.pluginapi.TransactionInstrumentation$UseServiceInfoForClassLoaderInstrumentation
70+
return this;
71+
}
72+
5973
@Nonnull
6074
@Deprecated
6175
@Override

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,9 @@ private void afterTransactionStart(@Nullable ClassLoader initiatingClassLoader,
231231
new RuntimeException("this exception is just used to record where the transaction has been started from"));
232232
}
233233
}
234-
final ServiceInfo serviceInfo = getServiceInfo(initiatingClassLoader);
234+
final ServiceInfo serviceInfo = getServiceInfoForClassLoader(initiatingClassLoader);
235235
if (serviceInfo != null) {
236-
transaction.getTraceContext().setServiceName(serviceInfo.getServiceName());
237-
transaction.getTraceContext().setServiceVersion(serviceInfo.getServiceVersion());
236+
transaction.getTraceContext().setServiceInfo(serviceInfo.getServiceName(), serviceInfo.getServiceVersion());
238237
}
239238
}
240239

@@ -344,10 +343,9 @@ private ErrorCapture captureException(long epochMicros, @Nullable Throwable e, @
344343
parent.setNonDiscardable();
345344
} else {
346345
error.getTraceContext().getId().setToRandomValue();
347-
ServiceInfo serviceInfo = getServiceInfo(initiatingClassLoader);
346+
ServiceInfo serviceInfo = getServiceInfoForClassLoader(initiatingClassLoader);
348347
if (serviceInfo != null) {
349-
error.getTraceContext().setServiceName(serviceInfo.getServiceName());
350-
error.getTraceContext().setServiceVersion(serviceInfo.getServiceVersion());
348+
error.getTraceContext().setServiceInfo(serviceInfo.getServiceName(), serviceInfo.getServiceVersion());
351349
}
352350
}
353351
return error;
@@ -750,7 +748,7 @@ public List<ServiceInfo> getServiceInfoOverrides() {
750748
}
751749

752750
@Override
753-
public void overrideServiceInfoForClassLoader(@Nullable ClassLoader classLoader, ServiceInfo serviceInfo) {
751+
public void setServiceInfoForClassLoader(@Nullable ClassLoader classLoader, ServiceInfo serviceInfo) {
754752
// overriding the service name/version for the bootstrap class loader is not an actual use-case
755753
// null may also mean we don't know about the initiating class loader
756754
if (classLoader == null
@@ -767,7 +765,8 @@ public void overrideServiceInfoForClassLoader(@Nullable ClassLoader classLoader,
767765
}
768766

769767
@Nullable
770-
public ServiceInfo getServiceInfo(@Nullable ClassLoader initiatingClassLoader) {
768+
@Override
769+
public ServiceInfo getServiceInfoForClassLoader(@Nullable ClassLoader initiatingClassLoader) {
771770
if (initiatingClassLoader == null) {
772771
return null;
773772
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,15 @@ public TracerState getState() {
204204
return tracer.getState();
205205
}
206206

207+
@Nullable
208+
@Override
209+
public ServiceInfo getServiceInfoForClassLoader(@Nullable ClassLoader classLoader) {
210+
return tracer.getServiceInfoForClassLoader(classLoader);
211+
}
212+
207213
@Override
208-
public void overrideServiceInfoForClassLoader(@Nullable ClassLoader classLoader, ServiceInfo serviceInfo) {
209-
tracer.overrideServiceInfoForClassLoader(classLoader, serviceInfo);
214+
public void setServiceInfoForClassLoader(@Nullable ClassLoader classLoader, ServiceInfo serviceInfo) {
215+
tracer.setServiceInfoForClassLoader(classLoader, serviceInfo);
210216
}
211217

212218
@Override

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,14 @@ public TracerState getState() {
130130
return TracerState.UNINITIALIZED;
131131
}
132132

133+
@Nullable
134+
@Override
135+
public ServiceInfo getServiceInfoForClassLoader(@Nullable ClassLoader classLoader) {
136+
return null;
137+
}
138+
133139
@Override
134-
public void overrideServiceInfoForClassLoader(@Nullable ClassLoader classLoader, ServiceInfo serviceInfo) {
140+
public void setServiceInfoForClassLoader(@Nullable ClassLoader classLoader, ServiceInfo serviceInfo) {
135141
}
136142

137143
@Override

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,11 @@ <C> Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGet
153153

154154
TracerState getState();
155155

156+
@Nullable
157+
ServiceInfo getServiceInfoForClassLoader(@Nullable ClassLoader classLoader);
158+
156159
/**
157-
* Overrides the service name and version for all {@link Transaction}s,
160+
* Sets the service name and version for all {@link Transaction}s,
158161
* {@link Span}s and {@link ErrorCapture}s which are created by the service which corresponds to the provided {@link ClassLoader}.
159162
* <p>
160163
* The main use case is being able to differentiate between multiple services deployed to the same application server.
@@ -163,7 +166,7 @@ <C> Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGet
163166
* @param classLoader the class loader which corresponds to a particular service
164167
* @param serviceInfo the service name and version for this class loader
165168
*/
166-
void overrideServiceInfoForClassLoader(@Nullable ClassLoader classLoader, ServiceInfo serviceInfo);
169+
void setServiceInfoForClassLoader(@Nullable ClassLoader classLoader, ServiceInfo serviceInfo);
167170

168171
/**
169172
* Called when the container shuts down.

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -682,28 +682,24 @@ public String getServiceName() {
682682
}
683683

684684
/**
685-
* Overrides the {@code co.elastic.apm.agent.impl.payload.Service#name} property sent via the meta data Intake V2 event.
685+
* Overrides the {@code co.elastic.apm.agent.impl.payload.Service#name} and {@code co.elastic.apm.agent.impl.payload.Service#version} properties sent via the meta data Intake V2 event.
686686
*
687-
* @param serviceName the service name for this event
687+
* @param serviceName the service name for this event
688+
* @param serviceVersion the service version for this event
688689
*/
689-
public void setServiceName(@Nullable String serviceName) {
690+
public void setServiceInfo(@Nullable String serviceName, @Nullable String serviceVersion) {
691+
if (serviceName == null || serviceName.isEmpty()) {
692+
return;
693+
}
690694
this.serviceName = serviceName;
695+
this.serviceVersion = serviceVersion;
691696
}
692697

693698
@Nullable
694699
public String getServiceVersion() {
695700
return serviceVersion;
696701
}
697702

698-
/**
699-
* Overrides the {@code co.elastic.apm.agent.impl.payload.Service#version} property sent via the meta data Intake V2 event.
700-
*
701-
* @param serviceVersion the service version for this event
702-
*/
703-
public void setServiceVersion(@Nullable String serviceVersion) {
704-
this.serviceVersion = serviceVersion;
705-
}
706-
707703
public Span createSpan() {
708704
return tracer.startSpan(fromParentContext(), this);
709705
}

0 commit comments

Comments
 (0)