Skip to content

Commit 33ce3e1

Browse files
carlosalbertoOberon00MrAliasyurishkurojkwatson
authored
Add the Context notion. (#424)
* Initial context integration. * Update api-propagators.md * Remove the mention of binary propagators. * Mention that the tracing's propagation must happen internally. * Make context -> Context. * Rename api-context.md to context.md * Remove the notion of key. * Add a note on Context being a SDK API. * Note on the optional global operations. * Clean up pass. * Remove context-prop from the *tracing* index. * Update specification/api-propagators.md Co-Authored-By: Christian Neumüller <[email protected]> * Update specification/api-propagators.md Co-Authored-By: Christian Neumüller <[email protected]> * Update specification/api-propagators.md Co-Authored-By: Christian Neumüller <[email protected]> * Update specification/context.md Co-Authored-By: Christian Neumüller <[email protected]> * First clean up pass over Context. * Update specification/api-propagators.md Co-Authored-By: Christian Neumüller <[email protected]> * Overview and layout updates. * Make the MD linter happy (long lines though). * Update specification/context.md Co-Authored-By: Christian Neumüller <[email protected]> * Update specification/context.md Co-Authored-By: Christian Neumüller <[email protected]> * Update specification/context.md Co-Authored-By: Christian Neumüller <[email protected]> * Update specification/context.md Co-Authored-By: Christian Neumüller <[email protected]> * Use MUST/SHOULD for clarifying propagators expectations. * Move the IsRemote deserialization note to the Extraction section. * Improve wording for Context handling in Extraction. * Clarify the protected global methods in context.md * Remove the extra explanation of Context in overview.md * Improve the wording for set/remove values in context.md * Update specification/api-propagators.md Co-Authored-By: Christian Neumüller <[email protected]> * Update specification/api-propagators.md Co-Authored-By: Christian Neumüller <[email protected]> * Update specification/api-propagators.md Co-Authored-By: Christian Neumüller <[email protected]> * Update specification/api-propagators.md Co-Authored-By: Tyler Yahn <[email protected]> * Fix style? * Clarify the Context usage in Extract. * Update specification/context.md Co-Authored-By: Tyler Yahn <[email protected]> * Clarify IsRemote on Extract. * More style changes. * OTel MUST provide Context impl if none exists. * Update specification/context.md Co-Authored-By: Tyler Yahn <[email protected]> * Update specification/context.md Co-Authored-By: Tyler Yahn <[email protected]> * Update specification/context.md Co-Authored-By: Tyler Yahn <[email protected]> * Reword the optionality of Context in propagators. * Clarify again the expected usage for implicit `Context`. * Update specification/context.md Co-Authored-By: Tyler Yahn <[email protected]> * Be more strict on the Context parameters. * Clarify that SDK/OTel instrumentation only SHOULD use global Context. * Mention that Context will be used through concerns APIs. * Update specification/api-propagators.md Co-Authored-By: Tyler Yahn <[email protected]> * Update specification/api-propagators.md Co-Authored-By: Tyler Yahn <[email protected]> * We do not use the Required word anymore. * Do not require extract to set null/empty upon errors. * Better wording for failed extraction values. * Update specification/context.md Co-Authored-By: Yuri Shkuro <[email protected]> * Clarify ONCE that Context is immutable. * Use "derived" instead of "child" for Context-Context relationships. * Remove a misleading line. * Mention tracing uses Context specifially. * Update specification/api-propagators.md Co-Authored-By: John Watson <[email protected]> * Update specification/api-propagators.md Co-Authored-By: John Watson <[email protected]> * Clarify that Context keeps the untouched original values. * Clarify a new Context is created only for write operations. * Misc fixes. * Set current Context should return a handle. * Note on global Context operations. * Remove the Remove operation on Context. * Clarify the usage of cross-cutting concern APIs. * Restore "Create a key" section. * Minor fix to a previous commit. * Add a detach operation. * Update specification/api-propagators.md Co-Authored-By: John Watson <[email protected]> * Update specification/api-propagators.md Co-Authored-By: John Watson <[email protected]> * Update specification/context.md Co-Authored-By: John Watson <[email protected]> * Update specification/context.md Co-Authored-By: John Watson <[email protected]> * Update specification/context.md Co-Authored-By: John Watson <[email protected]> * Update specification/context.md Co-Authored-By: John Watson <[email protected]> * Update specification/context.md Co-Authored-By: John Watson <[email protected]> * Update specification/api-tracing.md Co-Authored-By: John Watson <[email protected]> * Use value instead of object for Token. Co-authored-by: Christian Neumüller <[email protected]> Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]> Co-authored-by: John Watson <[email protected]> Co-authored-by: Bogdan Drutu <[email protected]>
1 parent 4470532 commit 33ce3e1

File tree

4 files changed

+143
-21
lines changed

4 files changed

+143
-21
lines changed

specification/api-propagators.md

+16-10
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ Cross-cutting concerns send their state to the next process using
2626
context data to and from messages exchanged by the applications.
2727
Each concern creates a set of `Propagator`s for every supported `Format`.
2828

29+
Propagators leverage the `Context` to inject and extract data for each
30+
cross-cutting concern, such as traces and correlation context.
31+
2932
The Propagators API is expected to be leveraged by users writing
3033
instrumentation libraries.
3134

32-
Propagators API currently consists of one `Format`:
35+
The Propagators API currently consists of one `Format`:
3336

34-
- `HTTPTextFormat` is used to inject and extract a value as text into carriers that travel
37+
- `HTTPTextFormat` is used to inject values into and extract values from carriers as text that travel
3538
in-band across process boundaries.
3639

37-
Deserializing must set `IsRemote` to true on the returned `SpanContext`.
38-
3940
A binary `Format` will be added in the future.
4041

4142
## HTTP Text Format
@@ -75,7 +76,7 @@ Injects the value downstream. For example, as http headers.
7576

7677
Required arguments:
7778

78-
- the cross-cutting concern value to be injected, such as `SpanContext` or `DistributedContext`.
79+
- A `Context`. The Propagator MUST retrieve the appropriate value from the `Context` first, such as `SpanContext`, `CorrelationContext` or another cross-cutting concern context. For languages supporting current `Context` state, this argument is OPTIONAL, defaulting to the current `Context` instance.
7980
- the carrier that holds propagation fields. For example, an outgoing message or http request.
8081
- the `Setter` invoked for each propagation key to add or remove.
8182

@@ -101,18 +102,23 @@ The implemenation SHOULD preserve casing (e.g. it should not transform `Content-
101102

102103
### Extract
103104

104-
Extracts the value from upstream. For example, as http headers.
105+
Extracts the value from an incoming request. For example, from the headers of an HTTP request.
105106

106-
If the value could not be parsed, the underlying implementation will decide to return an
107-
object representing either an empty value, an invalid value, or a valid value. Implementations
108-
MUST not return null.
107+
If a value can not be parsed from the carrier for a cross-cutting concern,
108+
the implementation MUST NOT throw an exception. It MUST store a value in the `Context`
109+
that the implementation can recognize as a null or empty value.
109110

110111
Required arguments:
111112

113+
- A `Context`. For languages supporting current `Context` state this argument is OPTIONAL, defaulting to the current `Context` instance.
112114
- the carrier holds propagation fields. For example, an outgoing message or http request.
113115
- the instance of `Getter` invoked for each propagation key to get.
114116

115-
Returns the non-null cross-cutting concern extracted value.
117+
Returns a new `Context` derived from the `Context` passed as argument,
118+
containing the extracted value, which can be a `SpanContext`,
119+
`CorrelationContext` or another cross-cutting concern context.
120+
121+
If the extracted value is a `SpanContext`, its `IsRemote` property MUST be set to true.
116122

117123
#### Getter argument
118124

specification/api-tracing.md

+7-10
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,15 @@ mechanism, for instance the `ServiceLoader` class in Java.
122122

123123
The `Tracer` MUST provide functions to:
124124

125-
- Get the currently active `Span`
126125
- Create a new `Span`
126+
127+
The `Tracer` SHOULD provide methods to:
128+
129+
- Get the currently active `Span`
127130
- Make a given `Span` as active
128131

129-
The `Tracer` SHOULD allow end users to configure other tracing components that
130-
control how `Span`s are passed across process boundaries, including the text
131-
format `Propagator` used to serialize `Span`s created by the `Tracer`.
132+
The `Tracer` MUST internally leverage the `Context` in order to get and set the
133+
current `Span` state and how `Span`s are passed across process boundaries.
132134

133135
When getting the current span, the `Tracer` MUST return a placeholder `Span`
134136
with an invalid `SpanContext` if there is no currently active `Span`.
@@ -139,18 +141,13 @@ SHOULD create each new `Span` as a child of its active `Span` unless an
139141
explicit parent is provided or the option to create a span without a parent is
140142
selected, or the current active `Span` is invalid.
141143

142-
The `Tracer` MUST provide a way to update its active `Span`, and MAY provide
144+
The `Tracer` SHOULD provide a way to update its active `Span` and MAY provide
143145
convenience functions to manage a `Span`'s lifetime and the scope in which a
144146
`Span` is active. When an active `Span` is made inactive, the previously-active
145147
`Span` SHOULD be made active. A `Span` maybe finished (i.e. have a non-null end
146148
time) but stil active. A `Span` may be active on one thread after it has been
147149
made inactive on another.
148150

149-
The implementation MUST provide a text `Propagator`, which the
150-
`Tracer` SHOULD use by default if other propagators are not configured. SDKs
151-
SHOULD use the W3C HTTP Trace Context as the default text format. For more
152-
details, see [trace-context](https://github.com/w3c/trace-context).
153-
154151
## SpanContext
155152

156153
A `SpanContext` represents the portion of a `Span` which must be serialized and

specification/context.md

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Context
2+
3+
<details>
4+
<summary>
5+
Table of Contents
6+
</summary>
7+
8+
- [Overview](#overview)
9+
- [Create a key](#create-a-key)
10+
- [Get value](#get-value)
11+
- [Set value](#set-value)
12+
- [Optional operations](#optional-operations)
13+
- [Get current Context](#get-current-context)
14+
- [Attach Context](#attach-context)
15+
- [Detach Context](#detach-context)
16+
17+
</details>
18+
19+
## Overview
20+
21+
A `Context` is a propagation mechanism which carries execution-scoped values
22+
across API boundaries and between logically associated execution units.
23+
Cross-cutting concerns access their data in-process using the same shared
24+
`Context` object.
25+
26+
A `Context` MUST be immutable, and its write operations MUST
27+
result in the creation of a new `Context` containing the original
28+
values and the specified values updated.
29+
30+
Languages are expected to use the single, widely used `Context` implementation
31+
if one exists for them. In the cases where an extremely clear, pre-existing
32+
option is not available, OpenTelemetry MUST provide its own `Context`
33+
implementation. Depending on the language, its usage may be either explicit
34+
or implicit.
35+
36+
Users writing instrumentation in languages that use `Context` implicitly are
37+
discouraged from using the `Context` API directly. In those cases, users will
38+
manipulate `Context` through cross-cutting concerns APIs instead, in order to
39+
perform operations such as setting trace or correlation context values for
40+
a specified `Context`.
41+
42+
A `Context` is expected to have the following operations, with their
43+
respective language differences:
44+
45+
## Create a key
46+
47+
Keys are used to allow cross-cutting concerns to control access to their local state,
48+
and they cannot be guessed by third parties. It is recommended that concerns mediate
49+
data access via an API, rather than provide direct public access to their keys.
50+
51+
The API MUST accept the following parameter:
52+
53+
- The key identifier. Different languages may impose different restrictions on the expected types, so this parameter remains an implementation detail.
54+
55+
The API MUST return an opaque object representing the newly created key.
56+
57+
## Get value
58+
59+
Concerns can access their local state in the current execution state
60+
represented by a `Context`.
61+
62+
The API MUST accept the following parameters:
63+
64+
- The `Context`.
65+
- The key.
66+
67+
The API MUST return the value in the `Context` for the specified key.
68+
69+
## Set value
70+
71+
Concerns can record their local state in the current execution state
72+
represented by a `Context`.
73+
74+
The API MUST accept the following parameters:
75+
76+
- The `Context`.
77+
- The key.
78+
- The value to be set.
79+
80+
The API MUST return a new `Context` containing the new value.
81+
82+
## Optional Global operations
83+
84+
These operations are expected to only be implemented by languages
85+
using `Context` implicitly, and thus are optional. These operations
86+
SHOULD only be used to implement automatic scope switching and define
87+
higher level APIs by SDK components and OpenTelemetry instrumentation libraries.
88+
89+
### Get current Context
90+
91+
The API MUST return the `Context` associated with the caller's current execution unit.
92+
93+
### Attach Context
94+
95+
Associates a `Context` with the caller's current execution unit.
96+
97+
The API MUST accept the following parameters:
98+
99+
- The `Context`.
100+
101+
The API MUST return a value that can be used as a `Token` to restore the previous
102+
`Context`.
103+
104+
### Detach Context
105+
106+
Resets the `Context` associated with the caller's current execution unit
107+
to the value it had before attaching a specified `Context`.
108+
109+
The API MUST accept the following parameters:
110+
111+
- A `Token` that was returned by a previous call to attach a `Context`.

specification/overview.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,18 @@ OpenTelemetry
235235
[proto](https://github.com/open-telemetry/opentelemetry-proto/blob/a46c815aa5e85a52deb6cb35b8bc182fb3ca86a0/src/opentelemetry/proto/agent/common/v1/common.proto#L28-L96)
236236
for an example.
237237

238+
## Context Propagation
239+
240+
All of OpenTelemetry cross-cutting concerns, such as traces and metrics,
241+
share an underlying `Context` mechanism for storing state and
242+
accessing data across the lifespan of a distributed transaction.
243+
244+
See the [Context](context.md)
245+
238246
## Propagators
239247

240248
OpenTelemetry uses `Propagators` to serialize and deserialize cross-cutting concern values
241-
such as `SpanContext` and `DistributedContext` into a `Format`. Currently there is one
249+
such as `SpanContext` and `CorrelationContext` into a `Format`. Currently there is one
242250
type of propagator:
243251

244252
- `HTTPTextFormat` which is used to inject and extract a value as text into carriers that travel

0 commit comments

Comments
 (0)