Skip to content

Commit c60a696

Browse files
committed
feat: implement new tracing hooks
Signed-off-by: Lukas Reining <[email protected]>
1 parent 8662ea1 commit c60a696

14 files changed

+1309
-308
lines changed
Lines changed: 123 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OpenTelemetry Hooks
22

3-
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.
44
Since feature flags are dynamic and affect runtime behavior, it’s important to collect relevant feature flag telemetry signals.
55
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.
66

@@ -13,16 +13,43 @@ $ npm install @openfeature/open-telemetry-hooks
1313
### Peer dependencies
1414

1515
Confirm that the following peer dependencies are installed.
16+
If you use the `MetricsHook`, `SpanEventHook` or `SpanHook`, you need to install:
1617

1718
```
18-
$ npm install @openfeature/server-sdk @opentelemetry/api
19+
$ npm install @openfeature/core @opentelemetry/api
1920
```
2021

22+
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+
2133
## Hooks
2234

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.
2441

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.
2653

2754
### MetricsHook
2855

@@ -36,52 +63,103 @@ This hook performs metric collection by tapping into various hook stages. Below
3663
## Usage
3764

3865
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.
4168

4269
More information on hooks can be found in the [OpenFeature documentation][hook-concept].
4370

4471
### Register Globally
4572

46-
The `TracingHook` and `MetricsHook` can both be set on the OpenFeature singleton.
73+
The hooks can be set on the OpenFeature singleton.
4774
This will ensure that every flag evaluation will always generate the applicable telemetry signals.
4875

4976
```typescript
50-
import { OpenFeature } from '@openfeature/server-sdk';
51-
import { TracingHook } from '@openfeature/open-telemetry-hooks';
77+
import { OpenFeature } from '@openfeature/core';
78+
import { EventHook, MetricsHook } from '@openfeature/open-telemetry-hooks';
5279

53-
OpenFeature.addHooks(new TracingHook());
80+
OpenFeature.addHooks(new EventHook(), new MetricsHook());
5481
```
5582

5683
### Register Per Client
5784

58-
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.
6087

6188
```typescript
62-
import { OpenFeature } from '@openfeature/server-sdk';
63-
import { MetricsHook } from '@openfeature/open-telemetry-hooks';
89+
import { OpenFeature } from '@openfeature/core';
90+
import { SpanHook, MetricsHook } from '@openfeature/open-telemetry-hooks';
91+
import { SpanEventHook } from './tracing-hooks';
6492

6593
const client = OpenFeature.getClient('my-app');
66-
client.addHooks(new MetricsHook());
94+
client.addHooks(new SpanEventHook(), new MetricsHook());
6795
```
6896

69-
### Custom Attributes
97+
### Hook Selection Guide
98+
99+
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.
70105

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
72107

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 [flag metadata](https://openfeature.dev/specification/types#flag-metadata) by supplying an `attributeMapper`:
75113

76114
```typescript
77-
// configure an attributeMapper function for a custom property
78-
const attributeMapper: AttributeMapper = (flagMetadata) => {
79-
return {
80-
myCustomAttribute: flagMetadata.someFlagMetadataField,
81-
};
82-
};
83-
const metricsHook = new MetricsHook({ attributeMapper });
84-
const tracingHook = new TracingHook({ attributeMapper });
115+
import { EventHook } from '@openfeature/open-telemetry-hooks';
116+
117+
const attributeMapper = (flagMetadata) => ({
118+
myCustomAttribute: flagMetadata.someFlagMetadataField,
119+
});
120+
121+
const eventHook = new EventHook({ attributeMapper });
122+
```
123+
124+
#### Exclude Attributes
125+
126+
Exclude specific attributes from being added to telemetry events:
127+
128+
```typescript
129+
import { EventHook } from '@openfeature/open-telemetry-hooks';
130+
import { TelemetryAttribute } from '@openfeature/core';
131+
132+
const eventHook = new EventHook({
133+
excludeAttributes: [TelemetryAttribute.VALUE, 'sensitive_value']
134+
});
135+
```
136+
137+
#### Exclude Exceptions
138+
139+
Prevent exceptions from being recorded:
140+
141+
```typescript
142+
import { SpanHook } from '@openfeature/open-telemetry-hooks';
143+
144+
const spanHook = new SpanHook({ excludeExceptions: true });
145+
```
146+
147+
#### Event Mutation
148+
149+
Transform events before they are emitted:
150+
151+
```typescript
152+
import { EventHook } from '@openfeature/open-telemetry-hooks';
153+
154+
const eventMutator = (event) => ({
155+
...event,
156+
attributes: {
157+
...event.attributes,
158+
environment: 'production'
159+
}
160+
});
161+
162+
const eventHook = new EventHook({ eventMutator });
85163
```
86164

87165
## Development
@@ -94,5 +172,22 @@ Run `nx package hooks-open-telemetry` to build the library.
94172

95173
Run `nx test hooks-open-telemetry` to execute the unit tests via [Jest](https://jestjs.io).
96174

97-
[otel-spec]: https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/feature-flags/
175+
[otel-span]: https://opentelemetry.io/docs/concepts/signals/traces/#spans
176+
177+
[otel-span-attributes]: https://opentelemetry.io/docs/concepts/signals/traces/#attributes
178+
179+
[otel-span-events]: https://opentelemetry.io/docs/concepts/signals/traces/#span-events
180+
181+
[otel-logs]: https://opentelemetry.io/docs/concepts/signals/logs
182+
183+
[otel-semconv]: https://opentelemetry.io/docs/specs/semconv/feature-flags/feature-flags-logs/
184+
98185
[hook-concept]: https://openfeature.dev/docs/reference/concepts/hooks
186+
187+
[span-event-deprecation-otep]: https://github.com/open-telemetry/opentelemetry-specification/blob/fbcd7a3126a545debd9e6e5c69b7b67d4ef1c156/oteps/4430-span-event-api-deprecation-plan.md
188+
189+
[otel-tracing-js]: https://opentelemetry.io/docs/languages/js/instrumentation/#initialize-tracing
190+
191+
[otel-metrics-js]: https://opentelemetry.io/docs/languages/js/instrumentation/#initialize-metrics
192+
193+
[otel-logs-js]: https://opentelemetry.io/docs/languages/js/instrumentation/#logsl

libs/hooks/open-telemetry/package-lock.json

Lines changed: 170 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)