Skip to content

Commit

Permalink
config: update resource to return only configured values (#6289)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Tyler Yahn <[email protected]>
Co-authored-by: Damien Mathieu <[email protected]>
  • Loading branch information
3 people authored Nov 4, 2024
1 parent 8bc4bb1 commit da6b6a0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,17 @@ 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

for _, v := range res.Attributes {
attrs = append(attrs, keyVal(v.Name, v.Value))
}

return resource.Merge(resource.Default(),
resource.NewWithAttributes(*res.SchemaUrl,
attrs...,
))
return resource.NewWithAttributes(*res.SchemaUrl,
attrs...,
)
}
62 changes: 22 additions & 40 deletions config/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -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{
Expand Down Expand Up @@ -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)
})
}
Expand Down

0 comments on commit da6b6a0

Please sign in to comment.