-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve
open-telemetry/context
documentation (#830)
- Loading branch information
Showing
7 changed files
with
135 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,52 @@ | ||
# OpenTelemetry Context | ||
|
||
Immutable execution scoped propagation mechanism, for further details see [opentelemetry-specification][1]. | ||
|
||
## Installation | ||
|
||
```shell | ||
composer require open-telemetry/context | ||
``` | ||
|
||
## Usage | ||
|
||
### Implicit propagation | ||
|
||
```php | ||
$context = Context::getCurrent(); | ||
// modify context | ||
$scope = $context->activate(); | ||
try { | ||
// run within new context | ||
} finally { | ||
$scope->detach(); | ||
} | ||
``` | ||
|
||
It is recommended to use a `try-finally` statement after `::activate()` to ensure that the created scope is properly `::detach()`ed. | ||
|
||
## Async applications | ||
|
||
### Fiber support | ||
|
||
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`. | ||
|
||
### Event loops | ||
|
||
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: | ||
|
||
```php | ||
function bindContext(Closure $closure): Closure { | ||
$context = Context::getCurrent(); | ||
return static function (mixed ...$args) use ($closure, $context): mixed { | ||
$scope = $context->activate(); | ||
try { | ||
return $closure(...$args); | ||
} finally { | ||
$scope->detach(); | ||
} | ||
}; | ||
} | ||
``` | ||
|
||
[1]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md#context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters