diff --git a/cmd/tesseract/gcp/otel.go b/cmd/tesseract/gcp/otel.go index bd641705..0b8c4f0e 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,14 +51,20 @@ 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.WithFromEnv(), // unpacks OTEL_RESOURCE_ATTRIBUTES resource.WithDetectors(gcp.NewDetector()), ) if err != nil { diff --git a/go.mod b/go.mod index 864cadeb..91750735 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