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
16 changes: 14 additions & 2 deletions app/controlplane/internal/sentrycontext/sentry_context.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2024 The Chainloop Authors.
// Copyright 2024-2026 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,7 @@ import (
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/logging"
"github.com/go-kratos/kratos/v2/transport"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/metadata"
)

Expand Down Expand Up @@ -89,12 +90,23 @@ func buildRequestContext(ctx context.Context, req interface{}) map[string]interf
operation = info.Operation()
}

return map[string]interface{}{
requestContext := map[string]interface{}{
"protocol": protocol,
"operation": operation,
"args": extractArgs(req),
"request-id": extractTracingIDFromMetadata(ctx),
}

// Surface the OpenTelemetry trace/span IDs so a Sentry event can be cross-referenced
// with its distributed trace in the OTLP backend (Tempo/Jaeger). The gRPC server is
// instrumented via otelgrpc, so a valid span context is present on ctx when tracing is
// enabled. These are informational fields and do not alter Sentry's canonical trace_id.
if sc := trace.SpanContextFromContext(ctx); sc.IsValid() {
requestContext["otel-trace-id"] = sc.TraceID().String()
requestContext["otel-span-id"] = sc.SpanID().String()
}

return requestContext
}

// extractTracingIDFromMetadata extracts the tracing ID from the metadata.
Expand Down
23 changes: 22 additions & 1 deletion app/controlplane/internal/sentrycontext/sentry_context_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2024 The Chainloop Authors.
// Copyright 2024-2026 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@ import (

"github.com/go-kratos/kratos/v2/transport"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/metadata"
)

Expand Down Expand Up @@ -122,6 +123,26 @@ func TestBuildRequestContext(t *testing.T) {
assert.Equal(t, "", requestContext["protocol"])
assert.Equal(t, "", requestContext["operation"])
})

t.Run("with a valid otel span context", func(t *testing.T) {
traceID, _ := trace.TraceIDFromHex("36b9df80c3e0c37e920caef35fa6eca0")
spanID, _ := trace.SpanIDFromHex("a1b2c3d4e5f60718")
sc := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
})
ctx := trace.ContextWithSpanContext(context.Background(), sc)

requestContext := buildRequestContext(ctx, req)
assert.Equal(t, "36b9df80c3e0c37e920caef35fa6eca0", requestContext["otel-trace-id"])
assert.Equal(t, "a1b2c3d4e5f60718", requestContext["otel-span-id"])
})

t.Run("without a valid otel span context", func(t *testing.T) {
requestContext := buildRequestContext(context.Background(), req)
assert.NotContains(t, requestContext, "otel-trace-id")
assert.NotContains(t, requestContext, "otel-span-id")
})
}

func TestExtractTracingIDFromMetadata(t *testing.T) {
Expand Down
Loading