diff --git a/src/test/groovy/WithOtelEnvStepTests.groovy b/src/test/groovy/WithOtelEnvStepTests.groovy index 8936cd168..4295acd1b 100644 --- a/src/test/groovy/WithOtelEnvStepTests.groovy +++ b/src/test/groovy/WithOtelEnvStepTests.groovy @@ -42,4 +42,20 @@ class WithOtelEnvStepTests extends ApmBasePipelineTest { assertTrue(assertMethodCallContainsPattern('error', 'withOtelEnv: opentelemetry plugin is not available')) assertJobStatusFailure() } + + @Test + void test_env_variable_defined() throws Exception { + addEnvVar('OTEL_EXPORTER_OTLP_HEADERS', 'foo') + try { + script.call(){ + //NOOP + } + } catch(e){ + //NOOP + } + printCallStack() + assertTrue(assertMethodCallContainsPattern('error', 'withOtelEnv: OTEL_EXPORTER_OTLP_HEADERS env variable is already defined')) + assertJobStatusFailure() + } + } diff --git a/vars/README.md b/vars/README.md index bef3dac0b..1c023e288 100644 --- a/vars/README.md +++ b/vars/README.md @@ -3141,7 +3141,7 @@ environment variables: * `OTEL_EXPORTER_OTLP_ENDPOINT` * `OTEL_SERVICE_NAME` -* `OTEL_TOKEN_ID` +* `OTEL_EXPORTER_OTLP_HEADERS` ``` withOtelEnv() { diff --git a/vars/withOtelEnv.groovy b/vars/withOtelEnv.groovy index 5ef194b56..16473a1f6 100644 --- a/vars/withOtelEnv.groovy +++ b/vars/withOtelEnv.groovy @@ -33,6 +33,10 @@ def call(Map args = [:], Closure body) { error('withOtelEnv: opentelemetry plugin is not available') } + if (env.OTEL_EXPORTER_OTLP_HEADERS?.trim()) { + error('withOtelEnv: OTEL_EXPORTER_OTLP_HEADERS env variable is already defined.') + } + def otelPlugin = getOtelPlugin() // In case the credentialsId argument was not passed, then let's use the @@ -44,12 +48,13 @@ def call(Map args = [:], Closure body) { } } - // Then, mask and provide the environemnt variables. - withEnvMask(vars: [ - [var: "OTEL_EXPORTER_OTLP_ENDPOINT", password: otelPlugin.getEndpoint()], - [var: "OTEL_SERVICE_NAME", password: otelPlugin.getServiceName()], - ]) { - withCredentials([string(credentialsId: credentialsId, variable: 'OTEL_TOKEN_ID')]) { + // Then, mask and provide the environment variables. + withCredentials([string(credentialsId: credentialsId, variable: 'OTEL_TOKEN_ID')]) { + withEnvMask(vars: [ + [var: 'OTEL_EXPORTER_OTLP_ENDPOINT', password: otelPlugin.getEndpoint()], + [var: 'OTEL_SERVICE_NAME', password: otelPlugin.getServiceName()], + [var: 'OTEL_EXPORTER_OTLP_HEADERS', password: "authorization=Bearer ${env.OTEL_TOKEN_ID}"] + ]) { body() } } diff --git a/vars/withOtelEnv.txt b/vars/withOtelEnv.txt index bb116cc06..7a67097b4 100644 --- a/vars/withOtelEnv.txt +++ b/vars/withOtelEnv.txt @@ -3,7 +3,7 @@ environment variables: * `OTEL_EXPORTER_OTLP_ENDPOINT` * `OTEL_SERVICE_NAME` -* `OTEL_TOKEN_ID` +* `OTEL_EXPORTER_OTLP_HEADERS` ``` withOtelEnv() {