You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add the capability to observe/hook methods and functions via WithSpan and SpanAttribute
attributes. This capability is disabled by default (it has some runtime overhead), and can be enabled via php.ini
WithSpan is a signal that a function or method should be auto-instrumented. SpanAttribute enables arguments to be passed through to pre hook as attributes to be added to a span.
The attributes are provided by the API, as OpenTelemetry\API\Instumentation\WithSpan and OpenTelemetry\API\Instrumentation\SpanAttribute
Two extra parameters are passed to pre hooks:
1. span name + span kind (from WithSpan's arguments, if provided)
2. attributes (from WithSpan's 3rd argument, and from SpanAttribute)
WithSpan and SpanAttribute can be applied to a methods, functions, or interface methods.
The default pre/post hook callbacks for WithSpan are provided by the API, as OpenTelemetry\API\Instrumentation\WithSpanHandler. They can be changed via php.ini
There is a restriction that WithSpan hooks can only be added to functions/methods that
are not already hooked by something else (because the hooks are added at runtime: when
a method is executed and has no hooks, we then check for attribute and add hooks.
Copy file name to clipboardExpand all lines: README.md
+89-1Lines changed: 89 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,10 @@ Issues have been disabled for this repo in order to help maintain consistency be
13
13
This is a PHP extension for OpenTelemetry, to enable auto-instrumentation.
14
14
It is based on [zend_observer](https://www.datadoghq.com/blog/engineering/php-8-observability-baked-right-in/) and requires php8+
15
15
16
-
The extension allows creating `pre` and `post` hook functions to arbitrary PHP functions and methods, which allows those methods to be wrapped with telemetry.
16
+
The extension allows:
17
+
18
+
- creating `pre` and `post` hook functions to arbitrary PHP functions and methods, which allows those methods to be wrapped with telemetry
19
+
- adding attributes to functions and methods to enable observers at runtime
17
20
18
21
In PHP 8.2+, internal/built-in PHP functions can also be observed.
19
22
@@ -241,5 +244,90 @@ string(3) "new"
241
244
string(8) "original"
242
245
```
243
246
247
+
## Attribute-based hooking
248
+
249
+
By applying attributes to source code, the OpenTelemetry extension can add hooks at runtime.
250
+
251
+
Default pre and post hook methods are provided by the OpenTelemetry API: `OpenTelemetry\API\Instrumentation\Handler::pre`
252
+
and `::post`.
253
+
254
+
This feature is disabled by default, but can be enabled by setting `opentelemetry.attr_hooks_enabled = On` in php.ini
255
+
256
+
## Restrictions
257
+
258
+
Attribute-based hooks can only be applied to a function/method that does not already have
259
+
hooks applied.
260
+
Only one hook can be applied to a function/method, including via interfaces.
261
+
262
+
Since the attributes are evaluated at runtime, the extension checks whether a hook already
263
+
exists to decide whether it should apply a new runtime hook.
264
+
265
+
## Configuration
266
+
267
+
This feature can be configured via `.ini` by modifying the following entries:
268
+
269
+
-`opentelemetry.attr_hooks_enabled` - boolean, default Off
270
+
-`opentelemetry.attr_pre_handler_function` - FQN of pre method/function
271
+
-`opentelemetry.attr_post_handler_function` - FQN of post method/function
0 commit comments