From 9f282a1e1db15b967f119c11fc4e22c90f710f2c Mon Sep 17 00:00:00 2001 From: Peter Bryant Date: Mon, 26 May 2025 02:33:04 -0500 Subject: [PATCH] Fix/issue 6560 install trace bridge test (#6814) Fixes #6560 ## Summary Adds tests for the `InstallTraceBridge` function to verify that the `octrace.DefaultTracer` is correctly set when the function is called with various configuration options. ## Changes - Added `TestInstallTraceBridge` test function in `bridge/opencensus/trace_test.go` - Tests cover multiple scenarios: - Installation with default options - Installation with custom tracer provider - Installation with tracer provider and exporter - Verifies that `octrace.DefaultTracer` is properly updated. - Verifies that the installed tracer can create functional spans. - Ensures proper cleanup of global state after tests. ## Test Results ```bash === RUN TestInstallTraceBridge === RUN TestInstallTraceBridge/install_with_default_options === RUN TestInstallTraceBridge/install_with_custom_tracer_provider === RUN TestInstallTraceBridge/install_with_tracer_provider_with_exporter --- PASS: TestInstallTraceBridge (0.00s) ``` --------- Co-authored-by: Damien Mathieu <42@dmathieu.com> Co-authored-by: Tyler Yahn Co-authored-by: dmathieu --- bridge/opencensus/trace_test.go | 93 +++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/bridge/opencensus/trace_test.go b/bridge/opencensus/trace_test.go index f1ec18fb4..b4e7dbdd7 100644 --- a/bridge/opencensus/trace_test.go +++ b/bridge/opencensus/trace_test.go @@ -144,3 +144,96 @@ func TestOTelSpanContextToOC(t *testing.T) { }) } } + +func TestInstallTraceBridge(t *testing.T) { + originalTracer := octrace.DefaultTracer + defer func() { + octrace.DefaultTracer = originalTracer + }() + + tests := []struct { + name string + opts []TraceOption + expectValidSpans bool + }{ + { + name: "install with default options", + opts: nil, + expectValidSpans: false, + }, + { + name: "install with custom tracer provider", + opts: []TraceOption{ + WithTracerProvider(trace.NewTracerProvider()), + }, + expectValidSpans: true, + }, + { + name: "install with tracer provider with exporter", + opts: []TraceOption{ + WithTracerProvider( + trace.NewTracerProvider( + trace.WithSyncer(tracetest.NewInMemoryExporter()), + ), + ), + }, + expectValidSpans: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + beforeTracer := octrace.DefaultTracer + + InstallTraceBridge(tt.opts...) + + assert.NotEqual( + t, + beforeTracer, + octrace.DefaultTracer, + "DefaultTracer should be updated", + ) + assert.NotNil( + t, + octrace.DefaultTracer, + "DefaultTracer should not be nil", + ) + + ctx, span := octrace.DefaultTracer.StartSpan( + context.Background(), + "test-span", + ) + assert.NotNil( + t, + span, + "Should be able to create spans", + ) + assert.NotNil(t, ctx, "Should return a valid context") + + spanContext := span.SpanContext() + if tt.expectValidSpans { + assert.NotEqual( + t, + octrace.TraceID{}, + spanContext.TraceID, + "Span should have a non-zero TraceID", + ) + assert.NotEqual( + t, + octrace.SpanID{}, + spanContext.SpanID, + "Span should have a non-zero SpanID", + ) + } + + span.End() + + spanFromContext := octrace.DefaultTracer.FromContext(ctx) + assert.NotNil( + t, + spanFromContext, + "Should be able to get span from context", + ) + }) + } +}