1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-07-03 00:27:03 +02:00

Deprecate Library and move all uses to Scope (#2977)

* Deprecate Library and move all uses to Scope

* Add PR number to changelog

* Don't change signatures in stable modules

* Revert some changes

* Rename internal struct names

* A bit more renaming

* Update sdk/trace/span.go

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Update based on feedback

* Revert change

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
This commit is contained in:
Craig Pastro
2022-07-06 11:55:46 -07:00
committed by GitHub
parent 5795c70e0c
commit 575e1bb270
18 changed files with 156 additions and 122 deletions

View File

@ -17,6 +17,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Support for go1.16. Support is now only for go1.17 and go1.18 (#2917)
### Deprecated
- The `Library` struct in the `go.opentelemetry.io/otel/sdk/instrumentation` package is deprecated.
Use the equivalent `Scope` struct instead. (#2977)
- The `ReadOnlySpan.InstrumentationLibrary` method from the `go.opentelemetry.io/otel/sdk/trace` package is deprecated.
Use the equivalent `ReadOnlySpan.InstrumentationScope` method instead. (#2977)
## [1.7.0/0.30.0] - 2022-04-28
### Added

View File

@ -142,10 +142,10 @@ func spanToThrift(ss sdktrace.ReadOnlySpan) *gen.Span {
}
}
if il := ss.InstrumentationLibrary(); il.Name != "" {
tags = append(tags, getStringTag(keyInstrumentationLibraryName, il.Name))
if il.Version != "" {
tags = append(tags, getStringTag(keyInstrumentationLibraryVersion, il.Version))
if is := ss.InstrumentationScope(); is.Name != "" {
tags = append(tags, getStringTag(keyInstrumentationLibraryName, is.Name))
if is.Version != "" {
tags = append(tags, getStringTag(keyInstrumentationLibraryVersion, is.Version))
}
}

View File

@ -19,8 +19,8 @@ import (
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
)
func InstrumentationScope(il instrumentation.Library) *commonpb.InstrumentationScope {
if il == (instrumentation.Library{}) {
func InstrumentationScope(il instrumentation.Scope) *commonpb.InstrumentationScope {
if il == (instrumentation.Scope{}) {
return nil
}
return &commonpb.InstrumentationScope{

View File

@ -34,7 +34,7 @@ func Spans(sdl []tracesdk.ReadOnlySpan) []*tracepb.ResourceSpans {
type key struct {
r attribute.Distinct
il instrumentation.Library
is instrumentation.Scope
}
ssm := make(map[key]*tracepb.ScopeSpans)
@ -47,15 +47,15 @@ func Spans(sdl []tracesdk.ReadOnlySpan) []*tracepb.ResourceSpans {
rKey := sd.Resource().Equivalent()
k := key{
r: rKey,
il: sd.InstrumentationLibrary(),
is: sd.InstrumentationScope(),
}
scopeSpan, iOk := ssm[k]
if !iOk {
// Either the resource or instrumentation library were unknown.
// Either the resource or instrumentation scope were unknown.
scopeSpan = &tracepb.ScopeSpans{
Scope: InstrumentationScope(sd.InstrumentationLibrary()),
Scope: InstrumentationScope(sd.InstrumentationScope()),
Spans: []*tracepb.Span{},
SchemaUrl: sd.InstrumentationLibrary().SchemaURL,
SchemaUrl: sd.InstrumentationScope().SchemaURL,
}
}
scopeSpan.Spans = append(scopeSpan.Spans, span(sd))

View File

@ -264,7 +264,7 @@ func TestSpanData(t *testing.T) {
attribute.Int64("rk2", 5),
attribute.StringSlice("rk3", []string{"sv1", "sv2"}),
),
InstrumentationLibrary: instrumentation.Library{
InstrumentationLibrary: instrumentation.Scope{
Name: "go.opentelemetry.io/test/otel",
Version: "v0.0.1",
SchemaURL: semconv.SchemaURL,

View File

@ -218,10 +218,10 @@ func toZipkinTags(data tracesdk.ReadOnlySpan) map[string]string {
delete(m, "error")
}
if il := data.InstrumentationLibrary(); il.Name != "" {
m[keyInstrumentationLibraryName] = il.Name
if il.Version != "" {
m[keyInstrumentationLibraryVersion] = il.Version
if is := data.InstrumentationScope(); is.Name != "" {
m[keyInstrumentationLibraryName] = is.Name
if is.Version != "" {
m[keyInstrumentationLibraryVersion] = is.Version
}
}

View File

@ -22,4 +22,5 @@ For more information see
package instrumentation // import "go.opentelemetry.io/otel/sdk/instrumentation"
// Library represents the instrumentation library.
// Deprecated: please use Scope instead.
type Library = Scope

View File

@ -59,7 +59,7 @@ var ErrControllerStarted = fmt.Errorf("controller already started")
type Controller struct {
// lock synchronizes Start() and Stop().
lock sync.Mutex
libraries sync.Map
scopes sync.Map
checkpointerFactory export.CheckpointerFactory
resource *resource.Resource
@ -85,21 +85,21 @@ var _ metric.MeterProvider = &Controller{}
// with opts.
func (c *Controller) Meter(instrumentationName string, opts ...metric.MeterOption) metric.Meter {
cfg := metric.NewMeterConfig(opts...)
library := instrumentation.Library{
scope := instrumentation.Scope{
Name: instrumentationName,
Version: cfg.InstrumentationVersion(),
SchemaURL: cfg.SchemaURL(),
}
m, ok := c.libraries.Load(library)
m, ok := c.scopes.Load(scope)
if !ok {
checkpointer := c.checkpointerFactory.NewCheckpointer()
m, _ = c.libraries.LoadOrStore(
library,
m, _ = c.scopes.LoadOrStore(
scope,
registry.NewUniqueInstrumentMeterImpl(&accumulatorCheckpointer{
Accumulator: sdk.NewAccumulator(checkpointer),
checkpointer: checkpointer,
library: library,
scope: scope,
}))
}
return sdkapi.WrapMeterImpl(m.(*registry.UniqueInstrumentMeterImpl))
@ -108,7 +108,7 @@ func (c *Controller) Meter(instrumentationName string, opts ...metric.MeterOptio
type accumulatorCheckpointer struct {
*sdk.Accumulator
checkpointer export.Checkpointer
library instrumentation.Library
scope instrumentation.Scope
}
var _ sdkapi.MeterImpl = &accumulatorCheckpointer{}
@ -248,7 +248,7 @@ func (c *Controller) collect(ctx context.Context) error {
// registered to this controller. This briefly locks the controller.
func (c *Controller) accumulatorList() []*accumulatorCheckpointer {
var r []*accumulatorCheckpointer
c.libraries.Range(func(key, value interface{}) bool {
c.scopes.Range(func(key, value interface{}) bool {
acc, ok := value.(*registry.UniqueInstrumentMeterImpl).MeterImpl().(*accumulatorCheckpointer)
if ok {
r = append(r, acc)
@ -272,7 +272,7 @@ func (c *Controller) checkpoint(ctx context.Context) error {
}
// checkpointSingleAccumulator checkpoints a single instrumentation
// library's accumulator, which involves calling
// scope's accumulator, which involves calling
// checkpointer.StartCollection, accumulator.Collect, and
// checkpointer.FinishCollection in sequence.
func (c *Controller) checkpointSingleAccumulator(ctx context.Context, ac *accumulatorCheckpointer) error {
@ -330,7 +330,7 @@ func (c *Controller) ForEach(readerFunc func(l instrumentation.Library, r export
if err := func() error {
reader.RLock()
defer reader.RUnlock()
return readerFunc(acPair.library, reader)
return readerFunc(acPair.scope, reader)
}(); err != nil {
return err
}

View File

@ -43,7 +43,7 @@ func getMap(t *testing.T, cont *controller.Controller) map[string]float64 {
out := processortest.NewOutput(attribute.DefaultEncoder())
require.NoError(t, cont.ForEach(
func(_ instrumentation.Library, reader export.Reader) error {
func(_ instrumentation.Scope, reader export.Reader) error {
return reader.ForEach(
aggregation.CumulativeTemporalitySelector(),
func(record export.Record) error {

View File

@ -64,9 +64,12 @@ func NewTestMeterProvider(opts ...Option) (metric.MeterProvider, *Exporter) {
return c, exp
}
// Library is the same as "sdk/instrumentation".Library but there is
// Deprecated: please use Scope instead.
type Library = Scope
// Scope is the same as "sdk/instrumentation".Scope but there is
// a package cycle to use it so it is redeclared here.
type Library struct {
type Scope struct {
InstrumentationName string
InstrumentationVersion string
SchemaURL string

View File

@ -74,7 +74,7 @@ func (cfg tracerProviderConfig) MarshalLog() interface{} {
// instrumentation so it can trace operational flow through a system.
type TracerProvider struct {
mu sync.Mutex
namedTracer map[instrumentation.Library]*tracer
namedTracer map[instrumentation.Scope]*tracer
spanProcessors atomic.Value
// These fields are not protected by the lock mu. They are assumed to be
@ -110,7 +110,7 @@ func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider {
o = ensureValidTracerProviderConfig(o)
tp := &TracerProvider{
namedTracer: make(map[instrumentation.Library]*tracer),
namedTracer: make(map[instrumentation.Scope]*tracer),
sampler: o.sampler,
idGenerator: o.idGenerator,
spanLimits: o.spanLimits,
@ -141,18 +141,18 @@ func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T
if name == "" {
name = defaultTracerName
}
il := instrumentation.Library{
is := instrumentation.Scope{
Name: name,
Version: c.InstrumentationVersion(),
SchemaURL: c.SchemaURL(),
}
t, ok := p.namedTracer[il]
t, ok := p.namedTracer[is]
if !ok {
t = &tracer{
provider: p,
instrumentationLibrary: il,
instrumentationScope: is,
}
p.namedTracer[il] = t
p.namedTracer[is] = t
global.Info("Tracer created", "name", name, "version", c.InstrumentationVersion(), "schemaURL", c.SchemaURL())
}
return t

View File

@ -96,7 +96,7 @@ func TestSchemaURL(t *testing.T) {
// Verify that the SchemaURL of the constructed Tracer is correctly populated.
tracerStruct := tracerIface.(*tracer)
assert.EqualValues(t, schemaURL, tracerStruct.instrumentationLibrary.SchemaURL)
assert.EqualValues(t, schemaURL, tracerStruct.instrumentationScope.SchemaURL)
}
func TestTracerProviderSamplerConfigFromEnv(t *testing.T) {

View File

@ -41,7 +41,7 @@ type snapshot struct {
droppedEventCount int
droppedLinkCount int
resource *resource.Resource
instrumentationLibrary instrumentation.Library
instrumentationScope instrumentation.Scope
}
var _ ReadOnlySpan = snapshot{}
@ -102,10 +102,16 @@ func (s snapshot) Status() Status {
return s.status
}
// InstrumentationScope returns information about the instrumentation
// scope that created the span.
func (s snapshot) InstrumentationScope() instrumentation.Scope {
return s.instrumentationScope
}
// InstrumentationLibrary returns information about the instrumentation
// library that created the span.
func (s snapshot) InstrumentationLibrary() instrumentation.Library {
return s.instrumentationLibrary
return s.instrumentationScope
}
// Resource returns information about the entity that produced the span.

View File

@ -63,8 +63,12 @@ type ReadOnlySpan interface {
Events() []Event
// Status returns the spans status.
Status() Status
// InstrumentationScope returns information about the instrumentation
// scope that created the span.
InstrumentationScope() instrumentation.Scope
// InstrumentationLibrary returns information about the instrumentation
// library that created the span.
// Deprecated: please use InstrumentationScope instead.
InstrumentationLibrary() instrumentation.Library
// Resource returns information about the entity that produced the span.
Resource() *resource.Resource
@ -584,12 +588,20 @@ func (s *recordingSpan) Status() Status {
return s.status
}
// InstrumentationScope returns the instrumentation.Scope associated with
// the Tracer that created this span.
func (s *recordingSpan) InstrumentationScope() instrumentation.Scope {
s.mu.Lock()
defer s.mu.Unlock()
return s.tracer.instrumentationScope
}
// InstrumentationLibrary returns the instrumentation.Library associated with
// the Tracer that created this span.
func (s *recordingSpan) InstrumentationLibrary() instrumentation.Library {
s.mu.Lock()
defer s.mu.Unlock()
return s.tracer.instrumentationLibrary
return s.tracer.instrumentationScope
}
// Resource returns the Resource associated with the Tracer that created this
@ -668,7 +680,7 @@ func (s *recordingSpan) snapshot() ReadOnlySpan {
defer s.mu.Unlock()
sd.endTime = s.endTime
sd.instrumentationLibrary = s.tracer.instrumentationLibrary
sd.instrumentationScope = s.tracer.instrumentationScope
sd.name = s.name
sd.parent = s.parent
sd.resource = s.tracer.provider.resource

View File

@ -67,7 +67,7 @@ func (f InstrumentationBlacklist) ForceFlush(ctx context.Context) error {
return f.Next.ForceFlush(ctx)
}
func (f InstrumentationBlacklist) OnEnd(s ReadOnlySpan) {
if f.Blacklist != nil && f.Blacklist[s.InstrumentationLibrary().Name] {
if f.Blacklist != nil && f.Blacklist[s.InstrumentationScope().Name] {
// Drop spans from this instrumentation
return
}

View File

@ -412,7 +412,7 @@ func TestSetSpanAttributesOnStart(t *testing.T) {
attribute.String("key2", "value2"),
},
spanKind: trace.SpanKindInternal,
instrumentationLibrary: instrumentation.Library{Name: "StartSpanAttribute"},
instrumentationScope: instrumentation.Scope{Name: "StartSpanAttribute"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("SetSpanAttributesOnStart: -got +want %s", diff)
@ -667,7 +667,7 @@ func TestEvents(t *testing.T) {
{Name: "bar", Attributes: []attribute.KeyValue{k2v2, k3v3}},
},
spanKind: trace.SpanKindInternal,
instrumentationLibrary: instrumentation.Library{Name: "Events"},
instrumentationScope: instrumentation.Scope{Name: "Events"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("Message Events: -got +want %s", diff)
@ -719,7 +719,7 @@ func TestEventsOverLimit(t *testing.T) {
},
droppedEventCount: 2,
spanKind: trace.SpanKindInternal,
instrumentationLibrary: instrumentation.Library{Name: "EventsOverLimit"},
instrumentationScope: instrumentation.Scope{Name: "EventsOverLimit"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("Message Event over limit: -got +want %s", diff)
@ -757,7 +757,7 @@ func TestLinks(t *testing.T) {
name: "span0",
links: []Link{{l1.SpanContext, l1.Attributes, 0}, {l2.SpanContext, l2.Attributes, 0}},
spanKind: trace.SpanKindInternal,
instrumentationLibrary: instrumentation.Library{Name: "Links"},
instrumentationScope: instrumentation.Scope{Name: "Links"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("Link: -got +want %s", diff)
@ -813,7 +813,7 @@ func TestLinksOverLimit(t *testing.T) {
},
droppedLinkCount: 1,
spanKind: trace.SpanKindInternal,
instrumentationLibrary: instrumentation.Library{Name: "LinksOverLimit"},
instrumentationScope: instrumentation.Scope{Name: "LinksOverLimit"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("Link over limit: -got +want %s", diff)
@ -861,7 +861,7 @@ func TestSetSpanStatus(t *testing.T) {
Code: codes.Error,
Description: "Error",
},
instrumentationLibrary: instrumentation.Library{Name: "SpanStatus"},
instrumentationScope: instrumentation.Scope{Name: "SpanStatus"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("SetSpanStatus: -got +want %s", diff)
@ -891,7 +891,7 @@ func TestSetSpanStatusWithoutMessageWhenStatusIsNotError(t *testing.T) {
Code: codes.Ok,
Description: "",
},
instrumentationLibrary: instrumentation.Library{Name: "SpanStatus"},
instrumentationScope: instrumentation.Scope{Name: "SpanStatus"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("SetSpanStatus: -got +want %s", diff)
@ -1230,7 +1230,7 @@ func TestRecordError(t *testing.T) {
},
},
},
instrumentationLibrary: instrumentation.Library{Name: "RecordError"},
instrumentationScope: instrumentation.Scope{Name: "RecordError"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("SpanErrorOptions: -got +want %s", diff)
@ -1274,7 +1274,7 @@ func TestRecordErrorWithStackTrace(t *testing.T) {
},
},
},
instrumentationLibrary: instrumentation.Library{Name: "RecordError"},
instrumentationScope: instrumentation.Scope{Name: "RecordError"},
}
assert.Equal(t, got.spanContext, want.spanContext)
@ -1314,7 +1314,7 @@ func TestRecordErrorNil(t *testing.T) {
Code: codes.Unset,
Description: "",
},
instrumentationLibrary: instrumentation.Library{Name: "RecordErrorNil"},
instrumentationScope: instrumentation.Scope{Name: "RecordErrorNil"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("SpanErrorOptions: -got +want %s", diff)
@ -1430,7 +1430,7 @@ func TestWithResource(t *testing.T) {
},
spanKind: trace.SpanKindInternal,
resource: tc.want,
instrumentationLibrary: instrumentation.Library{Name: "WithResource"},
instrumentationScope: instrumentation.Scope{Name: "WithResource"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("WithResource:\n -got +want %s", diff)
@ -1463,7 +1463,7 @@ func TestWithInstrumentationVersionAndSchema(t *testing.T) {
parent: sc.WithRemote(true),
name: "span0",
spanKind: trace.SpanKindInternal,
instrumentationLibrary: instrumentation.Library{
instrumentationScope: instrumentation.Scope{
Name: "WithInstrumentationVersion",
Version: "v0.1.0",
SchemaURL: "https://opentelemetry.io/schemas/1.2.0",
@ -1572,6 +1572,8 @@ func TestReadOnlySpan(t *testing.T) {
assert.Equal(t, "", ro.Status().Description)
assert.Equal(t, "ReadOnlySpan", ro.InstrumentationLibrary().Name)
assert.Equal(t, "3", ro.InstrumentationLibrary().Version)
assert.Equal(t, "ReadOnlySpan", ro.InstrumentationScope().Name)
assert.Equal(t, "3", ro.InstrumentationScope().Version)
assert.Equal(t, kv.Key, ro.Resource().Attributes()[0].Key)
assert.Equal(t, kv.Value, ro.Resource().Attributes()[0].Value)
@ -1696,7 +1698,7 @@ func TestAddEventsWithMoreAttributesThanLimit(t *testing.T) {
},
},
spanKind: trace.SpanKindInternal,
instrumentationLibrary: instrumentation.Library{Name: "AddSpanEventWithOverLimitedAttributes"},
instrumentationScope: instrumentation.Scope{Name: "AddSpanEventWithOverLimitedAttributes"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("SetSpanAttributesOverLimit: -got +want %s", diff)
@ -1751,7 +1753,7 @@ func TestAddLinksWithMoreAttributesThanLimit(t *testing.T) {
},
},
spanKind: trace.SpanKindInternal,
instrumentationLibrary: instrumentation.Library{Name: "Links"},
instrumentationScope: instrumentation.Scope{Name: "Links"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("Link: -got +want %s", diff)

View File

@ -24,7 +24,7 @@ import (
type tracer struct {
provider *TracerProvider
instrumentationLibrary instrumentation.Library
instrumentationScope instrumentation.Scope
}
var _ trace.Tracer = &tracer{}

View File

@ -96,7 +96,7 @@ func SpanStubFromReadOnlySpan(ro tracesdk.ReadOnlySpan) SpanStub {
DroppedLinks: ro.DroppedLinks(),
ChildSpanCount: ro.ChildSpanCount(),
Resource: ro.Resource(),
InstrumentationLibrary: ro.InstrumentationLibrary(),
InstrumentationLibrary: ro.InstrumentationScope(),
}
}
@ -118,7 +118,7 @@ func (s SpanStub) Snapshot() tracesdk.ReadOnlySpan {
droppedLinks: s.DroppedLinks,
childSpanCount: s.ChildSpanCount,
resource: s.Resource,
instrumentationLibrary: s.InstrumentationLibrary,
instrumentationScope: s.InstrumentationLibrary,
}
}
@ -141,7 +141,7 @@ type spanSnapshot struct {
droppedLinks int
childSpanCount int
resource *resource.Resource
instrumentationLibrary instrumentation.Library
instrumentationScope instrumentation.Scope
}
func (s spanSnapshot) Name() string { return s.name }
@ -159,6 +159,9 @@ func (s spanSnapshot) DroppedLinks() int { return s.droppedLinks
func (s spanSnapshot) DroppedEvents() int { return s.droppedEvents }
func (s spanSnapshot) ChildSpanCount() int { return s.childSpanCount }
func (s spanSnapshot) Resource() *resource.Resource { return s.resource }
func (s spanSnapshot) InstrumentationLibrary() instrumentation.Library {
return s.instrumentationLibrary
func (s spanSnapshot) InstrumentationScope() instrumentation.Scope {
return s.instrumentationScope
}
func (s spanSnapshot) InstrumentationLibrary() instrumentation.Library {
return s.instrumentationScope
}