| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2018-03-09 16:08:57 -04:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 	"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-04-25 15:27:25 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/metrics"
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:37:10 -07:00
										 |  |  | 	// Plugin the trace package.
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	_ "github.com/coredns/coredns/plugin/pkg/trace"
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:37:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-09 16:08:57 -04:00
										 |  |  | 	ddtrace "github.com/DataDog/dd-trace-go/opentracing"
 | 
					
						
							| 
									
										
										
										
											2017-01-29 12:06:26 -08:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | 	ot "github.com/opentracing/opentracing-go"
 | 
					
						
							|  |  |  | 	zipkin "github.com/openzipkin/zipkin-go-opentracing"
 | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 10:41:54 -05:00
										 |  |  | type trace struct {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	Next            plugin.Handler
 | 
					
						
							| 
									
										
										
										
											2017-01-29 12:06:26 -08:00
										 |  |  | 	Endpoint        string
 | 
					
						
							|  |  |  | 	EndpointType    string
 | 
					
						
							| 
									
										
										
										
											2017-03-01 10:41:54 -05:00
										 |  |  | 	tracer          ot.Tracer
 | 
					
						
							| 
									
										
										
										
											2018-04-25 15:27:25 +01:00
										 |  |  | 	serviceEndpoint string
 | 
					
						
							| 
									
										
										
										
											2017-02-22 07:25:58 +00:00
										 |  |  | 	serviceName     string
 | 
					
						
							|  |  |  | 	clientServer    bool
 | 
					
						
							|  |  |  | 	every           uint64
 | 
					
						
							|  |  |  | 	count           uint64
 | 
					
						
							| 
									
										
										
										
											2017-01-29 12:06:26 -08:00
										 |  |  | 	Once            sync.Once
 | 
					
						
							| 
									
										
										
										
											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":
 | 
					
						
							|  |  |  | 			err = t.setupDatadog()
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	collector, err := zipkin.NewHTTPCollector(t.Endpoint)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-25 15:27:25 +01:00
										 |  |  | 	recorder := zipkin.NewRecorder(collector, false, t.serviceEndpoint, t.serviceName)
 | 
					
						
							| 
									
										
										
										
											2017-03-01 10:41:54 -05:00
										 |  |  | 	t.tracer, err = zipkin.NewTracer(recorder, zipkin.ClientServerSameSpan(t.clientServer))
 | 
					
						
							| 
									
										
										
										
											2017-08-06 05:54:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return err
 | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-09 16:08:57 -04:00
										 |  |  | 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
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if span := ot.SpanFromContext(ctx); span == nil && trace {
 | 
					
						
							| 
									
										
										
										
											2018-04-25 15:27:25 +01:00
										 |  |  | 		span := t.Tracer().StartSpan("servedns:" + metrics.WithServer(ctx))
 | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 		defer span.Finish()
 | 
					
						
							|  |  |  | 		ctx = ot.ContextWithSpan(ctx, span)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	return plugin.NextOrFailure(t.Name(), t.Next, ctx, w, r)
 | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | }
 |