Commit 98993ea
committed
Add recordingSpan and currentSpan methods to TracerProtocol
**Motivation:**
Currently, it is only possible to implicitly work with the current span by transparently
creating a child span (using ServiceContext.current) under the hood. This is sufficient
for almost all use-cases, but does not work in case a piece of code wants to add an event
to the current span without having a handle on said span, e.g. if the span was created by a
library.
**Modifications:**
I added a `recordingSpan(identifiedBy context: ServiceContext) -> Span?` requirement to `TracerProtocol`. This way, `Tracer` implementations may look up and return a span identified by the data they stored in the provided `ServiceContext`. It's worth noting that this method is only intended for obtaining spans which are still recording as opposed to already ended ones that may not even be in memory anymore.
I also added a default implementation of this method to avoid introducing a breaking change.
On top of this new protocol requirement, I added an extension to `TracerProtocol` with sugar to obtain the current span based on the task-local `ServiceContext`.
**Result:**
Library authors and application developers are now able to look up the current recording span to interact with it by e.g. adding events and attributes.1 parent 55a0bf7 commit 98993ea
1 file changed
+26
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
62 | 70 | | |
63 | 71 | | |
64 | 72 | | |
| |||
106 | 114 | | |
107 | 115 | | |
108 | 116 | | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
109 | 135 | | |
110 | 136 | | |
111 | 137 | | |
| |||
0 commit comments