Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use OTel AgentDetector instead of just system property - review comments from earlier PR #8374

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading