| 
									
										
										
										
											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"
 | 
					
						
							|  |  |  | 	"sync"
 | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 	"sync/atomic"
 | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2018-10-05 13:13:16 -07:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							| 
									
										
										
										
											2020-07-23 00:40:17 -07:00
										 |  |  | 	"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"
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2018-10-05 13:13:16 -07:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 21:30:55 +02:00
										 |  |  | 	Next                 plugin.Handler
 | 
					
						
							|  |  |  | 	Endpoint             string
 | 
					
						
							|  |  |  | 	EndpointType         string
 | 
					
						
							|  |  |  | 	tracer               ot.Tracer
 | 
					
						
							|  |  |  | 	serviceEndpoint      string
 | 
					
						
							|  |  |  | 	serviceName          string
 | 
					
						
							|  |  |  | 	clientServer         bool
 | 
					
						
							|  |  |  | 	every                uint64
 | 
					
						
							|  |  |  | 	datadogAnalyticsRate float64
 | 
					
						
							|  |  |  | 	Once                 sync.Once
 | 
					
						
							| 
									
										
										
										
											2021-01-28 16:38:24 +01:00
										 |  |  | 	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),
 | 
					
						
							|  |  |  | 				tracer.WithDebugMode(log.D.Value()),
 | 
					
						
							|  |  |  | 				tracer.WithGlobalTag(ext.SpanTypeDNS, true),
 | 
					
						
							|  |  |  | 				tracer.WithServiceName(t.serviceName),
 | 
					
						
							|  |  |  | 				tracer.WithAnalyticsRate(t.datadogAnalyticsRate),
 | 
					
						
							|  |  |  | 			)
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							| 
									
										
										
										
											2020-09-17 02:33:08 +08:00
										 |  |  | 	reporter := zipkinhttp.NewReporter(t.Endpoint)
 | 
					
						
							|  |  |  | 	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
										 |  |  | 
 | 
					
						
							|  |  |  | 	req := request.Request{W: w, Req: r}
 | 
					
						
							| 
									
										
										
										
											2020-10-12 21:30:55 +02:00
										 |  |  | 	span = t.Tracer().StartSpan(defaultTopLevelSpanName)
 | 
					
						
							| 
									
										
										
										
											2018-10-05 13:13:16 -07:00
										 |  |  | 	defer span.Finish()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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())
 | 
					
						
							|  |  |  | 	span.SetTag(t.tagSet.Rcode, rcode.ToString(rw.Rcode))
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | }
 |