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

Accept scope attributes during Tracer creation #3739

Merged
merged 8 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
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