Skip to content

Commit

Permalink
Use OTel AgentDetector instead of just system property - review comme…
Browse files Browse the repository at this point in the history
…nts from earlier PR (helidon-io#8374)

* Review comments from earlier PR

* One more place to use the AgentDetector

* remove duplicate constant declarations
  • Loading branch information
tjquinno authored Feb 13, 2024
1 parent 509ec52 commit 9e8b2b4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Objects;

import io.helidon.common.context.Contexts;
import io.helidon.config.mp.MpConfig;
import io.helidon.tracing.providers.opentelemetry.HelidonOpenTelemetry;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.baggage.Baggage;
Expand Down Expand Up @@ -93,7 +95,7 @@ public Iterable<String> keys(ContainerRequestContext containerRequestContext) {
HelidonTelemetryContainerFilter(Tracer tracer, OpenTelemetry openTelemetry, org.eclipse.microprofile.config.Config mpConfig) {
this.tracer = tracer;
this.openTelemetry = openTelemetry;
isAgentPresent = Boolean.getBoolean(TelemetryCdiExtension.OTEL_AGENT_PRESENT);
isAgentPresent = HelidonOpenTelemetry.AgentDetector.isAgentPresent(MpConfig.toHelidonConfig(mpConfig));

mpConfig.getOptionalValue(SPAN_NAME_FULL_URL, Boolean.class).ifPresent(e -> spanNameFullUrl = e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,8 @@
*/
public class TelemetryCdiExtension implements Extension {

/**
* Property name indicating the presence of the OpenTelemetry Java agent.
*/
static final String OTEL_AGENT_PRESENT = "otel.agent.present";

private static final System.Logger LOGGER = System.getLogger(TelemetryCdiExtension.class.getName());

private boolean isAgentPresent;

/**
* Add {@code HelidonWithSpan} annotation with interceptor.
*
Expand All @@ -45,8 +38,6 @@ public class TelemetryCdiExtension implements Extension {
void before(@Observes BeforeBeanDiscovery discovery) {
LOGGER.log(System.Logger.Level.TRACE, () -> "Before Telemetry bean discovery " + discovery);

isAgentPresent = Boolean.getBoolean(OTEL_AGENT_PRESENT);

// Register annotations, interceptors and producers.
discovery.addAnnotatedType(HelidonWithSpan.class, HelidonWithSpan.class.getName());
discovery.addAnnotatedType(WithSpanInterceptor.class, WithSpanInterceptor.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

import io.helidon.config.Config;
import io.helidon.tracing.providers.opentelemetry.HelidonOpenTelemetry;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.Tracer;
Expand Down Expand Up @@ -46,9 +49,9 @@ class WithSpanInterceptor {
private final boolean isAgentPresent;

@Inject
WithSpanInterceptor(Tracer tracer) {
WithSpanInterceptor(Tracer tracer, Config config) {
this.tracer = tracer;
isAgentPresent = Boolean.getBoolean(TelemetryCdiExtension.OTEL_AGENT_PRESENT);
isAgentPresent = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@
@AddExtension(ServerCdiExtension.class)
class AgentDetectorTest {

public static final String IO_OPENTELEMETRY_JAVAAGENT = "io.opentelemetry.javaagent";

@Test
@AddConfig(key = TelemetryCdiExtension.OTEL_AGENT_PRESENT, value = "true")
@AddConfig(key = HelidonOpenTelemetry.OTEL_AGENT_PRESENT_PROPERTY, value = "true")
void shouldBeNoOpTelemetry(){
Config config = CDI.current().select(Config.class).get();
boolean present = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config);
assertThat(present, is(true));
}

@Test
@AddConfig(key = TelemetryCdiExtension.OTEL_AGENT_PRESENT, value = "false")
@AddConfig(key = HelidonOpenTelemetry.OTEL_AGENT_PRESENT_PROPERTY, value = "false")
void shouldNotBeNoOpTelemetry(){
Config config = CDI.current().select(Config.class).get();
boolean present = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config);
Expand All @@ -56,7 +54,7 @@ void shouldNotBeNoOpTelemetry(){

@Test
void checkEnvVariable(){
System.setProperty(IO_OPENTELEMETRY_JAVAAGENT, "true");
System.setProperty(HelidonOpenTelemetry.IO_OPENTELEMETRY_JAVAAGENT, "true");
Config config = CDI.current().select(Config.class).get();
boolean present = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config);
assertThat(present, is(true));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,9 +32,17 @@
*/
public final class HelidonOpenTelemetry {

/**
* OpenTelemetry property for indicating if the Java agent is present.
*/
public static final String OTEL_AGENT_PRESENT_PROPERTY = "otel.agent.present";

/**
* OpenTelemetry property for the Java agent.
*/
public static final String IO_OPENTELEMETRY_JAVAAGENT = "io.opentelemetry.javaagent";

private static final System.Logger LOGGER = System.getLogger(HelidonOpenTelemetry.class.getName());
private static final String OTEL_AGENT_PRESENT_PROPERTY = "otel.agent.present";
private static final String IO_OPENTELEMETRY_JAVAAGENT = "io.opentelemetry.javaagent";
private HelidonOpenTelemetry() {
}
/**
Expand Down

0 comments on commit 9e8b2b4

Please sign in to comment.