-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspan_opt.go
115 lines (89 loc) · 2.27 KB
/
span_opt.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
110
111
112
113
114
115
package klogga
import "time"
type SpanOption interface {
apply(*Span)
}
type SpanOptionFunc func(*Span)
func (f SpanOptionFunc) apply(span *Span) { f(span) }
type withTimestampOption struct {
ts time.Time
}
// WithTimestamp make span to have started with custom timestamp
// to create a done span, use WithDone
func WithTimestamp(ts time.Time) SpanOption {
return &withTimestampOption{ts: ts}
}
func (o withTimestampOption) apply(span *Span) {
span.startedTs = o.ts
}
func WithTimestampUtcNow() SpanOption {
return (SpanOptionFunc)(func(span *Span) {
span.startedTs = time.Now().UTC()
})
}
func WithTimestampNow() SpanOption {
return (SpanOptionFunc)(func(span *Span) {
span.startedTs = time.Now()
})
}
func WithNewSpanID() SpanOption {
return (SpanOptionFunc)(func(span *Span) {
span.id = NewSpanID()
})
}
func WithHostName() SpanOption {
return (SpanOptionFunc)(func(span *Span) {
span.host = host
})
}
type withNameOption struct {
name string
}
func WithName(name string) SpanOption {
return &withNameOption{name: name}
}
func (o withNameOption) apply(span *Span) {
span.name = o.name
}
type withTraceIDOption struct {
traceID TraceID
}
func WithTraceID(traceID TraceID) SpanOption {
return &withTraceIDOption{traceID: traceID}
}
func (o withTraceIDOption) apply(span *Span) {
span.traceID = o.traceID
}
type withParentSpanIDOption struct {
parentSpanID SpanID
}
func WithParentSpanID(parentSpanID SpanID) SpanOption {
return &withParentSpanIDOption{parentSpanID: parentSpanID}
}
func (o withParentSpanIDOption) apply(span *Span) {
span.parentID = o.parentSpanID
}
type withDurationOption struct {
ts time.Time
duration time.Duration
}
// WithDone make already finished span
func WithDone(ts time.Time, duration time.Duration) SpanOption {
return &withDurationOption{ts: ts, duration: duration}
}
func (o withDurationOption) apply(span *Span) {
span.startedTs = o.ts
span.duration = o.duration
span.finishedTs = o.ts.Add(o.duration)
}
type withPackageClassOption struct {
p, c string
}
// WithPackageClass overrides reflection-retrieved package and class
func WithPackageClass(p, c string) SpanOption {
return &withPackageClassOption{p: p, c: c}
}
func (o withPackageClassOption) apply(span *Span) {
span.packageName = o.p
span.className = o.c
}