|
| 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`. |
0 commit comments