| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2021-07-14 09:21:41 +02:00
										 |  |  | 	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
 | 
					
						
							| 
									
										
										
										
											2020-10-12 21:30:55 +02:00
										 |  |  | 	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:19:26 +00:00
										 |  |  | 	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentracer"
 | 
					
						
							|  |  |  | 	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 	tracer                 ot.Tracer
 | 
					
						
							|  |  |  | 	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 {
 | 
					
						
							|  |  |  | 	return t.tracer
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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":
 | 
					
						
							| 
									
										
										
										
											2020-10-12 21:30:55 +02:00
										 |  |  | 			tracer := opentracer.New(
 | 
					
						
							|  |  |  | 				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),
 | 
					
						
							|  |  |  | 				tracer.WithServiceName(t.serviceName),
 | 
					
						
							|  |  |  | 				tracer.WithAnalyticsRate(t.datadogAnalyticsRate),
 | 
					
						
							| 
									
										
										
										
											2022-06-23 12:40:13 +02:00
										 |  |  | 				tracer.WithLogger(&loggerAdapter{log}),
 | 
					
						
							| 
									
										
										
										
											2020-10-12 21:30:55 +02:00
										 |  |  | 			)
 | 
					
						
							| 
									
										
										
										
											2019-06-10 02:02:03 -07:00
										 |  |  | 			t.tracer = tracer
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-09-17 02:33:08 +08:00
										 |  |  | 	t.tracer = 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) {
 | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 	trace := false
 | 
					
						
							|  |  |  | 	if t.every > 0 {
 | 
					
						
							|  |  |  | 		queryNr := atomic.AddUint64(&t.count, 1)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if queryNr%t.every == 0 {
 | 
					
						
							|  |  |  | 			trace = true
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-10-05 13:13:16 -07:00
										 |  |  | 	span := ot.SpanFromContext(ctx)
 | 
					
						
							|  |  |  | 	if !trace || span != nil {
 | 
					
						
							|  |  |  | 		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()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-14 09:21:41 +02:00
										 |  |  | 	switch spanCtx := span.Context().(type) {
 | 
					
						
							|  |  |  | 	case zipkinot.SpanContext:
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	case ddtrace.SpanContext:
 | 
					
						
							| 
									
										
										
										
											2021-07-19 10:28:07 +00:00
										 |  |  | 		metadata.SetValueFunc(ctx, metaTraceIdKey, func() string { return fmt.Sprint(spanCtx.TraceID()) })
 | 
					
						
							| 
									
										
										
										
											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)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							|  |  |  | 		otext.Error.Set(span, true)
 | 
					
						
							|  |  |  | 		span.LogFields(otlog.Event("error"), otlog.Error(err))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-10-05 13:13:16 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return status, err
 | 
					
						
							|  |  |  | }
 |