trace plugin can mark traces with error tag (#4720)

Signed-off-by: Ondrej Benkovsky <ondrej.benkovsky@wandera.com>
This commit is contained in:
Ondřej Benkovský
2021-06-29 09:10:22 +02:00
committed by GitHub
parent a5ab94eabb
commit 9e90d6231e
2 changed files with 21 additions and 1 deletions

View File

@@ -16,6 +16,8 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
ot "github.com/opentracing/opentracing-go" ot "github.com/opentracing/opentracing-go"
otext "github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing" zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing"
"github.com/openzipkin/zipkin-go" "github.com/openzipkin/zipkin-go"
zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http" zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
@@ -148,6 +150,10 @@ func (t *trace) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
span.SetTag(t.tagSet.Proto, req.Proto()) span.SetTag(t.tagSet.Proto, req.Proto())
span.SetTag(t.tagSet.Remote, req.IP()) span.SetTag(t.tagSet.Remote, req.IP())
span.SetTag(t.tagSet.Rcode, rcode.ToString(rw.Rcode)) span.SetTag(t.tagSet.Rcode, rcode.ToString(rw.Rcode))
if err != nil {
otext.Error.Set(span, true)
span.LogFields(otlog.Event("error"), otlog.Error(err))
}
return status, err return status, err
} }

View File

@@ -2,6 +2,7 @@ package trace
import ( import (
"context" "context"
"errors"
"testing" "testing"
"github.com/coredns/caddy" "github.com/coredns/caddy"
@@ -44,6 +45,7 @@ func TestTrace(t *testing.T) {
rcode int rcode int
question *dns.Msg question *dns.Msg
server string server string
err error
}{ }{
{ {
name: "NXDOMAIN", name: "NXDOMAIN",
@@ -55,6 +57,12 @@ func TestTrace(t *testing.T) {
rcode: dns.RcodeSuccess, rcode: dns.RcodeSuccess,
question: new(dns.Msg).SetQuestion("example.net.", dns.TypeCNAME), question: new(dns.Msg).SetQuestion("example.net.", dns.TypeCNAME),
}, },
{
name: "SERVFAIL",
rcode: dns.RcodeServerFailure,
question: new(dns.Msg).SetQuestion("example.net.", dns.TypeA),
err: errors.New("test error"),
},
} }
defaultTagSet := tagByProvider["default"] defaultTagSet := tagByProvider["default"]
for _, tc := range cases { for _, tc := range cases {
@@ -66,6 +74,9 @@ func TestTrace(t *testing.T) {
m := new(dns.Msg) m := new(dns.Msg)
m.SetRcode(r, tc.rcode) m.SetRcode(r, tc.rcode)
w.WriteMsg(m) w.WriteMsg(m)
if tc.err != nil {
return tc.rcode, tc.err
}
return tc.rcode, nil return tc.rcode, nil
}), }),
every: 1, every: 1,
@@ -73,7 +84,7 @@ func TestTrace(t *testing.T) {
tagSet: defaultTagSet, tagSet: defaultTagSet,
} }
ctx := context.TODO() ctx := context.TODO()
if _, err := tr.ServeDNS(ctx, w, tc.question); err != nil { if _, err := tr.ServeDNS(ctx, w, tc.question); err != nil && tc.err == nil {
t.Fatalf("Error during tr.ServeDNS(ctx, w, %v): %v", tc.question, err) t.Fatalf("Error during tr.ServeDNS(ctx, w, %v): %v", tc.question, err)
} }
@@ -104,6 +115,9 @@ func TestTrace(t *testing.T) {
if rootSpan.Tag(defaultTagSet.Rcode) != rcode.ToString(tc.rcode) { if rootSpan.Tag(defaultTagSet.Rcode) != rcode.ToString(tc.rcode) {
t.Errorf("Unexpected span tag: rootSpan.Tag(%v): want %v, got %v", defaultTagSet.Rcode, rcode.ToString(tc.rcode), rootSpan.Tag(defaultTagSet.Rcode)) t.Errorf("Unexpected span tag: rootSpan.Tag(%v): want %v, got %v", defaultTagSet.Rcode, rcode.ToString(tc.rcode), rootSpan.Tag(defaultTagSet.Rcode))
} }
if tc.err != nil && rootSpan.Tag("error") != true {
t.Errorf("Unexpected span tag: rootSpan.Tag(%v): want %v, got %v", "error", true, rootSpan.Tag("error"))
}
}) })
} }
} }