forked from openzipkin-contrib/zipkin-go-opentracing
/
event_nettrace.go
43 lines (38 loc) · 1.02 KB
/
event_nettrace.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
package events
import (
"bytes"
"fmt"
"golang.org/x/net/trace"
"github.com/openzipkin/zipkin-go-opentracing"
)
// NetTraceIntegrator can be passed into a zipkintracer as NewSpanEventListener
// and causes all traces to be registered with the net/trace endpoint.
var NetTraceIntegrator = func() func(zipkintracer.SpanEvent) {
var tr trace.Trace
return func(e zipkintracer.SpanEvent) {
switch t := e.(type) {
case zipkintracer.EventCreate:
tr = trace.New("tracing", t.OperationName)
tr.SetMaxEvents(1000)
case zipkintracer.EventFinish:
tr.Finish()
case zipkintracer.EventTag:
tr.LazyPrintf("%s:%v", t.Key, t.Value)
case zipkintracer.EventLogFields:
var buf bytes.Buffer
for i, f := range t.Fields {
if i > 0 {
buf.WriteByte(' ')
}
fmt.Fprintf(&buf, "%s:%v", f.Key(), f.Value())
}
tr.LazyPrintf("%s", buf.String())
case zipkintracer.EventLog:
if t.Payload != nil {
tr.LazyPrintf("%s (payload %v)", t.Event, t.Payload)
} else {
tr.LazyPrintf("%s", t.Event)
}
}
}
}