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

feat: Add resource option configuration #47

Merged
merged 2 commits into from
Jun 1, 2023
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
19 changes: 17 additions & 2 deletions otelconfig/otelconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ func WithResourceAttributes(attributes map[string]string) Option {
}
}

// WithResourceOption configures options on the resource; These are appended
// after the default options and can override them.
func WithResourceOption(option resource.Option) Option {
return func(c *Config) {
c.ResourceOptions = append(c.ResourceOptions, option)
}
}

// WithPropagators configures propagators.
func WithPropagators(propagators []string) Option {
return func(c *Config) {
Expand Down Expand Up @@ -316,6 +324,7 @@ type Config struct {
ResourceAttributes map[string]string
SpanProcessors []trace.SpanProcessor
Sampler trace.Sampler
ResourceOptions []resource.Option
Resource *resource.Resource
Logger Logger `json:"-"`
ShutdownFunctions []func(c *Config) error `json:"-"`
Expand Down Expand Up @@ -411,11 +420,17 @@ func newResource(c *Config) *resource.Resource {

attributes = append(r.Attributes(), attributes...)

baseOptions := []resource.Option{
resource.WithSchemaURL(semconv.SchemaURL),
resource.WithAttributes(attributes...),
}

options := append(baseOptions, c.ResourceOptions...)

// These detectors can't actually fail, ignoring the error.
r, _ = resource.New(
context.Background(),
resource.WithSchemaURL(semconv.SchemaURL),
resource.WithAttributes(attributes...),
options...,
)

// Note: There are new detectors we may wish to take advantage
Expand Down
17 changes: 16 additions & 1 deletion otelconfig/otelconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ func TestEnvironmentVariables(t *testing.T) {
unsetEnvironment()
}

type testDetector struct{}

var _ resource.Detector = (*testDetector)(nil)

// Detect implements resource.Detector.
func (testDetector) Detect(ctx context.Context) (*resource.Resource, error) {
return resource.New(ctx)
}

func TestConfigurationOverrides(t *testing.T) {
setEnvironment()
logger := &testLogger{}
Expand All @@ -397,10 +406,12 @@ func TestConfigurationOverrides(t *testing.T) {
WithExporterProtocol("http/protobuf"),
WithMetricsExporterProtocol("http/protobuf"),
WithTracesExporterProtocol("http/protobuf"),
WithResourceOption(resource.WithAttributes(attribute.String("host.name", "hardcoded-hostname"))),
WithResourceOption(resource.WithDetectors(&testDetector{})),
)

attributes := []attribute.KeyValue{
attribute.String("host.name", host()),
attribute.String("host.name", "hardcoded-hostname"),
attribute.String("service.name", "override-service-name"),
attribute.String("service.version", "override-service-version"),
attribute.String("telemetry.sdk.name", "otelconfig"),
Expand Down Expand Up @@ -433,6 +444,10 @@ func TestConfigurationOverrides(t *testing.T) {
MetricsExporterProtocol: "http/protobuf",
errorHandler: handler,
Sampler: trace.AlwaysSample(),
ResourceOptions: []resource.Option{
resource.WithAttributes(attribute.String("host.name", "hardcoded-hostname")),
resource.WithDetectors(&testDetector{}),
},
}
assert.Equal(t, expected, config)
unsetEnvironment()
Expand Down