From 9863dd307d56a1f2055a836d0bc2182d9b6d32cf Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Fri, 8 Nov 2024 15:54:48 -0600 Subject: [PATCH] detect: sever semconv relationship to otel sdk Copies over the telemetry sdk detection to utilize our own version of semconv rather than mix the versions between different packages. This will keep a consistent schema url instead of constantly chasing whichever one the otel sdk is using. The only thing left from the otel sdk is `WithFromEnv` which is schemaless and won't conflict for that reason so we can continue to use it. Signed-off-by: Jonathan A. Sternberg --- util/tracing/detect/resource.go | 26 +++++++++++++++++++++++-- util/tracing/detect/resource_test.go | 29 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 util/tracing/detect/resource_test.go diff --git a/util/tracing/detect/resource.go b/util/tracing/detect/resource.go index 79abea9526cb..b4c1f81a8671 100644 --- a/util/tracing/detect/resource.go +++ b/util/tracing/detect/resource.go @@ -1,3 +1,6 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + package detect import ( @@ -7,6 +10,7 @@ import ( "sync" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/sdk" "go.opentelemetry.io/otel/sdk/resource" semconv "go.opentelemetry.io/otel/semconv/v1.21.0" ) @@ -23,7 +27,7 @@ func Resource() *resource.Resource { res, err := resource.New(context.Background(), resource.WithDetectors(serviceNameDetector{}), resource.WithFromEnv(), - resource.WithTelemetrySDK(), + resource.WithDetectors(telemetrySDK{}), ) if err != nil { otel.Handle(err) @@ -42,7 +46,15 @@ func OverrideResource(res *resource.Resource) { }) } -type serviceNameDetector struct{} +type ( + telemetrySDK struct{} + serviceNameDetector struct{} +) + +var ( + _ resource.Detector = telemetrySDK{} + _ resource.Detector = serviceNameDetector{} +) func (serviceNameDetector) Detect(ctx context.Context) (*resource.Resource, error) { return resource.StringDetector( @@ -56,3 +68,13 @@ func (serviceNameDetector) Detect(ctx context.Context) (*resource.Resource, erro }, ).Detect(ctx) } + +// Detect returns a *Resource that describes the OpenTelemetry SDK used. +func (telemetrySDK) Detect(context.Context) (*resource.Resource, error) { + return resource.NewWithAttributes( + semconv.SchemaURL, + semconv.TelemetrySDKName("opentelemetry"), + semconv.TelemetrySDKLanguageGo, + semconv.TelemetrySDKVersion(sdk.Version()), + ), nil +} diff --git a/util/tracing/detect/resource_test.go b/util/tracing/detect/resource_test.go new file mode 100644 index 000000000000..3863b4ffa3d0 --- /dev/null +++ b/util/tracing/detect/resource_test.go @@ -0,0 +1,29 @@ +package detect + +import ( + "testing" + + "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel" +) + +func TestResource(t *testing.T) { + prevHandler := otel.GetErrorHandler() + t.Cleanup(func() { + otel.SetErrorHandler(prevHandler) + }) + + var resourceErr error + otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) { + resourceErr = err + })) + + res := Resource() + + // Should not have an empty schema url. Only happens when + // there is a schema conflict. + require.NotEqual(t, "", res.SchemaURL()) + + // No error should have been invoked. + require.NoError(t, resourceErr) +}