...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package trace
16
17 import (
18 "time"
19
20 "go.opentelemetry.io/otel/attribute"
21 )
22
23
24 type TracerConfig struct {
25 instrumentationVersion string
26
27 schemaURL string
28 attrs attribute.Set
29 }
30
31
32 func (t *TracerConfig) InstrumentationVersion() string {
33 return t.instrumentationVersion
34 }
35
36
37
38 func (t *TracerConfig) InstrumentationAttributes() attribute.Set {
39 return t.attrs
40 }
41
42
43 func (t *TracerConfig) SchemaURL() string {
44 return t.schemaURL
45 }
46
47
48 func NewTracerConfig(options ...TracerOption) TracerConfig {
49 var config TracerConfig
50 for _, option := range options {
51 config = option.apply(config)
52 }
53 return config
54 }
55
56
57 type TracerOption interface {
58 apply(TracerConfig) TracerConfig
59 }
60
61 type tracerOptionFunc func(TracerConfig) TracerConfig
62
63 func (fn tracerOptionFunc) apply(cfg TracerConfig) TracerConfig {
64 return fn(cfg)
65 }
66
67
68 type SpanConfig struct {
69 attributes []attribute.KeyValue
70 timestamp time.Time
71 links []Link
72 newRoot bool
73 spanKind SpanKind
74 stackTrace bool
75 }
76
77
78 func (cfg *SpanConfig) Attributes() []attribute.KeyValue {
79 return cfg.attributes
80 }
81
82
83 func (cfg *SpanConfig) Timestamp() time.Time {
84 return cfg.timestamp
85 }
86
87
88 func (cfg *SpanConfig) StackTrace() bool {
89 return cfg.stackTrace
90 }
91
92
93 func (cfg *SpanConfig) Links() []Link {
94 return cfg.links
95 }
96
97
98
99
100 func (cfg *SpanConfig) NewRoot() bool {
101 return cfg.newRoot
102 }
103
104
105 func (cfg *SpanConfig) SpanKind() SpanKind {
106 return cfg.spanKind
107 }
108
109
110
111
112
113 func NewSpanStartConfig(options ...SpanStartOption) SpanConfig {
114 var c SpanConfig
115 for _, option := range options {
116 c = option.applySpanStart(c)
117 }
118 return c
119 }
120
121
122
123
124
125 func NewSpanEndConfig(options ...SpanEndOption) SpanConfig {
126 var c SpanConfig
127 for _, option := range options {
128 c = option.applySpanEnd(c)
129 }
130 return c
131 }
132
133
134
135 type SpanStartOption interface {
136 applySpanStart(SpanConfig) SpanConfig
137 }
138
139 type spanOptionFunc func(SpanConfig) SpanConfig
140
141 func (fn spanOptionFunc) applySpanStart(cfg SpanConfig) SpanConfig {
142 return fn(cfg)
143 }
144
145
146
147 type SpanEndOption interface {
148 applySpanEnd(SpanConfig) SpanConfig
149 }
150
151
152 type EventConfig struct {
153 attributes []attribute.KeyValue
154 timestamp time.Time
155 stackTrace bool
156 }
157
158
159 func (cfg *EventConfig) Attributes() []attribute.KeyValue {
160 return cfg.attributes
161 }
162
163
164 func (cfg *EventConfig) Timestamp() time.Time {
165 return cfg.timestamp
166 }
167
168
169 func (cfg *EventConfig) StackTrace() bool {
170 return cfg.stackTrace
171 }
172
173
174
175
176
177 func NewEventConfig(options ...EventOption) EventConfig {
178 var c EventConfig
179 for _, option := range options {
180 c = option.applyEvent(c)
181 }
182 if c.timestamp.IsZero() {
183 c.timestamp = time.Now()
184 }
185 return c
186 }
187
188
189 type EventOption interface {
190 applyEvent(EventConfig) EventConfig
191 }
192
193
194 type SpanOption interface {
195 SpanStartOption
196 SpanEndOption
197 }
198
199
200 type SpanStartEventOption interface {
201 SpanStartOption
202 EventOption
203 }
204
205
206 type SpanEndEventOption interface {
207 SpanEndOption
208 EventOption
209 }
210
211 type attributeOption []attribute.KeyValue
212
213 func (o attributeOption) applySpan(c SpanConfig) SpanConfig {
214 c.attributes = append(c.attributes, []attribute.KeyValue(o)...)
215 return c
216 }
217 func (o attributeOption) applySpanStart(c SpanConfig) SpanConfig { return o.applySpan(c) }
218 func (o attributeOption) applyEvent(c EventConfig) EventConfig {
219 c.attributes = append(c.attributes, []attribute.KeyValue(o)...)
220 return c
221 }
222
223 var _ SpanStartEventOption = attributeOption{}
224
225
226
227
228
229
230
231
232
233
234 func WithAttributes(attributes ...attribute.KeyValue) SpanStartEventOption {
235 return attributeOption(attributes)
236 }
237
238
239 type SpanEventOption interface {
240 SpanOption
241 EventOption
242 }
243
244 type timestampOption time.Time
245
246 func (o timestampOption) applySpan(c SpanConfig) SpanConfig {
247 c.timestamp = time.Time(o)
248 return c
249 }
250 func (o timestampOption) applySpanStart(c SpanConfig) SpanConfig { return o.applySpan(c) }
251 func (o timestampOption) applySpanEnd(c SpanConfig) SpanConfig { return o.applySpan(c) }
252 func (o timestampOption) applyEvent(c EventConfig) EventConfig {
253 c.timestamp = time.Time(o)
254 return c
255 }
256
257 var _ SpanEventOption = timestampOption{}
258
259
260
261 func WithTimestamp(t time.Time) SpanEventOption {
262 return timestampOption(t)
263 }
264
265 type stackTraceOption bool
266
267 func (o stackTraceOption) applyEvent(c EventConfig) EventConfig {
268 c.stackTrace = bool(o)
269 return c
270 }
271
272 func (o stackTraceOption) applySpan(c SpanConfig) SpanConfig {
273 c.stackTrace = bool(o)
274 return c
275 }
276 func (o stackTraceOption) applySpanEnd(c SpanConfig) SpanConfig { return o.applySpan(c) }
277
278
279 func WithStackTrace(b bool) SpanEndEventOption {
280 return stackTraceOption(b)
281 }
282
283
284
285 func WithLinks(links ...Link) SpanStartOption {
286 return spanOptionFunc(func(cfg SpanConfig) SpanConfig {
287 cfg.links = append(cfg.links, links...)
288 return cfg
289 })
290 }
291
292
293
294
295 func WithNewRoot() SpanStartOption {
296 return spanOptionFunc(func(cfg SpanConfig) SpanConfig {
297 cfg.newRoot = true
298 return cfg
299 })
300 }
301
302
303 func WithSpanKind(kind SpanKind) SpanStartOption {
304 return spanOptionFunc(func(cfg SpanConfig) SpanConfig {
305 cfg.spanKind = kind
306 return cfg
307 })
308 }
309
310
311 func WithInstrumentationVersion(version string) TracerOption {
312 return tracerOptionFunc(func(cfg TracerConfig) TracerConfig {
313 cfg.instrumentationVersion = version
314 return cfg
315 })
316 }
317
318
319
320
321 func WithInstrumentationAttributes(attr ...attribute.KeyValue) TracerOption {
322 return tracerOptionFunc(func(config TracerConfig) TracerConfig {
323 config.attrs = attribute.NewSet(attr...)
324 return config
325 })
326 }
327
328
329 func WithSchemaURL(schemaURL string) TracerOption {
330 return tracerOptionFunc(func(cfg TracerConfig) TracerConfig {
331 cfg.schemaURL = schemaURL
332 return cfg
333 })
334 }
335
View as plain text