-
Notifications
You must be signed in to change notification settings - Fork 56
/
scheduling_trace.go
109 lines (87 loc) · 2.99 KB
/
scheduling_trace.go
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
104
105
106
107
108
109
/*
Copyright 2023 The Godel Scheduler Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tracing
import (
"context"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
)
type SchedulingTrace interface {
GetRootSpanContext() SpanContext
NewTraceContext(parentSpanName, name string, options ...trace.SpanOption) TraceContext
GetTraceContext(name string) TraceContext
}
type NoopSchedulingTrace struct{}
func (n *NoopSchedulingTrace) GetRootSpanContext() SpanContext {
return NewEmptySpanContext()
}
func (n *NoopSchedulingTrace) NewTraceContext(parentSpanName, name string, options ...trace.SpanOption) TraceContext {
return noopTraceContext{}
}
func (n *NoopSchedulingTrace) GetTraceContext(name string) TraceContext {
return noopTraceContext{}
}
var _ SchedulingTrace = &schedulingTraceImpl{}
type schedulingTraceImpl struct {
componentName string
rootSpanContext SpanContext
// key is span name
contexts map[string]TraceContext
// basicSpanOptions will be applied to each span
basicSpanOptions []trace.SpanOption
}
func NewSchedulingTrace(pod *v1.Pod, options ...trace.SpanOption) *schedulingTraceImpl {
rootSpanContext := GetSpanContextFromPod(pod)
options = []trace.SpanOption{
trace.WithAttributes(attribute.String(PodTag, klog.KObj(pod).String())),
}
if rootSpanContext.IsEmpty() {
rootTraceContext := createTraceContext(context.Background(), RootSpan, NewEmptySpanContext(), options...)
rootSpanContext = rootTraceContext.SpanContext()
rootTraceContext.Finish()
}
nOpts := len(options)
return &schedulingTraceImpl{
rootSpanContext: rootSpanContext,
basicSpanOptions: options[:nOpts:nOpts],
contexts: map[string]TraceContext{},
}
}
func (p *schedulingTraceImpl) GetRootSpanContext() SpanContext {
return p.rootSpanContext
}
func (p *schedulingTraceImpl) NewTraceContext(parentSpanName, name string, options ...trace.SpanOption) TraceContext {
opts := append(p.basicSpanOptions, options...)
parentSpanCtx := p.rootSpanContext
if parentSpanName != RootSpan {
pCtx := p.contexts[parentSpanName]
if pCtx != nil {
parentSpanCtx = pCtx.SpanContext()
}
}
ctx := createTraceContext(context.Background(), name, parentSpanCtx, opts...)
p.contexts[name] = ctx
return ctx
}
func (p *schedulingTraceImpl) GetTraceContext(name string) TraceContext {
return p.contexts[name]
}
func AsyncFinishTraceContext(t TraceContext, endTime time.Time) {
if t == nil {
return
}
go t.FinishWithOptions(trace.WithTimestamp(endTime))
}