util/tracing: test resource schema and environment detection

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax committed 2026-09-18 15:41:57 +02:00
1 parent ba2effb9f6
commit 7e4d2af246
1 file changed
+25 -3
+25 -3
View File
@@ -5,6 +5,9 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk"
"go.opentelemetry.io/otel/sdk/resource"
)
func TestResource(t *testing.T) {
@@ -20,9 +23,8 @@ func TestResource(t *testing.T) {
res := Resource()
// Should not have an empty schema url. Only happens when
// there is a schema conflict.
require.NotEmpty(t, res.SchemaURL())
// Keep our schema independent of the version used by the OTel SDK.
require.Equal(t, schemaURL, res.SchemaURL())
var found bool
for iter := res.Iter(); iter.Next(); {
@@ -36,3 +38,23 @@ func TestResource(t *testing.T) {
// No error should have been invoked.
require.NoError(t, resourceErr)
}
func TestResourceWithFromEnv(t *testing.T) {
t.Setenv("OTEL_SERVICE_NAME", "buildkit-test")
t.Setenv("OTEL_RESOURCE_ATTRIBUTES", "service.name=overridden,custom.attribute=value")
res, err := resource.New(t.Context(),
resource.WithDetectors(serviceNameDetector{}),
resource.WithFromEnv(),
resource.WithDetectors(telemetrySDK{}),
)
require.NoError(t, err)
require.Equal(t, schemaURL, res.SchemaURL())
require.ElementsMatch(t, []attribute.KeyValue{
attribute.String(serviceNameKey, "buildkit-test"),
attribute.String("custom.attribute", "value"),
attribute.String(telemetrySDKNameKey, "opentelemetry"),
attribute.String(telemetrySDKLanguageKey, "go"),
attribute.String(telemetrySDKVersionKey, sdk.Version()),
}, res.Attributes())
}