@mark1russell7/otel-ts 0.2.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 +39 -0
- package/dist/.tsbuildinfo +1 -1
- 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/package.json +8 -4
- package/src/context.ts +207 -0
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
OpenTelemetry and Grafana Faro for TypeScript web apps, with one `init()` call.
|
|
4
4
|
|
|
5
|
+
Try the [live lab](https://mark1russell7.github.io/otel-ts/). It starts the real `init()` in your browser, and it shows each span, metric and log record as it leaves the package. The data stays in your browser.
|
|
6
|
+
|
|
5
7
|
## Install
|
|
6
8
|
|
|
7
9
|
Install the package from npm:
|
|
@@ -192,6 +194,43 @@ const otel = init();
|
|
|
192
194
|
|
|
193
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`.
|
|
194
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
|
+
|
|
195
234
|
## Writer identity
|
|
196
235
|
|
|
197
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.
|