publish trace id as metadata from trace plugin (#4749)

Signed-off-by: Ondrej Benkovsky <ondrej.benkovsky@wandera.com>
This commit is contained in:
Ondřej Benkovský
2021-07-14 09:21:41 +02:00
committed by GitHub
parent e62600a196
commit 936b483a3a
2 changed files with 17 additions and 0 deletions

View File

@@ -93,6 +93,13 @@ trace tracinghost:9411 {
}
~~~
## Metadata
The trace plugin will publish the following metadata, if the *metadata*
plugin is also enabled:
* `trace/traceid`: identifier of (zipkin/datadog) trace of processed request
## See Also
See the *debug* plugin for more information about debug logging.

View File

@@ -8,6 +8,7 @@ import (
"sync/atomic"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metadata"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/rcode"
@@ -21,6 +22,7 @@ import (
zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing"
"github.com/openzipkin/zipkin-go"
zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
@@ -28,6 +30,7 @@ import (
const (
defaultTopLevelSpanName = "servedns"
metaTraceIdKey = "trace/traceid"
)
type traceTags struct {
@@ -141,6 +144,13 @@ func (t *trace) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
span = t.Tracer().StartSpan(defaultTopLevelSpanName)
defer span.Finish()
switch spanCtx := span.Context().(type) {
case zipkinot.SpanContext:
metadata.SetValueFunc(ctx, metaTraceIdKey, func() string{ return spanCtx.TraceID.String() })
case ddtrace.SpanContext:
metadata.SetValueFunc(ctx, metaTraceIdKey, func() string{ return fmt.Sprint(spanCtx.TraceID()) })
}
rw := dnstest.NewRecorder(w)
ctx = ot.ContextWithSpan(ctx, span)
status, err := plugin.NextOrFailure(t.Name(), t.Next, ctx, rw, r)