/
mockspan.go
175 lines (153 loc) · 3.27 KB
/
mockspan.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
// 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 mocktracer // import "github.com/DataDog/dd-trace-go/v2/ddtrace/mocktracer"
import (
"fmt"
"time"
"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
)
func newSpan(operationName string, cfg *tracer.StartSpanConfig) *tracer.Span {
return tracer.SpanStart(operationName, func(c *tracer.StartSpanConfig) {
*c = *cfg
})
}
type Span struct {
sp *tracer.Span
m map[string]interface{}
}
func MockSpan(s *tracer.Span) *Span {
if s == nil {
return nil
}
return &Span{sp: s, m: s.AsMap()}
}
func (s *Span) OperationName() string {
if s == nil {
return ""
}
return s.m[ext.SpanName].(string)
}
func (s *Span) SetTag(k string, v interface{}) {
if s == nil {
return
}
s.m[k] = v
s.sp.SetTag(k, v)
}
func (s *Span) AddLink(spanContext *tracer.SpanContext, attributes map[string]string) {
s.sp.AddLink(spanContext, attributes)
}
func (s *Span) Tag(k string) interface{} {
if s == nil {
return nil
}
// It's possible that a tag wasn't set through mocktracer.Span.SetTag,
// in which case we need to retrieve it from the underlying tracer.Span.
v := s.sp.AsMap()[k]
if v != nil {
return v
}
v, ok := s.m[k]
if !ok {
return nil
}
return v
}
func (s *Span) Tags() map[string]interface{} {
if s == nil {
return make(map[string]interface{})
}
tm := s.sp.AsMap()
m := make(map[string]interface{}, len(s.m)+len(tm))
extractTags(s.m, m)
extractTags(tm, m)
return m
}
func extractTags(src, m map[string]interface{}) {
for k, v := range src {
switch k {
case ext.MapSpanStart:
continue
case ext.MapSpanDuration:
continue
case ext.MapSpanID:
continue
case ext.MapSpanTraceID:
continue
case ext.MapSpanParentID:
continue
case ext.MapSpanError:
continue
}
m[k] = v
}
}
func (s *Span) String() string {
if s == nil {
return ""
}
sc := s.sp.Context()
baggage := make(map[string]string)
sc.ForeachBaggageItem(func(k, v string) bool {
baggage[k] = v
return true
})
return fmt.Sprintf(`
name: %s
tags: %#v
start: %s
duration: %s
id: %d
parent: %d
trace: %v
baggage: %#v
`, s.OperationName(), s.Tags(), s.StartTime(), s.Duration(), sc.SpanID(), s.ParentID(), sc.TraceID(), baggage)
}
func (s *Span) ParentID() uint64 {
if s == nil {
return 0
}
return s.m[ext.MapSpanParentID].(uint64)
}
func (s *Span) SpanID() uint64 {
if s == nil {
return 0
}
return s.m[ext.MapSpanID].(uint64)
}
func (s *Span) TraceID() uint64 {
if s == nil {
return 0
}
return s.m[ext.MapSpanTraceID].(uint64)
}
func (s *Span) StartTime() time.Time {
if s == nil {
return time.Unix(0, 0)
}
return time.Unix(0, s.m[ext.MapSpanStart].(int64))
}
func (s *Span) Duration() time.Duration {
if s == nil {
return time.Duration(0)
}
return time.Duration(s.m[ext.MapSpanDuration].(int64))
}
func (s *Span) FinishTime() time.Time {
if s == nil {
return time.Unix(0, 0)
}
return s.StartTime().Add(s.Duration())
}
func (s *Span) Unwrap() *tracer.Span {
if s == nil {
return nil
}
return s.sp
}
func (s *Span) Context() *tracer.SpanContext {
return s.sp.Context()
}