Skip to content

Commit d48175b

Browse files
authored
Improve open-telemetry/context documentation (#830)
1 parent 0e3b693 commit d48175b

File tree

6 files changed

+134
-13
lines changed

6 files changed

+134
-13
lines changed

ContextInterface.php

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,83 @@
44

55
namespace OpenTelemetry\Context;
66

7+
/**
8+
* Immutable execution scoped propagation mechanism.
9+
*
10+
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md#context
11+
*/
712
interface ContextInterface
813
{
914
/**
10-
* @param non-empty-string $key
15+
* Creates a new context key.
1116
*
12-
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.1/specification/context/context.md#create-a-key
17+
* @param non-empty-string $key name of the key
18+
* @return ContextKeyInterface created key
19+
*
20+
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md#create-a-key
1321
*/
1422
public static function createKey(string $key): ContextKeyInterface;
1523

24+
/**
25+
* Returns the current context.
26+
*
27+
* @return ContextInterface current context
28+
*
29+
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md#get-current-context
30+
*/
1631
public static function getCurrent(): ContextInterface;
1732

1833
/**
19-
* Makes `$this` the currently active {@see ContextInterface}.
34+
* Attaches this context as active context.
35+
*
36+
* The returned scope has to be {@link ScopeInterface::detach()}ed. In most
37+
* cases this should be done using a `try-finally` statement:
38+
* ```php
39+
* $scope = $context->activate();
40+
* try {
41+
* // ...
42+
* } finally {
43+
* $scope->detach();
44+
* }
45+
* ```
46+
*
47+
* @return ScopeInterface scope to detach the context and restore the previous
48+
* context
49+
*
50+
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md#attach-context
2051
*/
2152
public function activate(): ScopeInterface;
2253

2354
/**
24-
* This adds a key/value pair to this Context.
55+
* Returns a context with the given key set to the given value.
56+
*
57+
* @template T
58+
* @param ContextKeyInterface<T> $key key to set
59+
* @param T|null $value value to set
60+
* @return ContextInterface a context with the given key set to `$value`
2561
*
26-
* @psalm-template T
27-
* @psalm-param ContextKeyInterface<T> $key
28-
* @psalm-param T|null $value
62+
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md#set-value
2963
*/
3064
public function with(ContextKeyInterface $key, $value): ContextInterface;
3165

66+
/**
67+
* Returns a context with the given value set.
68+
*
69+
* @param ImplicitContextKeyedInterface $value value to set
70+
* @return ContextInterface a context with the given `$value`
71+
*
72+
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md#set-value
73+
*/
3274
public function withContextValue(ImplicitContextKeyedInterface $value): ContextInterface;
3375

3476
/**
35-
* Fetch a value from the Context given a key value.
77+
* Returns the value assigned to the given key.
78+
*
79+
* @template T
80+
* @param ContextKeyInterface<T> $key key to get
81+
* @return T|null value assigned to `$key`, or null if no such value exists
3682
*
37-
* @psalm-template T
38-
* @psalm-param ContextKeyInterface<T> $key
39-
* @psalm-return T|null
83+
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md#get-value
4084
*/
4185
public function get(ContextKeyInterface $key);
4286
}

ContextStorageInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,27 @@
66

77
interface ContextStorageInterface
88
{
9+
/**
10+
* Returns the current scope.
11+
*
12+
* @return ContextStorageScopeInterface|null current scope, or null if no
13+
* scope was attached in the current execution unit
14+
*/
915
public function scope(): ?ContextStorageScopeInterface;
1016

17+
/**
18+
* Returns the current context.
19+
*
20+
* @return ContextInterface current context
21+
*/
1122
public function current(): ContextInterface;
1223

24+
/**
25+
* Attaches the context as active context.
26+
*
27+
* @param ContextInterface $context context to attach
28+
* @return ContextStorageScopeInterface scope to detach the context and
29+
* restore the previous context
30+
*/
1331
public function attach(ContextInterface $context): ContextStorageScopeInterface;
1432
}

ContextStorageScopeInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
interface ContextStorageScopeInterface extends ScopeInterface, ArrayAccess
1010
{
11+
/**
12+
* Returns the context associated with this scope.
13+
*
14+
* @return ContextInterface associated context
15+
*/
1116
public function context(): ContextInterface;
1217

1318
/**

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
11
# OpenTelemetry Context
2+
3+
Immutable execution scoped propagation mechanism, for further details see [opentelemetry-specification][1].
4+
5+
## Installation
6+
7+
```shell
8+
composer require open-telemetry/context
9+
```
10+
11+
## Usage
12+
13+
### Implicit propagation
14+
15+
```php
16+
$context = Context::getCurrent();
17+
// modify context
18+
$scope = $context->activate();
19+
try {
20+
// run within new context
21+
} finally {
22+
$scope->detach();
23+
}
24+
```
25+
26+
It is recommended to use a `try-finally` statement after `::activate()` to ensure that the created scope is properly `::detach()`ed.
27+
28+
## Async applications
29+
30+
### Fiber support
31+
32+
Requires `PHP >= 8.1`, `ext-ffi` and setting the environment variable `OTEL_PHP_FIBERS_ENABLED` to a truthy value. Additionally `vendor/autoload.php` has to be preloaded for non-CLI SAPIs if [`ffi.enable`](https://www.php.net/manual/en/ffi.configuration.php#ini.ffi.enable) is set to `preload`.
33+
34+
### Event loops
35+
36+
Event loops have to restore the original context on callback execution. A basic implementation could look like the following, though implementations should avoid keeping unnecessary references to arguments if possible:
37+
38+
```php
39+
function bindContext(Closure $closure): Closure {
40+
$context = Context::getCurrent();
41+
return static function (mixed ...$args) use ($closure, $context): mixed {
42+
$scope = $context->activate();
43+
try {
44+
return $closure(...$args);
45+
} finally {
46+
$scope->detach();
47+
}
48+
};
49+
}
50+
```
51+
52+
[1]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md#context

ScopeInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
interface ScopeInterface
1010
{
11+
/** Already detached. */
1112
public const DETACHED = 1 << (PHP_INT_SIZE << 3) - 1;
13+
/** Execution context inactive. */
1214
public const INACTIVE = 1 << (PHP_INT_SIZE << 3) - 2;
15+
/** Not current context. */
1316
public const MISMATCH = 1 << (PHP_INT_SIZE << 3) - 3;
1417

1518
/**
@@ -23,7 +26,7 @@ interface ScopeInterface
2326
* @see self::INACTIVE
2427
* @see self::MISMATCH
2528
*
26-
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/specification/context/context.md#detach-context
29+
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md#detach-context
2730
*/
2831
public function detach(): int;
2932
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "open-telemetry/context",
33
"description": "Context implementation for OpenTelemetry PHP.",
4-
"keywords": ["opentelemetry", "otel", "metrics", "tracing", "logging", "apm", "context"],
4+
"keywords": ["opentelemetry", "otel", "context"],
55
"type": "library",
66
"license": "Apache-2.0",
77
"authors": [

0 commit comments

Comments
 (0)