@webmcp-agents/telemetry 0.0.0-stage → 0.5.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 +38 -2
- package/dist/bridge-n3EZZQrm.js +76 -0
- package/dist/engine-K_Cwwt9r.js +420 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.js +19 -0
- package/dist/observe-Bei_BEpM.js +225 -0
- package/dist/react.d.ts +19 -0
- package/dist/react.js +29 -0
- package/dist/semconv-74esS-9v.js +222 -0
- package/dist/semconv-BXKrNtuf.d.ts +221 -0
- package/dist/server.d.ts +244 -0
- package/dist/server.js +1431 -0
- package/dist/targets-vd2wpXrO.js +22 -0
- package/dist/types-8Tdrt7C_.d.ts +96 -0
- package/dist/types-Bqk6T-HL.d.ts +169 -0
- package/dist/util-4und6m9s.js +123 -0
- package/package.json +94 -3
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/browser/targets.ts
|
|
2
|
+
/** An exporter for one `exporters` entry, or undefined (logged once) when it cannot be used in a browser. */
|
|
3
|
+
function toExporter(entry, resource, post, warn) {
|
|
4
|
+
if (typeof entry === "string") return post(entry, resource);
|
|
5
|
+
if (typeof entry.export === "function") return entry;
|
|
6
|
+
const target = entry;
|
|
7
|
+
if (target.endpoint && target.endpoints) {
|
|
8
|
+
warn("an OTLP target has both `endpoint` and `endpoints`: give one. That target is off.");
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if ((target.protocol ?? "http/json") !== "http/json") {
|
|
12
|
+
warn(`a browser sends OTLP as "http/json" only, not "${target.protocol}": send through the relay instead. That target is off.`);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const url = target.endpoints?.traces ?? (target.endpoint && `${target.endpoint.replace(/\/+$/, "")}/v1/traces`);
|
|
16
|
+
if (!url) {
|
|
17
|
+
warn("an OTLP target in the browser needs `endpoint` or `endpoints` (a browser has no OTEL_* env). That target is off.");
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
return post(url, resource, target.headers);
|
|
21
|
+
}
|
|
22
|
+
export { toExporter };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { d as TracerProviderOption, i as OtlpTarget, n as Attributes, o as Sampling } from "./types-Bqk6T-HL.js";
|
|
2
|
+
import { RedactOptions, Redactor } from "@webmcp-agents/core/redact";
|
|
3
|
+
/**
|
|
4
|
+
* A finished browser span: a run (`invoke_agent`), a tool call (`execute_tool`), for an on-device run a model step
|
|
5
|
+
* (`chat`), or the app's own (`withAgentSpan`, named as the app named it, with `webmcp.custom: true`). Times are milliseconds since the epoch. What `spanFilter` and a {@link BrowserSpanExporter} receive,
|
|
6
|
+
* already redacted.
|
|
7
|
+
*
|
|
8
|
+
* @experimental
|
|
9
|
+
*/
|
|
10
|
+
interface SpanRecord {
|
|
11
|
+
/** `invoke_agent <agent>`, `execute_tool <tool>`, `chat <model>`, or a custom span's own name. */
|
|
12
|
+
name: string;
|
|
13
|
+
/** 32 hex digits: the run's trace, shared with the server's spans of the same run. */
|
|
14
|
+
traceId: string;
|
|
15
|
+
/** 16 hex digits. A run span's id is the one its `traceparent` carries, so the server's step spans are its children. */
|
|
16
|
+
spanId: string;
|
|
17
|
+
/** The parent span: the run for a call or step; the delegating call for a sub-agent's run. */
|
|
18
|
+
parentSpanId?: string;
|
|
19
|
+
/** Start, ms since the epoch. */
|
|
20
|
+
startTime: number;
|
|
21
|
+
/** End, ms since the epoch. */
|
|
22
|
+
endTime: number;
|
|
23
|
+
/** The span's attributes (names in `semconv`). */
|
|
24
|
+
attributes: Attributes;
|
|
25
|
+
/** `webmcp.park`, `webmcp.yield`, `webmcp.resume` on a tool call; the app's own (`recordAgentEvent`) on a run. */
|
|
26
|
+
events: {
|
|
27
|
+
name: string;
|
|
28
|
+
time: number;
|
|
29
|
+
attributes?: Attributes;
|
|
30
|
+
}[];
|
|
31
|
+
/** `error` for a failed run, call or custom span (with `error.type` set), else `unset`. */
|
|
32
|
+
status: {
|
|
33
|
+
code: "unset" | "ok" | "error";
|
|
34
|
+
message?: string;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Where browser spans go when they are not sent to the relay: any object with `export`. A throw or rejection is
|
|
39
|
+
* swallowed (the page never breaks for telemetry).
|
|
40
|
+
*
|
|
41
|
+
* @experimental
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* observeAgents({ exporters: [{ export: (spans) => analytics.track("agent_spans", { count: spans.length }) }] });
|
|
45
|
+
*/
|
|
46
|
+
interface BrowserSpanExporter {
|
|
47
|
+
/** Called with a batch of finished, redacted spans. */
|
|
48
|
+
export(spans: readonly SpanRecord[]): void | Promise<void>;
|
|
49
|
+
/** Send anything held back (called when the page is hidden and when observing stops). */
|
|
50
|
+
flush?(): Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Options for `observeAgents()` / `<ObserveAgents>`. Every field is optional.
|
|
54
|
+
*
|
|
55
|
+
* @experimental
|
|
56
|
+
*/
|
|
57
|
+
interface ObserveAgentsOptions {
|
|
58
|
+
/**
|
|
59
|
+
* Where spans go. Default: the page's global tracer provider when one is registered (the OpenTelemetry Web SDK,
|
|
60
|
+
* Sentry, Datadog RUM), else the app's relay at `"/api/telemetry"`. A string is the URL of a relay; an OTLP target
|
|
61
|
+
* posts OTLP JSON straight to a collector (its headers end up in the page: never put a key there). `[]` sends
|
|
62
|
+
* nothing and builds no span.
|
|
63
|
+
*/
|
|
64
|
+
exporters?: readonly (string | OtlpTarget | BrowserSpanExporter)[];
|
|
65
|
+
/** A provider to use as is, `"global"` to require the page's registered one, or `"own"` never to use it. Default: the global one when registered. */
|
|
66
|
+
tracerProvider?: TracerProviderOption;
|
|
67
|
+
/** `service.name` of the browser's spans. Default `location.host`. */
|
|
68
|
+
serviceName?: string;
|
|
69
|
+
/** Which runs are exported, decided at each run's start. Default `{ type: "always" }`. */
|
|
70
|
+
sampling?: Sampling;
|
|
71
|
+
/** Put tool inputs on `execute_tool` spans (`gen_ai.tool.call.arguments`), redacted. Default false. */
|
|
72
|
+
recordInputs?: boolean;
|
|
73
|
+
/** Put tool results on `execute_tool` spans (`gen_ai.tool.call.result`), redacted. Default false. */
|
|
74
|
+
recordOutputs?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Redaction of everything exported. Default: the page's redactor (`configureRedaction`), read at each use. `false`
|
|
77
|
+
* only for local debugging.
|
|
78
|
+
*/
|
|
79
|
+
redact?: false | RedactOptions | Redactor;
|
|
80
|
+
/**
|
|
81
|
+
* Consent: read when each run starts; `false` and that run produces no span and no request. Default `() => true`.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* observeAgents({ enabled: () => consent.analytics })
|
|
85
|
+
*/
|
|
86
|
+
enabled?: () => boolean;
|
|
87
|
+
/** Keep (true) or drop (false) a span, after redaction. A throw drops it. */
|
|
88
|
+
spanFilter?: (span: SpanRecord) => boolean;
|
|
89
|
+
/** Copy an attribute to the name a backend groups by, e.g. `{ "webmcp.conversation.id": "session.id" }`. */
|
|
90
|
+
metadataMapping?: Record<string, string>;
|
|
91
|
+
/** Send at most this often, in ms. Default 2000. */
|
|
92
|
+
batchMs?: number;
|
|
93
|
+
/** Send sooner once this many spans are waiting. Default 64. */
|
|
94
|
+
batchSize?: number;
|
|
95
|
+
}
|
|
96
|
+
export { ObserveAgentsOptions as n, SpanRecord as r, BrowserSpanExporter as t };
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types both entries share. The OpenTelemetry shapes here are structural on purpose: any `SpanExporter`,
|
|
3
|
+
* `SpanProcessor` or `TracerProvider` from the OpenTelemetry SDK (or a vendor package built on it) fits them, and this
|
|
4
|
+
* package's public types never import `@opentelemetry/*`, which stays an optional install.
|
|
5
|
+
*/
|
|
6
|
+
/** A span attribute value, as OpenTelemetry allows it. */
|
|
7
|
+
type AttributeValue = string | number | boolean | readonly string[] | readonly number[] | readonly boolean[];
|
|
8
|
+
/** Span attributes. */
|
|
9
|
+
type Attributes = Record<string, AttributeValue>;
|
|
10
|
+
/**
|
|
11
|
+
* Attributes of the app's own event or span (`recordAgentEvent`, `withAgentSpan`, `observability.event` / `.span`).
|
|
12
|
+
* Keys are free, except that `webmcp.*` is this library's: such keys are dropped (a custom span carries the run's
|
|
13
|
+
* real ids, and app code must not be able to forge them). Values that are not attribute values (objects, `null`,
|
|
14
|
+
* `undefined`) are left out. Every value is redacted like the package's own.
|
|
15
|
+
*
|
|
16
|
+
* @experimental
|
|
17
|
+
*/
|
|
18
|
+
type CustomAttributes = Readonly<Record<string, AttributeValue | null | undefined>>;
|
|
19
|
+
/** The fields every OTLP target has, whichever way its URL is given. */
|
|
20
|
+
interface OtlpTargetFields {
|
|
21
|
+
/**
|
|
22
|
+
* The wire format. `"http/json"` (default) needs no dependency and runs on the edge and in the browser;
|
|
23
|
+
* `"http/protobuf"` loads `@opentelemetry/exporter-trace-otlp-proto` and `"grpc"` loads
|
|
24
|
+
* `@opentelemetry/exporter-trace-otlp-grpc` (optional installs, server only). Default: `OTEL_EXPORTER_OTLP_PROTOCOL`,
|
|
25
|
+
* else `"http/json"`.
|
|
26
|
+
*/
|
|
27
|
+
protocol?: "http/json" | "http/protobuf" | "grpc";
|
|
28
|
+
/**
|
|
29
|
+
* Headers sent with every export (an API key, a project). Merged over `OTEL_EXPORTER_OTLP_HEADERS` only when the URL
|
|
30
|
+
* also comes from the env (no `endpoint` / `endpoints`): a target with its own URL gets only these, so the env's key
|
|
31
|
+
* never reaches another backend. Server side only: never put a key in the page.
|
|
32
|
+
*/
|
|
33
|
+
headers?: Record<string, string>;
|
|
34
|
+
/** How long one export may take before it is dropped, in ms. Default `OTEL_EXPORTER_OTLP_TIMEOUT`, else 30000. */
|
|
35
|
+
timeout?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* An OTLP destination. Give a base `endpoint` (`/v1/traces` is appended) or exact `endpoints.traces` (used as given:
|
|
39
|
+
* query strings, tenant paths), never both; with neither, the `OTEL_EXPORTER_OTLP_*` variables decide.
|
|
40
|
+
*
|
|
41
|
+
* @experimental
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* otlp({ endpoint: "https://otel.example.com", headers: { "x-api-key": process.env.OTEL_KEY! } })
|
|
45
|
+
* otlp({ endpoints: { traces: "https://traces.example.com/api/ingest/?tenant=acme" } })
|
|
46
|
+
*/
|
|
47
|
+
type OtlpTarget = OtlpTargetFields & ({
|
|
48
|
+
endpoint?: undefined;
|
|
49
|
+
endpoints?: undefined;
|
|
50
|
+
} | {
|
|
51
|
+
/** Base collector URL: `/v1/traces` is appended (for `grpc`, used as given). Give this or `endpoints`, never both. */
|
|
52
|
+
endpoint: string;
|
|
53
|
+
endpoints?: undefined;
|
|
54
|
+
} | {
|
|
55
|
+
endpoint?: undefined;
|
|
56
|
+
/** Exact per-signal URL, used as given. */
|
|
57
|
+
endpoints: {
|
|
58
|
+
traces: string;
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
/** What a `custom` sampler is told about the trace it decides on. */
|
|
62
|
+
interface SamplingContext {
|
|
63
|
+
/** The agent's id, when the run has one. */
|
|
64
|
+
agent?: string;
|
|
65
|
+
/** The conversation the run belongs to. */
|
|
66
|
+
conversation?: string;
|
|
67
|
+
/** The request (server side: the chat request, or the relay's). */
|
|
68
|
+
request?: Request;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Which traces are exported. `ratio` keeps about that share of traces, decided by trace id so a trace is kept or
|
|
72
|
+
* dropped whole; `custom` decides per trace (a throw drops it).
|
|
73
|
+
*
|
|
74
|
+
* @experimental
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* sampling: { type: "ratio", probability: 0.2 }
|
|
78
|
+
* sampling: { type: "custom", sampler: ({ agent }) => agent !== "smalltalk" }
|
|
79
|
+
*/
|
|
80
|
+
type Sampling = {
|
|
81
|
+
type: "always";
|
|
82
|
+
} | {
|
|
83
|
+
type: "never";
|
|
84
|
+
} | {
|
|
85
|
+
type: "ratio";
|
|
86
|
+
probability: number;
|
|
87
|
+
} | {
|
|
88
|
+
type: "custom";
|
|
89
|
+
sampler: (ctx: SamplingContext) => boolean;
|
|
90
|
+
};
|
|
91
|
+
/** An OpenTelemetry span context, as the SDK reports it. */
|
|
92
|
+
interface SpanContextLike {
|
|
93
|
+
traceId: string;
|
|
94
|
+
spanId: string;
|
|
95
|
+
traceFlags: number;
|
|
96
|
+
isRemote?: boolean;
|
|
97
|
+
}
|
|
98
|
+
/** `[seconds, nanoseconds]`, OpenTelemetry's time. */
|
|
99
|
+
type HrTime = [number, number];
|
|
100
|
+
/**
|
|
101
|
+
* A finished span as OpenTelemetry's `ReadableSpan` describes it: what a `spanFilter` reads and what exporters get.
|
|
102
|
+
* Only the fields this package uses are typed; a real `ReadableSpan` has them all.
|
|
103
|
+
*
|
|
104
|
+
* @experimental
|
|
105
|
+
*/
|
|
106
|
+
interface ReadableSpanLike {
|
|
107
|
+
readonly name: string;
|
|
108
|
+
readonly kind: number;
|
|
109
|
+
spanContext(): SpanContextLike;
|
|
110
|
+
readonly parentSpanContext?: SpanContextLike;
|
|
111
|
+
readonly startTime: HrTime;
|
|
112
|
+
readonly endTime: HrTime;
|
|
113
|
+
readonly status: {
|
|
114
|
+
code: number;
|
|
115
|
+
message?: string;
|
|
116
|
+
};
|
|
117
|
+
readonly attributes: Readonly<Record<string, unknown>>;
|
|
118
|
+
readonly events: readonly {
|
|
119
|
+
name: string;
|
|
120
|
+
time: HrTime;
|
|
121
|
+
attributes?: Readonly<Record<string, unknown>>;
|
|
122
|
+
}[];
|
|
123
|
+
readonly instrumentationScope: {
|
|
124
|
+
name: string;
|
|
125
|
+
version?: string;
|
|
126
|
+
};
|
|
127
|
+
readonly resource: {
|
|
128
|
+
attributes: Readonly<Record<string, unknown>>;
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/** The result an exporter reports: code 0 is success, 1 failure. */
|
|
132
|
+
interface ExportResultLike {
|
|
133
|
+
code: number;
|
|
134
|
+
error?: Error;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Any OpenTelemetry `SpanExporter` (structural): an in-memory one, an OTLP one, a vendor's.
|
|
138
|
+
*
|
|
139
|
+
* @experimental
|
|
140
|
+
*/
|
|
141
|
+
interface SpanExporter {
|
|
142
|
+
export(spans: ReadableSpanLike[], resultCallback: (result: ExportResultLike) => void): void;
|
|
143
|
+
shutdown(): Promise<void>;
|
|
144
|
+
forceFlush?(): Promise<void>;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Any OpenTelemetry `SpanProcessor` (structural): a vendor's (Langfuse, Braintrust, Arize) or your own.
|
|
148
|
+
*
|
|
149
|
+
* @experimental
|
|
150
|
+
*/
|
|
151
|
+
interface SpanProcessor {
|
|
152
|
+
onStart(span: any, parentContext: any): void;
|
|
153
|
+
onEnd(span: ReadableSpanLike): void;
|
|
154
|
+
forceFlush(): Promise<void>;
|
|
155
|
+
shutdown(): Promise<void>;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Any OpenTelemetry `TracerProvider` (structural): the SDK's, `@vercel/otel`'s, Sentry's, Datadog's.
|
|
159
|
+
*
|
|
160
|
+
* @experimental
|
|
161
|
+
*/
|
|
162
|
+
interface TracerProvider {
|
|
163
|
+
getTracer(name: string, version?: string, options?: {
|
|
164
|
+
schemaUrl?: string;
|
|
165
|
+
}): any;
|
|
166
|
+
}
|
|
167
|
+
/** Where spans go: a provider to use as is, `"global"` to require the app's registered one, `"own"` never to use it. */
|
|
168
|
+
type TracerProviderOption = TracerProvider | "global" | "own";
|
|
169
|
+
export { ReadableSpanLike as a, SpanExporter as c, TracerProviderOption as d, OtlpTarget as i, SpanProcessor as l, Attributes as n, Sampling as o, CustomAttributes as r, SamplingContext as s, AttributeValue as t, TracerProvider as u };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// src/shared/util.ts
|
|
2
|
+
/** This package's version, on every span's instrumentation scope. Read from package.json, so a release bump keeps it true. */
|
|
3
|
+
const VERSION = "0.5.0";
|
|
4
|
+
const anyValue = (v) => typeof v === "string" ? { stringValue: v } : typeof v === "boolean" ? { boolValue: v } : typeof v === "number" ? Number.isInteger(v) ? { intValue: v } : { doubleValue: v } : { arrayValue: { values: v.map(anyValue) } };
|
|
5
|
+
/** OTLP JSON `KeyValue`s from attributes (values that are not attribute values are left out). */
|
|
6
|
+
const keyValues = (attributes = {}) => Object.entries(attributes).filter(([, v]) => isAttributeValue(v)).map(([key, v]) => ({
|
|
7
|
+
key,
|
|
8
|
+
value: anyValue(v)
|
|
9
|
+
}));
|
|
10
|
+
const scalar = (v) => typeof v === "string" || typeof v === "boolean" || typeof v === "number" && Number.isFinite(v);
|
|
11
|
+
/** A value OpenTelemetry accepts as an attribute: a string, finite number, boolean, or an array of one of those. */
|
|
12
|
+
const isAttributeValue = (v) => scalar(v) || Array.isArray(v) && v.every(scalar);
|
|
13
|
+
/** Keep only attribute values. */
|
|
14
|
+
const cleanAttributes = (value) => {
|
|
15
|
+
const out = {};
|
|
16
|
+
for (const [k, v] of Object.entries(value ?? {})) if (isAttributeValue(v)) out[k] = v;
|
|
17
|
+
return out;
|
|
18
|
+
};
|
|
19
|
+
/** `n` random bytes as lowercase hex (a trace id is 16, a span id 8). */
|
|
20
|
+
const randomHex = (n) => Array.from(crypto.getRandomValues(new Uint8Array(n)), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
21
|
+
/** `[trace id, span id]` of a W3C `traceparent` (`00-<32 hex>-<16 hex>-<2 hex>`), or undefined when it is not one. */
|
|
22
|
+
const parseTraceparent = (header) => {
|
|
23
|
+
const m = /^00-([0-9a-f]{32})-([0-9a-f]{16})-[0-9a-f]{2}$/.exec(header?.trim().toLowerCase() ?? "");
|
|
24
|
+
return m && !/^0+$/.test(m[1]) && !/^0+$/.test(m[2]) ? [m[1], m[2]] : void 0;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Whether a valid `traceparent` has its sampled flag set (the last byte's lowest bit). The server reads it as "the
|
|
28
|
+
* page records this run span": only then is that span a real parent to hang the server's spans under. It never
|
|
29
|
+
* decides whether the server exports (its own sampling does).
|
|
30
|
+
*/
|
|
31
|
+
const traceparentSampled = (header) => {
|
|
32
|
+
if (!parseTraceparent(header)) return false;
|
|
33
|
+
return (Number.parseInt(header.trim().slice(-2), 16) & 1) === 1;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Whether a trace is kept. `ratio` reads the trace id's last 8 hex digits (random in any W3C id), so every span of one
|
|
37
|
+
* trace gets the same answer on the browser and the server; without an id it draws at random. A throwing `custom`
|
|
38
|
+
* sampler drops the trace.
|
|
39
|
+
*/
|
|
40
|
+
function isSampled(sampling, traceId, ctx) {
|
|
41
|
+
if (!sampling || sampling.type === "always") return true;
|
|
42
|
+
if (sampling.type === "never") return false;
|
|
43
|
+
if (sampling.type === "ratio") {
|
|
44
|
+
const p = sampling.probability;
|
|
45
|
+
if (!(p > 0)) return false;
|
|
46
|
+
if (p >= 1) return true;
|
|
47
|
+
return (traceId && /^[0-9a-f]{8,}$/i.test(traceId) ? Number.parseInt(traceId.slice(-8), 16) / 4294967296 : Math.random()) < p;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
return sampling.sampler(ctx) === true;
|
|
51
|
+
} catch {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The OpenTelemetry API's global registry (`@opentelemetry/api` 1.x keeps it on `globalThis` under this key; checked
|
|
57
|
+
* against the installed 1.10.0, `src/internal/global-utils.ts`). Its `trace` entry exists only once something called
|
|
58
|
+
* `trace.setGlobalTracerProvider()`: reading it tells whether the app registered a provider without loading the API.
|
|
59
|
+
*/
|
|
60
|
+
const OTEL_API_KEY = Symbol.for("opentelemetry.js.api.1");
|
|
61
|
+
/** The registry's `trace` entry: the API's proxy provider, when the app registered one. */
|
|
62
|
+
const registeredGlobalTrace = () => globalThis[OTEL_API_KEY]?.trace;
|
|
63
|
+
const allowing = /* @__PURE__ */ new WeakMap();
|
|
64
|
+
/**
|
|
65
|
+
* `redactor`, with the `metadataMapping` targets allowed as keys: the app copied one of this library's ids there on
|
|
66
|
+
* purpose (`session.id`, `langsmith.trace.session_id`), and a default sensitive key ("session") must not hide it.
|
|
67
|
+
* Cached per redactor and mapping, so the hot path builds nothing.
|
|
68
|
+
*/
|
|
69
|
+
function withMappedKeys(redactor, mapping) {
|
|
70
|
+
const targets = Object.values(mapping);
|
|
71
|
+
if (!targets.length) return redactor;
|
|
72
|
+
const key = JSON.stringify([...targets].sort());
|
|
73
|
+
let byMapping = allowing.get(redactor);
|
|
74
|
+
if (!byMapping) {
|
|
75
|
+
byMapping = /* @__PURE__ */ new Map();
|
|
76
|
+
allowing.set(redactor, byMapping);
|
|
77
|
+
}
|
|
78
|
+
let out = byMapping.get(key);
|
|
79
|
+
if (!out) {
|
|
80
|
+
out = redactor.extend({ allowKeys: targets });
|
|
81
|
+
byMapping.set(key, out);
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
/** An app's attributes without the keys this library reserves (`webmcp.*`: app code must not forge a run's ids). */
|
|
86
|
+
const appAttributes = (a = {}) => {
|
|
87
|
+
const out = {};
|
|
88
|
+
for (const [k, v] of Object.entries(a)) if (!k.startsWith("webmcp.")) out[k] = v;
|
|
89
|
+
return out;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* `fn()`, with `end` called once it returned, threw, or its promise settled (with what it failed with). What `fn`
|
|
93
|
+
* returns, throws or rejects with is passed on unchanged; a promise is followed, so the result is a new promise of
|
|
94
|
+
* the same value. `end` must not throw.
|
|
95
|
+
*/
|
|
96
|
+
function around(fn, end) {
|
|
97
|
+
let out;
|
|
98
|
+
try {
|
|
99
|
+
out = fn();
|
|
100
|
+
} catch (err) {
|
|
101
|
+
end(err ?? /* @__PURE__ */ new Error("thrown"));
|
|
102
|
+
throw err;
|
|
103
|
+
}
|
|
104
|
+
const thenable = out;
|
|
105
|
+
if (thenable && typeof thenable.then === "function") return thenable.then((value) => {
|
|
106
|
+
end();
|
|
107
|
+
return value;
|
|
108
|
+
}, (err) => {
|
|
109
|
+
end(err ?? /* @__PURE__ */ new Error("rejected"));
|
|
110
|
+
throw err;
|
|
111
|
+
});
|
|
112
|
+
end();
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
/** `error.type` and the status message of a custom span that failed: the error's name and message (redacted later). */
|
|
116
|
+
const failure = (error) => {
|
|
117
|
+
const e = error;
|
|
118
|
+
return {
|
|
119
|
+
type: typeof e?.name === "string" && e.name ? e.name : "error",
|
|
120
|
+
message: typeof e?.message === "string" && e.message ? e.message : void 0
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
export { failure as a, parseTraceparent as c, traceparentSampled as d, withMappedKeys as f, cleanAttributes as i, randomHex as l, appAttributes as n, isSampled as o, around as r, keyValues as s, VERSION as t, registeredGlobalTrace as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,97 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webmcp-agents/telemetry",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Send WebMCP agent runs to any OpenTelemetry backend: model steps from the chat handler, runs and tool calls from the browser through a relay, with standard OTEL_* env, no vendor code and redaction on by default.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"@webmcp-agents/source": "./src/index.ts",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./server": {
|
|
19
|
+
"@webmcp-agents/source": "./src/server/index.ts",
|
|
20
|
+
"types": "./dist/server.d.ts",
|
|
21
|
+
"import": "./dist/server.js"
|
|
22
|
+
},
|
|
23
|
+
"./react": {
|
|
24
|
+
"@webmcp-agents/source": "./src/react.tsx",
|
|
25
|
+
"types": "./dist/react.d.ts",
|
|
26
|
+
"import": "./dist/react.js"
|
|
27
|
+
},
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@ai-sdk/otel": ">=1",
|
|
32
|
+
"@opentelemetry/api": ">=1.9.0 <2",
|
|
33
|
+
"@opentelemetry/exporter-trace-otlp-grpc": ">=0.200",
|
|
34
|
+
"@opentelemetry/exporter-trace-otlp-proto": ">=0.200",
|
|
35
|
+
"@opentelemetry/sdk-trace": ">=2.9",
|
|
36
|
+
"react": ">=18"
|
|
37
|
+
},
|
|
38
|
+
"peerDependenciesMeta": {
|
|
39
|
+
"@ai-sdk/otel": {
|
|
40
|
+
"optional": true
|
|
41
|
+
},
|
|
42
|
+
"@opentelemetry/api": {
|
|
43
|
+
"optional": true
|
|
44
|
+
},
|
|
45
|
+
"@opentelemetry/exporter-trace-otlp-grpc": {
|
|
46
|
+
"optional": true
|
|
47
|
+
},
|
|
48
|
+
"@opentelemetry/exporter-trace-otlp-proto": {
|
|
49
|
+
"optional": true
|
|
50
|
+
},
|
|
51
|
+
"@opentelemetry/sdk-trace": {
|
|
52
|
+
"optional": true
|
|
53
|
+
},
|
|
54
|
+
"react": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@webmcp-agents/chat": "^0.5.0",
|
|
60
|
+
"@webmcp-agents/core": "^0.5.0"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@ai-sdk/otel": "1.0.163",
|
|
64
|
+
"@opentelemetry/api": "1.10.0",
|
|
65
|
+
"@opentelemetry/exporter-trace-otlp-grpc": "0.301.2",
|
|
66
|
+
"@opentelemetry/exporter-trace-otlp-proto": "0.301.2",
|
|
67
|
+
"@opentelemetry/sdk-trace": "3.1.1",
|
|
68
|
+
"@types/node": "^24.19.2",
|
|
69
|
+
"@types/react": "^19.3.0",
|
|
70
|
+
"@types/react-dom": "^19.3.0",
|
|
71
|
+
"@vitejs/plugin-react": "^6.2.1",
|
|
72
|
+
"ai": "^7.0.163",
|
|
73
|
+
"react": "^19.3.1",
|
|
74
|
+
"react-dom": "^19.3.1",
|
|
75
|
+
"tsdown": "^0.23.7",
|
|
76
|
+
"vite": "^8.3.6",
|
|
77
|
+
"vitest-browser-react": "^2.3.0",
|
|
78
|
+
"@webmcp-agents/devtools": "^0.5.0"
|
|
79
|
+
},
|
|
80
|
+
"repository": {
|
|
81
|
+
"type": "git",
|
|
82
|
+
"url": "git+https://github.com/webmcp-agents/webmcp-agents.git",
|
|
83
|
+
"directory": "packages/telemetry"
|
|
84
|
+
},
|
|
85
|
+
"homepage": "https://webmcp-agents.dev",
|
|
86
|
+
"publishConfig": {
|
|
87
|
+
"access": "public"
|
|
88
|
+
},
|
|
89
|
+
"scripts": {
|
|
90
|
+
"build": "tsdown",
|
|
91
|
+
"dev": "tsdown --watch",
|
|
92
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
93
|
+
"test": "vitest run",
|
|
94
|
+
"size": "node scripts/size.mjs",
|
|
95
|
+
"check-exports": "publint && attw --pack . --profile esm-only"
|
|
96
|
+
}
|
|
6
97
|
}
|