From ce0847aff95b42362f0c310523f3467cc580bb58 Mon Sep 17 00:00:00 2001 From: Cyrille Le Clerc Date: Thu, 12 Aug 2021 11:50:10 +0200 Subject: [PATCH 1/4] Export Otel configuration in shell steps `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_HEADERS`... --- ...nkinsOpenTelemetryPluginConfiguration.java | 34 +++++++++ .../BearerTokenAuthentication.java | 7 ++ .../authentication/HeaderAuthentication.java | 6 ++ .../authentication/NoAuthentication.java | 5 ++ .../authentication/OtlpAuthentication.java | 16 +++++ .../job/EnvironmentContributorUtils.java | 35 ---------- .../job/OtelEnvironmentContributor.java | 9 ++- .../OtelEnvironmentContributorService.java | 70 +++++++++++++++++++ .../job/OtelStepEnvironmentContributor.java | 9 ++- .../config.jelly | 5 ++ ...elConfigurationAsEnvironmentVariables.html | 11 +++ ...configuration-as-code-default-expected.yml | 1 + .../jcasc/configuration-as-code-expected.yml | 1 + .../jcasc/configuration-as-code.yml | 1 + 14 files changed, 173 insertions(+), 37 deletions(-) delete mode 100644 src/main/java/io/jenkins/plugins/opentelemetry/job/EnvironmentContributorUtils.java create mode 100644 src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributorService.java create mode 100644 src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/help-dontExportOtelConfigurationAsEnvironmentVariables.html diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java b/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java index ee14fcc9..a09883ce 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java @@ -41,6 +41,7 @@ import javax.inject.Inject; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -55,6 +56,9 @@ public class JenkinsOpenTelemetryPluginConfiguration extends GlobalConfiguration { private final static Logger LOGGER = Logger.getLogger(JenkinsOpenTelemetryPluginConfiguration.class.getName()); + /** + * OTLP endpoint prefixed by "http://" or "https://" + */ private String endpoint; private String trustedCertificatesPem; @@ -71,6 +75,8 @@ public class JenkinsOpenTelemetryPluginConfiguration extends GlobalConfiguration private transient OpenTelemetrySdkProvider openTelemetrySdkProvider; + private boolean dontExportOtelConfigurationAsEnvironmentVariables; + private transient SpanNamingStrategy spanNamingStrategy; private transient ConcurrentMap loadedStepsPlugins = new ConcurrentHashMap<>(); @@ -209,6 +215,34 @@ public void setIgnoredSteps(String ignoredSteps) { this.ignoredSteps = ignoredSteps; } + public boolean isDontExportOtelConfigurationAsEnvironmentVariables() { + return dontExportOtelConfigurationAsEnvironmentVariables; + } + + @DataBoundSetter + public void setDontExportOtelConfigurationAsEnvironmentVariables(boolean dontExportOtelConfigurationAsEnvironmentVariables) { + this.dontExportOtelConfigurationAsEnvironmentVariables = dontExportOtelConfigurationAsEnvironmentVariables; + } + + @Nonnull + public Map getOtelConfigurationAsEnvironmentVariables() { + Map environmentVariables = new HashMap<>(); + environmentVariables.put("OTEL_EXPORTER_OTLP_ENDPOINT", this.endpoint); + String sanitizeOtlpEndpoint = sanitizeOtlpEndpoint(this.endpoint); + if (sanitizeOtlpEndpoint != null && sanitizeOtlpEndpoint.startsWith("http://")) { + environmentVariables.put("OTEL_EXPORTER_OTLP_INSECURE", Boolean.TRUE.toString()); + } + this.authentication.enrichOtelEnvironmentVariables(environmentVariables); + String trustedCertificatesPem = this.getTrustedCertificatesPem(); + if (trustedCertificatesPem != null && !trustedCertificatesPem.isEmpty()) { + environmentVariables.put("OTEL_EXPORTER_OTLP_CERTIFICATE", trustedCertificatesPem); + } + if (this.exporterTimeoutMillis != 0) { + environmentVariables.put("OTEL_EXPORTER_OTLP_TIMEOUT", java.util.Objects.toString(this.exporterTimeoutMillis)); + } + return environmentVariables; + } + /** * For visualisation in config.jelly */ diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/authentication/BearerTokenAuthentication.java b/src/main/java/io/jenkins/plugins/opentelemetry/authentication/BearerTokenAuthentication.java index be37f538..05b58366 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/authentication/BearerTokenAuthentication.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/authentication/BearerTokenAuthentication.java @@ -23,6 +23,7 @@ import javax.annotation.Nonnull; import java.util.Collections; +import java.util.Map; import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; @@ -70,6 +71,12 @@ public void configure(@Nonnull OtlpGrpcSpanExporterBuilder spanExporterBuilder) spanExporterBuilder.addHeader("Authorization", "Bearer " + this.getAuthenticationHeaderValue()); } + @Override + public void enrichOtelEnvironmentVariables(Map environmentVariables) { + // TODO don't overwrite OTEL_EXPORTER_OTLP_HEADERS if already defined, just append to it + environmentVariables.put("OTEL_EXPORTER_OTLP_HEADERS", "Authorization=Bearer " + this.getAuthenticationHeaderValue()); + } + public String getTokenId() { return tokenId; } diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/authentication/HeaderAuthentication.java b/src/main/java/io/jenkins/plugins/opentelemetry/authentication/HeaderAuthentication.java index 6efa35cf..4611a1fa 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/authentication/HeaderAuthentication.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/authentication/HeaderAuthentication.java @@ -22,6 +22,7 @@ import org.kohsuke.stapler.DataBoundSetter; import java.util.Collections; +import java.util.Map; import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; @@ -63,6 +64,11 @@ public void configure(OtlpGrpcSpanExporterBuilder spanExporterBuilder) { spanExporterBuilder.addHeader(this.getHeaderName(), this.getAuthenticationHeaderValue()); } + @Override + public void enrichOtelEnvironmentVariables(Map environmentVariables) { + // TODO don't overwrite OTEL_EXPORTER_OTLP_HEADERS if already defined, just append to it + environmentVariables.put("OTEL_EXPORTER_OTLP_HEADERS", this.getHeaderName() + "=" + this.getAuthenticationHeaderValue()); + } public String getHeaderName() { return headerName; } diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/authentication/NoAuthentication.java b/src/main/java/io/jenkins/plugins/opentelemetry/authentication/NoAuthentication.java index 55f72f75..93737b27 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/authentication/NoAuthentication.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/authentication/NoAuthentication.java @@ -12,6 +12,7 @@ import org.kohsuke.stapler.DataBoundConstructor; import javax.annotation.Nonnull; +import java.util.Map; import java.util.logging.Logger; @Extension @@ -30,6 +31,10 @@ public void configure(@Nonnull OtlpGrpcMetricExporterBuilder metricExporterBuild public void configure(@Nonnull OtlpGrpcSpanExporterBuilder spanExporterBuilder) { } + @Override + public void enrichOtelEnvironmentVariables(Map environmentVariables) { + } + @Override public String toString() { return "NoAuthentication{}"; diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/authentication/OtlpAuthentication.java b/src/main/java/io/jenkins/plugins/opentelemetry/authentication/OtlpAuthentication.java index 4b7073bc..c9f9bfa0 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/authentication/OtlpAuthentication.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/authentication/OtlpAuthentication.java @@ -15,12 +15,28 @@ import jenkins.model.Jenkins; import javax.annotation.Nonnull; +import java.util.Map; public abstract class OtlpAuthentication implements Describable, ExtensionPoint { + /** + * Configure the given {@code metricExporterBuilder} injecting authentication settings + * @param metricExporterBuilder the builder to configure + */ public abstract void configure(@Nonnull OtlpGrpcMetricExporterBuilder metricExporterBuilder); + /** + * Configure the given {@code spanExporterBuilder} injecting authentication settings + * @param spanExporterBuilder the builder to configure + */ public abstract void configure(@Nonnull OtlpGrpcSpanExporterBuilder spanExporterBuilder); + /** + * Enrich the provided environment variables injecting the authentication settings, + * typically appending credentials to the {@code OTEL_EXPORTER_OTLP_HEADERS} variable + * @param environmentVariables the builder to configure + */ + public abstract void enrichOtelEnvironmentVariables(@Nonnull Map environmentVariables); + @Override public Descriptor getDescriptor() { return Jenkins.get().getDescriptorOrDie(getClass()); diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/job/EnvironmentContributorUtils.java b/src/main/java/io/jenkins/plugins/opentelemetry/job/EnvironmentContributorUtils.java deleted file mode 100644 index 106b1fcd..00000000 --- a/src/main/java/io/jenkins/plugins/opentelemetry/job/EnvironmentContributorUtils.java +++ /dev/null @@ -1,35 +0,0 @@ -package io.jenkins.plugins.opentelemetry.job; - -import hudson.EnvVars; -import hudson.model.Run; -import io.jenkins.plugins.opentelemetry.semconv.JenkinsOtelSemanticAttributes; -import io.opentelemetry.api.trace.Span; -import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; -import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.TextMapSetter; - -import javax.annotation.Nonnull; - -public class EnvironmentContributorUtils { - - public static void setEnvironmentVariables(@Nonnull Run run, @Nonnull EnvVars envs, @Nonnull Span span) { - String spanId = span.getSpanContext().getSpanId(); - String traceId = span.getSpanContext().getTraceId(); - try (Scope ignored = span.makeCurrent()) { - envs.putIfAbsent(JenkinsOtelSemanticAttributes.TRACE_ID, traceId); - envs.put(JenkinsOtelSemanticAttributes.SPAN_ID, spanId); - TextMapSetter setter = (carrier, key, value) -> carrier.put(key.toUpperCase(), value); - W3CTraceContextPropagator.getInstance().inject(Context.current(), envs, setter); - } - - MonitoringAction action = new MonitoringAction(traceId, spanId); - action.onAttached(run); - for (MonitoringAction.ObservabilityBackendLink link : action.getLinks()) { - // Default backend link got an empty environment variable. - if (link.getEnvironmentVariableName() != null) { - envs.put(link.getEnvironmentVariableName(), link.getUrl()); - } - } - } -} diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributor.java b/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributor.java index a2d258f6..0ede6cd2 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributor.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributor.java @@ -19,15 +19,22 @@ public class OtelEnvironmentContributor extends EnvironmentContributor { private final static Logger LOGGER = Logger.getLogger(OtelEnvironmentContributor.class.getName()); + private OtelEnvironmentContributorService otelEnvironmentContributorService; + private OtelTraceService otelTraceService; @Override public void buildEnvironmentFor(@Nonnull Run run, @Nonnull EnvVars envs, @Nonnull TaskListener listener) { - EnvironmentContributorUtils.setEnvironmentVariables(run, envs, otelTraceService.getSpan(run, false)); + otelEnvironmentContributorService.addEnvironmentVariables(run, envs, otelTraceService.getSpan(run, false)); } @Inject public void setOtelTraceService(OtelTraceService otelTraceService) { this.otelTraceService = otelTraceService; } + + @Inject + public void setEnvironmentContributorService(OtelEnvironmentContributorService otelEnvironmentContributorService) { + this.otelEnvironmentContributorService = otelEnvironmentContributorService; + } } diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributorService.java b/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributorService.java new file mode 100644 index 00000000..19ec422c --- /dev/null +++ b/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributorService.java @@ -0,0 +1,70 @@ +package io.jenkins.plugins.opentelemetry.job; + +import hudson.EnvVars; +import hudson.Extension; +import hudson.model.Run; +import io.jenkins.plugins.opentelemetry.JenkinsOpenTelemetryPluginConfiguration; +import io.jenkins.plugins.opentelemetry.semconv.JenkinsOtelSemanticAttributes; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.context.propagation.TextMapSetter; +import org.jenkinsci.plugins.workflow.steps.StepEnvironmentContributor; + +import javax.annotation.Nonnull; +import javax.inject.Inject; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Inject environment variables in shell steps + * + * @see StepEnvironmentContributor + * @see hudson.model.EnvironmentContributor + */ +@Extension +public class OtelEnvironmentContributorService { + + private final static Logger LOGGER = Logger.getLogger(OtelEnvironmentContributorService.class.getName()); + + private JenkinsOpenTelemetryPluginConfiguration jenkinsOpenTelemetryPluginConfiguration; + + public void addEnvironmentVariables(@Nonnull Run run, @Nonnull EnvVars envs, @Nonnull Span span) { + String spanId = span.getSpanContext().getSpanId(); + String traceId = span.getSpanContext().getTraceId(); + try (Scope ignored = span.makeCurrent()) { + envs.putIfAbsent(JenkinsOtelSemanticAttributes.TRACE_ID, traceId); + envs.put(JenkinsOtelSemanticAttributes.SPAN_ID, spanId); + TextMapSetter setter = (carrier, key, value) -> carrier.put(key.toUpperCase(), value); + W3CTraceContextPropagator.getInstance().inject(Context.current(), envs, setter); + } + + MonitoringAction action = new MonitoringAction(traceId, spanId); + action.onAttached(run); + for (MonitoringAction.ObservabilityBackendLink link : action.getLinks()) { + // Default backend link got an empty environment variable. + if (link.getEnvironmentVariableName() != null) { + envs.put(link.getEnvironmentVariableName(), link.getUrl()); + } + } + + if (this.jenkinsOpenTelemetryPluginConfiguration.isDontExportOtelConfigurationAsEnvironmentVariables()) { + // skip + } else { + Map otelConfiguration = jenkinsOpenTelemetryPluginConfiguration.getOtelConfigurationAsEnvironmentVariables(); + for (Map.Entry otelEnvironmentVariable : otelConfiguration.entrySet()) { + String previousValue = envs.put(otelEnvironmentVariable.getKey(), otelEnvironmentVariable.getValue()); + if (previousValue != null) { + LOGGER.log(Level.FINE, "Overwrite environment variable '" + otelEnvironmentVariable.getKey() + "'"); + } + } + } + } + + @Inject + public void setJenkinsOpenTelemetryPluginConfiguration(JenkinsOpenTelemetryPluginConfiguration jenkinsOpenTelemetryPluginConfiguration) { + this.jenkinsOpenTelemetryPluginConfiguration = jenkinsOpenTelemetryPluginConfiguration; + } +} diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelStepEnvironmentContributor.java b/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelStepEnvironmentContributor.java index 7b9e955f..b2d06ea2 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelStepEnvironmentContributor.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelStepEnvironmentContributor.java @@ -25,6 +25,8 @@ public class OtelStepEnvironmentContributor extends StepEnvironmentContributor { private final static Logger LOGGER = Logger.getLogger(OtelStepEnvironmentContributor.class.getName()); + private OtelEnvironmentContributorService otelEnvironmentContributorService; + private OtelTraceService otelTraceService; @Override @@ -42,7 +44,12 @@ public void buildEnvironmentFor(@Nonnull StepContext stepContext, @Nonnull EnvVa span = otelTraceService.getSpan(run, flowNode); } - EnvironmentContributorUtils.setEnvironmentVariables(run, envs, span); + otelEnvironmentContributorService.addEnvironmentVariables(run, envs, span); + } + + @Inject + public void setEnvironmentContributorService(OtelEnvironmentContributorService otelEnvironmentContributorService) { + this.otelEnvironmentContributorService = otelEnvironmentContributorService; } @Inject diff --git a/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/config.jelly b/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/config.jelly index 0f0f9a6b..cd03c988 100644 --- a/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/config.jelly +++ b/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/config.jelly @@ -40,6 +40,11 @@ + + + diff --git a/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/help-dontExportOtelConfigurationAsEnvironmentVariables.html b/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/help-dontExportOtelConfigurationAsEnvironmentVariables.html new file mode 100644 index 00000000..8e5ae823 --- /dev/null +++ b/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/help-dontExportOtelConfigurationAsEnvironmentVariables.html @@ -0,0 +1,11 @@ +
+By default, the OpenTelemetry configuration is exported as environment variables in shell steps using the + + standard OpenTelemetry environment variables: + OTEL_EXPORTER_OTLP_ENDPOINT, + OTEL_EXPORTER_OTLP_INSECURE, + OTEL_EXPORTER_OTLP_HEADERS, + OTEL_EXPORTER_OTLP_TIMEOUT, + OTEL_EXPORTER_OTLP_CERTIFICATE... +This option disables the export of the OpenTelemetry configuration as environment variables. +
\ No newline at end of file diff --git a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-default-expected.yml b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-default-expected.yml index 1de5acc2..b824b1ad 100644 --- a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-default-expected.yml +++ b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-default-expected.yml @@ -1,4 +1,5 @@ authentication: "noAuthentication" +dontExportOtelConfigurationAsEnvironmentVariables: false endpoint: "http://otel-collector-contrib:4317" exporterIntervalMillis: 60000 exporterTimeoutMillis: 30000 diff --git a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-expected.yml b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-expected.yml index 28fe605a..6c6195bb 100644 --- a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-expected.yml +++ b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-expected.yml @@ -1,4 +1,5 @@ authentication: "noAuthentication" +dontExportOtelConfigurationAsEnvironmentVariables: false endpoint: "http://otel-collector-contrib:4317" exporterIntervalMillis: 60000 exporterTimeoutMillis: 30000 diff --git a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code.yml b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code.yml index 34baac98..28fda5eb 100644 --- a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code.yml +++ b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code.yml @@ -1,5 +1,6 @@ unclassified: openTelemetry: + dontExportOtelConfigurationAsEnvironmentVariables: false endpoint: "http://otel-collector-contrib:4317" observabilityBackends: - elastic: From 15a2630a2d4ed430e9b09655fd9e38c58c5856a3 Mon Sep 17 00:00:00 2001 From: Cyrille Le Clerc Date: Tue, 17 Aug 2021 23:09:41 +0200 Subject: [PATCH 2/4] Update src/main/java/io/jenkins/plugins/opentelemetry/authentication/BearerTokenAuthentication.java Co-authored-by: Victor Martinez --- .../opentelemetry/authentication/BearerTokenAuthentication.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/authentication/BearerTokenAuthentication.java b/src/main/java/io/jenkins/plugins/opentelemetry/authentication/BearerTokenAuthentication.java index 05b58366..dfb0b594 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/authentication/BearerTokenAuthentication.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/authentication/BearerTokenAuthentication.java @@ -74,7 +74,7 @@ public void configure(@Nonnull OtlpGrpcSpanExporterBuilder spanExporterBuilder) @Override public void enrichOtelEnvironmentVariables(Map environmentVariables) { // TODO don't overwrite OTEL_EXPORTER_OTLP_HEADERS if already defined, just append to it - environmentVariables.put("OTEL_EXPORTER_OTLP_HEADERS", "Authorization=Bearer " + this.getAuthenticationHeaderValue()); + environmentVariables.put("OTEL_EXPORTER_OTLP_HEADERS", "authorization=Bearer " + this.getAuthenticationHeaderValue()); } public String getTokenId() { From 9866067a9e3c5d3fc9a3f396ad0d411fd50cc16d Mon Sep 17 00:00:00 2001 From: Cyrille Le Clerc Date: Thu, 19 Aug 2021 21:19:47 +0200 Subject: [PATCH 3/4] Disable by default "Export OpenTelemetry configuration as environment variables" to prevent security leaks --- .../JenkinsOpenTelemetryPluginConfiguration.java | 10 +++++----- .../job/OtelEnvironmentContributorService.java | 6 +++--- .../config.jelly | 10 +++++----- ...exportOtelConfigurationAsEnvironmentVariables.html} | 4 ++-- .../jcasc/configuration-as-code-default-expected.yml | 2 +- .../jcasc/configuration-as-code-expected.yml | 2 +- .../opentelemetry/jcasc/configuration-as-code.yml | 2 +- 7 files changed, 18 insertions(+), 18 deletions(-) rename src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/{help-dontExportOtelConfigurationAsEnvironmentVariables.html => help-exportOtelConfigurationAsEnvironmentVariables.html} (63%) diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java b/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java index a09883ce..a7aa5e7d 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java @@ -75,7 +75,7 @@ public class JenkinsOpenTelemetryPluginConfiguration extends GlobalConfiguration private transient OpenTelemetrySdkProvider openTelemetrySdkProvider; - private boolean dontExportOtelConfigurationAsEnvironmentVariables; + private boolean exportOtelConfigurationAsEnvironmentVariables; private transient SpanNamingStrategy spanNamingStrategy; @@ -215,13 +215,13 @@ public void setIgnoredSteps(String ignoredSteps) { this.ignoredSteps = ignoredSteps; } - public boolean isDontExportOtelConfigurationAsEnvironmentVariables() { - return dontExportOtelConfigurationAsEnvironmentVariables; + public boolean isExportOtelConfigurationAsEnvironmentVariables() { + return exportOtelConfigurationAsEnvironmentVariables; } @DataBoundSetter - public void setDontExportOtelConfigurationAsEnvironmentVariables(boolean dontExportOtelConfigurationAsEnvironmentVariables) { - this.dontExportOtelConfigurationAsEnvironmentVariables = dontExportOtelConfigurationAsEnvironmentVariables; + public void setExportOtelConfigurationAsEnvironmentVariables(boolean exportOtelConfigurationAsEnvironmentVariables) { + this.exportOtelConfigurationAsEnvironmentVariables = exportOtelConfigurationAsEnvironmentVariables; } @Nonnull diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributorService.java b/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributorService.java index 19ec422c..f3cb889a 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributorService.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/job/OtelEnvironmentContributorService.java @@ -50,9 +50,7 @@ public void addEnvironmentVariables(@Nonnull Run run, @Nonnull EnvVars envs, @No } } - if (this.jenkinsOpenTelemetryPluginConfiguration.isDontExportOtelConfigurationAsEnvironmentVariables()) { - // skip - } else { + if (this.jenkinsOpenTelemetryPluginConfiguration.isExportOtelConfigurationAsEnvironmentVariables()) { Map otelConfiguration = jenkinsOpenTelemetryPluginConfiguration.getOtelConfigurationAsEnvironmentVariables(); for (Map.Entry otelEnvironmentVariable : otelConfiguration.entrySet()) { String previousValue = envs.put(otelEnvironmentVariable.getKey(), otelEnvironmentVariable.getValue()); @@ -60,6 +58,8 @@ public void addEnvironmentVariables(@Nonnull Run run, @Nonnull EnvVars envs, @No LOGGER.log(Level.FINE, "Overwrite environment variable '" + otelEnvironmentVariable.getKey() + "'"); } } + } else { + // skip } } diff --git a/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/config.jelly b/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/config.jelly index cd03c988..5f3cf124 100644 --- a/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/config.jelly +++ b/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/config.jelly @@ -9,6 +9,11 @@ + + + @@ -40,11 +45,6 @@ - - - diff --git a/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/help-dontExportOtelConfigurationAsEnvironmentVariables.html b/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/help-exportOtelConfigurationAsEnvironmentVariables.html similarity index 63% rename from src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/help-dontExportOtelConfigurationAsEnvironmentVariables.html rename to src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/help-exportOtelConfigurationAsEnvironmentVariables.html index 8e5ae823..fd3973f5 100644 --- a/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/help-dontExportOtelConfigurationAsEnvironmentVariables.html +++ b/src/main/resources/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration/help-exportOtelConfigurationAsEnvironmentVariables.html @@ -1,5 +1,5 @@
-By default, the OpenTelemetry configuration is exported as environment variables in shell steps using the +Export the OpenTelemetry configuration as environment variables in shell and bat steps using the standard OpenTelemetry environment variables: OTEL_EXPORTER_OTLP_ENDPOINT, @@ -7,5 +7,5 @@ OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_TIMEOUT, OTEL_EXPORTER_OTLP_CERTIFICATE... -This option disables the export of the OpenTelemetry configuration as environment variables. + Note that OpenTelemetry credentials, if configured, will be exposed as environment variables (likely in OTEL_EXPORTER_OTLP_HEADERS).
\ No newline at end of file diff --git a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-default-expected.yml b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-default-expected.yml index b824b1ad..1623e93f 100644 --- a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-default-expected.yml +++ b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-default-expected.yml @@ -1,6 +1,6 @@ authentication: "noAuthentication" -dontExportOtelConfigurationAsEnvironmentVariables: false endpoint: "http://otel-collector-contrib:4317" +exportOtelConfigurationAsEnvironmentVariables: false exporterIntervalMillis: 60000 exporterTimeoutMillis: 30000 ignoredSteps: "dir,echo,isUnix,pwd,properties" diff --git a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-expected.yml b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-expected.yml index 6c6195bb..55bf1f98 100644 --- a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-expected.yml +++ b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code-expected.yml @@ -1,6 +1,6 @@ authentication: "noAuthentication" -dontExportOtelConfigurationAsEnvironmentVariables: false endpoint: "http://otel-collector-contrib:4317" +exportOtelConfigurationAsEnvironmentVariables: false exporterIntervalMillis: 60000 exporterTimeoutMillis: 30000 ignoredSteps: "dir,echo,isUnix,pwd,properties" diff --git a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code.yml b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code.yml index 28fda5eb..85c625e9 100644 --- a/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code.yml +++ b/src/test/resources/io/jenkins/plugins/opentelemetry/jcasc/configuration-as-code.yml @@ -1,7 +1,7 @@ unclassified: openTelemetry: - dontExportOtelConfigurationAsEnvironmentVariables: false endpoint: "http://otel-collector-contrib:4317" + exportOtelConfigurationAsEnvironmentVariables: false observabilityBackends: - elastic: name: "My Elastic" From 72b2ca155d89c1b0faf4c7d26ce3661cbdac0548 Mon Sep 17 00:00:00 2001 From: Cyrille Le Clerc Date: Thu, 19 Aug 2021 21:32:39 +0200 Subject: [PATCH 4/4] Better documentation --- README.md | 8 ++++++++ .../JenkinsOpenTelemetryPluginConfiguration.java | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 41732fae..afa58135 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,13 @@ The context of the current span is exposed as environment variables to ease inte * `TRACE_ID`: the trace id of the job / pipeline * `SPAN_ID`: the id of the pipeline shell step span +When the configuration options "Export OpenTelemetry configuration as environment variables", the following [OpenTelemetry environment variables](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.0/specification/protocol/exporter.md) will be exported according to the settings of the plugin: +* `OTEL_EXPORTER_OTLP_ENDPOINT`: Target to which the exporter is going to send spans or metrics. +* `OTEL_EXPORTER_OTLP_INSECURE`: Whether to enable client transport security for the exporter's gRPC connection +* `OTEL_EXPORTER_OTLP_HEADERS`: Key-value pairs to be used as headers associated with gRPC or HTTP requests. Typically used to pass credentials. +* `OTEL_EXPORTER_OTLP_TIMEOUT`: Maximum time the OTLP exporter will wait for each batch export. +* `OTEL_EXPORTER_OTLP_CERTIFICATE`: The trusted certificate to use when verifying a server's TLS credentials. + In addition, if the backends were configured then there will be an environment variable for each of them pointing to the URL with the span/transactions: * `OTEL_CUSTOM_URL` @@ -316,6 +323,7 @@ unclassified: openTelemetry: authentication: "noAuthentication" endpoint: "otel-collector-contrib:4317" + exportOtelConfigurationAsEnvironmentVariables: true exporterIntervalMillis: 60000 exporterTimeoutMillis: 30000 ignoredSteps: "dir,echo,isUnix,pwd,properties" diff --git a/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java b/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java index a7aa5e7d..123254ca 100644 --- a/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java +++ b/src/main/java/io/jenkins/plugins/opentelemetry/JenkinsOpenTelemetryPluginConfiguration.java @@ -238,7 +238,7 @@ public Map getOtelConfigurationAsEnvironmentVariables() { environmentVariables.put("OTEL_EXPORTER_OTLP_CERTIFICATE", trustedCertificatesPem); } if (this.exporterTimeoutMillis != 0) { - environmentVariables.put("OTEL_EXPORTER_OTLP_TIMEOUT", java.util.Objects.toString(this.exporterTimeoutMillis)); + environmentVariables.put("OTEL_EXPORTER_OTLP_TIMEOUT", this.exporterTimeoutMillis + "ms"); } return environmentVariables; }