From da6b6a0c456c33b9a8f3981eabd44f3974cf6e34 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Mon, 4 Nov 2024 08:38:59 -0800 Subject: [PATCH] config: update resource to return only configured values (#6289) This changes the resource configured to match what an end user would choose to configure. Without this, Collector users would have no way to ensure that only the resource attributes they configure are added to the telemetry. --------- Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> Co-authored-by: Tyler Yahn Co-authored-by: Damien Mathieu <42@dmathieu.com> --- CHANGELOG.md | 1 + config/config.go | 5 +--- config/resource.go | 11 ++++---- config/resource_test.go | 62 +++++++++++++++-------------------------- 4 files changed, 29 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65dcca7a2a6..d4eb8cd92d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Changed - Upgrade `go.opentelemetry.io/otel/semconv/v1.17.0` to `go.opentelemetry.io/otel/semconv/v1.21.0` in `go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo`. (#6272) +- Resource doesn't merge with defaults if a valid resource is configured in `go.opentelemetry.io/contrib/config`. (#6289) ### Fixed diff --git a/config/config.go b/config/config.go index e2599766fe3..7031f1cabd5 100644 --- a/config/config.go +++ b/config/config.go @@ -82,10 +82,7 @@ func NewSDK(opts ...ConfigurationOption) (SDK, error) { return noopSDK, nil } - r, err := newResource(o.opentelemetryConfig.Resource) - if err != nil { - return noopSDK, err - } + r := newResource(o.opentelemetryConfig.Resource) mp, mpShutdown, err := meterProvider(o, r) if err != nil { diff --git a/config/resource.go b/config/resource.go index 1e62d29c295..7a453c24204 100644 --- a/config/resource.go +++ b/config/resource.go @@ -45,9 +45,9 @@ func keyVal(k string, v any) attribute.KeyValue { } } -func newResource(res *Resource) (*resource.Resource, error) { +func newResource(res *Resource) *resource.Resource { if res == nil || res.Attributes == nil { - return resource.Default(), nil + return resource.Default() } var attrs []attribute.KeyValue @@ -55,8 +55,7 @@ func newResource(res *Resource) (*resource.Resource, error) { attrs = append(attrs, keyVal(v.Name, v.Value)) } - return resource.Merge(resource.Default(), - resource.NewWithAttributes(*res.SchemaUrl, - attrs..., - )) + return resource.NewWithAttributes(*res.SchemaUrl, + attrs..., + ) } diff --git a/config/resource_test.go b/config/resource_test.go index b51650429b0..5a9f756f2b8 100644 --- a/config/resource_test.go +++ b/config/resource_test.go @@ -8,7 +8,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/sdk/resource" @@ -18,37 +17,32 @@ import ( type mockType struct{} func TestNewResource(t *testing.T) { - res, err := resource.Merge(resource.Default(), - resource.NewWithAttributes(semconv.SchemaURL, - semconv.ServiceName("service-a"), - )) + res := resource.NewWithAttributes(semconv.SchemaURL, + semconv.ServiceName("service-a"), + ) other := mockType{} - require.NoError(t, err) - resWithAttrs, err := resource.Merge(resource.Default(), - resource.NewWithAttributes(semconv.SchemaURL, - semconv.ServiceName("service-a"), - attribute.Bool("attr-bool", true), - attribute.String("attr-uint64", fmt.Sprintf("%d", 164)), - attribute.Int64("attr-int64", int64(-164)), - attribute.Float64("attr-float64", float64(64.0)), - attribute.Int64("attr-int8", int64(-18)), - attribute.Int64("attr-uint8", int64(18)), - attribute.Int64("attr-int16", int64(-116)), - attribute.Int64("attr-uint16", int64(116)), - attribute.Int64("attr-int32", int64(-132)), - attribute.Int64("attr-uint32", int64(132)), - attribute.Float64("attr-float32", float64(32.0)), - attribute.Int64("attr-int", int64(-1)), - attribute.String("attr-uint", fmt.Sprintf("%d", 1)), - attribute.String("attr-string", "string-val"), - attribute.String("attr-default", fmt.Sprintf("%v", other)), - )) - require.NoError(t, err) + resWithAttrs := resource.NewWithAttributes(semconv.SchemaURL, + semconv.ServiceName("service-a"), + attribute.Bool("attr-bool", true), + attribute.String("attr-uint64", fmt.Sprintf("%d", 164)), + attribute.Int64("attr-int64", int64(-164)), + attribute.Float64("attr-float64", float64(64.0)), + attribute.Int64("attr-int8", int64(-18)), + attribute.Int64("attr-uint8", int64(18)), + attribute.Int64("attr-int16", int64(-116)), + attribute.Int64("attr-uint16", int64(116)), + attribute.Int64("attr-int32", int64(-132)), + attribute.Int64("attr-uint32", int64(132)), + attribute.Float64("attr-float32", float64(32.0)), + attribute.Int64("attr-int", int64(-1)), + attribute.String("attr-uint", fmt.Sprintf("%d", 1)), + attribute.String("attr-string", "string-val"), + attribute.String("attr-default", fmt.Sprintf("%v", other)), + ) tests := []struct { name string config *Resource wantResource *resource.Resource - wantErr error }{ { name: "no-resource-configuration", @@ -59,17 +53,6 @@ func TestNewResource(t *testing.T) { config: &Resource{}, wantResource: resource.Default(), }, - { - name: "resource-with-attributes-invalid-schema", - config: &Resource{ - SchemaUrl: ptr("https://opentelemetry.io/invalid-schema"), - Attributes: []AttributeNameValue{ - {Name: "service.name", Value: "service-a"}, - }, - }, - wantResource: resource.NewSchemaless(res.Attributes()...), - wantErr: resource.ErrSchemaURLConflict, - }, { name: "resource-with-attributes-and-schema", config: &Resource{ @@ -108,8 +91,7 @@ func TestNewResource(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := newResource(tt.config) - assert.ErrorIs(t, err, tt.wantErr) + got := newResource(tt.config) assert.Equal(t, tt.wantResource, got) }) }