2017-01-23 15:40:47 -05:00
|
|
|
// Package trace implements OpenTracing-based tracing
|
|
|
|
|
package trace
|
|
|
|
|
|
|
|
|
|
import (
|
2018-04-22 08:34:35 +01:00
|
|
|
"context"
|
2017-01-23 15:40:47 -05:00
|
|
|
"fmt"
|
2022-06-23 12:40:13 +02:00
|
|
|
stdlog "log"
|
2022-06-20 16:08:53 +02:00
|
|
|
"net/http"
|
2017-01-23 15:40:47 -05:00
|
|
|
"sync"
|
2017-02-16 12:13:18 -05:00
|
|
|
"sync/atomic"
|
2022-07-08 13:20:19 +02:00
|
|
|
"time"
|
2017-01-23 15:40:47 -05:00
|
|
|
|
2022-06-20 16:08:53 +02:00
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
2017-09-14 09:36:06 +01:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2021-07-14 09:21:41 +02:00
|
|
|
"github.com/coredns/coredns/plugin/metadata"
|
2018-10-05 13:13:16 -07:00
|
|
|
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
2022-06-23 12:40:13 +02:00
|
|
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
2018-10-05 13:13:16 -07:00
|
|
|
"github.com/coredns/coredns/plugin/pkg/rcode"
|
2020-01-30 09:19:26 +00:00
|
|
|
_ "github.com/coredns/coredns/plugin/pkg/trace" // Plugin the trace package.
|
2018-10-05 13:13:16 -07:00
|
|
|
"github.com/coredns/coredns/request"
|
2017-06-14 09:37:10 -07:00
|
|
|
|
2025-08-21 02:00:21 +03:00
|
|
|
"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
|
|
|
|
|
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
|
2017-01-29 12:06:26 -08:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
ot "github.com/opentracing/opentracing-go"
|
2021-06-29 09:10:22 +02:00
|
|
|
otext "github.com/opentracing/opentracing-go/ext"
|
|
|
|
|
otlog "github.com/opentracing/opentracing-go/log"
|
2020-09-17 02:33:08 +08:00
|
|
|
zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing"
|
|
|
|
|
"github.com/openzipkin/zipkin-go"
|
|
|
|
|
zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
|
2017-01-23 15:40:47 -05:00
|
|
|
)
|
|
|
|
|
|
2018-10-05 13:13:16 -07:00
|
|
|
const (
|
2020-10-12 21:30:55 +02:00
|
|
|
defaultTopLevelSpanName = "servedns"
|
2021-07-19 10:28:07 +00:00
|
|
|
metaTraceIdKey = "trace/traceid"
|
2018-10-05 13:13:16 -07:00
|
|
|
)
|
|
|
|
|
|
2022-06-23 12:40:13 +02:00
|
|
|
var log = clog.NewWithPlugin("trace")
|
|
|
|
|
|
2021-01-28 16:38:24 +01:00
|
|
|
type traceTags struct {
|
|
|
|
|
Name string
|
|
|
|
|
Type string
|
|
|
|
|
Rcode string
|
|
|
|
|
Proto string
|
|
|
|
|
Remote string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tagByProvider = map[string]traceTags{
|
|
|
|
|
"default": {
|
|
|
|
|
Name: "coredns.io/name",
|
|
|
|
|
Type: "coredns.io/type",
|
|
|
|
|
Rcode: "coredns.io/rcode",
|
|
|
|
|
Proto: "coredns.io/proto",
|
|
|
|
|
Remote: "coredns.io/remote",
|
|
|
|
|
},
|
|
|
|
|
"datadog": {
|
|
|
|
|
Name: "coredns.io@name",
|
|
|
|
|
Type: "coredns.io@type",
|
|
|
|
|
Rcode: "coredns.io@rcode",
|
|
|
|
|
Proto: "coredns.io@proto",
|
|
|
|
|
Remote: "coredns.io@remote",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 10:41:54 -05:00
|
|
|
type trace struct {
|
2020-09-09 10:45:14 +02:00
|
|
|
count uint64 // as per Go spec, needs to be first element in a struct
|
|
|
|
|
|
2022-07-08 13:20:19 +02:00
|
|
|
Next plugin.Handler
|
|
|
|
|
Endpoint string
|
|
|
|
|
EndpointType string
|
2025-08-21 02:00:21 +03:00
|
|
|
zipkinTracer ot.Tracer
|
2022-07-08 13:20:19 +02:00
|
|
|
serviceEndpoint string
|
|
|
|
|
serviceName string
|
|
|
|
|
clientServer bool
|
|
|
|
|
every uint64
|
|
|
|
|
datadogAnalyticsRate float64
|
|
|
|
|
zipkinMaxBacklogSize int
|
|
|
|
|
zipkinMaxBatchSize int
|
|
|
|
|
zipkinMaxBatchInterval time.Duration
|
|
|
|
|
Once sync.Once
|
|
|
|
|
tagSet traceTags
|
2017-01-23 15:40:47 -05:00
|
|
|
}
|
|
|
|
|
|
2017-03-01 10:41:54 -05:00
|
|
|
func (t *trace) Tracer() ot.Tracer {
|
2025-08-21 02:00:21 +03:00
|
|
|
return t.zipkinTracer
|
2017-03-01 10:41:54 -05:00
|
|
|
}
|
|
|
|
|
|
2017-01-23 15:40:47 -05:00
|
|
|
// OnStartup sets up the tracer
|
2017-03-01 10:41:54 -05:00
|
|
|
func (t *trace) OnStartup() error {
|
2017-01-23 15:40:47 -05:00
|
|
|
var err error
|
|
|
|
|
t.Once.Do(func() {
|
|
|
|
|
switch t.EndpointType {
|
|
|
|
|
case "zipkin":
|
|
|
|
|
err = t.setupZipkin()
|
2018-03-09 16:08:57 -04:00
|
|
|
case "datadog":
|
2025-08-21 02:00:21 +03:00
|
|
|
tracer.Start(
|
2020-10-12 21:30:55 +02:00
|
|
|
tracer.WithAgentAddr(t.Endpoint),
|
2022-06-23 12:40:13 +02:00
|
|
|
tracer.WithDebugMode(clog.D.Value()),
|
2020-10-12 21:30:55 +02:00
|
|
|
tracer.WithGlobalTag(ext.SpanTypeDNS, true),
|
2025-08-21 02:00:21 +03:00
|
|
|
tracer.WithService(t.serviceName),
|
2020-10-12 21:30:55 +02:00
|
|
|
tracer.WithAnalyticsRate(t.datadogAnalyticsRate),
|
2022-06-23 12:40:13 +02:00
|
|
|
tracer.WithLogger(&loggerAdapter{log}),
|
2020-10-12 21:30:55 +02:00
|
|
|
)
|
2021-01-28 16:38:24 +01:00
|
|
|
t.tagSet = tagByProvider["datadog"]
|
2017-01-23 15:40:47 -05:00
|
|
|
default:
|
2017-06-14 09:37:10 -07:00
|
|
|
err = fmt.Errorf("unknown endpoint type: %s", t.EndpointType)
|
2017-01-23 15:40:47 -05:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 02:00:21 +03:00
|
|
|
// OnShutdown cleans up the tracer
|
|
|
|
|
func (t *trace) OnShutdown() error {
|
|
|
|
|
if t.EndpointType == "datadog" {
|
|
|
|
|
tracer.Stop()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 10:41:54 -05:00
|
|
|
func (t *trace) setupZipkin() error {
|
2022-07-08 13:20:19 +02:00
|
|
|
var opts []zipkinhttp.ReporterOption
|
|
|
|
|
opts = append(opts, zipkinhttp.Logger(stdlog.New(&loggerAdapter{log}, "", 0)))
|
|
|
|
|
if t.zipkinMaxBacklogSize != 0 {
|
|
|
|
|
opts = append(opts, zipkinhttp.MaxBacklog(t.zipkinMaxBacklogSize))
|
|
|
|
|
}
|
|
|
|
|
if t.zipkinMaxBatchSize != 0 {
|
|
|
|
|
opts = append(opts, zipkinhttp.BatchSize(t.zipkinMaxBatchSize))
|
|
|
|
|
}
|
|
|
|
|
if t.zipkinMaxBatchInterval != 0 {
|
|
|
|
|
opts = append(opts, zipkinhttp.BatchInterval(t.zipkinMaxBatchInterval))
|
|
|
|
|
}
|
|
|
|
|
reporter := zipkinhttp.NewReporter(t.Endpoint, opts...)
|
2020-09-17 02:33:08 +08:00
|
|
|
recorder, err := zipkin.NewEndpoint(t.serviceName, t.serviceEndpoint)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warningf("build Zipkin endpoint found err: %v", err)
|
|
|
|
|
}
|
2020-10-12 21:30:55 +02:00
|
|
|
tracer, err := zipkin.NewTracer(
|
|
|
|
|
reporter,
|
|
|
|
|
zipkin.WithLocalEndpoint(recorder),
|
2020-11-10 20:03:14 +01:00
|
|
|
zipkin.WithSharedSpans(t.clientServer),
|
2020-10-12 21:30:55 +02:00
|
|
|
)
|
2017-01-23 15:40:47 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-08-21 02:00:21 +03:00
|
|
|
t.zipkinTracer = zipkinot.Wrap(tracer)
|
2021-01-28 16:38:24 +01:00
|
|
|
|
|
|
|
|
t.tagSet = tagByProvider["default"]
|
2017-08-06 05:54:24 -07:00
|
|
|
return err
|
2017-01-23 15:40:47 -05:00
|
|
|
}
|
|
|
|
|
|
2017-01-29 12:06:26 -08:00
|
|
|
// Name implements the Handler interface.
|
2018-04-25 15:27:25 +01:00
|
|
|
func (t *trace) Name() string { return "trace" }
|
2017-01-23 15:40:47 -05:00
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
// ServeDNS implements the plugin.Handle interface.
|
2017-03-01 10:41:54 -05:00
|
|
|
func (t *trace) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
2025-08-21 02:00:21 +03:00
|
|
|
shouldTrace := false
|
2017-02-16 12:13:18 -05:00
|
|
|
if t.every > 0 {
|
|
|
|
|
queryNr := atomic.AddUint64(&t.count, 1)
|
|
|
|
|
if queryNr%t.every == 0 {
|
2025-08-21 02:00:21 +03:00
|
|
|
shouldTrace = true
|
2017-02-16 12:13:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-21 02:00:21 +03:00
|
|
|
|
|
|
|
|
if t.EndpointType == "datadog" {
|
|
|
|
|
return t.serveDNSDatadog(ctx, w, r, shouldTrace)
|
|
|
|
|
}
|
|
|
|
|
return t.serveDNSZipkin(ctx, w, r, shouldTrace)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *trace) serveDNSDatadog(ctx context.Context, w dns.ResponseWriter, r *dns.Msg, shouldTrace bool) (int, error) {
|
|
|
|
|
if !shouldTrace {
|
|
|
|
|
return plugin.NextOrFailure(t.Name(), t.Next, ctx, w, r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
span, spanCtx := tracer.StartSpanFromContext(ctx, defaultTopLevelSpanName)
|
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
|
|
metadata.SetValueFunc(ctx, metaTraceIdKey, func() string { return span.Context().TraceID() })
|
|
|
|
|
|
|
|
|
|
req := request.Request{W: w, Req: r}
|
|
|
|
|
rw := dnstest.NewRecorder(w)
|
|
|
|
|
status, err := plugin.NextOrFailure(t.Name(), t.Next, spanCtx, rw, r)
|
|
|
|
|
|
|
|
|
|
t.setDatadogSpanTags(span, req, rw, status, err)
|
|
|
|
|
|
|
|
|
|
return status, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *trace) serveDNSZipkin(ctx context.Context, w dns.ResponseWriter, r *dns.Msg, shouldTrace bool) (int, error) {
|
2018-10-05 13:13:16 -07:00
|
|
|
span := ot.SpanFromContext(ctx)
|
2025-08-21 02:00:21 +03:00
|
|
|
if !shouldTrace || span != nil {
|
2018-10-05 13:13:16 -07:00
|
|
|
return plugin.NextOrFailure(t.Name(), t.Next, ctx, w, r)
|
2017-02-16 12:13:18 -05:00
|
|
|
}
|
2018-10-05 13:13:16 -07:00
|
|
|
|
2022-06-20 16:08:53 +02:00
|
|
|
var spanCtx ot.SpanContext
|
|
|
|
|
if val := ctx.Value(dnsserver.HTTPRequestKey{}); val != nil {
|
|
|
|
|
if httpReq, ok := val.(*http.Request); ok {
|
|
|
|
|
spanCtx, _ = t.Tracer().Extract(ot.HTTPHeaders, ot.HTTPHeadersCarrier(httpReq.Header))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 13:13:16 -07:00
|
|
|
req := request.Request{W: w, Req: r}
|
2022-06-20 16:08:53 +02:00
|
|
|
span = t.Tracer().StartSpan(defaultTopLevelSpanName, otext.RPCServerOption(spanCtx))
|
2018-10-05 13:13:16 -07:00
|
|
|
defer span.Finish()
|
|
|
|
|
|
2025-08-21 02:00:21 +03:00
|
|
|
if spanCtx, ok := span.Context().(zipkinot.SpanContext); ok {
|
2021-07-19 10:28:07 +00:00
|
|
|
metadata.SetValueFunc(ctx, metaTraceIdKey, func() string { return spanCtx.TraceID.String() })
|
2021-07-14 09:21:41 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-05 13:13:16 -07:00
|
|
|
rw := dnstest.NewRecorder(w)
|
|
|
|
|
ctx = ot.ContextWithSpan(ctx, span)
|
|
|
|
|
status, err := plugin.NextOrFailure(t.Name(), t.Next, ctx, rw, r)
|
|
|
|
|
|
2025-08-21 02:00:21 +03:00
|
|
|
t.setZipkinSpanTags(span, req, rw, status, err)
|
|
|
|
|
|
|
|
|
|
return status, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setDatadogSpanTags sets span tags using DataDog v2 API
|
|
|
|
|
func (t *trace) setDatadogSpanTags(span *tracer.Span, req request.Request, rw *dnstest.Recorder, status int, err error) {
|
|
|
|
|
span.SetTag(t.tagSet.Name, req.Name())
|
|
|
|
|
span.SetTag(t.tagSet.Type, req.Type())
|
|
|
|
|
span.SetTag(t.tagSet.Proto, req.Proto())
|
|
|
|
|
span.SetTag(t.tagSet.Remote, req.IP())
|
|
|
|
|
rc := rw.Rcode
|
|
|
|
|
if !plugin.ClientWrite(status) {
|
|
|
|
|
rc = status
|
|
|
|
|
}
|
|
|
|
|
span.SetTag(t.tagSet.Rcode, rcode.ToString(rc))
|
|
|
|
|
if err != nil {
|
|
|
|
|
span.SetTag("error.message", err.Error())
|
|
|
|
|
span.SetTag("error", true)
|
|
|
|
|
span.SetTag("error.type", "dns_error")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setZipkinSpanTags sets span tags for Zipkin/OpenTracing spans
|
|
|
|
|
func (t *trace) setZipkinSpanTags(span ot.Span, req request.Request, rw *dnstest.Recorder, status int, err error) {
|
2021-01-28 16:38:24 +01:00
|
|
|
span.SetTag(t.tagSet.Name, req.Name())
|
|
|
|
|
span.SetTag(t.tagSet.Type, req.Type())
|
|
|
|
|
span.SetTag(t.tagSet.Proto, req.Proto())
|
|
|
|
|
span.SetTag(t.tagSet.Remote, req.IP())
|
2021-07-10 07:34:53 +02:00
|
|
|
rc := rw.Rcode
|
|
|
|
|
if !plugin.ClientWrite(status) {
|
|
|
|
|
// when no response was written, fallback to status returned from next plugin as this status
|
|
|
|
|
// is actually used as rcode of DNS response
|
|
|
|
|
// see https://github.com/coredns/coredns/blob/master/core/dnsserver/server.go#L318
|
|
|
|
|
rc = status
|
|
|
|
|
}
|
|
|
|
|
span.SetTag(t.tagSet.Rcode, rcode.ToString(rc))
|
2021-06-29 09:10:22 +02:00
|
|
|
if err != nil {
|
2025-08-21 02:00:21 +03:00
|
|
|
// Use OpenTracing error handling
|
2021-06-29 09:10:22 +02:00
|
|
|
otext.Error.Set(span, true)
|
|
|
|
|
span.LogFields(otlog.Event("error"), otlog.Error(err))
|
|
|
|
|
}
|
2018-10-05 13:13:16 -07:00
|
|
|
}
|