@cedvict/http-guardian-plugin-otel 0.0.1-next.10 → 0.0.1-next.8
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 +1 -68
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,76 +1,9 @@
|
|
|
1
1
|
# @cedvict/http-guardian-plugin-otel
|
|
2
2
|
|
|
3
|
-
OpenTelemetry plugin for http-guardian.
|
|
4
|
-
sets standard `http.*` attributes and propagates status into
|
|
5
|
-
`SpanStatusCode.OK` / `ERROR`.
|
|
3
|
+
OpenTelemetry plugin for http-guardian.
|
|
6
4
|
|
|
7
5
|
## Usage
|
|
8
6
|
|
|
9
7
|
```ts
|
|
10
8
|
import { otelPlugin } from "@cedvict/http-guardian-plugin-otel";
|
|
11
|
-
import { createHttpClient, createApiParserStructured, bearerAuth } from "@cedvict/http-guardian";
|
|
12
|
-
|
|
13
|
-
const api = createHttpClient({
|
|
14
|
-
baseUrl: "https://api.example.com",
|
|
15
|
-
auth: bearerAuth(() => token),
|
|
16
|
-
parser: createApiParserStructured(),
|
|
17
|
-
plugins: [
|
|
18
|
-
otelPlugin({
|
|
19
|
-
tracerName: "my-app",
|
|
20
|
-
spanName: (ctx) => `HTTP ${ctx.method} ${new URL(ctx.url).pathname}`,
|
|
21
|
-
redactUrl: (u) => u,
|
|
22
|
-
}),
|
|
23
|
-
],
|
|
24
|
-
});
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
By default the plugin auto-detects `@opentelemetry/api` at runtime. You can
|
|
28
|
-
also inject the API module explicitly via `otelPlugin({ api })`.
|
|
29
|
-
|
|
30
|
-
## Linking to a backend trace
|
|
31
|
-
|
|
32
|
-
When the response envelope carries a `traceId` / `spanId` (e.g. via
|
|
33
|
-
[`createApiParserStructured`](../http-guardian)), you can stamp those on the
|
|
34
|
-
client-side span so traces stitch together end-to-end.
|
|
35
|
-
|
|
36
|
-
The plugin exposes `setAttributes(span, ctx, res, err)` for exactly this:
|
|
37
|
-
|
|
38
|
-
```ts
|
|
39
|
-
import { createApiParserStructured, type Guard } from "@cedvict/http-guardian";
|
|
40
|
-
|
|
41
|
-
otelPlugin({
|
|
42
|
-
setAttributes: (span, ctx) => {
|
|
43
|
-
const traceId = ctx.meta?.traceId;
|
|
44
|
-
const spanId = ctx.meta?.spanId;
|
|
45
|
-
if (traceId) span.setAttribute("backend.trace_id", traceId);
|
|
46
|
-
if (spanId) span.setAttribute("backend.span_id", spanId);
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### Outbound `traceparent` header
|
|
52
|
-
|
|
53
|
-
If your backend joins traces via the W3C `traceparent` header, inject it
|
|
54
|
-
**before** the request leaves with a small guard. The current OTel context
|
|
55
|
-
exposes the active span; format follows
|
|
56
|
-
[trace-context spec](https://www.w3.org/TR/trace-context/):
|
|
57
|
-
|
|
58
|
-
```ts
|
|
59
|
-
import * as otel from "@opentelemetry/api";
|
|
60
|
-
import type { Guard } from "@cedvict/http-guardian";
|
|
61
|
-
|
|
62
|
-
const traceparentGuard: Guard = async (ctx, next) => {
|
|
63
|
-
const span = otel.trace.getActiveSpan();
|
|
64
|
-
const sc = span?.spanContext();
|
|
65
|
-
if (sc && sc.traceId && sc.spanId) {
|
|
66
|
-
const flags = (sc.traceFlags ?? 0).toString(16).padStart(2, "0");
|
|
67
|
-
ctx.headers.set("traceparent", `00-${sc.traceId}-${sc.spanId}-${flags}`);
|
|
68
|
-
}
|
|
69
|
-
return next(ctx);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
createHttpClient({
|
|
73
|
-
guards: [traceparentGuard], // runs before the otel guard's next()
|
|
74
|
-
plugins: [otelPlugin()],
|
|
75
|
-
});
|
|
76
9
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cedvict/http-guardian-plugin-otel",
|
|
3
|
-
"version": "0.0.1-next.
|
|
3
|
+
"version": "0.0.1-next.8",
|
|
4
4
|
"description": "OpenTelemetry plugin for http-guardian.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -54,6 +54,6 @@
|
|
|
54
54
|
"@typescript-eslint/eslint-plugin": "^8.0.0"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@cedvict/http-guardian": "^0.0.1-next.
|
|
57
|
+
"@cedvict/http-guardian": "^0.0.1-next.8"
|
|
58
58
|
}
|
|
59
59
|
}
|