Skip to content

Commit f9b51bb

Browse files
KarstenWintermannyaron2artursouza
authored
Prevent invalid traceparent ("00-00000...") from being returned to the caller (dapr#7406)
* Prevent invalid traceparent ("00-00000...") from being returned to the caller Signed-off-by: Karsten Wintermann <[email protected]> * OpenTelemetry trace context not transmitted when using rawPayload with Kafka Fixes dapr#7372 Signed-off-by: Karsten Wintermann <[email protected]> --------- Signed-off-by: Karsten Wintermann <[email protected]> Co-authored-by: Yaron Schneider <[email protected]> Co-authored-by: Artur Souza <[email protected]>
1 parent 38fb965 commit f9b51bb

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

pkg/channel/http/http_channel.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,11 @@ func (h *Channel) constructRequest(ctx context.Context, req *invokev1.InvokeMeth
350350

351351
// HTTP client needs to inject traceparent header for proper tracing stack.
352352
span := diagUtils.SpanFromContext(ctx)
353-
tp := diag.SpanContextToW3CString(span.SpanContext())
353+
if span.SpanContext().HasTraceID() {
354+
tp := diag.SpanContextToW3CString(span.SpanContext())
355+
channelReq.Header.Set("traceparent", tp)
356+
}
354357
ts := diag.TraceStateToW3CString(span.SpanContext())
355-
channelReq.Header.Set("traceparent", tp)
356358
if ts != "" {
357359
channelReq.Header.Set("tracestate", ts)
358360
}

pkg/channel/http/http_channel_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -811,3 +811,37 @@ func TestHealthProbe(t *testing.T) {
811811
require.NoError(t, err)
812812
assert.False(t, success)
813813
}
814+
815+
func TestNoInvalidTraceContext(t *testing.T) {
816+
ctx := context.Background()
817+
818+
handler := &testHandlerHeaders{}
819+
testServer := httptest.NewServer(handler)
820+
c := Channel{
821+
baseAddress: testServer.URL,
822+
client: http.DefaultClient,
823+
compStore: compstore.New(),
824+
}
825+
req := invokev1.NewInvokeMethodRequest("method").
826+
WithContentType("text/plain").
827+
WithMetadata(map[string][]string{invokev1.ContentLengthHeader: {"1"}}).
828+
WithHTTPExtension(http.MethodPost, "").
829+
WithRawDataString("1")
830+
defer req.Close()
831+
832+
// act
833+
resp, err := c.InvokeMethod(ctx, req, "")
834+
835+
// assert
836+
require.NoError(t, err)
837+
defer resp.Close()
838+
body, _ := resp.RawDataFull()
839+
actual := map[string]string{}
840+
json.Unmarshal(body, &actual)
841+
traceparent, hasTraceparent := actual["Traceparent"]
842+
require.NoError(t, err)
843+
if hasTraceparent {
844+
assert.NotEqual(t, "00-00000000000000000000000000000000-0000000000000000-00", traceparent)
845+
}
846+
testServer.Close()
847+
}

0 commit comments

Comments
 (0)