From 59e677da60d03c0bc4195c19ba35f23eda92a298 Mon Sep 17 00:00:00 2001 From: Martin Hutchinson Date: Thu, 12 Feb 2026 16:41:14 +0000 Subject: [PATCH 1/2] Explicitly set OTel Service Instance ID This uses os.Hostname, falling back on a UUID if for some reason it can't be looked up. This should work on VMs and CloudRun instances. Fixes #699. --- cmd/tesseract/gcp/otel.go | 10 +++++++++- go.mod | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/tesseract/gcp/otel.go b/cmd/tesseract/gcp/otel.go index bd6417054..372e45f1f 100644 --- a/cmd/tesseract/gcp/otel.go +++ b/cmd/tesseract/gcp/otel.go @@ -17,6 +17,7 @@ package main import ( "context" "errors" + "os" "go.opentelemetry.io/contrib/detectors/gcp" "go.opentelemetry.io/otel" @@ -27,6 +28,7 @@ import ( mexporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric" texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace" + "github.com/google/uuid" "k8s.io/klog/v2" ) @@ -49,13 +51,19 @@ func initOTel(ctx context.Context, traceFraction float64, origin string, project } } + instanceID, err := os.Hostname() + if err != nil { + klog.Errorf("os.Hostname() failed, setting OTel service instance ID to UUID. Error: %v", err) + instanceID = uuid.NewString() + } resources, err := resource.New(ctx, resource.WithTelemetrySDK(), resource.WithFromEnv(), // unpacks OTEL_RESOURCE_ATTRIBUTES // Add your own custom attributes to identify your application resource.WithAttributes( - semconv.ServiceNameKey.String(origin), semconv.ServiceNamespaceKey.String("tesseract"), + semconv.ServiceNameKey.String(origin), + semconv.ServiceInstanceIDKey.String(instanceID), ), resource.WithDetectors(gcp.NewDetector()), ) diff --git a/go.mod b/go.mod index 864cadeb8..917507354 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/gdamore/tcell/v2 v2.13.8 github.com/go-sql-driver/mysql v1.9.3 github.com/google/go-cmp v0.7.0 + github.com/google/uuid v1.6.0 github.com/johannesboyne/gofakes3 v0.0.0-20250916175020-ebf3e50324d3 github.com/kylelemons/godebug v1.1.0 github.com/rivo/tview v0.42.0 @@ -99,7 +100,6 @@ require ( github.com/google/flatbuffers v25.2.10+incompatible // indirect github.com/google/renameio/v2 v2.0.0 // indirect github.com/google/s2a-go v0.1.9 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.11 // indirect github.com/googleapis/gax-go/v2 v2.16.0 // indirect github.com/gorilla/handlers v1.5.2 // indirect From 02a345205442cbf3d2fd33b402119b56e074c062 Mon Sep 17 00:00:00 2001 From: Martin Hutchinson Date: Mon, 16 Feb 2026 09:42:50 +0000 Subject: [PATCH 2/2] Allow environment variables to override code-configured IDs Whichever is last wins. Previously we used the env variables as the base, and then the binary would replace them. This way around is more user-first: if the user is strongly opinionated that in their telemetry they want, e.g. service name to be something different, they can do. --- cmd/tesseract/gcp/otel.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/tesseract/gcp/otel.go b/cmd/tesseract/gcp/otel.go index 372e45f1f..0b8c4f0e1 100644 --- a/cmd/tesseract/gcp/otel.go +++ b/cmd/tesseract/gcp/otel.go @@ -58,13 +58,13 @@ func initOTel(ctx context.Context, traceFraction float64, origin string, project } resources, err := resource.New(ctx, resource.WithTelemetrySDK(), - resource.WithFromEnv(), // unpacks OTEL_RESOURCE_ATTRIBUTES // Add your own custom attributes to identify your application resource.WithAttributes( semconv.ServiceNamespaceKey.String("tesseract"), semconv.ServiceNameKey.String(origin), semconv.ServiceInstanceIDKey.String(instanceID), ), + resource.WithFromEnv(), // unpacks OTEL_RESOURCE_ATTRIBUTES resource.WithDetectors(gcp.NewDetector()), ) if err != nil {