You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-11-25 22:41:46 +02:00
Split from #7316
[Follow
guidelines](a5dcd68ebb/CONTRIBUTING.md (encapsulation))
and move instrumentation into its own type.
### Benchmarks
#### Added `sdk/trace/internal/observ` benchmarks
```
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/otel/sdk/trace/internal/observ
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
│ enc-trace-sdk-tracer-obs.out │
│ sec/op │
Tracer/SpanStarted-8 7.436n ± 6%
Tracer/SpanLive-8 9.987n ± 8%
Tracer/SpanEnded-8 11.32n ± 7%
NewTracer-8 87.64n ± 6%
geomean 16.48n
│ enc-trace-sdk-tracer-obs.out │
│ B/op │
Tracer/SpanStarted-8 0.000 ± 0%
Tracer/SpanLive-8 0.000 ± 0%
Tracer/SpanEnded-8 0.000 ± 0%
NewTracer-8 0.000 ± 0%
geomean ¹
¹ summaries must be >0 to compute geomean
│ enc-trace-sdk-tracer-obs.out │
│ allocs/op │
Tracer/SpanStarted-8 0.000 ± 0%
Tracer/SpanLive-8 0.000 ± 0%
Tracer/SpanEnded-8 0.000 ± 0%
NewTracer-8 0.000 ± 0%
geomean ¹
¹ summaries must be >0 to compute geomean
```
#### Existing `sdk/trace` benchmarks
```console
> benchstat main.out enc-trace-sdk-tracer-obs.out
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/otel/sdk/trace
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
│ main.out │ enc-trace-sdk-tracer-obs.out │
│ sec/op │ sec/op vs base │
SpanEnd/ObservabilityEnabled-8 188.5n ± 4% 131.5n ± 32% -30.24% (p=0.000 n=10)
TraceStart/ObservabilityEnabled-8 886.9n ± 8% 663.9n ± 2% -25.14% (p=0.000 n=10)
geomean 408.9n 295.5n -27.73%
│ main.out │ enc-trace-sdk-tracer-obs.out │
│ B/op │ B/op vs base │
SpanEnd/ObservabilityEnabled-8 16.00 ± 0% 0.00 ± 0% -100.00% (p=0.000 n=10)
TraceStart/ObservabilityEnabled-8 608.0 ± 0% 576.0 ± 0% -5.26% (p=0.000 n=10)
geomean 98.63 ? ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean
│ main.out │ enc-trace-sdk-tracer-obs.out │
│ allocs/op │ allocs/op vs base │
SpanEnd/ObservabilityEnabled-8 1.000 ± 0% 0.000 ± 0% -100.00% (p=0.000 n=10)
TraceStart/ObservabilityEnabled-8 5.000 ± 0% 3.000 ± 0% -40.00% (p=0.000 n=10)
geomean 2.236 ? ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean
```
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
189 lines
5.4 KiB
Go
189 lines
5.4 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package trace // import "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
|
"go.opentelemetry.io/otel/sdk/trace/internal/observ"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"go.opentelemetry.io/otel/trace/embedded"
|
|
)
|
|
|
|
type tracer struct {
|
|
embedded.Tracer
|
|
|
|
provider *TracerProvider
|
|
instrumentationScope instrumentation.Scope
|
|
|
|
inst observ.Tracer
|
|
}
|
|
|
|
var _ trace.Tracer = &tracer{}
|
|
|
|
// Start starts a Span and returns it along with a context containing it.
|
|
//
|
|
// The Span is created with the provided name and as a child of any existing
|
|
// span context found in the passed context. The created Span will be
|
|
// configured appropriately by any SpanOption passed.
|
|
func (tr *tracer) Start(
|
|
ctx context.Context,
|
|
name string,
|
|
options ...trace.SpanStartOption,
|
|
) (context.Context, trace.Span) {
|
|
config := trace.NewSpanStartConfig(options...)
|
|
|
|
if ctx == nil {
|
|
// Prevent trace.ContextWithSpan from panicking.
|
|
ctx = context.Background()
|
|
}
|
|
|
|
// For local spans created by this SDK, track child span count.
|
|
if p := trace.SpanFromContext(ctx); p != nil {
|
|
if sdkSpan, ok := p.(*recordingSpan); ok {
|
|
sdkSpan.addChild()
|
|
}
|
|
}
|
|
|
|
s := tr.newSpan(ctx, name, &config)
|
|
newCtx := trace.ContextWithSpan(ctx, s)
|
|
if tr.inst.Enabled() {
|
|
if o, ok := s.(interface{ setOrigCtx(context.Context) }); ok {
|
|
// If this is a recording span, store the original context.
|
|
// This allows later retrieval of baggage and other information
|
|
// that may have been stored in the context at span start time and
|
|
// to avoid the allocation of repeatedly calling
|
|
// trace.ContextWithSpan.
|
|
o.setOrigCtx(newCtx)
|
|
}
|
|
psc := trace.SpanContextFromContext(ctx)
|
|
tr.inst.SpanStarted(newCtx, psc, s)
|
|
}
|
|
|
|
if rw, ok := s.(ReadWriteSpan); ok && s.IsRecording() {
|
|
sps := tr.provider.getSpanProcessors()
|
|
for _, sp := range sps {
|
|
// Use original context.
|
|
sp.sp.OnStart(ctx, rw)
|
|
}
|
|
}
|
|
if rtt, ok := s.(runtimeTracer); ok {
|
|
newCtx = rtt.runtimeTrace(newCtx)
|
|
}
|
|
|
|
return newCtx, s
|
|
}
|
|
|
|
type runtimeTracer interface {
|
|
// runtimeTrace starts a "runtime/trace".Task for the span and
|
|
// returns a context containing the task.
|
|
runtimeTrace(ctx context.Context) context.Context
|
|
}
|
|
|
|
// newSpan returns a new configured span.
|
|
func (tr *tracer) newSpan(ctx context.Context, name string, config *trace.SpanConfig) trace.Span {
|
|
// If told explicitly to make this a new root use a zero value SpanContext
|
|
// as a parent which contains an invalid trace ID and is not remote.
|
|
var psc trace.SpanContext
|
|
if config.NewRoot() {
|
|
ctx = trace.ContextWithSpanContext(ctx, psc)
|
|
} else {
|
|
psc = trace.SpanContextFromContext(ctx)
|
|
}
|
|
|
|
// If there is a valid parent trace ID, use it to ensure the continuity of
|
|
// the trace. Always generate a new span ID so other components can rely
|
|
// on a unique span ID, even if the Span is non-recording.
|
|
var tid trace.TraceID
|
|
var sid trace.SpanID
|
|
if !psc.TraceID().IsValid() {
|
|
tid, sid = tr.provider.idGenerator.NewIDs(ctx)
|
|
} else {
|
|
tid = psc.TraceID()
|
|
sid = tr.provider.idGenerator.NewSpanID(ctx, tid)
|
|
}
|
|
|
|
samplingResult := tr.provider.sampler.ShouldSample(SamplingParameters{
|
|
ParentContext: ctx,
|
|
TraceID: tid,
|
|
Name: name,
|
|
Kind: config.SpanKind(),
|
|
Attributes: config.Attributes(),
|
|
Links: config.Links(),
|
|
})
|
|
|
|
scc := trace.SpanContextConfig{
|
|
TraceID: tid,
|
|
SpanID: sid,
|
|
TraceState: samplingResult.Tracestate,
|
|
}
|
|
if isSampled(samplingResult) {
|
|
scc.TraceFlags = psc.TraceFlags() | trace.FlagsSampled
|
|
} else {
|
|
scc.TraceFlags = psc.TraceFlags() &^ trace.FlagsSampled
|
|
}
|
|
sc := trace.NewSpanContext(scc)
|
|
|
|
if !isRecording(samplingResult) {
|
|
return tr.newNonRecordingSpan(sc)
|
|
}
|
|
return tr.newRecordingSpan(ctx, psc, sc, name, samplingResult, config)
|
|
}
|
|
|
|
// newRecordingSpan returns a new configured recordingSpan.
|
|
func (tr *tracer) newRecordingSpan(
|
|
ctx context.Context,
|
|
psc, sc trace.SpanContext,
|
|
name string,
|
|
sr SamplingResult,
|
|
config *trace.SpanConfig,
|
|
) *recordingSpan {
|
|
startTime := config.Timestamp()
|
|
if startTime.IsZero() {
|
|
startTime = time.Now()
|
|
}
|
|
|
|
s := &recordingSpan{
|
|
// Do not pre-allocate the attributes slice here! Doing so will
|
|
// allocate memory that is likely never going to be used, or if used,
|
|
// will be over-sized. The default Go compiler has been tested to
|
|
// dynamically allocate needed space very well. Benchmarking has shown
|
|
// it to be more performant than what we can predetermine here,
|
|
// especially for the common use case of few to no added
|
|
// attributes.
|
|
|
|
parent: psc,
|
|
spanContext: sc,
|
|
spanKind: trace.ValidateSpanKind(config.SpanKind()),
|
|
name: name,
|
|
startTime: startTime,
|
|
events: newEvictedQueueEvent(tr.provider.spanLimits.EventCountLimit),
|
|
links: newEvictedQueueLink(tr.provider.spanLimits.LinkCountLimit),
|
|
tracer: tr,
|
|
}
|
|
|
|
for _, l := range config.Links() {
|
|
s.AddLink(l)
|
|
}
|
|
|
|
s.SetAttributes(sr.Attributes...)
|
|
s.SetAttributes(config.Attributes()...)
|
|
|
|
if tr.inst.Enabled() {
|
|
// Propagate any existing values from the context with the new span to
|
|
// the measurement context.
|
|
ctx = trace.ContextWithSpan(ctx, s)
|
|
tr.inst.SpanLive(ctx, s)
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
// newNonRecordingSpan returns a new configured nonRecordingSpan.
|
|
func (tr *tracer) newNonRecordingSpan(sc trace.SpanContext) nonRecordingSpan {
|
|
return nonRecordingSpan{tracer: tr, sc: sc}
|
|
}
|