From 4914b6c26c18fd13b4ca79417be49e4e9af03061 Mon Sep 17 00:00:00 2001
From: AvivGuiser <avivguiser@gmail.com>
Date: Wed, 12 Jun 2024 13:24:45 +0300
Subject: [PATCH] feat(instrumentation): add option to overwrite otlp service
 name/namespace/version with env var (#29583)

Signed-off-by: Aviv Guiser <avivguiser@gmail.com>
Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
---
 docs/usage/opentelemetry.md  | 5 +++++
 lib/instrumentation/index.ts | 9 ++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/docs/usage/opentelemetry.md b/docs/usage/opentelemetry.md
index 0ff295ba39..bfec627621 100644
--- a/docs/usage/opentelemetry.md
+++ b/docs/usage/opentelemetry.md
@@ -30,6 +30,11 @@ This means that Renovate sends traces via [OTLP/HTTP](https://opentelemetry.io/d
 To activate the instrumentation, you must set the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable.
 This variable controls the endpoint for the telemetry data.
 Once this endpoint is set, you can use all environment variables listed in the [OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md).
+You can also set the following environment variables:
+
+- `OTEL_SERVICE_NAME`: to control the service name that will be emitted in traces, defaults to `renovate`
+- `OTEL_SERVICE_NAMESPACE`: to control the service namespace that will be emitted in traces, defaults to `renovatebot.com`
+- `OTEL_SERVICE_VERSION`: to control the service version that will be emitted in traces, defaults to using the release version of Renovate
 
 ## Debugging
 
diff --git a/lib/instrumentation/index.ts b/lib/instrumentation/index.ts
index 4ae908847b..c1a9349108 100644
--- a/lib/instrumentation/index.ts
+++ b/lib/instrumentation/index.ts
@@ -43,9 +43,12 @@ export function init(): void {
   const traceProvider = new NodeTracerProvider({
     resource: new Resource({
       // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/README.md#semantic-attributes-with-sdk-provided-default-value
-      [SemanticResourceAttributes.SERVICE_NAME]: 'renovate',
-      [SemanticResourceAttributes.SERVICE_NAMESPACE]: 'renovatebot.com',
-      [SemanticResourceAttributes.SERVICE_VERSION]: pkg.version,
+      [SemanticResourceAttributes.SERVICE_NAME]:
+        process.env.OTEL_SERVICE_NAME ?? 'renovate',
+      [SemanticResourceAttributes.SERVICE_NAMESPACE]:
+        process.env.OTEL_SERVICE_NAMESPACE ?? 'renovatebot.com',
+      [SemanticResourceAttributes.SERVICE_VERSION]:
+        process.env.OTEL_SERVICE_VERSION ?? pkg.version,
     }),
   });
 
-- 
GitLab