/
opentracing.go
173 lines (147 loc) · 4.58 KB
/
opentracing.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
package opentracing
import (
"encoding/json"
"fmt"
"io"
"time"
"github.com/manmanxing/go_common/beacon/etcd"
"github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
"github.com/uber/jaeger-lib/metrics"
)
//jaeger 相关配置信息
var JaegerConfig struct {
AgentUrl string `json:"agent_url"`
CollectorUrl string `json:"collector_url"`
Probability float64 `json:"probability"`
}
const (
//jaeger 的etcd配置路径
jaegerEtcdPath = "xxx"
//metric 打印日志间隔时间:10分钟
metricPrintLogTime = 60 * 10
//在开始删除新span之前可以在内存中保留多少span
reporterQueueSize = 1000
)
func init() {
v, err := etcd.GetValue(jaegerEtcdPath)
if err != nil {
panic(err)
}
err = json.Unmarshal([]byte(v), &JaegerConfig)
if err != nil {
panic(err)
}
//强校验jaeger采样率的概率范围
if JaegerConfig.Probability < 0.0 || JaegerConfig.Probability > 1.0 {
panic(fmt.Sprintf("jaeger config probability must between 0.0 and 1.0,received %f", JaegerConfig.Probability))
}
}
//这里可以自定义自己的 tace log
type customizeTaceLog struct {
}
func (*customizeTaceLog) Error(msg string) {
fmt.Println("customize opentracing err", msg)
}
func (*customizeTaceLog) Infof(msg string, args ...interface{}) {
fmt.Println("customize opentracing info", fmt.Sprintf(msg, args))
}
//自定义集中式度量系统 metrics
type Metric struct {
CounterRecord []*CountRecord `json:"counter_record,omitempty"`
TimerRecord []*TimeRecord `json:"timer_record,omitempty"`
GaugeRecord []*GaugeRecord `json:"gauge_record,omitempty"`
HistogramRecord []*HistogramRecord `json:"histogram_record,omitempty"`
NamespaceRecord []*NamespaceRecord `json:"namespace_record,omitempty"`
}
//自定义实现 metric 的类型
type CountRecord struct {
Option metrics.Options `json:"option"`
Count int64 `json:"count"`
}
func (c *CountRecord) Inc(n int64) {
c.Count += n
}
type TimeRecord struct {
Option metrics.TimerOptions `json:"option"`
RecordTime time.Duration `json:"record_time"`
}
func (t *TimeRecord) Record(dur time.Duration) {
t.RecordTime = dur
}
type GaugeRecord struct {
Option metrics.Options `json:"option"`
RecordValue int64 `json:"record_value"`
}
func (g *GaugeRecord) Update(n int64) {
g.RecordValue = n
}
type HistogramRecord struct {
Option metrics.HistogramOptions `json:"option"`
RecordValue float64 `json:"record_value"`
}
func (h *HistogramRecord) Record(n float64) {
h.RecordValue = n
}
type NamespaceRecord struct {
Namespace metrics.NSOptions `json:"namespace"`
SubMetric *Metric `json:"sub_metric"`
}
func (m *Metric) Counter(metric metrics.Options) metrics.Counter {
newCounter := &CountRecord{Option: metric}
m.CounterRecord = append(m.CounterRecord, newCounter)
return newCounter
}
func (m *Metric) Timer(metric metrics.TimerOptions) metrics.Timer {
newTimer := &TimeRecord{Option: metric}
m.TimerRecord = append(m.TimerRecord, newTimer)
return newTimer
}
func (m *Metric) Gauge(metric metrics.Options) metrics.Gauge {
newGauge := &GaugeRecord{Option: metric}
m.GaugeRecord = append(m.GaugeRecord, newGauge)
return newGauge
}
func (m *Metric) Histogram(metric metrics.HistogramOptions) metrics.Histogram {
newHistogram := &HistogramRecord{Option: metric}
m.HistogramRecord = append(m.HistogramRecord, newHistogram)
return newHistogram
}
func (m *Metric) Namespace(scope metrics.NSOptions) metrics.Factory {
newNS := &NamespaceRecord{Namespace: scope, SubMetric: &Metric{}}
m.NamespaceRecord = append(m.NamespaceRecord, newNS)
return newNS.SubMetric
}
//生成 metric 对象
func GetNewMetric() *Metric {
m := new(Metric)
m.PrintLog()
return m
}
//这里可以设置定时打印 metric 信息
func (m *Metric) PrintLog() {
go func() {
for {
time.Sleep(metricPrintLogTime * time.Second)
fmt.Println("opentracing metric info", m)
}
}()
}
//返回一个 Jaeger Trace,可以传入option 自定义采样
func NewTracer(serviceName string, options ...config.Option) (closer io.Closer, err error) {
cfg := &config.Configuration{
Sampler: &config.SamplerConfig{
Type: jaeger.SamplerTypeProbabilistic,
Param: JaegerConfig.Probability,
},
Reporter: &config.ReporterConfig{
QueueSize: reporterQueueSize,
LogSpans: true,
LocalAgentHostPort: JaegerConfig.AgentUrl,
CollectorEndpoint: JaegerConfig.CollectorUrl,
},
}
//这里组装自定义的 log 与 metric
options = append(options, config.Logger(&customizeTaceLog{}), config.Metrics(GetNewMetric()))
return cfg.InitGlobalTracer(serviceName, options...)
}