mirror of
https://github.com/coredns/coredns.git
synced 2025-11-18 09:52:17 -05:00
Add tracing option (#487)
Adds a middleware to enable tracing with OpenTracing/OpenZipkin. Enabling tracing will have a large impact on performance so it is not advisable in production.
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/miekg/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
ot "github.com/opentracing/opentracing-go"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
@@ -70,6 +71,8 @@ var tryDuration = 60 * time.Second
|
||||
|
||||
// ServeDNS satisfies the middleware.Handler interface.
|
||||
func (p Proxy) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
var span, child ot.Span
|
||||
span = ot.SpanFromContext(ctx)
|
||||
state := request.Request{W: w, Req: r}
|
||||
for _, upstream := range p.Upstreams {
|
||||
start := time.Now()
|
||||
@@ -85,12 +88,21 @@ func (p Proxy) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
|
||||
return dns.RcodeServerFailure, errUnreachable
|
||||
}
|
||||
|
||||
if span != nil {
|
||||
child = span.Tracer().StartSpan("exchange", ot.ChildOf(span.Context()))
|
||||
ctx = ot.ContextWithSpan(ctx, child)
|
||||
}
|
||||
|
||||
atomic.AddInt64(&host.Conns, 1)
|
||||
|
||||
reply, backendErr := host.Exchange(state)
|
||||
|
||||
atomic.AddInt64(&host.Conns, -1)
|
||||
|
||||
if child != nil {
|
||||
child.Finish()
|
||||
}
|
||||
|
||||
if backendErr == nil {
|
||||
w.WriteMsg(reply)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user