This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
trace.go
216 lines (195 loc) · 5.83 KB
/
trace.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
//
// Copyright 2022 SkyAPM org
//
// 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 go2sky
import (
"context"
"github.com/SkyAPM/go2sky/internal/idgen"
"github.com/pkg/errors"
"github.com/SkyAPM/go2sky/internal/tool"
"github.com/SkyAPM/go2sky/propagation"
)
const (
errParameter = tool.Error("parameter are nil")
)
// Tracer is go2sky tracer implementation.
type Tracer struct {
service string
instance string
reporter Reporter
// 0 not init 1 init
initFlag int32
sampler Sampler
correlation *CorrelationConfig
cdsWatchers []AgentConfigChangeWatcher
}
// TracerOption allows for functional options to adjust behaviour
// of a Tracer to be created by NewTracer
type TracerOption func(t *Tracer)
// NewTracer return a new go2sky Tracer
func NewTracer(service string, opts ...TracerOption) (tracer *Tracer, err error) {
// read the service in the environment variable
service = serviceFormEnv(service)
if service == "" {
return nil, errParameter
}
// read the options in the environment variable
envOps, err := traceOptionsFormEnv()
if err != nil {
return nil, err
}
opts = append(opts, envOps...)
t := &Tracer{
service: service,
initFlag: 0,
}
// default correlation config
t.correlation = &CorrelationConfig{MaxKeyCount: 3, MaxValueSize: 128}
for _, opt := range opts {
opt(t)
}
if t.sampler == nil {
t.sampler = NewDynamicSampler(1, t)
}
if t.reporter != nil {
if t.instance == "" {
id, err := idgen.UUID()
if err != nil {
return nil, err
}
t.instance = id + "@" + tool.IPV4()
}
t.reporter.Boot(t.service, t.instance, t.cdsWatchers)
t.initFlag = 1
}
return t, nil
}
// CreateEntrySpan creates and starts an entry span for incoming request
func (t *Tracer) CreateEntrySpan(ctx context.Context, operationName string, extractor propagation.Extractor) (s Span, nCtx context.Context, err error) {
if ctx == nil || operationName == "" || extractor == nil {
return nil, nil, errParameter
}
if s, nCtx = t.createNoop(ctx); s != nil {
return
}
var refSc = &propagation.SpanContext{}
err = refSc.Decode(extractor)
if err != nil {
return
}
if !refSc.Valid {
refSc = nil
}
s, nCtx, err = t.CreateLocalSpan(ctx, WithContext(refSc), WithSpanType(SpanTypeEntry), WithOperationName(operationName))
return
}
// CreateLocalSpan creates and starts a span for local usage
func (t *Tracer) CreateLocalSpan(ctx context.Context, opts ...SpanOption) (s Span, c context.Context, err error) {
if ctx == nil {
return nil, nil, errParameter
}
if s, c = t.createNoop(ctx); s != nil {
return
}
ds := newLocalSpan(t)
for _, opt := range opts {
opt(ds)
}
parentSpan, ok := ctx.Value(ctxKeyInstance).(segmentSpan)
if !ok {
parentSpan = nil
}
isForceSample := len(ds.Refs) > 0
// Try to sample when it is not force sample
if parentSpan == nil && !isForceSample {
// Force sample
sampled := t.sampler.IsSampled(ds.OperationName)
if !sampled {
// Filter by sample just return noop span
s = &NoopSpan{}
return s, context.WithValue(ctx, ctxKeyInstance, s), nil
}
}
s, err = newSegmentSpan(ds, parentSpan)
if err != nil {
return nil, nil, err
}
return s, context.WithValue(ctx, ctxKeyInstance, s), nil
}
// CreateExitSpan creates and starts an exit span for client
func (t *Tracer) CreateExitSpan(ctx context.Context, operationName string, peer string, injector propagation.Injector) (s Span, err error) {
s, _, err = t.CreateExitSpanWithContext(ctx, operationName, peer, injector)
return
}
// CreateExitSpanWithContext creates and starts an exit span for client with context
func (t *Tracer) CreateExitSpanWithContext(ctx context.Context, operationName string, peer string,
injector propagation.Injector) (s Span, nCtx context.Context, err error) {
if ctx == nil || operationName == "" || peer == "" || injector == nil {
return nil, nil, errParameter
}
if s, nCtx = t.createNoop(ctx); s != nil {
return
}
s, nCtx, err = t.CreateLocalSpan(ctx, WithSpanType(SpanTypeExit), WithOperationName(operationName))
if err != nil {
return
}
noopSpan, ok := interface{}(s).(*NoopSpan)
if ok {
// Ignored, there is no need to inject SW8 in the request header
return noopSpan, nCtx, nil
}
s.SetPeer(peer)
spanContext := &propagation.SpanContext{}
span, ok := s.(ReportedSpan)
if !ok {
return nil, nil, errors.New("span type is wrong")
}
firstSpan := span.Context().FirstSpan
spanContext.Sample = 1
spanContext.TraceID = span.Context().TraceID
spanContext.ParentSegmentID = span.Context().SegmentID
spanContext.ParentSpanID = span.Context().SpanID
spanContext.ParentService = t.service
spanContext.ParentServiceInstance = t.instance
spanContext.ParentEndpoint = firstSpan.GetOperationName()
spanContext.AddressUsedAtClient = peer
spanContext.CorrelationContext = span.Context().CorrelationContext
err = spanContext.Encode(injector)
if err != nil {
return nil, nil, err
}
return
}
func (t *Tracer) createNoop(ctx context.Context) (s Span, nCtx context.Context) {
if ns, ok := ctx.Value(ctxKeyInstance).(*NoopSpan); ok {
nCtx = ctx
s = ns
return
}
if t.initFlag == 0 {
s = &NoopSpan{}
nCtx = context.WithValue(ctx, ctxKeyInstance, s)
return
}
return
}
//Reporter is a data transit specification
type Reporter interface {
Boot(service string, serviceInstance string, cdsWatchers []AgentConfigChangeWatcher)
Send(spans []ReportedSpan)
SendLog(logData ReportedLogData)
Close()
}