-
Notifications
You must be signed in to change notification settings - Fork 100
/
span.go
159 lines (130 loc) · 3.71 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
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
// Copyright 2022 The OpenZipkin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zipkintracer
import (
"fmt"
"time"
otobserver "github.com/opentracing-contrib/go-observer"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"github.com/openzipkin/zipkin-go"
)
// FinisherWithDuration allows to finish span with given duration
type FinisherWithDuration interface {
FinishedWithDuration(d time.Duration)
}
type spanImpl struct {
tracer *tracerImpl
zipkinSpan zipkin.Span
startTime time.Time
observer otobserver.SpanObserver
}
func (s *spanImpl) SetOperationName(operationName string) opentracing.Span {
if s.observer != nil {
s.observer.OnSetOperationName(operationName)
}
s.zipkinSpan.SetName(operationName)
return s
}
func (s *spanImpl) SetTag(key string, value interface{}) opentracing.Span {
if s.observer != nil {
s.observer.OnSetTag(key, value)
}
if key == string(ext.SamplingPriority) {
// there are no means for now to change the sampling decision
// but when finishedSpanHandler is in place we could change this.
return s
}
if key == string(ext.SpanKind) ||
key == string(ext.PeerService) ||
key == string(ext.PeerHostIPv4) ||
key == string(ext.PeerHostIPv6) ||
key == string(ext.PeerPort) {
// this tags are translated into kind and remoteEndpoint which can
// only be set on span creation
return s
}
s.zipkinSpan.Tag(key, fmt.Sprint(value))
return s
}
func (s *spanImpl) LogKV(keyValues ...interface{}) {
fields, err := log.InterleavedKVToFields(keyValues...)
if err != nil {
return
}
for _, field := range fields {
s.zipkinSpan.Annotate(time.Now(), field.String())
}
}
func (s *spanImpl) LogFields(fields ...log.Field) {
s.logFields(time.Now(), fields...)
}
func (s *spanImpl) logFields(t time.Time, fields ...log.Field) {
for _, field := range fields {
s.zipkinSpan.Annotate(t, field.String())
}
}
func (s *spanImpl) LogEvent(event string) {
s.Log(opentracing.LogData{
Event: event,
})
}
func (s *spanImpl) LogEventWithPayload(event string, payload interface{}) {
s.Log(opentracing.LogData{
Event: event,
Payload: payload,
})
}
func (s *spanImpl) Log(ld opentracing.LogData) {
if ld.Timestamp.IsZero() {
ld.Timestamp = time.Now()
}
s.zipkinSpan.Annotate(ld.Timestamp, fmt.Sprintf("%s:%s", ld.Event, ld.Payload))
}
func (s *spanImpl) Finish() {
if s.observer != nil {
s.observer.OnFinish(opentracing.FinishOptions{})
}
s.zipkinSpan.Finish()
}
func (s *spanImpl) FinishWithOptions(opts opentracing.FinishOptions) {
if s.observer != nil {
s.observer.OnFinish(opts)
}
for _, lr := range opts.LogRecords {
s.logFields(lr.Timestamp, lr.Fields...)
}
if !opts.FinishTime.IsZero() {
f, ok := s.zipkinSpan.(FinisherWithDuration)
if !ok {
return
}
f.FinishedWithDuration(opts.FinishTime.Sub(s.startTime))
return
}
s.Finish()
}
func (s *spanImpl) Tracer() opentracing.Tracer {
return s.tracer
}
func (s *spanImpl) Context() opentracing.SpanContext {
return SpanContext(s.zipkinSpan.Context())
}
func (s *spanImpl) SetBaggageItem(key, val string) opentracing.Span {
return s
}
func (s *spanImpl) BaggageItem(key string) string {
return ""
}