@@ -15,6 +15,7 @@ import (
1515 "github.com/stretchr/testify/require"
1616 "go.opentelemetry.io/otel"
1717 "go.opentelemetry.io/otel/sdk/trace"
18+ "go.opentelemetry.io/otel/sdk/trace/tracetest"
1819
1920 "gofr.dev/pkg/gofr/config"
2021 "gofr.dev/pkg/gofr/container"
@@ -234,3 +235,30 @@ func TestGetAuthInfo_JWTClaims(t *testing.T) {
234235
235236 assert .Equal (t , claims , res )
236237}
238+
239+ func TestContext_GetCorrelationID (t * testing.T ) {
240+ // Setup OpenTelemetry tracer
241+ exporter := tracetest .NewInMemoryExporter ()
242+ tp := trace .NewTracerProvider (trace .WithSyncer (exporter ))
243+ otel .SetTracerProvider (tp )
244+ tracer := tp .Tracer ("test" )
245+
246+ t .Run ("with span" , func (t * testing.T ) {
247+ ctx , span := tracer .Start (t .Context (), "test-span" )
248+ defer span .End ()
249+
250+ gofCtx := & Context {Context : ctx }
251+ correlationID := gofCtx .GetCorrelationID ()
252+
253+ assert .Len (t , correlationID , 32 , "Expected correlation ID length 32, got %d" , len (correlationID ))
254+ assert .NotEqual (t , "00000000000000000000000000000000" , correlationID , "Expected non-empty correlation ID" )
255+ })
256+
257+ t .Run ("without span" , func (t * testing.T ) {
258+ gofCtx := & Context {Context : t .Context ()}
259+ correlationID := gofCtx .GetCorrelationID ()
260+
261+ expected := "00000000000000000000000000000000"
262+ assert .Equal (t , expected , correlationID , "Expected empty TraceID when no span present" )
263+ })
264+ }
0 commit comments