@mark1russell7/otel-ts 0.3.0 → 0.3.1

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