Skip to content
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

Add the Context notion. #424

Merged
merged 85 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
fb10415
Initial context integration.
carlosalberto Jan 22, 2020
8451596
Update api-propagators.md
carlosalberto Jan 24, 2020
5275f32
Remove the mention of binary propagators.
carlosalberto Jan 24, 2020
7c01d61
Mention that the tracing's propagation must happen internally.
carlosalberto Jan 24, 2020
dd834a7
Make context -> Context.
carlosalberto Jan 27, 2020
7f9c6b8
Rename api-context.md to context.md
carlosalberto Jan 27, 2020
f43b7db
Remove the notion of key.
carlosalberto Jan 27, 2020
548e3d0
Add a note on Context being a SDK API.
carlosalberto Jan 27, 2020
4942040
Note on the optional global operations.
carlosalberto Jan 27, 2020
dda30bb
Clean up pass.
carlosalberto Jan 27, 2020
59cf5f3
Remove context-prop from the *tracing* index.
carlosalberto Jan 27, 2020
278839b
Update specification/api-propagators.md
carlosalberto Jan 27, 2020
71f51e5
Update specification/api-propagators.md
carlosalberto Jan 27, 2020
a3502c1
Update specification/api-propagators.md
carlosalberto Jan 27, 2020
9be19e2
Update specification/context.md
carlosalberto Jan 27, 2020
ae1c052
First clean up pass over Context.
carlosalberto Jan 30, 2020
0a4f08d
Update specification/api-propagators.md
carlosalberto Jan 30, 2020
a491a5b
Overview and layout updates.
carlosalberto Jan 30, 2020
a36759c
Make the MD linter happy (long lines though).
carlosalberto Jan 30, 2020
58c6248
Update specification/context.md
carlosalberto Jan 30, 2020
ecdd80d
Update specification/context.md
carlosalberto Jan 30, 2020
95f7bd2
Update specification/context.md
carlosalberto Jan 30, 2020
656fe58
Update specification/context.md
carlosalberto Jan 30, 2020
9e8a3bd
Use MUST/SHOULD for clarifying propagators expectations.
carlosalberto Jan 30, 2020
086facb
Move the IsRemote deserialization note to the Extraction section.
carlosalberto Jan 30, 2020
63c40a6
Improve wording for Context handling in Extraction.
carlosalberto Jan 30, 2020
551a255
Clarify the protected global methods in context.md
carlosalberto Jan 30, 2020
bf398e1
Remove the extra explanation of Context in overview.md
carlosalberto Jan 30, 2020
5a9a679
Improve the wording for set/remove values in context.md
carlosalberto Jan 30, 2020
0f26bc7
Merge branch 'master' into context_aware
carlosalberto Jan 30, 2020
6b962ba
Update specification/api-propagators.md
carlosalberto Jan 30, 2020
60e22c0
Update specification/api-propagators.md
carlosalberto Jan 30, 2020
505c2f7
Update specification/api-propagators.md
carlosalberto Jan 30, 2020
e06c9b0
Update specification/api-propagators.md
carlosalberto Jan 30, 2020
af9287a
Fix style?
carlosalberto Jan 30, 2020
d38b6fc
Clarify the Context usage in Extract.
carlosalberto Jan 30, 2020
0e76724
Update specification/context.md
carlosalberto Jan 30, 2020
c697fef
Clarify IsRemote on Extract.
carlosalberto Jan 30, 2020
a4d588c
More style changes.
carlosalberto Jan 30, 2020
fdd79e1
OTel MUST provide Context impl if none exists.
carlosalberto Jan 30, 2020
48d98c2
Update specification/context.md
carlosalberto Jan 30, 2020
07398c1
Update specification/context.md
carlosalberto Jan 30, 2020
051f36f
Update specification/context.md
carlosalberto Jan 31, 2020
e307486
Reword the optionality of Context in propagators.
carlosalberto Jan 31, 2020
073a526
Clarify again the expected usage for implicit `Context`.
carlosalberto Jan 31, 2020
9745124
Update specification/context.md
carlosalberto Jan 31, 2020
4649574
Be more strict on the Context parameters.
carlosalberto Jan 31, 2020
c4b3423
Merge branch 'master' into context_aware
carlosalberto Feb 3, 2020
0b5c1f1
Clarify that SDK/OTel instrumentation only SHOULD use global Context.
carlosalberto Feb 4, 2020
3b58a29
Mention that Context will be used through concerns APIs.
carlosalberto Feb 4, 2020
3fe6238
Update specification/api-propagators.md
carlosalberto Feb 4, 2020
732f4e9
Update specification/api-propagators.md
carlosalberto Feb 4, 2020
01e0925
We do not use the Required word anymore.
carlosalberto Feb 4, 2020
a3e5134
Do not require extract to set null/empty upon errors.
carlosalberto Feb 5, 2020
13b1c02
Better wording for failed extraction values.
carlosalberto Feb 5, 2020
75ae4bc
Update specification/context.md
carlosalberto Feb 5, 2020
5f0b166
Clarify ONCE that Context is immutable.
carlosalberto Feb 5, 2020
7393c0c
Use "derived" instead of "child" for Context-Context relationships.
carlosalberto Feb 5, 2020
42adf76
Remove a misleading line.
carlosalberto Feb 5, 2020
eeb9afc
Mention tracing uses Context specifially.
carlosalberto Feb 5, 2020
0794250
Update specification/api-propagators.md
carlosalberto Feb 5, 2020
190da25
Update specification/api-propagators.md
carlosalberto Feb 6, 2020
c8bbf39
Merge branch 'master' into context_aware
carlosalberto Feb 6, 2020
f28568c
Clarify that Context keeps the untouched original values.
carlosalberto Feb 6, 2020
74d1084
Merge branch 'master' into context_aware
carlosalberto Feb 6, 2020
dc92999
Clarify a new Context is created only for write operations.
carlosalberto Feb 6, 2020
93a0cf7
Misc fixes.
carlosalberto Feb 10, 2020
e91a898
Set current Context should return a handle.
carlosalberto Feb 10, 2020
2db3f2e
Note on global Context operations.
carlosalberto Feb 11, 2020
4e02075
Remove the Remove operation on Context.
carlosalberto Feb 11, 2020
8bc320e
Clarify the usage of cross-cutting concern APIs.
carlosalberto Feb 11, 2020
96c26b9
Restore "Create a key" section.
carlosalberto Feb 11, 2020
85cbb5b
Merge branch 'master' into context_aware
carlosalberto Feb 11, 2020
f1431c8
Minor fix to a previous commit.
carlosalberto Feb 11, 2020
965f365
Add a detach operation.
carlosalberto Feb 11, 2020
a289dff
Update specification/api-propagators.md
carlosalberto Feb 12, 2020
27e701a
Update specification/api-propagators.md
carlosalberto Feb 12, 2020
980af1b
Update specification/context.md
carlosalberto Feb 12, 2020
c9c3cf1
Update specification/context.md
carlosalberto Feb 12, 2020
31af4d9
Update specification/context.md
carlosalberto Feb 12, 2020
a3567d9
Update specification/context.md
carlosalberto Feb 12, 2020
cbd3b71
Update specification/context.md
carlosalberto Feb 12, 2020
54735b8
Update specification/api-tracing.md
carlosalberto Feb 12, 2020
349af35
Use value instead of object for Token.
carlosalberto Feb 12, 2020
6ce4054
Merge branch 'master' into context_aware
bogdandrutu Feb 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions specification/api-propagators.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ Cross-cutting concerns send their state to the next process using
context data to and from messages exchanged by the applications.
Each concern creates a set of `Propagator`s for every supported `Format`.

