diff --git a/CHANGELOG.md b/CHANGELOG.md index f17a2c4ee0f..1de8428f827 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - `OtelLibraryVersion` -> `OTelLibraryVersion` - `OtelStatusDescription` -> `OTelStatusDescription` - The `WithInstrumentationAttributes` option to `go.opentelemetry.io/otel/metric`. (#3738) +- The `WithInstrumentationAttributes` option to `go.opentelemetry.io/otel/trace`. (#3739) ### Changed diff --git a/trace/config.go b/trace/config.go index f058cc781e0..cb3efbb9ad8 100644 --- a/trace/config.go +++ b/trace/config.go @@ -25,6 +25,7 @@ type TracerConfig struct { instrumentationVersion string // Schema URL of the telemetry emitted by the Tracer. schemaURL string + attrs attribute.Set } // InstrumentationVersion returns the version of the library providing instrumentation. @@ -32,6 +33,12 @@ func (t *TracerConfig) InstrumentationVersion() string { return t.instrumentationVersion } +// InstrumentationAttributes returns the attributes associated with the library +// providing instrumentation. +func (t *TracerConfig) InstrumentationAttributes() attribute.Set { + return t.attrs +} + // SchemaURL returns the Schema URL of the telemetry emitted by the Tracer. func (t *TracerConfig) SchemaURL() string { return t.schemaURL @@ -307,6 +314,16 @@ func WithInstrumentationVersion(version string) TracerOption { }) } +// WithInstrumentationAttributes sets the instrumentation attributes. +// +// The passed attributes will be de-duplicated. +func WithInstrumentationAttributes(attr ...attribute.KeyValue) TracerOption { + return tracerOptionFunc(func(config TracerConfig) TracerConfig { + config.attrs = attribute.NewSet(attr...) + return config + }) +} + // WithSchemaURL sets the schema URL for the Tracer. func WithSchemaURL(schemaURL string) TracerOption { return tracerOptionFunc(func(cfg TracerConfig) TracerConfig { diff --git a/trace/config_test.go b/trace/config_test.go index a4cafcbcd09..50c071169fa 100644 --- a/trace/config_test.go +++ b/trace/config_test.go @@ -211,46 +211,22 @@ func TestTracerConfig(t *testing.T) { v1 := "semver:0.0.1" v2 := "semver:1.0.0" schemaURL := "https://opentelemetry.io/schemas/1.2.0" - tests := []struct { - options []TracerOption - expected TracerConfig - }{ - { - // No non-zero-values should be set. - []TracerOption{}, - TracerConfig{}, - }, - { - []TracerOption{ - WithInstrumentationVersion(v1), - }, - TracerConfig{ - instrumentationVersion: v1, - }, - }, - { - []TracerOption{ - // Multiple calls should overwrite. - WithInstrumentationVersion(v1), - WithInstrumentationVersion(v2), - }, - TracerConfig{ - instrumentationVersion: v2, - }, - }, - { - []TracerOption{ - WithSchemaURL(schemaURL), - }, - TracerConfig{ - schemaURL: schemaURL, - }, - }, - } - for _, test := range tests { - config := NewTracerConfig(test.options...) - assert.Equal(t, test.expected, config) - } + attrs := attribute.NewSet( + attribute.String("user", "alice"), + attribute.Bool("admin", true), + ) + + c := NewTracerConfig( + // Multiple calls should overwrite. + WithInstrumentationVersion(v1), + WithInstrumentationVersion(v2), + WithSchemaURL(schemaURL), + WithInstrumentationAttributes(attrs.ToSlice()...), + ) + + assert.Equal(t, v2, c.InstrumentationVersion(), "instrumentation version") + assert.Equal(t, schemaURL, c.SchemaURL(), "schema URL") + assert.Equal(t, attrs, c.InstrumentationAttributes(), "instrumentation attributes") } // Save benchmark results to a file level var to avoid the compiler optimizing