Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect: sever semconv relationship to otel sdk #5503

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions util/tracing/detect/resource.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package detect

import (
Expand All @@ -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"
)
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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
}
29 changes: 29 additions & 0 deletions util/tracing/detect/resource_test.go
Original file line number Diff line number Diff line change
@@ -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)
}