diff --git a/sdk/trace/provider.go b/sdk/trace/provider.go index b4fdcd317a4..c6b311f9cdc 100644 --- a/sdk/trace/provider.go +++ b/sdk/trace/provider.go @@ -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{} diff --git a/sdk/trace/span.go b/sdk/trace/span.go index 0111c475895..bf0c41c1112 100644 --- a/sdk/trace/span.go +++ b/sdk/trace/span.go @@ -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 @@ -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) @@ -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() @@ -440,7 +429,7 @@ 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 @@ -448,7 +437,7 @@ func (s *recordingSpan) InstrumentationLibrary() instrumentation.Library { 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) { @@ -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}) @@ -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 diff --git a/sdk/trace/tracer.go b/sdk/trace/tracer.go index 1177c729a8f..b63b4196516 100644 --- a/sdk/trace/tracer.go +++ b/sdk/trace/tracer.go @@ -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() {