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

Allow passing in an OTel resource as an option to clue.NewConfig #402

Merged
merged 2 commits into from
May 15, 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
4 changes: 4 additions & 0 deletions clue/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
if err != nil {
return nil, err
}
res, err = resource.Merge(res, options.resource)
if err != nil {
return nil, err

Check warning on line 90 in clue/config.go

View check run for this annotation

Codecov / codecov/patch

clue/config.go#L90

Added line #L90 was not covered by tests
}
var meterProvider metric.MeterProvider
if metricExporter == nil {
meterProvider = metricnoop.NewMeterProvider()
Expand Down
10 changes: 10 additions & 0 deletions clue/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
)

type (
Expand All @@ -24,6 +25,8 @@ type (
sampleSize int
// propagators is the trace propagators.
propagators propagation.TextMapPropagator
// resource is the resource containing any additional attributes.
resource *resource.Resource
// errorHandler is the error handler used by the otel package.
errorHandler otel.ErrorHandler
}
Expand Down Expand Up @@ -71,6 +74,13 @@ func WithPropagators(propagator propagation.TextMapPropagator) Option {
}
}

// WithResource sets the resource containing any additional attributes.
func WithResource(res *resource.Resource) Option {
return func(opts *options) {
opts.resource = res
}
}

// WithErrorHandler sets the error handler used by the telemetry package.
func WithErrorHandler(errorHandler otel.ErrorHandler) Option {
return func(opts *options) {
Expand Down
9 changes: 9 additions & 0 deletions clue/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"

"goa.design/clue/log"
)

Expand Down Expand Up @@ -39,6 +42,11 @@ func TestOptions(t *testing.T) {
option: WithPropagators(propagation.TraceContext{}),
want: func(o *options) { o.propagators = propagation.TraceContext{} },
},
{
name: "with resource",
option: WithResource(resource.NewSchemaless(attribute.String("key", "value"))),
want: func(o *options) { o.resource = resource.NewSchemaless(attribute.String("key", "value")) },
},
{
name: "with error handler",
option: WithErrorHandler(dummyErrorHandler{}),
Expand All @@ -59,6 +67,7 @@ func TestOptions(t *testing.T) {
assert.Equal(t, want.sampleSize, got.sampleSize)
assert.Equal(t, want.readerInterval, got.readerInterval)
assert.Equal(t, want.propagators, got.propagators)
assert.Equal(t, want.resource, got.resource)
assert.IsType(t, want.errorHandler, got.errorHandler)
})
}
Expand Down