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

Add Attributes to Instrumentation Scope #2884

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ func InstrumentationScope(il instrumentation.Library) *commonpb.InstrumentationS
return &commonpb.InstrumentationScope{
Name: il.Name,
Version: il.Version,
// TODO: use Library attributes here once they are added to OTLP InstrumentationScope.
}
}
4 changes: 2 additions & 2 deletions exporters/otlp/otlptrace/internal/tracetransform/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Spans(sdl []tracesdk.ReadOnlySpan) []*tracepb.ResourceSpans {

type key struct {
r attribute.Distinct
il instrumentation.Library
il instrumentation.LibraryDistinct
}
ssm := make(map[key]*tracepb.ScopeSpans)

Expand All @@ -47,7 +47,7 @@ func Spans(sdl []tracesdk.ReadOnlySpan) []*tracepb.ResourceSpans {
rKey := sd.Resource().Equivalent()
k := key{
r: rKey,
il: sd.InstrumentationLibrary(),
il: sd.InstrumentationLibrary().Equivalent(),
}
scopeSpan, iOk := ssm[k]
if !iOk {
Expand Down
3 changes: 2 additions & 1 deletion exporters/stdout/stdouttrace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ func expectedJSON(now time.Time) string {
"InstrumentationLibrary": {
"Name": "",
"Version": "",
"SchemaURL": ""
"SchemaURL": "",
"Attrs": null
}
}
`
Expand Down
22 changes: 22 additions & 0 deletions sdk/instrumentation/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ For more information see
[this](https://github.com/open-telemetry/oteps/blob/main/text/0083-component.md).
*/
package instrumentation // import "go.opentelemetry.io/otel/sdk/instrumentation"
import "go.opentelemetry.io/otel/attribute"

// Library represents the instrumentation library.
type Library struct {
Expand All @@ -30,4 +31,25 @@ type Library struct {
Version string
// SchemaURL of the telemetry emitted by the library.
SchemaURL string
// Scope attributes.
Attrs attribute.Set
}

type LibraryDistinct struct {
name string
version string
schemaURL string
attrs attribute.Distinct
}

// Equivalent returns an object that can be compared for equality
// between two libraries. This value is suitable for use as a key in
// a map.
func (l Library) Equivalent() LibraryDistinct {
return LibraryDistinct{
name: l.Name,
version: l.Version,
schemaURL: l.SchemaURL,
attrs: l.Attrs.Equivalent(),
}
}
1 change: 1 addition & 0 deletions sdk/trace/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T
Name: name,
Version: c.InstrumentationVersion(),
SchemaURL: c.SchemaURL(),
Attrs: c.ScopeAttributes(),
}
t, ok := p.namedTracer[il]
if !ok {
Expand Down
20 changes: 13 additions & 7 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ func TestSetSpanAttributesOnStart(t *testing.T) {
}

want := &snapshot{
resource: resource.Empty(),
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
Expand All @@ -414,9 +415,7 @@ func TestSetSpanAttributesOnStart(t *testing.T) {
spanKind: trace.SpanKindInternal,
instrumentationLibrary: instrumentation.Library{Name: "StartSpanAttribute"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("SetSpanAttributesOnStart: -got +want %s", diff)
}
assert.EqualValues(t, want, got)
}

func TestSamplerAttributesLocalChildSpan(t *testing.T) {
Expand Down Expand Up @@ -1449,13 +1448,15 @@ func TestWithInstrumentationVersionAndSchema(t *testing.T) {
"WithInstrumentationVersion",
trace.WithInstrumentationVersion("v0.1.0"),
trace.WithSchemaURL("https://opentelemetry.io/schemas/1.2.0"),
trace.WithScopeAttributes(attribute.String("scope_key", "scope_val")),
).Start(ctx, "span0")
got, err := endSpan(te, span)
if err != nil {
t.Error(err.Error())
}

want := &snapshot{
resource: resource.Empty(),
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
Expand All @@ -1467,11 +1468,10 @@ func TestWithInstrumentationVersionAndSchema(t *testing.T) {
Name: "WithInstrumentationVersion",
Version: "v0.1.0",
SchemaURL: "https://opentelemetry.io/schemas/1.2.0",
Attrs: attribute.NewSet(attribute.String("scope_key", "scope_val")),
},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("WithResource:\n -got +want %s", diff)
}
assert.EqualValues(t, want, got)
}

func TestSpanCapturesPanic(t *testing.T) {
Expand Down Expand Up @@ -1526,7 +1526,8 @@ func TestReadOnlySpan(t *testing.T) {
kv := attribute.String("foo", "bar")

tp := NewTracerProvider(WithResource(resource.NewSchemaless(kv)))
tr := tp.Tracer("ReadOnlySpan", trace.WithInstrumentationVersion("3"))
tr := tp.Tracer("ReadOnlySpan", trace.WithInstrumentationVersion("3"),
trace.WithScopeAttributes(attribute.String("scope_key", "scope_val")))

// Initialize parent context.
tID, sID := tp.idGenerator.NewIDs(context.Background())
Expand Down Expand Up @@ -1575,6 +1576,11 @@ func TestReadOnlySpan(t *testing.T) {
assert.Equal(t, kv.Key, ro.Resource().Attributes()[0].Key)
assert.Equal(t, kv.Value, ro.Resource().Attributes()[0].Value)

scopeAttrs := ro.InstrumentationLibrary().Attrs
v, ok := scopeAttrs.Value("scope_key")
assert.True(t, ok)
assert.EqualValues(t, "scope_val", v.AsString())

// Verify changes to the original span are reflected in the ReadOnlySpan.
s.SetName("bar")
assert.Equal(t, "bar", ro.Name())
Expand Down
21 changes: 21 additions & 0 deletions trace/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type TracerConfig struct {
instrumentationVersion string
// Schema URL of the telemetry emitted by the Tracer.
schemaURL string
// Attributes of the Scope associated with the Tracer.
scopeAttrs attribute.Set
}

// InstrumentationVersion returns the version of the library providing instrumentation.
Expand All @@ -37,6 +39,11 @@ func (t *TracerConfig) SchemaURL() string {
return t.schemaURL
}

// ScopeAttributes returns the Schema URL of the telemetry emitted by the Tracer.
func (t *TracerConfig) ScopeAttributes() attribute.Set {
return t.scopeAttrs
}

// NewTracerConfig applies all the options to a returned TracerConfig.
func NewTracerConfig(options ...TracerOption) TracerConfig {
var config TracerConfig
Expand Down Expand Up @@ -314,3 +321,17 @@ func WithSchemaURL(schemaURL string) TracerOption {
return cfg
})
}

// WithScopeAttributes sets the attributes of the Scope associated with the Tracer.
func WithScopeAttributes(scopeAttrs ...attribute.KeyValue) TracerOption {
return tracerOptionFunc(func(cfg TracerConfig) TracerConfig {
// Ensure attributes comply with the specification:
// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.0.1/specification/common/common.md#attributes
s, _ := attribute.NewSetWithFiltered(
scopeAttrs, func(kv attribute.KeyValue) bool {
return kv.Valid()
})
cfg.scopeAttrs = s
return cfg
})
}