Skip to content

Commit

Permalink
Accept scope attributes during Tracer creation (#3739)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
MrAlias authored Feb 21, 2023
1 parent 1c5aed6 commit 99ec432
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions trace/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ 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.
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
Expand Down Expand Up @@ -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 {
Expand Down
56 changes: 16 additions & 40 deletions trace/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 99ec432

Please sign in to comment.