Skip to content

Commit b420234

Browse files
authored
Add ability for SpanProcessor to mutate spans on end (open-telemetry#4024)
Fixes open-telemetry#1089. In addition to the comments on the issue, this was discussed in the spec SIG Meeting on 2024/23/04: * The filtering use-case explained in [this comment](open-telemetry#1089 (comment)) should rather be solved by the upcoming samplerV2 instead of `SpanProcessor`s due to better conceptual fit * The buffering use-case also explained in [this comment](open-telemetry#1089 (comment)) seems to be not relevant enough to influence the design decision * Apparently there was a discussion around building the `SpanProcessor`s in a chaining fashion during the initial SDK spec design and it was actively decided against it. However, no one could recall the reason why.
1 parent 065e5c7 commit b420234

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ release.
1313

1414
- Remove restriction that sampler description is immutable.
1515
([#4137](https://github.com/open-telemetry/opentelemetry-specification/pull/4137))
16+
- Add in-development `OnEnding` callback to SDK `SpanProcessor` interface.
17+
([#4024](https://github.com/open-telemetry/opentelemetry-specification/pull/4024))
1618

1719
### Metrics
1820

spec-compliance-matrix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ formats is required. Implementing more than one format is optional.
5555
| events collection size limit | | + | + | + | + | + | + | + | + | - | - | + |
5656
| attribute collection size limit | | + | + | + | + | + | + | + | + | - | - | + |
5757
| links collection size limit | | + | + | + | + | + | + | + | + | - | - | + |
58+
| [SpanProcessor.OnEnding](specification/trace/sdk.md#onending) | X | - | - | - | - | - | - | - | - | - | - | - |
5859
| [Span attributes](specification/trace/api.md#set-attributes) | Optional | Go | Java | JS | Python | Ruby | Erlang | PHP | Rust | C++ | .NET | Swift |
5960
| SetAttribute | | + | + | + | + | + | + | + | + | + | + | + |
6061
| Set order preserved | X | + | - | + | + | + | + | + | + | + | + | + |

specification/trace/sdk.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ linkTitle: SDK
3838
- [Span processor](#span-processor)
3939
* [Interface definition](#interface-definition)
4040
+ [OnStart](#onstart)
41+
+ [OnEnding](#onending)
4142
+ [OnEnd(Span)](#onendspan)
4243
+ [Shutdown()](#shutdown)
4344
+ [ForceFlush()](#forceflush)
@@ -571,11 +572,23 @@ in the SDK:
571572

572573
### Interface definition
573574

575+
The `SpanProcessor` interface MUST declare the following methods:
576+
577+
* [OnStart](#onstart)
578+
* [OnEnd](#onendspan)
579+
* [Shutdown](#shutdown-1)
580+
* [ForceFlush](#forceflush-1)
581+
582+
The `SpanProcessor` interface SHOULD declare the following methods:
583+
584+
* [OnEnding](#onending) method.
585+
574586
#### OnStart
575587

576588
`OnStart` is called when a span is started. This method is called synchronously
577589
on the thread that started the span, therefore it should not block or throw
578-
exceptions.
590+
exceptions. If multiple `SpanProcessors` are registered, their `OnStart` callbacks
591+
are invoked in the order they have been registered.
579592

580593
**Parameters:**
581594

@@ -590,6 +603,25 @@ exceptions.
590603

591604
**Returns:** `Void`
592605

606+
#### OnEnding
607+
608+
**Status**: [Development](../document-status.md)
609+
610+
`OnEnding` is called during the span `End()` operation, after the end timestamp has been set. The Span object is still mutable (i.e., `SetAttribute`, `AddLink`, `AddEvent` can be called) while `OnEnding` is called.
611+
This method MUST be called synchronously within the [`Span.End()` API](api.md#end),
612+
therefore it should not block or throw an exception.
613+
If multiple `SpanProcessors` are registered, their `OnEnding` callbacks
614+
are invoked in the order they have been registered.
615+
The SDK MUST guarantee that the span can no longer be modified by any other thread
616+
before invoking `OnEnding` of the first `SpanProcessor`. From that point on, modifications
617+
are only allowed synchronously from within the invoked `OnEnding` callbacks. All registered SpanProcessor `OnEnding` callbacks are executed before any SpanProcessor's `OnEnd` callback is invoked.
618+
619+
**Parameters:**
620+
621+
* `span` - a [read/write span object](#additional-span-interfaces) for the span which is about to be ended.
622+
623+
**Returns:** `Void`
624+
593625
#### OnEnd(Span)
594626

595627
`OnEnd` is called after a span is ended (i.e., the end timestamp is already set).

0 commit comments

Comments
 (0)