-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: sdk/trace: span processed metric for simple span processor #7162
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
mahendrabishnoi2
wants to merge
13
commits into
open-telemetry:main
from
mahendrabishnoi2:sdk-trace-simple-processor-metrics
Closed
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f7c180e
implementation of `otel.sdk.processor.span.processed` metric for simp…
mahendrabishnoi2 f15ae4e
add correct PR number
mahendrabishnoi2 d743f6a
fix component type
mahendrabishnoi2 b686526
Merge branch 'main' into sdk-trace-simple-processor-metrics
mahendrabishnoi2 17eb6c7
processorIDCounter -> simpleProcessorIDCounter as processorIDCounter …
mahendrabishnoi2 f8aa01e
test cases for simple span processor
mahendrabishnoi2 5c0fafb
fix lint
mahendrabishnoi2 6f72bbb
review comments
mahendrabishnoi2 ec0b88a
review comments
mahendrabishnoi2 966d7d1
review comments
mahendrabishnoi2 df565df
run `make precommit`
mahendrabishnoi2 053ca17
Merge branch 'main' into sdk-trace-simple-processor-metrics
mahendrabishnoi2 97c11ff
fix issue caused by merge with main due to function name collision
mahendrabishnoi2 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 |
|---|---|---|
|
|
@@ -5,10 +5,18 @@ package trace // import "go.opentelemetry.io/otel/sdk/trace" | |
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "sync/atomic" | ||
|
|
||
| "go.opentelemetry.io/otel" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/internal/global" | ||
| "go.opentelemetry.io/otel/metric" | ||
| "go.opentelemetry.io/otel/sdk" | ||
| "go.opentelemetry.io/otel/sdk/trace/internal/x" | ||
| semconv "go.opentelemetry.io/otel/semconv/v1.36.0" | ||
| "go.opentelemetry.io/otel/semconv/v1.36.0/otelconv" | ||
| ) | ||
|
|
||
| // simpleSpanProcessor is a SpanProcessor that synchronously sends all | ||
|
|
@@ -17,6 +25,10 @@ type simpleSpanProcessor struct { | |
| exporterMu sync.Mutex | ||
| exporter SpanExporter | ||
| stopOnce sync.Once | ||
|
|
||
| selfObservabilityEnabled bool | ||
| componentNameAttr attribute.KeyValue | ||
| spansProcessedCounter otelconv.SDKProcessorSpanProcessed | ||
| } | ||
|
|
||
| var _ SpanProcessor = (*simpleSpanProcessor)(nil) | ||
|
|
@@ -33,11 +45,42 @@ func NewSimpleSpanProcessor(exporter SpanExporter) SpanProcessor { | |
| ssp := &simpleSpanProcessor{ | ||
| exporter: exporter, | ||
| } | ||
| ssp.configureSelfObservability() | ||
|
|
||
| global.Warn("SimpleSpanProcessor is not recommended for production use, consider using BatchSpanProcessor instead.") | ||
|
|
||
| return ssp | ||
| } | ||
|
|
||
| var simpleProcessorIDCounter atomic.Int64 | ||
|
|
||
| // nextSimpleProcessorID returns an identifier for this simple span processor, | ||
| // starting with 0 and incrementing by 1 each time it is called. | ||
| func nextSimpleProcessorID() int64 { | ||
| return simpleProcessorIDCounter.Add(1) - 1 | ||
| } | ||
mahendrabishnoi2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // configureSelfObservability configures metrics for the simple span processor. | ||
| func (ssp *simpleSpanProcessor) configureSelfObservability() { | ||
|
||
| if !x.SelfObservability.Enabled() { | ||
| return | ||
| } | ||
| ssp.selfObservabilityEnabled = true | ||
| ssp.componentNameAttr = semconv.OTelComponentName( | ||
| fmt.Sprintf("%s/%d", otelconv.ComponentTypeSimpleSpanProcessor, nextSimpleProcessorID())) | ||
| meter := otel.GetMeterProvider().Meter( | ||
| selfObsScopeName, | ||
| metric.WithInstrumentationVersion(sdk.Version()), | ||
| metric.WithSchemaURL(semconv.SchemaURL), | ||
| ) | ||
|
|
||
| var err error | ||
| ssp.spansProcessedCounter, err = otelconv.NewSDKProcessorSpanProcessed(meter) | ||
| if err != nil { | ||
| otel.Handle(err) | ||
| } | ||
| } | ||
|
|
||
| // OnStart does nothing. | ||
| func (*simpleSpanProcessor) OnStart(context.Context, ReadWriteSpan) {} | ||
|
|
||
|
|
@@ -47,8 +90,17 @@ func (ssp *simpleSpanProcessor) OnEnd(s ReadOnlySpan) { | |
| defer ssp.exporterMu.Unlock() | ||
|
|
||
| if ssp.exporter != nil && s.SpanContext().TraceFlags().IsSampled() { | ||
| if err := ssp.exporter.ExportSpans(context.Background(), []ReadOnlySpan{s}); err != nil { | ||
| attrs := []attribute.KeyValue{ | ||
mahendrabishnoi2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ssp.componentNameAttr, | ||
| ssp.spansProcessedCounter.AttrComponentType(otelconv.ComponentTypeSimpleSpanProcessor), | ||
| } | ||
| err := ssp.exporter.ExportSpans(context.Background(), []ReadOnlySpan{s}) | ||
| if err != nil { | ||
| otel.Handle(err) | ||
| attrs = append(attrs, semconv.ErrorType(err)) | ||
| } | ||
| if ssp.selfObservabilityEnabled { | ||
| ssp.spansProcessedCounter.Add(context.Background(), 1, attrs...) | ||
mahendrabishnoi2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.