Skip to content

Commit 8c061a2

Browse files
authored
Suppress Helidon handling of OpenTelemetry annotations if OTel Java agent is present (helidon-io#8360)
* Suppress Helidon handling of OpenTelemetry annotations if OTel Java agent is present Signed-off-by: Tim Quinn <tim.quinn@oracle.com> --------- Signed-off-by: Tim Quinn <tim.quinn@oracle.com>
1 parent 4fa36d9 commit 8c061a2

6 files changed

Lines changed: 31 additions & 6 deletions

File tree

microprofile/telemetry/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140
<configuration>
141141
<forkCount>1</forkCount>
142142
<reuseForks>false</reuseForks>
143+
<!-- The default for the following is 5000 ms. Reducing it significantly speeds up tests. -->
144+
<argLine>-Dotel.bsp.schedule.delay=100</argLine>
143145
</configuration>
144146
</plugin>
145147
</plugins>

microprofile/telemetry/src/main/java/io/helidon/microprofile/telemetry/HelidonTelemetryContainerFilter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public Iterable<String> keys(ContainerRequestContext containerRequestContext) {
8383

8484
private final Tracer tracer;
8585
private final OpenTelemetry openTelemetry;
86+
private final boolean isAgentPresent;
8687

8788

8889
@jakarta.ws.rs.core.Context
@@ -92,13 +93,18 @@ public Iterable<String> keys(ContainerRequestContext containerRequestContext) {
9293
HelidonTelemetryContainerFilter(Tracer tracer, OpenTelemetry openTelemetry, org.eclipse.microprofile.config.Config mpConfig) {
9394
this.tracer = tracer;
9495
this.openTelemetry = openTelemetry;
96+
isAgentPresent = Boolean.getBoolean(TelemetryCdiExtension.OTEL_AGENT_PRESENT);
9597

9698
mpConfig.getOptionalValue(SPAN_NAME_FULL_URL, Boolean.class).ifPresent(e -> spanNameFullUrl = e);
9799
}
98100

99101
@Override
100102
public void filter(ContainerRequestContext requestContext) {
101103

104+
if (isAgentPresent) {
105+
return;
106+
}
107+
102108
if (LOGGER.isLoggable(System.Logger.Level.TRACE)) {
103109
LOGGER.log(System.Logger.Level.TRACE, "Starting Span in a Container Request");
104110
}
@@ -132,6 +138,10 @@ public void filter(ContainerRequestContext requestContext) {
132138
@Override
133139
public void filter(final ContainerRequestContext request, final ContainerResponseContext response) {
134140

141+
if (isAgentPresent) {
142+
return;
143+
}
144+
135145
if (LOGGER.isLoggable(System.Logger.Level.TRACE)) {
136146
LOGGER.log(System.Logger.Level.TRACE, "Closing Span in a Container Request");
137147
}

microprofile/telemetry/src/main/java/io/helidon/microprofile/telemetry/TelemetryAutoDiscoverable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates.
2+
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

microprofile/telemetry/src/main/java/io/helidon/microprofile/telemetry/TelemetryCdiExtension.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@
2828
*/
2929
public class TelemetryCdiExtension implements Extension {
3030

31+
/**
32+
* Property name indicating the presence of the OpenTelemetry Java agent.
33+
*/
34+
static final String OTEL_AGENT_PRESENT = "otel.agent.present";
35+
3136
private static final System.Logger LOGGER = System.getLogger(TelemetryCdiExtension.class.getName());
3237

38+
private boolean isAgentPresent;
39+
3340
/**
3441
* Add {@code HelidonWithSpan} annotation with interceptor.
3542
*
@@ -38,6 +45,8 @@ public class TelemetryCdiExtension implements Extension {
3845
void before(@Observes BeforeBeanDiscovery discovery) {
3946
LOGGER.log(System.Logger.Level.TRACE, () -> "Before Telemetry bean discovery " + discovery);
4047

48+
isAgentPresent = Boolean.getBoolean(OTEL_AGENT_PRESENT);
49+
4150
// Register annotations, interceptors and producers.
4251
discovery.addAnnotatedType(HelidonWithSpan.class, HelidonWithSpan.class.getName());
4352
discovery.addAnnotatedType(WithSpanInterceptor.class, WithSpanInterceptor.class.getName());
@@ -61,6 +70,5 @@ void processAnnotations(@Observes @WithAnnotations(WithSpan.class) ProcessAnnota
6170
method.add(HelidonWithSpan.Literal.INSTANCE);
6271
}
6372
}
64-
6573
}
6674
}

microprofile/telemetry/src/main/java/io/helidon/microprofile/telemetry/WithSpanInterceptor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ class WithSpanInterceptor {
4343
private static final System.Logger LOGGER = System.getLogger(WithSpanInterceptor.class.getName());
4444

4545
private final Tracer tracer;
46+
private final boolean isAgentPresent;
4647

4748
@Inject
4849
WithSpanInterceptor(Tracer tracer) {
4950
this.tracer = tracer;
51+
isAgentPresent = Boolean.getBoolean(TelemetryCdiExtension.OTEL_AGENT_PRESENT);
5052
}
5153

5254
/**
@@ -59,6 +61,10 @@ class WithSpanInterceptor {
5961
@AroundInvoke
6062
public Object interceptSpan(InvocationContext context) throws Exception {
6163

64+
if (isAgentPresent) {
65+
return context.proceed();
66+
}
67+
6268
if (LOGGER.isLoggable(System.Logger.Level.TRACE)) {
6369
LOGGER.log(System.Logger.Level.TRACE, "Starting new Span on WithSpan annotated method");
6470
}

microprofile/telemetry/src/test/java/io/helidon/microprofile/telemetry/AgentDetectorTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates.
2+
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,19 +36,18 @@
3636
@AddExtension(ServerCdiExtension.class)
3737
class AgentDetectorTest {
3838

39-
public static final String OTEL_AGENT_PRESENT = "otel.agent.present";
4039
public static final String IO_OPENTELEMETRY_JAVAAGENT = "io.opentelemetry.javaagent";
4140

4241
@Test
43-
@AddConfig(key = OTEL_AGENT_PRESENT, value = "true")
42+
@AddConfig(key = TelemetryCdiExtension.OTEL_AGENT_PRESENT, value = "true")
4443
void shouldBeNoOpTelemetry(){
4544
Config config = CDI.current().select(Config.class).get();
4645
boolean present = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config);
4746
assertThat(present, is(true));
4847
}
4948

5049
@Test
51-
@AddConfig(key = OTEL_AGENT_PRESENT, value = "false")
50+
@AddConfig(key = TelemetryCdiExtension.OTEL_AGENT_PRESENT, value = "false")
5251
void shouldNotBeNoOpTelemetry(){
5352
Config config = CDI.current().select(Config.class).get();
5453
boolean present = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config);

0 commit comments

Comments
 (0)