Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions plugin/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
}

var (
span, child ot.Span
ret *dns.Msg
err error
i int
span ot.Span
ret *dns.Msg
err error
i int
)
span = ot.SpanFromContext(ctx)
list := g.list()
Expand All @@ -65,20 +65,25 @@ func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
proxy := list[i]
i++

callCtx := ctx
var child ot.Span
if span != nil {
child = span.Tracer().StartSpan("query", ot.ChildOf(span.Context()))
ctx = ot.ContextWithSpan(ctx, child)
child, callCtx = ot.StartSpanFromContext(callCtx, "query")
}

ret, err = proxy.query(ctx, r)
if err != nil {
// Continue with the next proxy
continue
}
var cancel context.CancelFunc
callCtx, cancel = context.WithDeadline(callCtx, deadline)

ret, err = proxy.query(callCtx, r)
cancel()

if child != nil {
child.Finish()
}
if err != nil {
// Continue with the next proxy
continue
}

// Check if the reply is correct; if not return FormErr.
if !state.Match(ret) {
Expand Down
77 changes: 77 additions & 0 deletions plugin/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import (
"errors"
"strings"
"testing"
"time"

"github.com/coredns/coredns/pb"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/pkg/fall"
"github.com/coredns/coredns/plugin/test"

"github.com/miekg/dns"
ot "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
grpcgo "google.golang.org/grpc"
)

func TestGRPC(t *testing.T) {
Expand Down Expand Up @@ -102,3 +106,76 @@ func TestGRPCFallthroughNoNext(t *testing.T) {
t.Errorf("Expected SERVFAIL when no backends and no next plugin, got: %d", rcode)
}
}

// deadlineCheckingClient records whether a deadline was attached to ctx.
type deadlineCheckingClient struct {
sawDeadline bool
lastDeadline time.Time
dnsPacket *pb.DnsPacket
err error
}

func (c *deadlineCheckingClient) Query(ctx context.Context, in *pb.DnsPacket, opts ...grpcgo.CallOption) (*pb.DnsPacket, error) {
if dl, ok := ctx.Deadline(); ok {
c.sawDeadline = true
c.lastDeadline = dl
}
return c.dnsPacket, c.err
}

// Test that on error paths we still finish child spans, and that we set a per-call deadline.
func TestGRPC_SpansOnErrorPath(t *testing.T) {
m := &dns.Msg{}
msgBytes, err := m.Pack()
if err != nil {
t.Fatalf("Error packing response: %s", err)
}
dnsPacket := &pb.DnsPacket{Msg: msgBytes}

// Proxy 1: returns error, we should still finish its child span and have a deadline
p1 := &deadlineCheckingClient{dnsPacket: nil, err: errors.New("kaboom")}
// Proxy 2: returns success
p2 := &deadlineCheckingClient{dnsPacket: dnsPacket, err: nil}

g := newGRPC()
g.from = "."
g.proxies = []*Proxy{{client: p1}, {client: p2}}

// Ensure deterministic order of the retries: try p1 then p2
g.p = new(sequential)

// Set a parent span in context so ServeDNS creates child spans per attempt
tracer := mocktracer.New()
prev := ot.GlobalTracer()
ot.SetGlobalTracer(tracer)
defer ot.SetGlobalTracer(prev)

parent := tracer.StartSpan("parent")
ctx := ot.ContextWithSpan(t.Context(), parent)

rec := dnstest.NewRecorder(&test.ResponseWriter{})
if _, err := g.ServeDNS(ctx, rec, m); err != nil {
t.Fatalf("ServeDNS returned error: %v", err)
}

// Assert both attempts finished child spans with retries
// (2 query spans: error + success)
finished := tracer.FinishedSpans()
var finishedQueries int
for _, s := range finished {
if s.OperationName == "query" {
finishedQueries++
}
}
if finishedQueries != 2 {
t.Fatalf("expected 2 finished 'query' spans, got %d (finished: %v)", finishedQueries, finished)
}

// Assert we set a deadline on the call contexts
if !p1.sawDeadline {
t.Fatalf("expected deadline to be set on first proxy call context")
}
if !p2.sawDeadline {
t.Fatalf("expected deadline to be set on second proxy call context")
}
}