|
| 1 | +--- |
| 2 | +summary: Instrument Bentocache with OpenTelemetry using @bentocache/otel |
| 3 | +--- |
| 4 | + |
| 5 | +# Telemetry (OpenTelemetry) |
| 6 | + |
| 7 | +:::warning |
| 8 | +This package is experimental and may change in future versions. |
| 9 | +::: |
| 10 | + |
| 11 | +Bentocache provides an official OpenTelemetry instrumentation package: `@bentocache/otel`. |
| 12 | + |
| 13 | + |
| 14 | +It listens to Bentocache tracing channels and emits spans like: |
| 15 | + |
| 16 | +- `cache.get` |
| 17 | +- `cache.getOrSet` |
| 18 | +- `cache.set` |
| 19 | +- `cache.delete` |
| 20 | +- `cache.deleteMany` |
| 21 | +- `cache.clear` |
| 22 | +- `cache.expire` |
| 23 | +- `cache.factory` |
| 24 | + |
| 25 | +## Tracing Channels |
| 26 | + |
| 27 | +Bentocache exposes [Node.js Diagnostic Channels](https://nodejs.org/api/diagnostics_channel.html#tracingchannel) for advanced instrumentation and APM integration, which `@bentocache/otel` utilizes for OpenTelemetry spans. You can also subscribe to these channels directly for custom telemetry or monitoring solutions. |
| 28 | + |
| 29 | +### Available channels |
| 30 | + |
| 31 | +#### `bentocache.cache.operation` |
| 32 | + |
| 33 | +Traces cache operations with timing information. |
| 34 | + |
| 35 | +```ts |
| 36 | +import { tracingChannels } from 'bentocache' |
| 37 | + |
| 38 | +tracingChannels.cacheOperation.subscribe({ |
| 39 | + start(message) { |
| 40 | + // Called when operation starts |
| 41 | + console.log(`Starting ${message.operation} on ${message.key}`) |
| 42 | + }, |
| 43 | + asyncEnd(message) { |
| 44 | + // Called when async operation completes |
| 45 | + console.log(`Completed ${message.operation}`, { |
| 46 | + key: message.key, |
| 47 | + store: message.store, |
| 48 | + hit: message.hit, |
| 49 | + tier: message.tier, |
| 50 | + graced: message.graced, |
| 51 | + }) |
| 52 | + }, |
| 53 | + error({ error, ...message }) { |
| 54 | + // Called when operation fails |
| 55 | + console.error(`Failed ${message.operation}:`, error) |
| 56 | + }, |
| 57 | +}) |
| 58 | +``` |
| 59 | + |
| 60 | +### Message properties |
| 61 | + |
| 62 | +| Property | Type | Description | |
| 63 | +| ----------- | ------------------------------------------------------------------- | ----------------------------------------------------- | |
| 64 | +| `operation` | `'get' \| 'set' \| 'delete' \| 'deleteMany' \| 'clear' \| 'expire'` | The operation type | |
| 65 | +| `key` | `string` | Cache key with full prefix (e.g., `'users:123'`) | |
| 66 | +| `keys` | `string[]` | Multiple keys for `deleteMany` operation | |
| 67 | +| `store` | `string` | Store name | |
| 68 | +| `hit` | `boolean` | Whether the key was found (only for `get`) | |
| 69 | +| `tier` | `'l1' \| 'l2'` | Which tier served the value (only for `get` hits) | |
| 70 | +| `graced` | `boolean` | Whether value came from grace period (only for `get`) | |
| 71 | + |
| 72 | +## Install |
| 73 | + |
| 74 | +:::codegroup |
| 75 | +```sh |
| 76 | +// title: npm |
| 77 | +npm i @bentocache/otel |
| 78 | +``` |
| 79 | + |
| 80 | +```sh |
| 81 | +// title: pnpm |
| 82 | +pnpm add @bentocache/otel |
| 83 | +``` |
| 84 | + |
| 85 | +```sh |
| 86 | +// title: yarn |
| 87 | +yarn add @bentocache/otel |
| 88 | +``` |
| 89 | +::: |
| 90 | + |
| 91 | +## Basic setup |
| 92 | + |
| 93 | +```ts |
| 94 | +import { NodeSDK } from '@opentelemetry/sdk-node' |
| 95 | +import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http' |
| 96 | +import { BentoCacheInstrumentation } from '@bentocache/otel' |
| 97 | + |
| 98 | +const traceExporter = new OTLPTraceExporter({ |
| 99 | + url: 'http://localhost:4318/v1/traces', |
| 100 | +}) |
| 101 | + |
| 102 | +const sdk = new NodeSDK({ |
| 103 | + serviceName: 'my-app', |
| 104 | + traceExporter, |
| 105 | + instrumentations: [ |
| 106 | + new BentoCacheInstrumentation({ |
| 107 | + requireParentSpan: true, |
| 108 | + includeKeys: false, |
| 109 | + suppressInternalOperations: true, |
| 110 | + }), |
| 111 | + ], |
| 112 | +}) |
| 113 | + |
| 114 | +sdk.start() |
| 115 | +``` |
| 116 | + |
| 117 | +## Parent spans and `requireParentSpan` |
| 118 | + |
| 119 | +By default, `requireParentSpan` is `true`. |
| 120 | +This means Bentocache spans are created only when a parent span is active. |
| 121 | + |
| 122 | +If you do not use HTTP/framework instrumentation yet, you can: |
| 123 | + |
| 124 | +- Set `requireParentSpan: false`, or |
| 125 | +- Create parent spans manually around your application logic. |
| 126 | + |
| 127 | +```ts |
| 128 | +import { trace } from '@opentelemetry/api' |
| 129 | + |
| 130 | +const tracer = trace.getTracer('app') |
| 131 | + |
| 132 | +await tracer.startActiveSpan('request', async (span) => { |
| 133 | + await bento.getOrSet({ |
| 134 | + key: 'user:1', |
| 135 | + ttl: '5m', |
| 136 | + factory: () => fetchUser(), |
| 137 | + }) |
| 138 | + |
| 139 | + span.end() |
| 140 | +}) |
| 141 | +``` |
| 142 | + |
| 143 | +## Span naming and attributes |
| 144 | + |
| 145 | +Default span names use `cache.<operation>`. |
| 146 | +You can customize naming with `spanName` or `spanNamePrefix`. |
| 147 | + |
| 148 | +Common attributes: |
| 149 | + |
| 150 | +- `cache.operation` |
| 151 | +- `cache.store` |
| 152 | +- `cache.key` |
| 153 | +- `cache.keys` |
| 154 | +- `cache.hit` |
| 155 | +- `cache.tier` |
| 156 | +- `cache.graced` |
| 157 | + |
| 158 | +## Configuration options |
| 159 | + |
| 160 | +- `requireParentSpan` (default: `true`): Create spans only when a parent span exists. |
| 161 | +- `includeKeys` (default: `false`): Include key/keys attributes on spans. |
| 162 | +- `keySanitizer`: Sanitize keys before adding them to span attributes. |
| 163 | +- `spanName`: Provide a custom span-name factory. |
| 164 | +- `spanNamePrefix` (default: `cache`): Prefix for default span names. |
| 165 | +- `suppressInternalOperations` (default: `true`): Suppress internal L2/bus operations. |
0 commit comments