Skip to content
Open
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
36 changes: 34 additions & 2 deletions go/core/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ func Tracer() trace.Tracer {
// such as one that writes to a file.
func WriteTelemetryImmediate(client TelemetryClient) {
e := newTelemetryServerExporter(client)
TracerProvider().RegisterSpanProcessor(sdktrace.NewSimpleSpanProcessor(e))
filtered := &filteringExporter{exporter: e}
TracerProvider().RegisterSpanProcessor(sdktrace.NewSimpleSpanProcessor(filtered))
}

// WriteTelemetryBatch adds a telemetry server to the global tracer provider.
Expand All @@ -146,7 +147,8 @@ func WriteTelemetryImmediate(client TelemetryClient) {
// and perform other cleanup.
func WriteTelemetryBatch(client TelemetryClient) (shutdown func(context.Context) error) {
e := newTelemetryServerExporter(client)
TracerProvider().RegisterSpanProcessor(sdktrace.NewBatchSpanProcessor(e))
filtered := &filteringExporter{exporter: e}
TracerProvider().RegisterSpanProcessor(sdktrace.NewBatchSpanProcessor(filtered))
return TracerProvider().Shutdown
}

Expand Down Expand Up @@ -402,3 +404,33 @@ func SpanPath(ctx context.Context) string {
func SpanTraceInfo(ctx context.Context) TraceInfo {
return spanMetaKey.FromContext(ctx).TraceInfo
}

type filteringExporter struct {
exporter sdktrace.SpanExporter
}

func (e *filteringExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
var genkitSpans []sdktrace.ReadOnlySpan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better performance and to avoid potential reallocations within the loop, it's a good practice to pre-allocate the slice with a known capacity. This ensures that the backing array for the slice is allocated only once.

Suggested change
var genkitSpans []sdktrace.ReadOnlySpan
genkitSpans := make([]sdktrace.ReadOnlySpan, 0, len(spans))
for _, span := range spans {
if isGenkitSpan(span) {
genkitSpans = append(genkitSpans, span)
}
}
if len(genkitSpans) == 0 {
return nil
}
return e.exporter.ExportSpans(ctx, genkitSpans)
}

func (e *filteringExporter) Shutdown(ctx context.Context) error {
return e.exporter.Shutdown(ctx)
}

func isGenkitSpan(span sdktrace.ReadOnlySpan) bool {
for _, attr := range span.Attributes() {
if strings.HasPrefix(string(attr.Key), "genkit") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and avoid magic strings, please use the existing attrPrefix constant instead of the hardcoded string "genkit".

Suggested change
if strings.HasPrefix(string(attr.Key), "genkit") {
if strings.HasPrefix(string(attr.Key), attrPrefix) {
return true
}
}
return false
}
Loading