1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-08-10 22:31:50 +02:00

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 <MrAlias@users.noreply.github.com>
Co-authored-by: dmathieu <damien.mathieu@elastic.co>
This commit is contained in:
Peter Bryant
2025-05-26 02:33:04 -05:00
committed by GitHub
parent 69f189f0b6
commit 9f282a1e1d

View File

@@ -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",
)
})
}
}