Skip to content

Commit

Permalink
address grpc-trace-bin propagator test comments
Browse files Browse the repository at this point in the history
  • Loading branch information
purnesh42H committed Nov 21, 2024
1 parent d708a8b commit 1e22ed3
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions stats/opentelemetry/grpc_trace_bin_propagator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,25 @@ var validSpanContext = oteltrace.SpanContext{}.WithTraceID(
// header is not set in the carrier's context metadata.
func (s) TestInject(t *testing.T) {
tests := []struct {
name string
injectSC oteltrace.SpanContext
wantSC oteltrace.SpanContext
md metadata.MD
wantKeys []string
name string
injectSC oteltrace.SpanContext
incomingMd metadata.MD
outgoingMd metadata.MD
wantKeys []string
}{
{
name: "valid OpenTelemetry span context",
injectSC: validSpanContext,
wantSC: validSpanContext,
md: metadata.MD{"incoming-key": []string{"incoming-value"}, "outgoing-key": []string{"outgoing-value"}},
wantKeys: []string{grpcTraceBinHeaderKey, "outgoing-key"},
name: "valid OpenTelemetry span context",
injectSC: validSpanContext,
incomingMd: metadata.MD{"incoming-key": []string{"incoming-value"}},
outgoingMd: metadata.MD{"outgoing-key": []string{"outgoing-value"}},
wantKeys: []string{grpcTraceBinHeaderKey, "outgoing-key"},
},
{
name: "invalid OpenTelemetry span context",
injectSC: oteltrace.SpanContext{},
wantSC: oteltrace.SpanContext{},
md: metadata.MD{"incoming-key": []string{"incoming-value"}, "outgoing-key": []string{"outgoing-value"}},
wantKeys: []string{"outgoing-key"},
name: "invalid OpenTelemetry span context",
injectSC: oteltrace.SpanContext{},
incomingMd: metadata.MD{"incoming-key": []string{"incoming-value"}},
outgoingMd: metadata.MD{"outgoing-key": []string{"outgoing-value"}},
wantKeys: []string{"outgoing-key"},
},
}

Expand All @@ -73,32 +73,32 @@ func (s) TestInject(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx = oteltrace.ContextWithSpanContext(ctx, test.injectSC)
ctx = metadata.NewIncomingContext(ctx, metadata.MD{"incoming-key": test.md["incoming-key"]})
ctx = metadata.NewOutgoingContext(ctx, metadata.MD{"outgoing-key": test.md["outgoing-key"]})
ctx = metadata.NewIncomingContext(ctx, test.incomingMd)
ctx = metadata.NewOutgoingContext(ctx, test.outgoingMd)

c := itracing.NewOutgoingCarrier(ctx)
p.Inject(ctx, c)

if gotKeys := c.Keys(); !cmp.Equal(test.wantKeys, gotKeys, cmpopts.SortSlices(func(a, b string) bool { return a < b })) {
t.Fatalf("c.Keys() = keys %v, want %v", gotKeys, test.wantKeys)
t.Errorf("c.Keys() = keys %v, want %v", gotKeys, test.wantKeys)
}
md, _ := metadata.FromOutgoingContext(c.Context())
gotH := md.Get(grpcTraceBinHeaderKey)
if !test.wantSC.IsValid() {
if !test.injectSC.IsValid() {
if len(gotH) > 0 {
t.Fatalf("got non-empty value from Carrier's context metadata grpc-trace-bin header, want empty")
t.Fatalf("got %v value from Carrier's context metadata grpc-trace-bin header, want empty", gotH)
}
return
}
if gotH[len(gotH)-1] == "" {
t.Fatalf("got empty value from Carrier's context metadata grpc-trace-bin header, want valid span context: %v", test.wantSC)
t.Fatalf("got empty value from Carrier's context metadata grpc-trace-bin header, want valid span context: %v", test.injectSC)
}
gotSC, ok := fromBinary([]byte(gotH[len(gotH)-1]))
if !ok {
t.Fatalf("got invalid span context from Carrier's context metadata grpc-trace-bin header, want valid span context: %v", test.wantSC)
t.Fatalf("got invalid span context %v from Carrier's context metadata grpc-trace-bin header, want valid span context: %v", gotSC, test.injectSC)
}
if test.wantSC.TraceID() != gotSC.TraceID() && test.wantSC.SpanID() != gotSC.SpanID() && test.wantSC.TraceFlags() != gotSC.TraceFlags() {
t.Fatalf("got span context = %v, want span contexts %v", gotSC, test.wantSC)
if test.injectSC.TraceID() != gotSC.TraceID() && test.injectSC.SpanID() != gotSC.SpanID() && test.injectSC.TraceFlags() != gotSC.TraceFlags() {
t.Fatalf("got span context = %v, want span contexts %v", gotSC, test.injectSC)
}
})
}
Expand All @@ -114,22 +114,25 @@ func (s) TestInject(t *testing.T) {
// context is not extracted.
func (s) TestExtract(t *testing.T) {
tests := []struct {
name string
wantSC oteltrace.SpanContext // expected span context from carrier
md metadata.MD
wantKeys []string
name string
wantSC oteltrace.SpanContext // expected span context from carrier
incomingMd metadata.MD
outgoingMd metadata.MD
wantKeys []string
}{
{
name: "valid OpenTelemetry span context",
wantSC: validSpanContext.WithRemote(true),
md: metadata.MD{"incoming-key": []string{"incoming-value"}, "outgoing-key": []string{"outgoing-value"}},
wantKeys: []string{grpcTraceBinHeaderKey, "incoming-key"},
name: "valid OpenTelemetry span context",
wantSC: validSpanContext.WithRemote(true),
incomingMd: metadata.MD{grpcTraceBinHeaderKey: []string{string(toBinary(validSpanContext.WithRemote(true)))}, "incoming-key": []string{"incoming-value"}},
outgoingMd: metadata.MD{"outgoing-key": []string{"outgoing-value"}},
wantKeys: []string{grpcTraceBinHeaderKey, "incoming-key"},
},
{
name: "invalid OpenTelemetry span context",
wantSC: oteltrace.SpanContext{},
md: metadata.MD{"incoming-key": []string{"incoming-value"}, "outgoing-key": []string{"outgoing-value"}},
wantKeys: []string{grpcTraceBinHeaderKey, "incoming-key"},
name: "invalid OpenTelemetry span context",
wantSC: oteltrace.SpanContext{},
incomingMd: metadata.MD{grpcTraceBinHeaderKey: []string{string(toBinary(oteltrace.SpanContext{}))}, "incoming-key": []string{"incoming-value"}},
outgoingMd: metadata.MD{"outgoing-key": []string{"outgoing-value"}},
wantKeys: []string{grpcTraceBinHeaderKey, "incoming-key"},
},
}

Expand All @@ -138,13 +141,13 @@ func (s) TestExtract(t *testing.T) {
p := GRPCTraceBinPropagator{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx = metadata.NewIncomingContext(ctx, metadata.MD{grpcTraceBinHeaderKey: []string{string(toBinary(test.wantSC))}, "incoming-key": test.md["incoming-key"]})
ctx = metadata.NewOutgoingContext(ctx, metadata.MD{"outgoing-key": test.md["outgoing-key"]})
ctx = metadata.NewIncomingContext(ctx, test.incomingMd)
ctx = metadata.NewOutgoingContext(ctx, test.outgoingMd)

c := itracing.NewIncomingCarrier(ctx)

if gotKeys := c.Keys(); !cmp.Equal(test.wantKeys, gotKeys, cmpopts.SortSlices(func(a, b string) bool { return a < b })) {
t.Fatalf("c.Keys() = keys %v, want %v", gotKeys, test.wantKeys)
t.Errorf("c.Keys() = keys %v, want %v", gotKeys, test.wantKeys)
}
tCtx := p.Extract(ctx, c)
got := oteltrace.SpanContextFromContext(tCtx)
Expand Down

0 comments on commit 1e22ed3

Please sign in to comment.