-
Notifications
You must be signed in to change notification settings - Fork 2
/
tracing.go
84 lines (78 loc) · 1.91 KB
/
tracing.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
package huaweiapm
import (
"fmt"
"github.com/go-mesh/openlogging"
"github.com/openzipkin-contrib/zipkin-go-opentracing/thrift/gen-go/zipkincore"
"sync"
"time"
)
type TracingReporter struct {
batch []*zipkincore.Span
batchMutex *sync.Mutex
spanC chan *zipkincore.Span
batchInterval string
batchSize int
tracingRunning bool
}
func NewTracingReporter(batchInterval string, batchSize int) *TracingReporter {
if batchInterval == "" {
batchInterval = DefaultTracingBatchInterval
}
if batchSize == 0 {
batchSize = DefaultTracingBatchSize
}
tr = &TracingReporter{
batch: make([]*zipkincore.Span, 0),
batchMutex: &sync.Mutex{},
spanC: make(chan *zipkincore.Span),
batchSize: batchSize,
batchInterval: batchInterval,
}
return tr
}
func (tr *TracingReporter) appendSpan(span *zipkincore.Span) int {
tr.batchMutex.Lock()
defer tr.batchMutex.Unlock()
tr.batch = append(tr.batch, span)
return len(tr.batch)
}
func (tr *TracingReporter) doReport() {
tr.batchMutex.Lock()
defer tr.batchMutex.Unlock()
err := client.ReportTracing(tr.batch)
if err != nil {
openlogging.Error("can not report tracing: " + err.Error())
return
}
openlogging.Debug(fmt.Sprintf("report %d spans", len(tr.batch)))
tr.batch = tr.batch[len(tr.batch):]
}
func (tr *TracingReporter) StartReportSpans() {
t, _ := time.ParseDuration(tr.batchInterval)
ticker := time.Tick(t)
openlogging.Debug("start tracing")
tr.tracingRunning = true
for {
select {
case <-ticker:
tr.doReport()
case span := <-tr.spanC:
size := tr.appendSpan(span)
if size >= tr.batchSize {
tr.doReport()
}
case stop := <-StopTracing:
if stop {
openlogging.Warn("stopped reporting spans, huawei apm tracing function lost")
tr.tracingRunning = false
break
}
}
}
}
func (tr *TracingReporter) WriteSpan(span *zipkincore.Span) {
if !tr.tracingRunning {
return
}
tr.spanC <- span
}