-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracer.go
530 lines (456 loc) · 12.8 KB
/
tracer.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package ksmux
import (
"bytes"
"context"
"fmt"
"net/http"
"sync"
"time"
"github.com/kamalshkeir/ksmux/jsonencdec"
)
// ExportType defines the type of trace exporter
type ExportType string
const (
// Export types
ExportTypeJaeger ExportType = "jaeger"
ExportTypeTempo ExportType = "tempo"
ExportTypeZipkin ExportType = "zipkin"
ExportTypeDatadog ExportType = "datadog"
ExportTypeNewRelic ExportType = "newrelic"
ExportTypeHoneycomb ExportType = "honeycomb"
ExportTypeOTLP ExportType = "otlp" // OpenTelemetry Protocol
ExportTypeSignoz ExportType = "signoz" // Open source alternative
// Default endpoints
DefaultJaegerEndpoint = "http://localhost:14268/api/traces"
DefaultTempoEndpoint = "http://localhost:9411/api/v2/spans"
DefaultZipkinEndpoint = "http://localhost:9411/api/v2/spans"
DefaultDatadogEndpoint = "https://trace.agent.datadoghq.com"
DefaultNewRelicEndpoint = "https://trace-api.newrelic.com/trace/v1"
DefaultHoneycombEndpoint = "https://api.honeycomb.io/1/traces"
DefaultOTLPEndpoint = "http://localhost:4318/v1/traces"
DefaultSignozEndpoint = "http://localhost:4318/v1/traces"
// DefaultMaxTraces is the default maximum number of traces to keep in memory
DefaultMaxTraces = 500
)
// Span represents a single operation within a trace
type Span struct {
traceID string
spanID string
parentID string
name string // Operation name
startTime time.Time // Start time of the operation
endTime time.Time // End time of the operation
duration time.Duration
tags map[string]string // Additional metadata
statusCode int // HTTP status code
err error // Error if any
}
// Getter methods
func (s *Span) TraceID() string { return s.traceID }
func (s *Span) SpanID() string { return s.spanID }
func (s *Span) ParentID() string { return s.parentID }
func (s *Span) Name() string { return s.name }
func (s *Span) StartTime() time.Time { return s.startTime }
func (s *Span) EndTime() time.Time { return s.endTime }
func (s *Span) Duration() time.Duration { return s.duration }
func (s *Span) Tags() map[string]string { return s.tags }
func (s *Span) StatusCode() int { return s.statusCode }
func (s *Span) Error() error { return s.err }
// Tracer handles the creation and management of traces
type Tracer struct {
mu sync.RWMutex
spans map[string][]*Span
isEnabled bool
handler TraceHandler
exportEndpoint string
exportType ExportType
maxTraces int
}
// TraceHandler is an interface for custom trace handling
type TraceHandler interface {
HandleTrace(span *Span)
}
var (
globalTracer = &Tracer{
spans: make(map[string][]*Span),
maxTraces: DefaultMaxTraces,
}
)
// EnableTracing enables tracing with an optional custom handler
func EnableTracing(handler TraceHandler) {
globalTracer.mu.Lock()
defer globalTracer.mu.Unlock()
globalTracer.isEnabled = true
globalTracer.handler = handler
}
// DisableTracing disables tracing
func DisableTracing() {
globalTracer.mu.Lock()
defer globalTracer.mu.Unlock()
globalTracer.isEnabled = false
}
const (
traceIDKey ContextKey = "trace_id"
spanIDKey ContextKey = "span_id"
)
// StartSpan creates a new span
func StartSpan(ctx context.Context, name string) (*Span, context.Context) {
if !globalTracer.isEnabled {
return nil, ctx
}
traceID := ctx.Value(traceIDKey)
if traceID == nil {
traceID = GenerateID()
}
parentID := ctx.Value(spanIDKey)
spanID := GenerateID()
// Initialize parentID string
var parentIDStr string
if parentID != nil {
parentIDStr = parentID.(string)
}
span := &Span{
traceID: traceID.(string),
spanID: spanID,
parentID: parentIDStr,
name: name,
startTime: time.Now(),
tags: make(map[string]string),
}
ctx = context.WithValue(ctx, traceIDKey, traceID)
ctx = context.WithValue(ctx, spanIDKey, spanID)
return span, ctx
}
// EndSpan ends a span and records it
func (s *Span) End() {
if s == nil {
return
}
s.endTime = time.Now()
s.duration = s.endTime.Sub(s.startTime)
globalTracer.mu.Lock()
// Check if we need to remove old traces
if len(globalTracer.spans) >= globalTracer.maxTraces {
// Remove oldest trace
var oldestTime time.Time
var oldestTraceID string
first := true
for traceID, spans := range globalTracer.spans {
if len(spans) > 0 {
spanStartTime := spans[0].startTime
if first || spanStartTime.Before(oldestTime) {
oldestTime = spanStartTime
oldestTraceID = traceID
first = false
}
}
}
if oldestTraceID != "" {
delete(globalTracer.spans, oldestTraceID)
}
}
if globalTracer.spans[s.traceID] == nil {
globalTracer.spans[s.traceID] = make([]*Span, 0)
}
globalTracer.spans[s.traceID] = append(globalTracer.spans[s.traceID], s)
globalTracer.mu.Unlock()
if globalTracer.handler != nil {
globalTracer.handler.HandleTrace(s)
}
// Export the span
go s.export()
}
// SetTag adds a tag to the span
func (s *Span) SetTag(key, value string) {
if s == nil {
return
}
s.tags[key] = value
}
// SetError sets an error on the span
func (s *Span) SetError(err error) {
if s == nil {
return
}
s.err = err
}
// SetStatusCode sets the HTTP status code
func (s *Span) SetStatusCode(code int) {
if s == nil {
return
}
s.statusCode = code
}
// SetStatusCode sets the HTTP status code
func (s *Span) SetDuration(d time.Duration) {
if s == nil {
return
}
s.duration = d
}
// GenerateID generates a unique ID for traces and spans
func GenerateID() string {
uuid, _ := GenerateUUID()
return uuid
}
// GetTraces returns all recorded traces
func GetTraces() map[string][]*Span {
globalTracer.mu.RLock()
defer globalTracer.mu.RUnlock()
traces := make(map[string][]*Span)
for k, v := range globalTracer.spans {
traces[k] = v
}
return traces
}
// ClearTraces clears all recorded traces
func ClearTraces() {
globalTracer.mu.Lock()
defer globalTracer.mu.Unlock()
globalTracer.spans = make(map[string][]*Span)
}
// ConfigureExport sets the export endpoint and type
func ConfigureExport(endpoint string, exportType ExportType) {
globalTracer.mu.Lock()
defer globalTracer.mu.Unlock()
// Validate export type
switch exportType {
case ExportTypeJaeger, ExportTypeTempo, ExportTypeZipkin, ExportTypeDatadog, ExportTypeNewRelic, ExportTypeHoneycomb, ExportTypeOTLP, ExportTypeSignoz:
globalTracer.exportType = exportType
default:
// Default to Jaeger if invalid type provided
globalTracer.exportType = ExportTypeJaeger
}
// Use default endpoints if none provided
if endpoint == "" {
switch globalTracer.exportType {
case ExportTypeJaeger:
globalTracer.exportEndpoint = DefaultJaegerEndpoint
case ExportTypeTempo:
globalTracer.exportEndpoint = DefaultTempoEndpoint
case ExportTypeZipkin:
globalTracer.exportEndpoint = DefaultZipkinEndpoint
case ExportTypeDatadog:
globalTracer.exportEndpoint = DefaultDatadogEndpoint
case ExportTypeNewRelic:
globalTracer.exportEndpoint = DefaultNewRelicEndpoint
case ExportTypeHoneycomb:
globalTracer.exportEndpoint = DefaultHoneycombEndpoint
case ExportTypeOTLP:
globalTracer.exportEndpoint = DefaultOTLPEndpoint
case ExportTypeSignoz:
globalTracer.exportEndpoint = DefaultSignozEndpoint
}
} else {
globalTracer.exportEndpoint = endpoint
}
}
// Export the span to the configured endpoint
func (s *Span) export() error {
if globalTracer.exportEndpoint == "" {
return nil
}
var payload interface{}
switch globalTracer.exportType {
case ExportTypeJaeger:
payload = convertToJaeger(s)
case ExportTypeTempo:
payload = convertToTempo(s)
case ExportTypeZipkin:
payload = convertToZipkin(s)
case ExportTypeDatadog:
payload = convertToDatadog(s)
case ExportTypeNewRelic:
payload = convertToNewRelic(s)
case ExportTypeHoneycomb:
payload = convertToHoneycomb(s)
case ExportTypeOTLP:
payload = convertToOTLP(s)
case ExportTypeSignoz:
payload = convertToSignoz(s)
default:
return nil
}
jsonData, err := jsonencdec.DefaultMarshal(payload)
if err != nil {
return err
}
req, err := http.NewRequest("POST", globalTracer.exportEndpoint, bytes.NewBuffer(jsonData))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
// Conversion functions
func convertToJaeger(s *Span) map[string]interface{} {
return map[string]interface{}{
"traceID": s.traceID,
"spanID": s.spanID,
"parentSpanID": s.parentID,
"operationName": s.name,
"startTime": s.startTime.UnixNano() / 1000, // Jaeger expects microseconds
"duration": s.duration.Microseconds(),
"tags": convertTags(s),
}
}
func convertToTempo(s *Span) map[string]interface{} {
return map[string]interface{}{
"traceId": s.traceID,
"id": s.spanID,
"parentId": s.parentID,
"name": s.name,
"timestamp": s.startTime.UnixNano() / 1000,
"duration": s.duration.Microseconds(),
"tags": convertTags(s),
}
}
func convertToZipkin(s *Span) map[string]interface{} {
return map[string]interface{}{
"traceId": s.traceID,
"id": s.spanID,
"parentId": s.parentID,
"name": s.name,
"timestamp": s.startTime.UnixNano() / 1000,
"duration": s.duration.Microseconds(),
"localEndpoint": map[string]string{
"serviceName": "your-service-name",
},
"tags": convertTagsToMap(s),
}
}
func convertToDatadog(s *Span) map[string]interface{} {
return map[string]interface{}{
"trace_id": s.traceID,
"span_id": s.spanID,
"parent_id": s.parentID,
"name": s.name,
"start": s.startTime.UnixNano(),
"duration": s.duration.Nanoseconds(),
"service": "your-service-name",
"resource": s.name,
"meta": convertTagsToMap(s),
}
}
func convertToNewRelic(s *Span) map[string]interface{} {
return map[string]interface{}{
"id": s.spanID,
"trace.id": s.traceID,
"parent.id": s.parentID,
"name": s.name,
"timestamp": s.startTime.UnixNano() / 1000000, // milliseconds
"duration.ms": s.duration.Milliseconds(),
"attributes": convertTagsToMap(s),
}
}
func convertToHoneycomb(s *Span) map[string]interface{} {
data := make(map[string]interface{})
// Copy tags
for k, v := range convertTagsToMap(s) {
data[k] = v
}
data["name"] = s.name
data["trace.trace_id"] = s.traceID
data["trace.parent_id"] = s.parentID
data["trace.span_id"] = s.spanID
data["duration_ms"] = fmt.Sprintf("%d", s.duration.Milliseconds())
return data
}
func convertToOTLP(s *Span) map[string]interface{} {
var errMsg string
if s.err != nil {
errMsg = s.err.Error()
}
return map[string]interface{}{
"traceId": s.traceID,
"spanId": s.spanID,
"parentSpanId": s.parentID,
"name": s.name,
"startTimeUnixNano": s.startTime.UnixNano(),
"endTimeUnixNano": s.endTime.UnixNano(),
"attributes": convertTagsToOTLP(s),
"status": map[string]interface{}{
"code": s.statusCode,
"message": errMsg,
},
}
}
func convertToSignoz(s *Span) map[string]interface{} {
return map[string]interface{}{
"traceID": s.traceID,
"spanID": s.spanID,
"parentSpanID": s.parentID,
"name": s.name,
"startTime": s.startTime.UnixNano() / 1000,
"duration": s.duration.Microseconds(),
"tags": convertTags(s),
}
}
func convertTags(s *Span) []map[string]interface{} {
tags := make([]map[string]interface{}, 0)
// Add status code tag
if s.statusCode != 0 {
tags = append(tags, map[string]interface{}{
"key": "http.status_code",
"type": "int64",
"value": s.statusCode,
})
}
// Add error tag
if s.err != nil {
tags = append(tags, map[string]interface{}{
"key": "error",
"type": "string",
"value": s.err.Error(),
})
}
// Add custom tags
for k, v := range s.tags {
tags = append(tags, map[string]interface{}{
"key": k,
"type": "string",
"value": v,
})
}
return tags
}
// Helper function for flat tag conversion
func convertTagsToMap(s *Span) map[string]string {
tags := make(map[string]string)
if s.statusCode != 0 {
tags["http.status_code"] = fmt.Sprintf("%d", s.statusCode)
}
if s.err != nil {
tags["error"] = s.err.Error()
}
for k, v := range s.tags {
tags[k] = v
}
return tags
}
// Helper function for OTLP attributes
func convertTagsToOTLP(s *Span) []map[string]interface{} {
attrs := make([]map[string]interface{}, 0)
if s.statusCode != 0 {
attrs = append(attrs, map[string]interface{}{
"key": "http.status_code",
"value": map[string]interface{}{
"intValue": s.statusCode,
},
})
}
// ... similar for other tags
return attrs
}
// SetMaxTraces sets the maximum number of traces to keep in memory
func SetMaxTraces(max int) {
globalTracer.mu.Lock()
defer globalTracer.mu.Unlock()
globalTracer.maxTraces = max
}