Skip to content

Commit

Permalink
Better OTel SDK config on Jenkins build agents (#1010)
Browse files Browse the repository at this point in the history
* Better OTel SDK config on Jenkins build agents

* Better OTel SDK config

* Fix javadoc

* Simplify code

* Simplify code

* Update src/main/java/io/jenkins/plugins/opentelemetry/job/log/OtelLogStorageFactory.java

Co-authored-by: Ivan Fernandez Calvo <[email protected]>

---------

Co-authored-by: Ivan Fernandez Calvo <[email protected]>
  • Loading branch information
cyrille-leclerc and kuisathaverat authored Jan 9, 2025
1 parent 639ed87 commit 2103789
Show file tree
Hide file tree
Showing 24 changed files with 309 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import static io.jenkins.plugins.opentelemetry.semconv.ConfigurationKey.OTEL_LOGS_EXPORTER;
import static io.jenkins.plugins.opentelemetry.semconv.ConfigurationKey.OTEL_LOGS_MIRROR_TO_DISK;

/**
* {@link OpenTelemetry} instance intended to live on the Jenkins Controller.
Expand All @@ -35,8 +38,6 @@ public class JenkinsControllerOpenTelemetry implements ExtensionPoint {
*/
public static final String DEFAULT_OTEL_JAVA_DISABLED_RESOURCE_PROVIDERS = ProcessResourceProvider.class.getName();

public final static AtomicInteger INSTANCE_COUNTER = new AtomicInteger(0);

@VisibleForTesting
@Inject
protected ReconfigurableOpenTelemetry openTelemetry;
Expand Down Expand Up @@ -86,13 +87,13 @@ public EventLogger getDefaultEventLogger() {
}

public boolean isLogsEnabled() {
String otelLogsExporter = openTelemetry.getConfig().getString("otel.logs.exporter");
return otelLogsExporter != null && !otelLogsExporter.equals("none");
String otelLogsExporter = openTelemetry.getConfig().getString(OTEL_LOGS_EXPORTER.asProperty(), "none");
return !Objects.equals(otelLogsExporter, "none");
}

public boolean isOtelLogsMirrorToDisk() {
String otelLogsExporter = openTelemetry.getConfig().getString("otel.logs.mirror_to_disk");
return otelLogsExporter != null && otelLogsExporter.equals("true");
String mirrorLogsToDisk = openTelemetry.getConfig().getString(OTEL_LOGS_MIRROR_TO_DISK.asProperty(), "false");
return Objects.equals(mirrorLogsToDisk, "true");
}

@VisibleForTesting
Expand All @@ -105,7 +106,8 @@ protected OpenTelemetrySdk getOpenTelemetrySdk() {
public void initialize(@NonNull OpenTelemetryConfiguration configuration) {
openTelemetry.configure(
configuration.toOpenTelemetryProperties(),
configuration.toOpenTelemetryResource());
configuration.toOpenTelemetryResource(),
true);
}

static public JenkinsControllerOpenTelemetry get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
import io.jenkins.plugins.opentelemetry.backend.ObservabilityBackend;
import io.jenkins.plugins.opentelemetry.backend.custom.CustomLogStorageRetriever;
import io.jenkins.plugins.opentelemetry.job.log.LogStorageRetriever;
import io.jenkins.plugins.opentelemetry.opentelemetry.autoconfigure.ConfigPropertiesUtils;
import io.jenkins.plugins.opentelemetry.semconv.JenkinsOtelSemanticAttributes;
import io.jenkins.plugins.opentelemetry.semconv.OTelEnvironmentVariablesConventions;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.ServiceAttributes;
import io.opentelemetry.semconv.incubating.ServiceIncubatingAttributes;
Expand Down Expand Up @@ -79,6 +77,7 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static io.jenkins.plugins.opentelemetry.semconv.ConfigurationKey.*;
import static io.jenkins.plugins.opentelemetry.OtelUtils.UNKNOWN;
import static io.jenkins.plugins.opentelemetry.backend.ObservabilityBackend.ICONS_PREFIX;

Expand Down Expand Up @@ -208,6 +207,8 @@ public OpenTelemetryConfiguration toOpenTelemetryConfiguration() {
getObservabilityBackends().forEach(backend -> configurationProperties.putAll(backend.getOtelConfigurationProperties()));
configurationProperties.put(JenkinsOtelSemanticAttributes.JENKINS_VERSION.getKey(), OtelUtils.getJenkinsVersion());
configurationProperties.put(JenkinsOtelSemanticAttributes.JENKINS_URL.getKey(), this.jenkinsLocationConfiguration.getUrl());
configurationProperties.put(OTEL_INSTRUMENTATION_JENKINS_EXPORT_OTEL_CONFIG_AS_ENV_VARS.asProperty(), Boolean.toString(this.exportOtelConfigurationAsEnvironmentVariables));

// use same Jenkins instance identifier as the Jenkins Support Core plugin. No need to add the complexity of the instance-identity-plugin
// https://github.com/jenkinsci/support-core-plugin/blob/support-core-2.81/src/main/java/com/cloudbees/jenkins/support/impl/AboutJenkins.java#L401
configurationProperties.put(ServiceIncubatingAttributes.SERVICE_INSTANCE_ID.getKey(), Jenkins.get().getLegacyInstanceId());
Expand Down Expand Up @@ -386,19 +387,19 @@ public Map<String, String> getOtelConfigurationAsEnvironmentVariables() {
}

Map<String, String> environmentVariables = new HashMap<>();
environmentVariables.put(OTelEnvironmentVariablesConventions.OTEL_TRACES_EXPORTER, "otlp");
environmentVariables.put(OTelEnvironmentVariablesConventions.OTEL_EXPORTER_OTLP_ENDPOINT, this.endpoint);
environmentVariables.put(OTEL_TRACES_EXPORTER.asEnvVar(), "otlp");
environmentVariables.put(OTEL_EXPORTER_OTLP_ENDPOINT.asEnvVar(), this.endpoint);
String sanitizeOtlpEndpoint = sanitizeOtlpEndpoint(this.endpoint);
if (sanitizeOtlpEndpoint != null && sanitizeOtlpEndpoint.startsWith("http://")) {
environmentVariables.put(OTelEnvironmentVariablesConventions.OTEL_EXPORTER_OTLP_INSECURE, Boolean.TRUE.toString());
environmentVariables.put(OTEL_EXPORTER_OTLP_INSECURE.asEnvVar(), Boolean.TRUE.toString());
}
this.authentication.enrichOtelEnvironmentVariables(environmentVariables);
String trustedCertificatesPem = this.getTrustedCertificatesPem();
if (trustedCertificatesPem != null && !trustedCertificatesPem.isEmpty()) {
environmentVariables.put(OTelEnvironmentVariablesConventions.OTEL_EXPORTER_OTLP_CERTIFICATE, trustedCertificatesPem);
environmentVariables.put(OTEL_EXPORTER_OTLP_CERTIFICATE.asEnvVar(), trustedCertificatesPem);
}
if (this.exporterTimeoutMillis != null) {
environmentVariables.put(OTelEnvironmentVariablesConventions.OTEL_EXPORTER_OTLP_TIMEOUT, Integer.toString(this.exporterTimeoutMillis));
environmentVariables.put(OTEL_EXPORTER_OTLP_TIMEOUT.asEnvVar(), Integer.toString(this.exporterTimeoutMillis));
}
return environmentVariables;
}
Expand Down Expand Up @@ -546,7 +547,7 @@ public String getResourceAsText() {
@NonNull
public ConfigProperties getConfigProperties() {
if (this.openTelemetry == null) {
return ConfigPropertiesUtils.emptyConfig();
return DefaultConfigProperties.createFromMap(Collections.emptyMap());

Check warning on line 550 in src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 390-550 are not covered by tests
} else {
return this.openTelemetry.getConfig();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Optional;
import java.util.function.BiFunction;

import static io.jenkins.plugins.opentelemetry.semconv.ConfigurationKey.*;

public class OpenTelemetryConfiguration {

@SuppressFBWarnings
Expand Down Expand Up @@ -96,25 +98,25 @@ public Optional<String> getDisabledResourceProviders() {
*/
@NonNull
public Map<String, String> toOpenTelemetryProperties() {
Map<String, String> properties = new HashMap<>();
properties.putAll(this.configurationProperties);
Map<String, String> properties = new HashMap<>(this.configurationProperties);
if (TESTING_INMEMORY_MODE) {
properties.put("otel.traces.exporter", "testing");
properties.put("otel.metrics.exporter", "testing");
properties.put("otel.imr.export.interval", "10ms");
properties.put(OTEL_TRACES_EXPORTER.asProperty(), "testing");
properties.put(OTEL_METRICS_EXPORTER.asProperty(), "testing");
properties.put(OTEL_IMR_EXPORT_INTERVAL.asProperty(), "10ms");
} else if (this.getEndpoint().isPresent()) {
this.getEndpoint().ifPresent(endpoint -> { // prepare of Optional.ifPResentOrElse()
properties.compute("otel.traces.exporter", (key, oldValue) -> {
if (oldValue == null) {
return "otlp";
} else if ("none".equals(oldValue)) {
return "none";
} else if (oldValue.contains("otlp")) {
return oldValue;
} else {
return oldValue.concat(",otlp");
}});
properties.compute("otel.metrics.exporter", (key, oldValue) -> {
properties.compute(OTEL_TRACES_EXPORTER.asProperty(), (key, oldValue) -> {
if (oldValue == null) {
return "otlp";
} else if ("none".equals(oldValue)) {
return "none";
} else if (oldValue.contains("otlp")) {
return oldValue;
} else {
return oldValue.concat(",otlp");
}
});
properties.compute(OTEL_METRICS_EXPORTER.asProperty(), (key, oldValue) -> {
if (oldValue == null) {
return "otlp";
} else if ("none".equals(oldValue)) {
Expand All @@ -123,27 +125,28 @@ public Map<String, String> toOpenTelemetryProperties() {
return oldValue;
} else {
return oldValue.concat(",otlp");
}});
properties.put("otel.exporter.otlp.endpoint", endpoint);
}
});
properties.put(OTEL_EXPORTER_OTLP_ENDPOINT.asProperty(), endpoint);
});
} else if (StringUtils.isBlank(OtelUtils.getSystemPropertyOrEnvironmentVariable("OTEL_TRACES_EXPORTER")) &&
StringUtils.isBlank(OtelUtils.getSystemPropertyOrEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT")) &&
StringUtils.isBlank(OtelUtils.getSystemPropertyOrEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"))) {
// Change default of "otel.traces.exporter" from "otlp" to "none" unless "otel.exporter.otlp.endpoint" or "otel.exporter.otlp.traces.endpoint" is defined
properties.put("otel.traces.exporter", "none");
properties.put(OTEL_TRACES_EXPORTER.asProperty(), "none");
}

this.getTrustedCertificatesPem().ifPresent(
trustedCertificatesPem -> properties.put("otel.exporter.otlp.certificate", trustedCertificatesPem));
trustedCertificatesPem -> properties.put(OTEL_EXPORTER_OTLP_CERTIFICATE.asProperty(), trustedCertificatesPem));

Check warning on line 140 in src/main/java/io/jenkins/plugins/opentelemetry/OpenTelemetryConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 140 is not covered by tests

this.getAuthentication().ifPresent(authentication ->
authentication.enrichOpenTelemetryAutoConfigureConfigProperties(properties));

this.getExporterTimeoutMillis().map(Object::toString).ifPresent(exporterTimeoutMillis ->
properties.put("otel.exporter.otlp.timeout", exporterTimeoutMillis));
properties.put(OTEL_EXPORTER_OTLP_TIMEOUT.asProperty(), exporterTimeoutMillis));

this.getExporterIntervalMillis().map(Object::toString).ifPresent(exporterIntervalMillis ->
properties.put("otel.imr.export.interval", exporterIntervalMillis));
properties.put(OTEL_IMR_EXPORT_INTERVAL.asProperty(), exporterIntervalMillis));

Check warning on line 149 in src/main/java/io/jenkins/plugins/opentelemetry/OpenTelemetryConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 149 is not covered by tests

this.getDisabledResourceProviders().ifPresent(disabledResourceProviders ->
properties.put("otel.java.disabled.resource.providers", disabledResourceProviders));
Expand Down
Loading

0 comments on commit 2103789

Please sign in to comment.