-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[otel-tracing] Added Tracing to Base package (driver) #4196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
milosgajdos
merged 1 commit into
distribution:main
from
gotgelf:otel-tracing-filesystem-instrumentation
Mar 4, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call with making
storage.driver
a namespace ✅