forked from coredns/coredns
/
trace.go
128 lines (105 loc) · 2.93 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
// Package trace implements OpenTracing-based tracing
package trace
import (
"context"
"fmt"
"strings"
"sync"
"sync/atomic"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/pkg/rcode"
// Plugin the trace package.
_ "github.com/coredns/coredns/plugin/pkg/trace"
"github.com/coredns/coredns/request"
ddtrace "github.com/DataDog/dd-trace-go/opentracing"
"github.com/miekg/dns"
ot "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
)
const (
tagName = "coredns.io/name"
tagType = "coredns.io/type"
tagRcode = "coredns.io/rcode"
)
type trace struct {
Next plugin.Handler
Endpoint string
EndpointType string
tracer ot.Tracer
serviceEndpoint string
serviceName string
clientServer bool
every uint64
count uint64
Once sync.Once
}
func (t *trace) Tracer() ot.Tracer {
return t.tracer
}
// OnStartup sets up the tracer
func (t *trace) OnStartup() error {
var err error
t.Once.Do(func() {
switch t.EndpointType {
case "zipkin":
err = t.setupZipkin()
case "datadog":
err = t.setupDatadog()
default:
err = fmt.Errorf("unknown endpoint type: %s", t.EndpointType)
}
})
return err
}
func (t *trace) setupZipkin() error {
collector, err := zipkin.NewHTTPCollector(t.Endpoint)
if err != nil {
return err
}
recorder := zipkin.NewRecorder(collector, false, t.serviceEndpoint, t.serviceName)
t.tracer, err = zipkin.NewTracer(recorder, zipkin.ClientServerSameSpan(t.clientServer))
return err
}
func (t *trace) setupDatadog() error {
config := ddtrace.NewConfiguration()
config.ServiceName = t.serviceName
host := strings.Split(t.Endpoint, ":")
config.AgentHostname = host[0]
if len(host) == 2 {
config.AgentPort = host[1]
}
tracer, _, err := ddtrace.NewTracer(config)
t.tracer = tracer
return err
}
// Name implements the Handler interface.
func (t *trace) Name() string { return "trace" }
// ServeDNS implements the plugin.Handle interface.
func (t *trace) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
trace := false
if t.every > 0 {
queryNr := atomic.AddUint64(&t.count, 1)
if queryNr%t.every == 0 {
trace = true
}
}
span := ot.SpanFromContext(ctx)
if !trace || span != nil {
return plugin.NextOrFailure(t.Name(), t.Next, ctx, w, r)
}
req := request.Request{W: w, Req: r}
span = t.Tracer().StartSpan(spanName(ctx, req))
defer span.Finish()
rw := dnstest.NewRecorder(w)
ctx = ot.ContextWithSpan(ctx, span)
status, err := plugin.NextOrFailure(t.Name(), t.Next, ctx, rw, r)
span.SetTag(tagName, req.Name())
span.SetTag(tagType, req.Type())
span.SetTag(tagRcode, rcode.ToString(rw.Rcode))
return status, err
}
func spanName(ctx context.Context, req request.Request) string {
return "servedns:" + metrics.WithServer(ctx) + " " + req.Name()
}