Skip to content

Commit

Permalink
Do not store TracerProvider or Tracer fields in SDK recordingSpan (#2575
Browse files Browse the repository at this point in the history
)

* Do not store TracerProvider fields in span

Instead of keeping a reference to the span's Tracer, and therefore also
it's TracerProvider, and the associated resource and spanLimits just
keep the reference to the Tracer. Refer to the TracerProvider fields
when needed instead.

* Make span refer to the inst lib via the Tracer

Instead of holding a field in the span, refer to the field in the parent
Tracer.
  • Loading branch information
MrAlias committed Feb 1, 2022
1 parent bd817df commit d5292e3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
11 changes: 7 additions & 4 deletions sdk/trace/provider.go
Expand Up @@ -74,10 +74,13 @@ type TracerProvider struct {
mu sync.Mutex
namedTracer map[instrumentation.Library]*tracer
spanProcessors atomic.Value
sampler Sampler
idGenerator IDGenerator
spanLimits SpanLimits
resource *resource.Resource

// These fields are not protected by the lock mu. They are assumed to be
// immutable after creation of the TracerProvider.
sampler Sampler
idGenerator IDGenerator
spanLimits SpanLimits
resource *resource.Resource
}

var _ trace.TracerProvider = &TracerProvider{}
Expand Down
31 changes: 10 additions & 21 deletions sdk/trace/span.go
Expand Up @@ -126,14 +126,6 @@ type recordingSpan struct {
// childSpanCount holds the number of child spans created for this span.
childSpanCount int

// resource contains attributes representing an entity that produced this
// span.
resource *resource.Resource

// instrumentationLibrary defines the instrumentation library used to
// provide instrumentation.
instrumentationLibrary instrumentation.Library

// spanContext holds the SpanContext of this span.
spanContext trace.SpanContext

Expand All @@ -152,9 +144,6 @@ type recordingSpan struct {

// tracer is the SDK tracer that created this span.
tracer *tracer

// spanLimits holds the limits to this span.
spanLimits SpanLimits
}

var _ ReadWriteSpan = (*recordingSpan)(nil)
Expand Down Expand Up @@ -336,9 +325,9 @@ func (s *recordingSpan) addEvent(name string, o ...trace.EventOption) {
// Discard over limited attributes
attributes := c.Attributes()
var discarded int
if len(attributes) > s.spanLimits.AttributePerEventCountLimit {
discarded = len(attributes) - s.spanLimits.AttributePerEventCountLimit
attributes = attributes[:s.spanLimits.AttributePerEventCountLimit]
if len(attributes) > s.tracer.provider.spanLimits.AttributePerEventCountLimit {
discarded = len(attributes) - s.tracer.provider.spanLimits.AttributePerEventCountLimit
attributes = attributes[:s.tracer.provider.spanLimits.AttributePerEventCountLimit]
}
s.mu.Lock()
defer s.mu.Unlock()
Expand Down Expand Up @@ -440,15 +429,15 @@ func (s *recordingSpan) Status() Status {
func (s *recordingSpan) InstrumentationLibrary() instrumentation.Library {
s.mu.Lock()
defer s.mu.Unlock()
return s.instrumentationLibrary
return s.tracer.instrumentationLibrary
}

// Resource returns the Resource associated with the Tracer that created this
// span.
func (s *recordingSpan) Resource() *resource.Resource {
s.mu.Lock()
defer s.mu.Unlock()
return s.resource
return s.tracer.provider.resource
}

func (s *recordingSpan) addLink(link trace.Link) {
Expand All @@ -461,9 +450,9 @@ func (s *recordingSpan) addLink(link trace.Link) {
var droppedAttributeCount int

// Discard over limited attributes
if len(link.Attributes) > s.spanLimits.AttributePerLinkCountLimit {
droppedAttributeCount = len(link.Attributes) - s.spanLimits.AttributePerLinkCountLimit
link.Attributes = link.Attributes[:s.spanLimits.AttributePerLinkCountLimit]
if len(link.Attributes) > s.tracer.provider.spanLimits.AttributePerLinkCountLimit {
droppedAttributeCount = len(link.Attributes) - s.tracer.provider.spanLimits.AttributePerLinkCountLimit
link.Attributes = link.Attributes[:s.tracer.provider.spanLimits.AttributePerLinkCountLimit]
}

s.links.add(Link{link.SpanContext, link.Attributes, droppedAttributeCount})
Expand Down Expand Up @@ -514,10 +503,10 @@ func (s *recordingSpan) snapshot() ReadOnlySpan {
defer s.mu.Unlock()

sd.endTime = s.endTime
sd.instrumentationLibrary = s.instrumentationLibrary
sd.instrumentationLibrary = s.tracer.instrumentationLibrary
sd.name = s.name
sd.parent = s.parent
sd.resource = s.resource
sd.resource = s.tracer.provider.resource
sd.spanContext = s.spanContext
sd.spanKind = s.spanKind
sd.startTime = s.startTime
Expand Down
21 changes: 9 additions & 12 deletions sdk/trace/tracer.go
Expand Up @@ -122,18 +122,15 @@ func (tr *tracer) newRecordingSpan(psc, sc trace.SpanContext, name string, sr Sa
}

s := &recordingSpan{
parent: psc,
spanContext: sc,
spanKind: trace.ValidateSpanKind(config.SpanKind()),
name: name,
startTime: startTime,
attributes: newAttributesMap(tr.provider.spanLimits.AttributeCountLimit),
events: newEvictedQueue(tr.provider.spanLimits.EventCountLimit),
links: newEvictedQueue(tr.provider.spanLimits.LinkCountLimit),
tracer: tr,
spanLimits: tr.provider.spanLimits,
resource: tr.provider.resource,
instrumentationLibrary: tr.instrumentationLibrary,
parent: psc,
spanContext: sc,
spanKind: trace.ValidateSpanKind(config.SpanKind()),
name: name,
startTime: startTime,
attributes: newAttributesMap(tr.provider.spanLimits.AttributeCountLimit),
events: newEvictedQueue(tr.provider.spanLimits.EventCountLimit),
links: newEvictedQueue(tr.provider.spanLimits.LinkCountLimit),
tracer: tr,
}

for _, l := range config.Links() {
Expand Down

0 comments on commit d5292e3

Please sign in to comment.