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
The OpenTelemetry hooks for OpenFeature provide a [spec compliant][otel-spec] way to automatically add feature flag evaluation information to traces and metrics.
3
+
The OpenTelemetry hooks for OpenFeature provide a [spec compliant][otel-semconv] way to automatically add feature flag evaluation information to traces, logs, and metrics.
4
4
Since feature flags are dynamic and affect runtime behavior, it’s important to collect relevant feature flag telemetry signals.
5
5
These can be used to determine the impact a feature has on application behavior, enabling enhanced observability use cases, such as A/B testing or progressive feature releases.
For the `EventHook`, you also need to install the OpenTelemetry logs SDK:
23
+
24
+
```
25
+
$ npm install @opentelemetry/sdk-logs
26
+
```
27
+
28
+
> [!NOTE]
29
+
> For the hooks to work, you must have the OpenTelemetry SDK configured in your application.
30
+
> Please refer to the [OpenTelemetry documentation](https://opentelemetry.io/docs/instrumentation/js/) for more information on setting up OpenTelemetry in your application.
31
+
> You need to set up the [tracing SDK][otel-tracing-js] for `SpanHook` and `SpanEventHook`, the [metrics SDK][[otel-metrics-js]] for `MetricsHook`, and the [logs SDK][[otel-logs-js]] for `EventHook`.
32
+
21
33
## Hooks
22
34
23
-
### TracingHook
35
+
### EventHook
36
+
37
+
This hook logs evaluation events to OpenTelemetry using an [EventLogger][otel-logs].
38
+
These are logged even if there is no active span.
39
+
This is useful for exporting evaluation events to a backend that supports [OpenTelemetry log events][otel-logs].
40
+
**Note:** Log Events are the recommended approach for capturing feature flag evaluation data.
24
41
25
-
This hook adds a [span event](https://opentelemetry.io/docs/concepts/signals/traces/#span-events) for each feature flag evaluation.
42
+
### SpanEventHook
43
+
44
+
This hook adds evaluation [span events][otel-span-events] to the current active span.
45
+
This is useful for associating evaluation events with a trace.
46
+
If there is no active span, the event is not logged.
47
+
**Note:**[Span events are being deprecated in OTEL][span-event-deprecation-otep] in favor of [using log events via `EventHook`](#eventhook).
48
+
49
+
### SpanHook
50
+
51
+
This hook creates a new [span][otel-span] for each flag evaluation and sets the evaluation details as [span attributes][otel-span-attributes].
52
+
This is useful for tracing flag evaluations as part of a larger trace.
26
53
27
54
### MetricsHook
28
55
@@ -36,52 +63,105 @@ This hook performs metric collection by tapping into various hook stages. Below
36
63
## Usage
37
64
38
65
OpenFeature provides various ways to register hooks. The location that a hook is registered affects when the hook is run.
39
-
It's recommended to register both the `TracingHook` and `MetricsHook`globally in most situations, but it's possible to only enable the hook on specific clients.
40
-
You should **never** register these hooks both globally and on a client.
66
+
It's recommended to register the desired hooks globally in most situations, but it's possible to only enable specific hooks on individual clients.
67
+
You should **never** register the same hook type both globally and on a client.
41
68
42
69
More information on hooks can be found in the [OpenFeature documentation][hook-concept].
43
70
44
71
### Register Globally
45
72
46
-
The `TracingHook` and `MetricsHook`can both be set on the OpenFeature singleton.
73
+
The hooks can be set on the OpenFeature singleton.
47
74
This will ensure that every flag evaluation will always generate the applicable telemetry signals.
The `TracingHook` and `MetricsHook`can both be set on an individual client. This should only be done if it wasn't set globally and other clients shouldn't use this hook.
59
-
Setting the hook on the client will ensure that every flag evaluation performed by this client will always generate the applicable telemetry signals.
85
+
The hooks can be set on an individual client. This should only be done if they weren't set globally and other clients shouldn't use these hooks.
86
+
Setting the hooks on the client will ensure that every flag evaluation performed by this client will always generate the applicable telemetry signals.
Choose the appropriate hook(s) based on your observability needs:
100
+
101
+
-**EventHook**: Recommended for future use cases. Logs evaluation events that can be backends supporting [OTEL Logs][otel-logs].
102
+
-**SpanEventHook**: Span events are being deprecated. Use only if your backend supports span events and you cannot use `EventHook`.
103
+
-**SpanHook**: Use when you want dedicated spans for each evaluation in your traces.
104
+
-**MetricsHook**: Use alongside any of the above when you need metrics about evaluation performance.
70
105
71
-
Custom attributes can be extracted from [flag metadata](https://openfeature.dev/specification/types#flag-metadata) by supplying a `attributeMapper` in the `MetricsHookOptions` or `TracingHookOptions`.
106
+
### Hook Options
72
107
73
-
In the case of the `MetricsHook`, these will be added to the `feature_flag.evaluation_success_total` metric.
74
-
The `TracingHook` adds them as [span event attributes](https://opentelemetry.io/docs/instrumentation/js/manual/#span-events).
108
+
All hooks support the following options via `OpenTelemetryHookOptions`:
109
+
110
+
#### Custom Attributes
111
+
112
+
Custom attributes can be extracted from hook metadata or evaluation details by supplying an `attributeMapper`:
75
113
76
114
```typescript
77
-
// configure an attributeMapper function for a custom property
0 commit comments