forked from toolshed/abra
chore: go mod tidy / vendor / make deps
This commit is contained in:
138
vendor/go.opentelemetry.io/otel/sdk/trace/tracer.go
generated
vendored
138
vendor/go.opentelemetry.io/otel/sdk/trace/tracer.go
generated
vendored
@ -7,7 +7,9 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/sdk/instrumentation"
|
||||
"go.opentelemetry.io/otel/semconv/v1.37.0/otelconv"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"go.opentelemetry.io/otel/trace/embedded"
|
||||
)
|
||||
@ -17,6 +19,10 @@ type tracer struct {
|
||||
|
||||
provider *TracerProvider
|
||||
instrumentationScope instrumentation.Scope
|
||||
|
||||
selfObservabilityEnabled bool
|
||||
spanLiveMetric otelconv.SDKSpanLive
|
||||
spanStartedMetric otelconv.SDKSpanStarted
|
||||
}
|
||||
|
||||
var _ trace.Tracer = &tracer{}
|
||||
@ -46,17 +52,25 @@ func (tr *tracer) Start(
|
||||
}
|
||||
|
||||
s := tr.newSpan(ctx, name, &config)
|
||||
newCtx := trace.ContextWithSpan(ctx, s)
|
||||
if tr.selfObservabilityEnabled {
|
||||
psc := trace.SpanContextFromContext(ctx)
|
||||
set := spanStartedSet(psc, s)
|
||||
tr.spanStartedMetric.AddSet(newCtx, 1, set)
|
||||
}
|
||||
|
||||
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 {
|
||||
ctx = rtt.runtimeTrace(ctx)
|
||||
newCtx = rtt.runtimeTrace(newCtx)
|
||||
}
|
||||
|
||||
return trace.ContextWithSpan(ctx, s), s
|
||||
return newCtx, s
|
||||
}
|
||||
|
||||
type runtimeTracer interface {
|
||||
@ -112,11 +126,12 @@ func (tr *tracer) newSpan(ctx context.Context, name string, config *trace.SpanCo
|
||||
if !isRecording(samplingResult) {
|
||||
return tr.newNonRecordingSpan(sc)
|
||||
}
|
||||
return tr.newRecordingSpan(psc, sc, name, samplingResult, config)
|
||||
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,
|
||||
@ -153,6 +168,14 @@ func (tr *tracer) newRecordingSpan(
|
||||
s.SetAttributes(sr.Attributes...)
|
||||
s.SetAttributes(config.Attributes()...)
|
||||
|
||||
if tr.selfObservabilityEnabled {
|
||||
// Propagate any existing values from the context with the new span to
|
||||
// the measurement context.
|
||||
ctx = trace.ContextWithSpan(ctx, s)
|
||||
set := spanLiveSet(s.spanContext.IsSampled())
|
||||
tr.spanLiveMetric.AddSet(ctx, 1, set)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
@ -160,3 +183,112 @@ func (tr *tracer) newRecordingSpan(
|
||||
func (tr *tracer) newNonRecordingSpan(sc trace.SpanContext) nonRecordingSpan {
|
||||
return nonRecordingSpan{tracer: tr, sc: sc}
|
||||
}
|
||||
|
||||
type parentState int
|
||||
|
||||
const (
|
||||
parentStateNoParent parentState = iota
|
||||
parentStateLocalParent
|
||||
parentStateRemoteParent
|
||||
)
|
||||
|
||||
type samplingState int
|
||||
|
||||
const (
|
||||
samplingStateDrop samplingState = iota
|
||||
samplingStateRecordOnly
|
||||
samplingStateRecordAndSample
|
||||
)
|
||||
|
||||
type spanStartedSetKey struct {
|
||||
parent parentState
|
||||
sampling samplingState
|
||||
}
|
||||
|
||||
var spanStartedSetCache = map[spanStartedSetKey]attribute.Set{
|
||||
{parentStateNoParent, samplingStateDrop}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
|
||||
),
|
||||
{parentStateLocalParent, samplingStateDrop}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
|
||||
),
|
||||
{parentStateRemoteParent, samplingStateDrop}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
|
||||
),
|
||||
|
||||
{parentStateNoParent, samplingStateRecordOnly}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
|
||||
),
|
||||
{parentStateLocalParent, samplingStateRecordOnly}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
|
||||
),
|
||||
{parentStateRemoteParent, samplingStateRecordOnly}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
|
||||
),
|
||||
|
||||
{parentStateNoParent, samplingStateRecordAndSample}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
|
||||
),
|
||||
{parentStateLocalParent, samplingStateRecordAndSample}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
|
||||
),
|
||||
{parentStateRemoteParent, samplingStateRecordAndSample}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
|
||||
),
|
||||
}
|
||||
|
||||
func spanStartedSet(psc trace.SpanContext, span trace.Span) attribute.Set {
|
||||
key := spanStartedSetKey{
|
||||
parent: parentStateNoParent,
|
||||
sampling: samplingStateDrop,
|
||||
}
|
||||
|
||||
if psc.IsValid() {
|
||||
if psc.IsRemote() {
|
||||
key.parent = parentStateRemoteParent
|
||||
} else {
|
||||
key.parent = parentStateLocalParent
|
||||
}
|
||||
}
|
||||
|
||||
if span.IsRecording() {
|
||||
if span.SpanContext().IsSampled() {
|
||||
key.sampling = samplingStateRecordAndSample
|
||||
} else {
|
||||
key.sampling = samplingStateRecordOnly
|
||||
}
|
||||
}
|
||||
|
||||
return spanStartedSetCache[key]
|
||||
}
|
||||
|
||||
type spanLiveSetKey struct {
|
||||
sampled bool
|
||||
}
|
||||
|
||||
var spanLiveSetCache = map[spanLiveSetKey]attribute.Set{
|
||||
{true}: attribute.NewSet(
|
||||
otelconv.SDKSpanLive{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordAndSample,
|
||||
),
|
||||
),
|
||||
{false}: attribute.NewSet(
|
||||
otelconv.SDKSpanLive{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordOnly,
|
||||
),
|
||||
),
|
||||
}
|
||||
|
||||
func spanLiveSet(sampled bool) attribute.Set {
|
||||
key := spanLiveSetKey{sampled: sampled}
|
||||
return spanLiveSetCache[key]
|
||||
}
|
||||
|
Reference in New Issue
Block a user