Skip to content

Commit

Permalink
Allow passing in an OTel resource as an option to clue.NewConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
douglaswth committed Apr 10, 2024
1 parent 0240cb3 commit bb25904
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clue/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ func NewConfig(
if err != nil {
return nil, err
}
if options.resource != nil {
res, err = resource.Merge(res, options.resource)
if err != nil {
return nil, err

Check warning on line 91 in clue/config.go

View check run for this annotation

Codecov / codecov/patch

clue/config.go#L89-L91

Added lines #L89 - L91 were 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

0 comments on commit bb25904

Please sign in to comment.