-
Notifications
You must be signed in to change notification settings - Fork 491
/
zipkin.go
102 lines (86 loc) · 2.75 KB
/
zipkin.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
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* 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 zipkin
import (
"io"
zipkingo "github.com/openzipkin/zipkin-go"
zipkingomodel "github.com/openzipkin/zipkin-go/model"
zipkingoreporter "github.com/openzipkin/zipkin-go/reporter"
zipkingohttp "github.com/openzipkin/zipkin-go/reporter/http"
"github.com/megaease/easegress/pkg/tracing/base"
"github.com/megaease/easegress/pkg/util/fasttime"
)
type (
// Spec describes Zipkin.
Spec struct {
Hostport string `yaml:"hostport" jsonschema:"omitempty"`
ServerURL string `yaml:"serverURL" jsonschema:"required,format=url"`
SampleRate float64 `yaml:"sampleRate" jsonschema:"required,minimum=0,maximum=1"`
SameSpan bool `yaml:"sameSpan" jsonschema:"omitempty"`
ID128Bit bool `yaml:"id128Bit" jsonschema:"omitempty"`
}
cancellableReporter struct {
reporter zipkingoreporter.Reporter
}
)
// CreateNoopTracer creates no-op tracer
func CreateNoopTracer() *zipkingo.Tracer {
tracer, _ := zipkingo.NewTracer(nil)
return tracer
}
func (cp *cancellableReporter) Send(sm zipkingomodel.SpanModel) {
_, cancelled := sm.Tags[base.CancelTagKey]
if cancelled {
return
}
cp.reporter.Send(sm)
}
func (cp *cancellableReporter) Close() error { return cp.reporter.Close() }
// Validate validates Spec.
func (spec Spec) Validate() error {
if spec.Hostport != "" {
_, err := zipkingo.NewEndpoint("", spec.Hostport)
if err != nil {
return err
}
}
return nil
}
// New creates zipkin tracer.
func New(serviceName string, spec *Spec, tags map[string]string) (*zipkingo.Tracer, io.Closer, error) {
endpoint, err := zipkingo.NewEndpoint(serviceName, spec.Hostport)
if err != nil {
return nil, nil, err
}
sampler, err := zipkingo.NewBoundarySampler(spec.SampleRate, fasttime.Now().Unix())
if err != nil {
return nil, nil, err
}
reporter := zipkingohttp.NewReporter(spec.ServerURL)
nativeTracer, err := zipkingo.NewTracer(
&cancellableReporter{reporter: reporter},
zipkingo.WithLocalEndpoint(endpoint),
zipkingo.WithSharedSpans(spec.SameSpan),
zipkingo.WithTraceID128Bit(spec.ID128Bit),
zipkingo.WithSampler(sampler),
zipkingo.WithTags(tags),
)
if err != nil {
return nil, nil, err
}
return nativeTracer, reporter, nil
}