Skip to content

Commit

Permalink
Merge pull request #17 from jenkinsci/support-otel-config-param-from-…
Browse files Browse the repository at this point in the history
…env-vars-and-sys-props

Support OpenTelemetry configuration properties passed through environment variables and system properties
  • Loading branch information
cyrille-leclerc authored Sep 5, 2024
2 parents 4442e9c + 8a738b7 commit 1e02bb4
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
import hudson.ExtensionPoint;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import io.jenkins.plugins.opentelemetry.api.logs.TestLogRecordData;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.incubator.events.EventLogger;
import io.opentelemetry.api.incubator.events.EventLoggerBuilder;
import io.opentelemetry.api.incubator.events.EventLoggerProvider;
import io.opentelemetry.api.incubator.events.GlobalEventLoggerProvider;
import io.opentelemetry.api.logs.LoggerProvider;
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.MeterBuilder;
import io.opentelemetry.api.metrics.MeterProvider;
Expand All @@ -32,7 +30,6 @@
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.logs.internal.SdkEventLoggerProvider;
Expand All @@ -41,7 +38,6 @@
import javax.annotation.OverridingMethodsMustInvokeSuper;
import javax.annotation.PreDestroy;
import java.io.Closeable;
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Expand Down Expand Up @@ -102,7 +98,7 @@ public static void init() {
* instantiation across Jenkins' @{@link Extension} and Google Guice @{@link com.google.inject.Inject}.
* </p>
* <p>
* This {@link #get()} factory method works in conjunction with {@link OpenTelemetryApiGuiceModule}
* This factory method works in conjunction with {@link OpenTelemetryApiGuiceModule}
* </p>
*/
@Extension(ordinal = Integer.MAX_VALUE)
Expand All @@ -116,6 +112,7 @@ public static ReconfigurableOpenTelemetry get() {
* <p>
* Initialize as NoOp.
* </p>
*
* @see #get()
*/
public ReconfigurableOpenTelemetry() {
Expand All @@ -137,6 +134,7 @@ public ReconfigurableOpenTelemetry() {

/**
* Configure the OpenTelemetry SDK with the given properties and resource disabling the OTel SDK shutdown hook
*
* @deprecated use {@link #configure(Map, Resource, boolean)} instead
*/
@Deprecated
Expand All @@ -159,7 +157,18 @@ public void configure(@NonNull Map<String, String> openTelemetryProperties, Reso
OpenTelemetrySdk openTelemetrySdk = AutoConfiguredOpenTelemetrySdk
.builder()
// properties
.addPropertiesSupplier(() -> openTelemetryProperties)
.addPropertiesCustomizer((Function<ConfigProperties, Map<String, String>>) configProperties -> {
// Overwrite OTel SDK Properties loaded through Environment variables and `-Dotel.*` system
// properties by properties passed through the Jenkins OTel Plugin config GUI
if (logger.isLoggable(Level.INFO)) {

Check warning on line 163 in src/main/java/io/jenkins/plugins/opentelemetry/api/ReconfigurableOpenTelemetry.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 163 is only partially covered, one branch is missing
for (Map.Entry<String, String> keyValue : openTelemetryProperties.entrySet()) {
if (configProperties.getString(keyValue.getKey()) != null) {
logger.log(Level.INFO, "Overwrite OTel SDK property: " + keyValue.getKey() + "=" + configProperties.getString(keyValue.getKey()) + " with Jenkins Plugin property: " + keyValue.getValue());
}
}
}
return openTelemetryProperties;
})
.addPropertiesCustomizer((Function<ConfigProperties, Map<String, String>>) configProperties -> {
// keep a reference to the computed config properties for future use in the plugin
this.config = configProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,102 @@

import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.ServiceAttributes;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

public class ReconfigurableOpenTelemetryTest {

static ReconfigurableOpenTelemetry reconfigurableOpenTelemetry;

@BeforeClass
public static void beforeClass() {
reconfigurableOpenTelemetry = ReconfigurableOpenTelemetry.get();
}

@Test
public void test_logRecordExporter() {

Map<String, String> otelConfig = new HashMap<>();
otelConfig.put("otel.exporter.otlp.endpoint", "http://localhost:4317");
Resource otelResource = Resource.builder().put("service.name", "ReconfigurableOpenTelemetry test").build();
reconfigurableOpenTelemetry.configure(otelConfig, otelResource, true);
LogRecordExporter logRecordExporter = reconfigurableOpenTelemetry.getLogRecordExporter();
System.out.println(logRecordExporter);
//reconfigurableOpenTelemetry.testLogRecordExporter();

otelConfig = new HashMap<>();
otelConfig.put("otel.exporter.otlp.endpoint", "http://localhost:4317/");
reconfigurableOpenTelemetry.configure(otelConfig, otelResource, true);
logRecordExporter = reconfigurableOpenTelemetry.getLogRecordExporter();
System.out.println(logRecordExporter);
}

@Test
public void test() {
try (ReconfigurableOpenTelemetry reconfigurableOpenTelemetry = new ReconfigurableOpenTelemetry()) {
public void test_configuration_through_system_properties() {
System.setProperty("otel.instrumentation.jdbc.enabled", "true");
System.setProperty("otel.service.name", "jenkins-456");
try {
Map<String, String> otelConfig = new HashMap<>();
otelConfig.put("otel.exporter.otlp.endpoint", "http://localhost:4317");
Resource otelResource = Resource.builder().put("service.name", "ReconfigurableOpenTelemetry test").build();
Resource otelResource = Resource.builder().build();
reconfigurableOpenTelemetry.configure(otelConfig, otelResource, true);
LogRecordExporter logRecordExporter = reconfigurableOpenTelemetry.getLogRecordExporter();
System.out.println(logRecordExporter);
//reconfigurableOpenTelemetry.testLogRecordExporter();

otelConfig = new HashMap<>();
otelConfig.put("otel.exporter.otlp.endpoint", "http://localhost:4317/");
// verify
assertEquals(Boolean.TRUE, reconfigurableOpenTelemetry.getConfig().getBoolean("otel.instrumentation.jdbc.enabled"));
assertEquals("jenkins-456", reconfigurableOpenTelemetry.getResource().getAttribute(ServiceAttributes.SERVICE_NAME));
} finally {
System.clearProperty("otel.instrumentation.jdbc.enabled");
}
}

@Test
public void test_configuration_through_passed_properties() {

try {
Map<String, String> otelConfig = new HashMap<>();
otelConfig.put("otel.exporter.otlp.endpoint", "http://localhost:4317");
otelConfig.put("otel.instrumentation.jdbc.enabled", "true");

Resource otelResource = Resource.builder().put(ServiceAttributes.SERVICE_NAME, "jenkins-123").build();
reconfigurableOpenTelemetry.configure(otelConfig, otelResource, true);
logRecordExporter = reconfigurableOpenTelemetry.getLogRecordExporter();
System.out.println(logRecordExporter);

// verify
assertEquals(Boolean.TRUE, reconfigurableOpenTelemetry.getConfig().getBoolean("otel.instrumentation.jdbc.enabled"));
assertEquals("jenkins-123", reconfigurableOpenTelemetry.getResource().getAttribute(ServiceAttributes.SERVICE_NAME));
} finally {
System.clearProperty("otel.instrumentation.jdbc.enabled");
}
}

@Test
public void test_configuration_through_passed_properties_overwrites() {
System.setProperty("otel.instrumentation.jdbc.enabled", "true");
System.setProperty("otel.service.name", "jenkins-456");
try {
Map<String, String> otelConfig = new HashMap<>();
otelConfig.put("otel.exporter.otlp.endpoint", "http://localhost:4317");
otelConfig.put("otel.instrumentation.jdbc.enabled", "false");

Resource otelResource = Resource.builder().put(ServiceAttributes.SERVICE_NAME, "jenkins-123").build();
reconfigurableOpenTelemetry.configure(otelConfig, otelResource, true);

// verify
assertEquals(Boolean.FALSE, reconfigurableOpenTelemetry.getConfig().getBoolean("otel.instrumentation.jdbc.enabled"));
assertEquals("jenkins-123", reconfigurableOpenTelemetry.getResource().getAttribute(ServiceAttributes.SERVICE_NAME));
} finally {
System.clearProperty("otel.instrumentation.jdbc.enabled");
}
}

@AfterClass
public static void afterClass() {
reconfigurableOpenTelemetry.close();
}
}

0 comments on commit 1e02bb4

Please sign in to comment.