-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[otel-tracing] Added Tracing to Base package (driver) (#4196)
- Loading branch information
Showing
5 changed files
with
274 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package tracing | ||
|
||
import ( | ||
"context" | ||
|
||
sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
) | ||
|
||
// compositeExporter is a custom exporter that wraps multiple SpanExporters. | ||
// It allows you to export spans to multiple destinations, e.g., different telemetry backends. | ||
type compositeExporter struct { | ||
exporters []sdktrace.SpanExporter | ||
} | ||
|
||
func newCompositeExporter(exporters ...sdktrace.SpanExporter) *compositeExporter { | ||
return &compositeExporter{exporters: exporters} | ||
} | ||
|
||
// ExportSpans iterates over each SpanExporter in the compositeExporter and | ||
// exports the spans. If any exporter returns an error, the process is stopped | ||
// and the error is returned. This ensures that span exporting behaves correctly | ||
// and reports errors as expected. | ||
func (ce *compositeExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error { | ||
for _, exporter := range ce.exporters { | ||
if err := exporter.ExportSpans(ctx, spans); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Shutdown iterates over each SpanExporter in the compositeExporter and | ||
// shuts them down. If any exporter returns an error during shutdown, the process | ||
// is stopped and the error is returned. This ensures proper shutdown of all exporters. | ||
func (ce *compositeExporter) Shutdown(ctx context.Context) error { | ||
for _, exporter := range ce.exporters { | ||
if err := exporter.Shutdown(ctx); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package tracing | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"go.opentelemetry.io/otel/sdk/trace" | ||
) | ||
|
||
type mockSpanExporter struct { | ||
exportSpansCalled bool | ||
shutdownCalled bool | ||
returnError bool | ||
} | ||
|
||
func (m *mockSpanExporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error { | ||
m.exportSpansCalled = true | ||
if m.returnError { | ||
return errors.New("export error") | ||
} | ||
return nil | ||
} | ||
|
||
func (m *mockSpanExporter) Shutdown(ctx context.Context) error { | ||
m.shutdownCalled = true | ||
if m.returnError { | ||
return errors.New("shutdown error") | ||
} | ||
return nil | ||
} | ||
func TestCompositeExporterExportSpans(t *testing.T) { | ||
mockExporter1 := &mockSpanExporter{} | ||
mockExporter2 := &mockSpanExporter{} | ||
composite := newCompositeExporter(mockExporter1, mockExporter2) | ||
|
||
err := composite.ExportSpans(context.Background(), nil) | ||
if err != nil { | ||
t.Errorf("ExportSpans() error = %v", err) | ||
} | ||
|
||
if !mockExporter1.exportSpansCalled || !mockExporter2.exportSpansCalled { | ||
t.Error("ExportSpans was not called on all exporters") | ||
} | ||
} | ||
|
||
func TestCompositeExporterExportSpans_Error(t *testing.T) { | ||
mockExporter1 := &mockSpanExporter{returnError: true} | ||
mockExporter2 := &mockSpanExporter{} | ||
composite := newCompositeExporter(mockExporter1, mockExporter2) | ||
|
||
err := composite.ExportSpans(context.Background(), nil) | ||
if err == nil { | ||
t.Error("Expected error from ExportSpans, but got none") | ||
} | ||
} | ||
|
||
func TestCompositeExporterShutdown(t *testing.T) { | ||
mockExporter1 := &mockSpanExporter{} | ||
mockExporter2 := &mockSpanExporter{} | ||
composite := newCompositeExporter(mockExporter1, mockExporter2) | ||
|
||
err := composite.Shutdown(context.Background()) | ||
if err != nil { | ||
t.Errorf("Shutdown() error = %v", err) | ||
} | ||
|
||
if !mockExporter1.shutdownCalled || !mockExporter2.shutdownCalled { | ||
t.Error("Shutdown was not called on all exporters") | ||
} | ||
} | ||
|
||
func TestCompositeExporterShutdown_Error(t *testing.T) { | ||
mockExporter1 := &mockSpanExporter{returnError: true} | ||
mockExporter2 := &mockSpanExporter{} | ||
composite := newCompositeExporter(mockExporter1, mockExporter2) | ||
|
||
err := composite.Shutdown(context.Background()) | ||
if err == nil { | ||
t.Error("Expected error from Shutdown, but got none") | ||
} | ||
} |
Oops, something went wrong.