Skip to content
Open
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
22 changes: 20 additions & 2 deletions modules/caddyhttp/tracing/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tracing
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -121,20 +122,26 @@ func TestTracing_ServeHTTP_Propagation_Without_Initial_Headers(t *testing.T) {
if err := ot.ServeHTTP(w, req, handler); err != nil {
t.Errorf("ServeHTTP error: %v", err)
}

responseTraceParent := w.Header().Get("Traceparent")
if responseTraceParent == "" {
t.Errorf("Missing traceparent in response headers")
}
}

func TestTracing_ServeHTTP_Propagation_With_Initial_Headers(t *testing.T) {
ot := &Tracing{
SpanName: "mySpan",
}

dummyTraceParentPrefix := "00-11111111111111111111111111111111"
req := createRequestWithContext("GET", "https://example.com/foo")
req.Header.Set("traceparent", "00-11111111111111111111111111111111-1111111111111111-01")
req.Header.Set("traceparent", fmt.Sprintf("%s-1111111111111111-01", dummyTraceParentPrefix))
w := httptest.NewRecorder()

var handler caddyhttp.HandlerFunc = func(writer http.ResponseWriter, request *http.Request) error {
traceparent := request.Header.Get("Traceparent")
if !strings.HasPrefix(traceparent, "00-11111111111111111111111111111111") {
if !strings.HasPrefix(traceparent, dummyTraceParentPrefix) {
t.Errorf("Invalid traceparent: %v", traceparent)
}

Expand All @@ -152,6 +159,17 @@ func TestTracing_ServeHTTP_Propagation_With_Initial_Headers(t *testing.T) {
if err := ot.ServeHTTP(w, req, handler); err != nil {
t.Errorf("ServeHTTP error: %v", err)
}

responseTraceParent := w.Header().Get("Traceparent")
if responseTraceParent == "" {
t.Error("Missing traceparent in response headers")
} else if !strings.HasPrefix(responseTraceParent, dummyTraceParentPrefix) {
t.Errorf(
"Traceparent prefix in response headers (%s) not same as initial (%s)",
responseTraceParent,
dummyTraceParentPrefix,
)
}
}

func TestTracing_ServeHTTP_Next_Error(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions modules/caddyhttp/tracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request
caddyhttp.SetVar(ctx, "trace_id", traceID)
// Add a span_id placeholder, accessible via `{http.vars.span_id}`.
caddyhttp.SetVar(ctx, "span_id", spanID)
// Add traceId and spanId to response headers
w.Header().Set("traceparent", fmt.Sprintf("00-%s-%s-01", traceID, spanID))
// Add the traceID and spanID to the log fields for the request.
if extra, ok := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields); ok {
extra.Add(zap.String("traceID", traceID))
Expand Down
Loading