Propagators leverage the `Context` to inject and extract data for each
cross-cutting concern, such as traces and correlation context.

The Propagators API is expected to be leveraged by users writing
instrumentation libraries.

Propagators API currently consists of one `Format`:
The Propagators API currently consists of one `Format`:

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

Deserializing must set `IsRemote` to true on the returned `SpanContext`.

A binary `Format` will be added in the future.

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

Required arguments:

- the cross-cutting concern value to be injected, such as `SpanContext` or `DistributedContext`.
- 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.
- the carrier that holds propagation fields. For example, an outgoing message or http request.
- the `Setter` invoked for each propagation key to add or remove.
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved

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

### Extract

Extracts the value from upstream. For example, as http headers.
Extracts the value from an incoming request. For example, from the headers of an HTTP request.

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

Required arguments:

- A `Context`. For languages supporting current `Context` state this argument is OPTIONAL, defaulting to the current `Context` instance.
- the carrier holds propagation fields. For example, an outgoing message or http request.
- the instance of `Getter` invoked for each propagation key to get.
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved

Returns the non-null cross-cutting concern extracted value.
Returns a new `Context` derived from the `Context` passed as argument,
containing the extracted value, which can be a `SpanContext`,
`CorrelationContext` or another cross-cutting concern context.

If the extracted value is a `SpanContext`, its `IsRemote` property MUST be set to true.

