-
Notifications
You must be signed in to change notification settings - Fork 536
Expand file tree
/
Copy pathcontext.go
More file actions
103 lines (95 loc) · 3.94 KB
/
Copy pathcontext.go
File metadata and controls
103 lines (95 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package tracer
import (
"context"
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
"github.com/DataDog/dd-trace-go/v2/internal"
illmobs "github.com/DataDog/dd-trace-go/v2/internal/llmobs"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/orchestrion"
)
// ContextWithSpan returns a copy of the given context which includes the span s.
// If ctx is nil, a new background context is created to avoid panicking.
func ContextWithSpan(ctx context.Context, s *Span) context.Context {
if ctx == nil {
log.Warn("ContextWithSpan: received nil context, falling back to context.Background()")
ctx = context.Background()
}
newCtx := orchestrion.CtxWithValue(ctx, internal.ActiveSpanKey, s)
return contextWithPropagatedLLMSpan(newCtx, s)
}
func contextWithPropagatedLLMSpan(ctx context.Context, s *Span) context.Context {
if s == nil {
return ctx
}
// if there is a propagated llm span already just skip
if _, ok := illmobs.PropagatedLLMSpanFromContext(ctx); ok {
return ctx
}
propagatedLLMObs := propagatedLLMSpanFromTags(s)
if propagatedLLMObs.SpanID == "" || propagatedLLMObs.TraceID == "" {
return ctx
}
return illmobs.ContextWithPropagatedLLMSpan(ctx, propagatedLLMObs)
}
// propagatedLLMSpanFromTags extracts LLMObs propagation information from the trace propagating tags.
// This is used during distributed tracing to set the correct parent span for the current span.
func propagatedLLMSpanFromTags(s *Span) *illmobs.PropagatedLLMSpan {
propagatedLLMObs := &illmobs.PropagatedLLMSpan{}
if s.context == nil || s.context.trace == nil {
return propagatedLLMObs
}
if parentID := s.context.trace.propagatingTag(keyPropagatedLLMObsParentID); parentID != "" {
propagatedLLMObs.SpanID = parentID
}
if mlApp := s.context.trace.propagatingTag(keyPropagatedLLMObsMLAPP); mlApp != "" {
propagatedLLMObs.MLApp = mlApp
}
if trID := s.context.trace.propagatingTag(keyPropagatedLLMObsTraceID); trID != "" {
propagatedLLMObs.TraceID = trID
}
return propagatedLLMObs
}
// SpanFromContext returns the span contained in the given context. A second return
// value indicates if a span was found in the context. If no span is found, a no-op
// span is returned.
func SpanFromContext(ctx context.Context) (*Span, bool) {
if ctx == nil {
return nil, false
}
v := orchestrion.WrapContext(ctx).Value(internal.ActiveSpanKey)
if s, ok := v.(*Span); ok {
// We may have a nil *Span wrapped in an interface in the GLS context stack,
// in which case we need to act as if there was nothing (otherwise we'll
// forcefully un-do a [ChildOf] option if one was passed).
if s == nil {
return nil, false
}
return s, true
}
return nil, false
}
// StartSpanFromContext returns a new span with the given operation name and options. If a span
// is found in the context, it will be used as the parent of the resulting span. If the ChildOf
// option is passed, it will only be used as the parent if there is no span found in `ctx`.
// +checklocksignore — Initialization time, span just created by StartSpan, not yet shared.
func StartSpanFromContext(ctx context.Context, operationName string, opts ...StartSpanOption) (*Span, context.Context) {
// copy opts in case the caller reuses the slice in parallel
// we will add at least 1, at most 2 items
optsLocal := options.Expand(opts, 0, 2)
if ctx == nil {
// default to context.Background() to avoid panics on Go >= 1.15
ctx = context.Background()
} else if s, ok := SpanFromContext(ctx); ok {
optsLocal = append(optsLocal, ChildOf(s.Context()))
}
optsLocal = append(optsLocal, withContext(ctx))
s := StartSpan(operationName, optsLocal...)
if s != nil && s.pprofCtxActive != nil {
ctx = s.pprofCtxActive
}
return s, ContextWithSpan(ctx, s)
}