Skip to content

Commit 9863dd3

Browse files
committed
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 <[email protected]>
1 parent 9d81766 commit 9863dd3

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

util/tracing/detect/resource.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package detect
25

36
import (
@@ -7,6 +10,7 @@ import (
710
"sync"
811

912
"go.opentelemetry.io/otel"
13+
"go.opentelemetry.io/otel/sdk"
1014
"go.opentelemetry.io/otel/sdk/resource"
1115
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
1216
)
@@ -23,7 +27,7 @@ func Resource() *resource.Resource {
2327
res, err := resource.New(context.Background(),
2428
resource.WithDetectors(serviceNameDetector{}),
2529
resource.WithFromEnv(),
26-
resource.WithTelemetrySDK(),
30+
resource.WithDetectors(telemetrySDK{}),
2731
)
2832
if err != nil {
2933
otel.Handle(err)
@@ -42,7 +46,15 @@ func OverrideResource(res *resource.Resource) {
4246
})
4347
}
4448

45-
type serviceNameDetector struct{}
49+
type (
50+
telemetrySDK struct{}
51+
serviceNameDetector struct{}
52+
)
53+
54+
var (
55+
_ resource.Detector = telemetrySDK{}
56+
_ resource.Detector = serviceNameDetector{}
57+
)
4658

4759
func (serviceNameDetector) Detect(ctx context.Context) (*resource.Resource, error) {
4860
return resource.StringDetector(
@@ -56,3 +68,13 @@ func (serviceNameDetector) Detect(ctx context.Context) (*resource.Resource, erro
5668
},
5769
).Detect(ctx)
5870
}
71+
72+
// Detect returns a *Resource that describes the OpenTelemetry SDK used.
73+
func (telemetrySDK) Detect(context.Context) (*resource.Resource, error) {
74+
return resource.NewWithAttributes(
75+
semconv.SchemaURL,
76+
semconv.TelemetrySDKName("opentelemetry"),
77+
semconv.TelemetrySDKLanguageGo,
78+
semconv.TelemetrySDKVersion(sdk.Version()),
79+
), nil
80+
}

util/tracing/detect/resource_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package detect
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"go.opentelemetry.io/otel"
8+
)
9+
10+
func TestResource(t *testing.T) {
11+
prevHandler := otel.GetErrorHandler()
12+
t.Cleanup(func() {
13+
otel.SetErrorHandler(prevHandler)
14+
})
15+
16+
var resourceErr error
17+
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) {
18+
resourceErr = err
19+
}))
20+
21+
res := Resource()
22+
23+
// Should not have an empty schema url. Only happens when
24+
// there is a schema conflict.
25+
require.NotEqual(t, "", res.SchemaURL())
26+
27+
// No error should have been invoked.
28+
require.NoError(t, resourceErr)
29+
}

0 commit comments

Comments
 (0)