#### Getter argument

Expand Down
17 changes: 7 additions & 10 deletions specification/api-tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ mechanism, for instance the `ServiceLoader` class in Java.

The `Tracer` MUST provide functions to:

- Get the currently active `Span`
- Create a new `Span`

The `Tracer` SHOULD provide methods to:

- Get the currently active `Span`
- Make a given `Span` as active

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

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

The `Tracer` MUST provide a way to update its active `Span`, and MAY provide
The `Tracer` SHOULD provide a way to update its active `Span` and MAY provide
convenience functions to manage a `Span`'s lifetime and the scope in which a
`Span` is active. When an active `Span` is made inactive, the previously-active
`Span` SHOULD be made active. A `Span` maybe finished (i.e. have a non-null end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/maybe/may be/

Is "finished" accurate? I wish we kept OT naming here, "span ended" sounds weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) I will update this line in another (tiny) PR.

time) but stil active. A `Span` may be active on one thread after it has been
made inactive on another.

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

## SpanContext

A `SpanContext` represents the portion of a `Span` which must be serialized and
Expand Down
111 changes: 111 additions & 0 deletions specification/context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Context

<details>
<summary>
Table of Contents
</summary>

- [Overview](#overview)
- [Create a key](#create-a-key)
- [Get value](#get-value)
- [Set value](#set-value)
- [Optional operations](#optional-operations)
- [Get current Context](#get-current-context)
- [Attach Context](#attach-context)
- [Detach Context](#detach-context)

</details>

## Overview
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved

A `Context` is a propagation mechanism which carries execution-scoped values
across API boundaries and between logically associated execution units.
Cross-cutting concerns access their data in-process using the same shared
`Context` object.

A `Context` MUST be immutable, and its write operations MUST
result in the creation of a new `Context` containing the original
values and the specified values updated.

Languages are expected to use the single, widely used `Context` implementation
if one exists for them. In the cases where an extremely clear, pre-existing
option is not available, OpenTelemetry MUST provide its own `Context`
implementation. Depending on the language, its usage may be either explicit
or implicit.

Users writing instrumentation in languages that use `Context` implicitly are
discouraged from using the `Context` API directly. In those cases, users will
manipulate `Context` through cross-cutting concerns APIs instead, in order to
perform operations such as setting trace or correlation context values for
a specified `Context`.

A `Context` is expected to have the following operations, with their
respective language differences:

## Create a key

Keys are used to allow cross-cutting concerns to control access to their local state,
and they cannot be guessed by third parties. It is recommended that concerns mediate
data access via an API, rather than provide direct public access to their keys.

The API MUST accept the following parameter:

- The key identifier. Different languages may impose different restrictions on the expected types, so this parameter remains an implementation detail.

The API MUST return an opaque object representing the newly created key.

## Get value
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved

Concerns can access their local state in the current execution state
represented by a `Context`.

The API MUST accept the following parameters:

- The `Context`.
- The key.

The API MUST return the value in the `Context` for the specified key.

## Set value

Concerns can record their local state in the current execution state
represented by a `Context`.

The API MUST accept the following parameters:

- The `Context`.
- The key.
- The value to be set.

The API MUST return a new `Context` containing the new value.
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved

## Optional Global operations

These operations are expected to only be implemented by languages
using `Context` implicitly, and thus are optional. These operations
SHOULD only be used to implement automatic scope switching and define
higher level APIs by SDK components and OpenTelemetry instrumentation libraries.

### Get current Context

The API MUST return the `Context` associated with the caller's current execution unit.

### Attach Context

Associates a `Context` with the caller's current execution unit.

The API MUST accept the following parameters:

- The `Context`.

The API MUST return a value that can be used as a `Token` to restore the previous
`Context`.

### Detach Context

Resets the `Context` associated with the caller's current execution unit
to the value it had before attaching a specified `Context`.

The API MUST accept the following parameters:

- A `Token` that was returned by a previous call to attach a `Context`.
10 changes: 9 additions & 1 deletion specification/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,18 @@ OpenTelemetry
[proto](https://github.com/open-telemetry/opentelemetry-proto/blob/a46c815aa5e85a52deb6cb35b8bc182fb3ca86a0/src/opentelemetry/proto/agent/common/v1/common.proto#L28-L96)
for an example.

## Context Propagation
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved

All of OpenTelemetry cross-cutting concerns, such as traces and metrics,
share an underlying `Context` mechanism for storing state and
accessing data across the lifespan of a distributed transaction.

See the [Context](context.md)

## Propagators

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

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