Add request and response context to traces (#2162)
Automatically submitted.
This commit is contained in:
parent
1018a8267a
commit
8a9c6174fc
7 changed files with 719 additions and 13 deletions
|
@ -10,8 +10,11 @@ import (
|
|||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/metrics"
|
||||
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
||||
"github.com/coredns/coredns/plugin/pkg/rcode"
|
||||
// Plugin the trace package.
|
||||
_ "github.com/coredns/coredns/plugin/pkg/trace"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
ddtrace "github.com/DataDog/dd-trace-go/opentracing"
|
||||
"github.com/miekg/dns"
|
||||
|
@ -19,6 +22,12 @@ import (
|
|||
zipkin "github.com/openzipkin/zipkin-go-opentracing"
|
||||
)
|
||||
|
||||
const (
|
||||
tagName = "coredns.io/name"
|
||||
tagType = "coredns.io/type"
|
||||
tagRcode = "coredns.io/rcode"
|
||||
)
|
||||
|
||||
type trace struct {
|
||||
Next plugin.Handler
|
||||
Endpoint string
|
||||
|
@ -94,10 +103,26 @@ func (t *trace) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
|
|||
trace = true
|
||||
}
|
||||
}
|
||||
if span := ot.SpanFromContext(ctx); span == nil && trace {
|
||||
span := t.Tracer().StartSpan("servedns:" + metrics.WithServer(ctx))
|
||||
defer span.Finish()
|
||||
ctx = ot.ContextWithSpan(ctx, span)
|
||||
span := ot.SpanFromContext(ctx)
|
||||
if !trace || span != nil {
|
||||
return plugin.NextOrFailure(t.Name(), t.Next, ctx, w, r)
|
||||
}
|
||||
return plugin.NextOrFailure(t.Name(), t.Next, ctx, w, r)
|
||||
|
||||
req := request.Request{W: w, Req: r}
|
||||
span = t.Tracer().StartSpan(spanName(ctx, req))
|
||||
defer span.Finish()
|
||||
|
||||
rw := dnstest.NewRecorder(w)
|
||||
ctx = ot.ContextWithSpan(ctx, span)
|
||||
status, err := plugin.NextOrFailure(t.Name(), t.Next, ctx, rw, r)
|
||||
|
||||
span.SetTag(tagName, req.Name())
|
||||
span.SetTag(tagType, req.Type())
|
||||
span.SetTag(tagRcode, rcode.ToString(rw.Rcode))
|
||||
|
||||
return status, err
|
||||
}
|
||||
|
||||
func spanName(ctx context.Context, req request.Request) string {
|
||||
return "servedns:" + metrics.WithServer(ctx) + " " + req.Name()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue