-
Notifications
You must be signed in to change notification settings - Fork 436
/
span.go
87 lines (74 loc) · 2.49 KB
/
span.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
// 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 opentracer
import (
"fmt"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
)
var _ opentracing.Span = (*span)(nil)
// span implements opentracing.Span on top of ddtrace.Span.
type span struct {
ddtrace.Span
*opentracer
}
func (s *span) Context() opentracing.SpanContext { return s.Span.Context() }
func (s *span) Finish() { s.Span.Finish() }
func (s *span) Tracer() opentracing.Tracer { return s.opentracer }
func (s *span) LogEvent(_ string) { /* deprecated */ }
func (s *span) LogEventWithPayload(_ string, _ interface{}) { /* deprecated */ }
func (s *span) Log(_ opentracing.LogData) { /* deprecated */ }
func (s *span) FinishWithOptions(opts opentracing.FinishOptions) {
for _, lr := range opts.LogRecords {
if len(lr.Fields) > 0 {
s.LogFields(lr.Fields...)
}
}
s.Span.Finish(tracer.FinishTime(opts.FinishTime))
}
func (s *span) LogFields(fields ...log.Field) {
// catch standard opentracing keys and adjust to internal ones as per spec:
// https://github.com/opentracing/specification/blob/master/semantic_conventions.md#log-fields-table
for _, f := range fields {
switch f.Key() {
case "event":
if v, ok := f.Value().(string); ok && v == "error" {
s.SetTag("error", true)
}
case "error", "error.object":
if err, ok := f.Value().(error); ok {
s.SetTag("error", err)
}
case "message":
s.SetTag(ext.ErrorMsg, fmt.Sprint(f.Value()))
case "stack":
s.SetTag(ext.ErrorStack, fmt.Sprint(f.Value()))
default:
// not implemented
}
}
}
func (s *span) LogKV(keyVals ...interface{}) {
fields, err := log.InterleavedKVToFields(keyVals...)
if err != nil {
return
}
s.LogFields(fields...)
}
func (s *span) SetBaggageItem(key, val string) opentracing.Span {
s.Span.SetBaggageItem(key, val)
return s
}
func (s *span) SetOperationName(operationName string) opentracing.Span {
s.Span.SetOperationName(operationName)
return s
}
func (s *span) SetTag(key string, value interface{}) opentracing.Span {
s.Span.SetTag(key, value)
return s
}