@mark1russell7/otel-ts 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/context-processors.d.ts +38 -0
- package/dist/context-processors.d.ts.map +1 -0
- package/dist/context-processors.js +76 -0
- package/dist/context-processors.js.map +1 -0
- package/dist/context-propagator.d.ts +39 -0
- package/dist/context-propagator.d.ts.map +1 -0
- package/dist/context-propagator.js +88 -0
- package/dist/context-propagator.js.map +1 -0
- package/dist/context.d.ts +90 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +169 -0
- package/dist/context.js.map +1 -0
- package/dist/node-exporters.d.ts +33 -0
- package/dist/node-exporters.d.ts.map +1 -0
- package/dist/node-exporters.js +70 -0
- package/dist/node-exporters.js.map +1 -0
- package/dist/node.d.ts +28 -10
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +51 -12
- package/dist/node.js.map +1 -1
- package/dist/session-processors.d.ts.map +1 -1
- package/dist/session-processors.js +6 -19
- package/dist/session-processors.js.map +1 -1
- package/package.json +7 -1
- package/src/context-processors.ts +84 -0
- package/src/context-propagator.ts +96 -0
- package/src/context.ts +207 -0
- package/src/node-exporters.ts +88 -0
- package/src/node.ts +75 -22
- package/src/session-processors.ts +6 -19
package/README.md
CHANGED
|
@@ -194,6 +194,43 @@ const otel = init();
|
|
|
194
194
|
|
|
195
195
|
The core entry imports no context library, and it patches nothing. Its default is `"stack"`: the `StackContextManager` of OpenTelemetry. You can also give your own `ContextManager`.
|
|
196
196
|
|
|
197
|
+
## Context ids
|
|
198
|
+
|
|
199
|
+
The entry `@mark1russell7/otel-ts/context` adds context ids, for example a user id or a request id, in one call. It depends on `@opentelemetry/api` only, so a library and a browser bundle can import it. It registers nothing: `init()` or `startNodeTelemetry()` registers the context manager. The design is in [docs/CONTEXT-DESIGN.md](docs/CONTEXT-DESIGN.md).
|
|
200
|
+
|
|
201
|
+
1. Define the keys one time, as data:
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
import { defineContextKeys } from "@mark1russell7/otel-ts/context";
|
|
205
|
+
|
|
206
|
+
export const keys = defineContextKeys({
|
|
207
|
+
"tenant.id": { cardinality: "low", propagate: "internal" },
|
|
208
|
+
"user.id": { cardinality: "high", propagate: "internal" },
|
|
209
|
+
"feature.flag": { cardinality: "low", propagate: "none", maxLength: 32 },
|
|
210
|
+
});
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
2. Run the code that knows the id in a context with it:
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { withContext, contextValue } from "@mark1russell7/otel-ts/context";
|
|
217
|
+
|
|
218
|
+
await withContext({ "user.id": user.id }, () => handle(request));
|
|
219
|
+
|
|
220
|
+
// Anywhere below, also after await:
|
|
221
|
+
contextValue("user.id");
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
The rules:
|
|
225
|
+
|
|
226
|
+
- `cardinality` tells how many values a key has. A key with `"high"` never becomes a metric attribute.
|
|
227
|
+
- `propagate: "internal"` puts the key in the W3C baggage, for internal calls. `propagate: "none"` keeps the key in the process.
|
|
228
|
+
- `maxLength` is the longest value. The default is 256.
|
|
229
|
+
- A key that no `defineContextKeys` call defined is an error. A second definition of a key with other rules is an error too.
|
|
230
|
+
- An `undefined` or `null` value adds nothing. A value that is not a string, or that is longer than `maxLength`, is an error.
|
|
231
|
+
- `contextValues()` gives the value of each defined key. A baggage entry from outside with a key that is not defined is not in it.
|
|
232
|
+
- `withInternalCall(fn)` marks the calls that `fn` starts as internal calls. `isInternalCall()` reads the mark.
|
|
233
|
+
|
|
197
234
|
## Writer identity
|
|
198
235
|
|
|
199
236
|
Prometheus and Mimir make the `instance` label from the `service.instance.id` resource attribute. Each SDK instance writes its own metric series. Two SDK instances with the same identity write to the same series. Then Mimir rejects samples, and `rate()` gives incorrect values.
|
|
@@ -357,9 +394,20 @@ node --import ./dist/instrumentation.js dist/server.js
|
|
|
357
394
|
|
|
358
395
|
The instrumentations patch a module when it loads, so this module must load before the modules of the service. In an ES module service, the CommonJS hook does not see a package that an ES module imports. When you give `importMetaUrl`, `startNodeTelemetry()` registers the loader hook of OpenTelemetry (`@opentelemetry/instrumentation/hook.mjs`) before the SDK starts. The hook then patches those imports too.
|
|
359
396
|
|
|
360
|
-
`startNodeTelemetry()`
|
|
397
|
+
`startNodeTelemetry()` exports nothing when `OTEL_EXPORTER_OTLP_ENDPOINT` has no value. Set `enabled` to change this. With export on or off, it registers the [context ids](#context-ids):
|
|
398
|
+
|
|
399
|
+
- AsyncLocalStorage as the context manager, so a value of `withContext()` stays after `await`.
|
|
400
|
+
- The propagator of otel-ts: `traceparent` and `tracestate` go on each call, and the context keys go in `baggage` only on an internal call (`withInternalCall()`).
|
|
401
|
+
- A metric view that removes each key with `cardinality: "high"` from each metric.
|
|
402
|
+
- With export on: a processor that adds the value of each context key to each span and each log record. An attribute that the caller sets stays.
|
|
403
|
+
|
|
404
|
+
Only one part of a program registers the context. A second `startNodeTelemetry()` before the `shutdown()` of the first is an error. `shutdown()` removes the registration.
|
|
405
|
+
|
|
406
|
+
With export on, the exporters come from `OTEL_TRACES_EXPORTER` and `OTEL_LOGS_EXPORTER`: `otlp` (the default), `console` or `none`. The OTLP protocol comes from `OTEL_EXPORTER_OTLP_TRACES_PROTOCOL` and `OTEL_EXPORTER_OTLP_LOGS_PROTOCOL`, else `OTEL_EXPORTER_OTLP_PROTOCOL`: `http/protobuf` (the default) or `http/json`. Another name is an error. For grpc, give `traceExporter` and `logExporter`. The sampler and the metric readers are those of the SDK.
|
|
407
|
+
|
|
408
|
+
By default, it stops the SDK and sends the last spans at `SIGTERM` and `SIGINT`. Then, when the service has no listener of its own for the signal, the process ends with the signal, as it does without `startNodeTelemetry()`. When the service has a listener, that listener ends the process. Set `stopOnSignals: false` to add no listener.
|
|
361
409
|
|
|
362
|
-
The option `env` sets
|
|
410
|
+
The option `env` sets `enabled`, the service name, the exporter names and the protocols. An OTLP exporter and the sampler read their other settings (the endpoint, the headers, `OTEL_TRACES_SAMPLER`) from `process.env`. To send to another endpoint, give `traceExporter` and `logExporter`.
|
|
363
411
|
|
|
364
412
|
### The trace of a write, in its echo
|
|
365
413
|
|