tracer.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package trace // import "go.opentelemetry.io/otel/sdk/trace"
  15. import (
  16. "context"
  17. "time"
  18. "go.opentelemetry.io/otel/sdk/instrumentation"
  19. "go.opentelemetry.io/otel/trace"
  20. )
  21. type tracer struct {
  22. provider *TracerProvider
  23. instrumentationLibrary instrumentation.Library
  24. }
  25. var _ trace.Tracer = &tracer{}
  26. // Start starts a Span and returns it along with a context containing it.
  27. //
  28. // The Span is created with the provided name and as a child of any existing
  29. // span context found in the passed context. The created Span will be
  30. // configured appropriately by any SpanOption passed.
  31. func (tr *tracer) Start(ctx context.Context, name string, options ...trace.SpanStartOption) (context.Context, trace.Span) {
  32. config := trace.NewSpanStartConfig(options...)
  33. // For local spans created by this SDK, track child span count.
  34. if p := trace.SpanFromContext(ctx); p != nil {
  35. if sdkSpan, ok := p.(*recordingSpan); ok {
  36. sdkSpan.addChild()
  37. }
  38. }
  39. s := tr.newSpan(ctx, name, &config)
  40. if rw, ok := s.(ReadWriteSpan); ok && s.IsRecording() {
  41. sps, _ := tr.provider.spanProcessors.Load().(spanProcessorStates)
  42. for _, sp := range sps {
  43. sp.sp.OnStart(ctx, rw)
  44. }
  45. }
  46. if rtt, ok := s.(runtimeTracer); ok {
  47. ctx = rtt.runtimeTrace(ctx)
  48. }
  49. return trace.ContextWithSpan(ctx, s), s
  50. }
  51. type runtimeTracer interface {
  52. // runtimeTrace starts a "runtime/trace".Task for the span and
  53. // returns a context containing the task.
  54. runtimeTrace(ctx context.Context) context.Context
  55. }
  56. // newSpan returns a new configured span.
  57. func (tr *tracer) newSpan(ctx context.Context, name string, config *trace.SpanConfig) trace.Span {
  58. // If told explicitly to make this a new root use a zero value SpanContext
  59. // as a parent which contains an invalid trace ID and is not remote.
  60. var psc trace.SpanContext
  61. if config.NewRoot() {
  62. ctx = trace.ContextWithSpanContext(ctx, psc)
  63. } else {
  64. psc = trace.SpanContextFromContext(ctx)
  65. }
  66. // If there is a valid parent trace ID, use it to ensure the continuity of
  67. // the trace. Always generate a new span ID so other components can rely
  68. // on a unique span ID, even if the Span is non-recording.
  69. var tid trace.TraceID
  70. var sid trace.SpanID
  71. if !psc.TraceID().IsValid() {
  72. tid, sid = tr.provider.idGenerator.NewIDs(ctx)
  73. } else {
  74. tid = psc.TraceID()
  75. sid = tr.provider.idGenerator.NewSpanID(ctx, tid)
  76. }
  77. samplingResult := tr.provider.sampler.ShouldSample(SamplingParameters{
  78. ParentContext: ctx,
  79. TraceID: tid,
  80. Name: name,
  81. Kind: config.SpanKind(),
  82. Attributes: config.Attributes(),
  83. Links: config.Links(),
  84. })
  85. scc := trace.SpanContextConfig{
  86. TraceID: tid,
  87. SpanID: sid,
  88. TraceState: samplingResult.Tracestate,
  89. }
  90. if isSampled(samplingResult) {
  91. scc.TraceFlags = psc.TraceFlags() | trace.FlagsSampled
  92. } else {
  93. scc.TraceFlags = psc.TraceFlags() &^ trace.FlagsSampled
  94. }
  95. sc := trace.NewSpanContext(scc)
  96. if !isRecording(samplingResult) {
  97. return tr.newNonRecordingSpan(sc)
  98. }
  99. return tr.newRecordingSpan(psc, sc, name, samplingResult, config)
  100. }
  101. // newRecordingSpan returns a new configured recordingSpan.
  102. func (tr *tracer) newRecordingSpan(psc, sc trace.SpanContext, name string, sr SamplingResult, config *trace.SpanConfig) *recordingSpan {
  103. startTime := config.Timestamp()
  104. if startTime.IsZero() {
  105. startTime = time.Now()
  106. }
  107. s := &recordingSpan{
  108. // Do not pre-allocate the attributes slice here! Doing so will
  109. // allocate memory that is likely never going to be used, or if used,
  110. // will be over-sized. The default Go compiler has been tested to
  111. // dynamically allocate needed space very well. Benchmarking has shown
  112. // it to be more performant than what we can predetermine here,
  113. // especially for the common use case of few to no added
  114. // attributes.
  115. parent: psc,
  116. spanContext: sc,
  117. spanKind: trace.ValidateSpanKind(config.SpanKind()),
  118. name: name,
  119. startTime: startTime,
  120. events: newEvictedQueue(tr.provider.spanLimits.EventCountLimit),
  121. links: newEvictedQueue(tr.provider.spanLimits.LinkCountLimit),
  122. tracer: tr,
  123. }
  124. for _, l := range config.Links() {
  125. s.addLink(l)
  126. }
  127. s.SetAttributes(sr.Attributes...)
  128. s.SetAttributes(config.Attributes()...)
  129. return s
  130. }
  131. // newNonRecordingSpan returns a new configured nonRecordingSpan.
  132. func (tr *tracer) newNonRecordingSpan(sc trace.SpanContext) nonRecordingSpan {
  133. return nonRecordingSpan{tracer: tr, sc: sc}
  134. }