-
Notifications
You must be signed in to change notification settings - Fork 4
feat: extensible OTEL span filtering and naming #81
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
Closed
thoriqzs25
wants to merge
2
commits into
go-coldbrew:main
from
thoriqzs25:feat/otel-span-customization
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,183 @@ | ||
| // Package otel provides OpenTelemetry utilities for ColdBrew services. | ||
| package otel | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
| ) | ||
|
|
||
| // SpanFilter allows clients to implement custom span filtering logic. | ||
| // Return true to drop the span (not export it), false to keep it. | ||
| type SpanFilter interface { | ||
| // ShouldDrop returns true if the span should be filtered out. | ||
| ShouldDrop(span sdktrace.ReadOnlySpan) bool | ||
| } | ||
|
|
||
| // SpanFilterFunc is a function adapter for SpanFilter interface. | ||
| type SpanFilterFunc func(span sdktrace.ReadOnlySpan) bool | ||
|
|
||
| // ShouldDrop implements SpanFilter. | ||
| func (f SpanFilterFunc) ShouldDrop(span sdktrace.ReadOnlySpan) bool { | ||
| return f(span) | ||
| } | ||
|
|
||
| // SpanTransformer allows clients to transform span data before export. | ||
| // This is useful for renaming spans, adding attributes, etc. | ||
| type SpanTransformer interface { | ||
| // Transform returns a modified span name. Return empty string to keep original. | ||
| Transform(span sdktrace.ReadOnlySpan) string | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // SpanTransformerFunc is a function adapter for SpanTransformer interface. | ||
| type SpanTransformerFunc func(span sdktrace.ReadOnlySpan) string | ||
|
|
||
| // Transform implements SpanTransformer. | ||
| func (f SpanTransformerFunc) Transform(span sdktrace.ReadOnlySpan) string { | ||
| return f(span) | ||
| } | ||
|
|
||
| // SpanProcessorConfig configures the custom span processor. | ||
| type SpanProcessorConfig struct { | ||
| // GRPCSpanNameFormat controls gRPC span naming. | ||
| // "short" extracts just the method name (e.g., "V0GetStats") | ||
| // "full" keeps the full path (e.g., "/pkg.Service/V0GetStats") - default | ||
| GRPCSpanNameFormat string | ||
|
|
||
| // FilterSpanNames is a list of span names to filter out (exact match). | ||
| // Common use: []string{"ServeHTTP"} to filter HTTP transport spans. | ||
| FilterSpanNames []string | ||
|
|
||
| // Filters are custom filters provided by the client. | ||
| // All filters are checked; if any returns true, the span is dropped. | ||
| Filters []SpanFilter | ||
|
|
||
| // Transformers are custom transformers provided by the client. | ||
| // Applied in order; first non-empty result wins. | ||
| Transformers []SpanTransformer | ||
| } | ||
|
|
||
| // SpanProcessor wraps a SpanProcessor with filtering and transformation. | ||
| // It implements sdktrace.SpanProcessor. | ||
| type SpanProcessor struct { | ||
| next sdktrace.SpanProcessor | ||
| config SpanProcessorConfig | ||
| filterNames map[string]struct{} | ||
| mu sync.RWMutex | ||
| } | ||
|
|
||
| // NewSpanProcessor creates a new SpanProcessor wrapping the given processor. | ||
| func NewSpanProcessor(next sdktrace.SpanProcessor, config SpanProcessorConfig) *SpanProcessor { | ||
| filterNames := make(map[string]struct{}, len(config.FilterSpanNames)) | ||
| for _, name := range config.FilterSpanNames { | ||
| filterNames[name] = struct{}{} | ||
| } | ||
| return &SpanProcessor{ | ||
| next: next, | ||
| config: config, | ||
| filterNames: filterNames, | ||
| } | ||
| } | ||
|
|
||
| // OnStart is called when a span starts. | ||
| func (p *SpanProcessor) OnStart(parent context.Context, s sdktrace.ReadWriteSpan) { | ||
| p.next.OnStart(parent, s) | ||
| } | ||
|
|
||
| // OnEnd is called when a span ends. This is where filtering and transformation happen. | ||
| func (p *SpanProcessor) OnEnd(s sdktrace.ReadOnlySpan) { | ||
| // Check exact name filter | ||
| if _, ok := p.filterNames[s.Name()]; ok { | ||
| return // filtered out | ||
| } | ||
|
|
||
| // Check custom filters (need lock since AddFilter can modify concurrently) | ||
| p.mu.RLock() | ||
| filters := p.config.Filters | ||
| p.mu.RUnlock() | ||
| for _, filter := range filters { | ||
| if filter.ShouldDrop(s) { | ||
| return // filtered out | ||
| } | ||
| } | ||
|
|
||
| // Apply transformations if needed | ||
| // Note: ReadOnlySpan doesn't allow modification, so we use a wrapper | ||
| // that applies name transformation on read. | ||
| transformed := p.maybeTransform(s) | ||
|
|
||
| p.next.OnEnd(transformed) | ||
| } | ||
|
|
||
| // maybeTransform applies transformations and returns a potentially wrapped span. | ||
| func (p *SpanProcessor) maybeTransform(s sdktrace.ReadOnlySpan) sdktrace.ReadOnlySpan { | ||
| newName := "" | ||
|
|
||
| // Apply short gRPC span name format | ||
| if p.config.GRPCSpanNameFormat == "short" { | ||
| name := s.Name() | ||
| // gRPC spans have format "/pkg.Service/Method" | ||
| if strings.HasPrefix(name, "/") && strings.Contains(name, "/") { | ||
| parts := strings.Split(name, "/") | ||
| if len(parts) >= 2 { | ||
| newName = parts[len(parts)-1] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Apply custom transformers (first non-empty wins) | ||
| // Need lock since AddTransformer can modify concurrently | ||
| p.mu.RLock() | ||
| transformers := p.config.Transformers | ||
| p.mu.RUnlock() | ||
| for _, t := range transformers { | ||
| if result := t.Transform(s); result != "" { | ||
| newName = result | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if newName != "" && newName != s.Name() { | ||
| return &renamedSpan{ReadOnlySpan: s, name: newName} | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| // Shutdown shuts down the processor. | ||
| func (p *SpanProcessor) Shutdown(ctx context.Context) error { | ||
| return p.next.Shutdown(ctx) | ||
| } | ||
|
|
||
| // ForceFlush forces a flush of the processor. | ||
| func (p *SpanProcessor) ForceFlush(ctx context.Context) error { | ||
| return p.next.ForceFlush(ctx) | ||
| } | ||
|
|
||
| // AddFilter adds a custom filter at runtime. | ||
| // Thread-safe. | ||
| func (p *SpanProcessor) AddFilter(f SpanFilter) { | ||
| p.mu.Lock() | ||
| defer p.mu.Unlock() | ||
| p.config.Filters = append(p.config.Filters, f) | ||
| } | ||
|
|
||
| // AddTransformer adds a custom transformer at runtime. | ||
| // Thread-safe. | ||
| func (p *SpanProcessor) AddTransformer(t SpanTransformer) { | ||
| p.mu.Lock() | ||
| defer p.mu.Unlock() | ||
| p.config.Transformers = append(p.config.Transformers, t) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // renamedSpan wraps a ReadOnlySpan to return a different name. | ||
| type renamedSpan struct { | ||
| sdktrace.ReadOnlySpan | ||
| name string | ||
| } | ||
|
|
||
| // Name returns the transformed span name. | ||
| func (s *renamedSpan) Name() string { | ||
| return s.name | ||
| } | ||
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.
Warn on unsupported
OTEL_GRPC_SPAN_NAME_FORMATvalues.Anything other than
"short"currently falls back to full naming silently, so a typo just disables the feature. Adding aValidate()warning here would match how other enum-like config fields are handled in this file.🤖 Prompt for AI Agents