1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-04-13 11:30:31 +02:00

Accept scope attributes during Tracer creation (#3739)

* Accept scope attributes during Tracer creation

The OTel specification requires the instrumentation attributes are
accepted by the API for the Tracer. This adds a TracerOption to satisfy
that requirement.
This commit is contained in:
Tyler Yahn 2023-02-21 11:31:37 -08:00 committed by GitHub
parent 1c5aed692c
commit 99ec432679
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 40 deletions

View File

@ -31,6 +31,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `OtelLibraryVersion` -> `OTelLibraryVersion` - `OtelLibraryVersion` -> `OTelLibraryVersion`
- `OtelStatusDescription` -> `OTelStatusDescription` - `OtelStatusDescription` -> `OTelStatusDescription`
- The `WithInstrumentationAttributes` option to `go.opentelemetry.io/otel/metric`. (#3738) - The `WithInstrumentationAttributes` option to `go.opentelemetry.io/otel/metric`. (#3738)
- The `WithInstrumentationAttributes` option to `go.opentelemetry.io/otel/trace`. (#3739)
### Changed ### Changed

View File

@ -25,6 +25,7 @@ type TracerConfig struct {
instrumentationVersion string instrumentationVersion string
// Schema URL of the telemetry emitted by the Tracer. // Schema URL of the telemetry emitted by the Tracer.
schemaURL string schemaURL string
attrs attribute.Set
} }
// InstrumentationVersion returns the version of the library providing instrumentation. // InstrumentationVersion returns the version of the library providing instrumentation.
@ -32,6 +33,12 @@ func (t *TracerConfig) InstrumentationVersion() string {
return t.instrumentationVersion 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. // SchemaURL returns the Schema URL of the telemetry emitted by the Tracer.
func (t *TracerConfig) SchemaURL() string { func (t *TracerConfig) SchemaURL() string {
return t.schemaURL 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. // WithSchemaURL sets the schema URL for the Tracer.
func WithSchemaURL(schemaURL string) TracerOption { func WithSchemaURL(schemaURL string) TracerOption {
return tracerOptionFunc(func(cfg TracerConfig) TracerConfig { return tracerOptionFunc(func(cfg TracerConfig) TracerConfig {

View File

@ -211,46 +211,22 @@ func TestTracerConfig(t *testing.T) {
v1 := "semver:0.0.1" v1 := "semver:0.0.1"
v2 := "semver:1.0.0" v2 := "semver:1.0.0"
schemaURL := "https://opentelemetry.io/schemas/1.2.0" schemaURL := "https://opentelemetry.io/schemas/1.2.0"
tests := []struct { attrs := attribute.NewSet(
options []TracerOption attribute.String("user", "alice"),
expected TracerConfig attribute.Bool("admin", true),
}{ )
{
// No non-zero-values should be set. c := NewTracerConfig(
[]TracerOption{}, // Multiple calls should overwrite.
TracerConfig{}, WithInstrumentationVersion(v1),
}, WithInstrumentationVersion(v2),
{ WithSchemaURL(schemaURL),
[]TracerOption{ WithInstrumentationAttributes(attrs.ToSlice()...),
WithInstrumentationVersion(v1), )
},
TracerConfig{ assert.Equal(t, v2, c.InstrumentationVersion(), "instrumentation version")
instrumentationVersion: v1, assert.Equal(t, schemaURL, c.SchemaURL(), "schema URL")
}, assert.Equal(t, attrs, c.InstrumentationAttributes(), "instrumentation attributes")
},
{
[]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)
}
} }
// Save benchmark results to a file level var to avoid the compiler optimizing // Save benchmark results to a file level var to avoid the compiler optimizing