@watchtowerobservability/node 0.0.0-stage → 0.3.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/LICENSE +202 -0
- package/README.md +304 -2
- package/dist/cjs/appsec.d.ts +95 -0
- package/dist/cjs/appsec.js +206 -0
- package/dist/cjs/buffer.d.ts +49 -0
- package/dist/cjs/buffer.js +134 -0
- package/dist/cjs/errors.d.ts +17 -0
- package/dist/cjs/errors.js +55 -0
- package/dist/cjs/index.d.ts +123 -0
- package/dist/cjs/index.js +315 -0
- package/dist/cjs/integrations/amqp.d.ts +13 -0
- package/dist/cjs/integrations/amqp.js +96 -0
- package/dist/cjs/integrations/azure-functions.d.ts +18 -0
- package/dist/cjs/integrations/azure-functions.js +97 -0
- package/dist/cjs/integrations/bullmq.d.ts +19 -0
- package/dist/cjs/integrations/bullmq.js +83 -0
- package/dist/cjs/integrations/cache.d.ts +13 -0
- package/dist/cjs/integrations/cache.js +51 -0
- package/dist/cjs/integrations/db.d.ts +62 -0
- package/dist/cjs/integrations/db.js +288 -0
- package/dist/cjs/integrations/duckdb.d.ts +22 -0
- package/dist/cjs/integrations/duckdb.js +66 -0
- package/dist/cjs/integrations/express.d.ts +25 -0
- package/dist/cjs/integrations/express.js +58 -0
- package/dist/cjs/integrations/fastify.d.ts +6 -0
- package/dist/cjs/integrations/fastify.js +44 -0
- package/dist/cjs/integrations/genai-agents.d.ts +36 -0
- package/dist/cjs/integrations/genai-agents.js +124 -0
- package/dist/cjs/integrations/genai-stream.d.ts +19 -0
- package/dist/cjs/integrations/genai-stream.js +135 -0
- package/dist/cjs/integrations/genai.d.ts +18 -0
- package/dist/cjs/integrations/genai.js +150 -0
- package/dist/cjs/integrations/graphile-worker.d.ts +22 -0
- package/dist/cjs/integrations/graphile-worker.js +50 -0
- package/dist/cjs/integrations/koa.d.ts +19 -0
- package/dist/cjs/integrations/koa.js +34 -0
- package/dist/cjs/integrations/lambda.d.ts +11 -0
- package/dist/cjs/integrations/lambda.js +68 -0
- package/dist/cjs/integrations/level.d.ts +18 -0
- package/dist/cjs/integrations/level.js +71 -0
- package/dist/cjs/integrations/lmdb.d.ts +26 -0
- package/dist/cjs/integrations/lmdb.js +78 -0
- package/dist/cjs/integrations/logs.d.ts +10 -0
- package/dist/cjs/integrations/logs.js +77 -0
- package/dist/cjs/integrations/messaging.d.ts +17 -0
- package/dist/cjs/integrations/messaging.js +73 -0
- package/dist/cjs/integrations/mqtt.d.ts +7 -0
- package/dist/cjs/integrations/mqtt.js +41 -0
- package/dist/cjs/integrations/nats.d.ts +17 -0
- package/dist/cjs/integrations/nats.js +73 -0
- package/dist/cjs/integrations/nestjs.d.ts +16 -0
- package/dist/cjs/integrations/nestjs.js +30 -0
- package/dist/cjs/integrations/next.d.ts +11 -0
- package/dist/cjs/integrations/next.js +59 -0
- package/dist/cjs/integrations/orm.d.ts +47 -0
- package/dist/cjs/integrations/orm.js +93 -0
- package/dist/cjs/integrations/pg-boss.d.ts +8 -0
- package/dist/cjs/integrations/pg-boss.js +63 -0
- package/dist/cjs/integrations/pools.d.ts +41 -0
- package/dist/cjs/integrations/pools.js +94 -0
- package/dist/cjs/integrations/postgres.d.ts +12 -0
- package/dist/cjs/integrations/postgres.js +92 -0
- package/dist/cjs/integrations/prisma.d.ts +3 -0
- package/dist/cjs/integrations/prisma.js +33 -0
- package/dist/cjs/integrations/sqlite.d.ts +36 -0
- package/dist/cjs/integrations/sqlite.js +125 -0
- package/dist/cjs/integrations/util.d.ts +9 -0
- package/dist/cjs/integrations/util.js +37 -0
- package/dist/cjs/jest.d.ts +21 -0
- package/dist/cjs/jest.js +45 -0
- package/dist/cjs/load.d.ts +1 -0
- package/dist/cjs/load.js +11 -0
- package/dist/cjs/metrics.d.ts +10 -0
- package/dist/cjs/metrics.js +81 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/payload.d.ts +42 -0
- package/dist/cjs/payload.js +145 -0
- package/dist/cjs/pprof.d.ts +6 -0
- package/dist/cjs/pprof.js +88 -0
- package/dist/cjs/probes.d.ts +97 -0
- package/dist/cjs/probes.js +422 -0
- package/dist/cjs/profiling.d.ts +84 -0
- package/dist/cjs/profiling.js +263 -0
- package/dist/cjs/reporter-core.d.ts +32 -0
- package/dist/cjs/reporter-core.js +102 -0
- package/dist/cjs/sql.d.ts +19 -0
- package/dist/cjs/sql.js +332 -0
- package/dist/cjs/test-plan.d.ts +102 -0
- package/dist/cjs/test-plan.js +352 -0
- package/dist/cjs/vitest.d.ts +27 -0
- package/dist/cjs/vitest.js +52 -0
- package/dist/esm/appsec.d.ts +95 -0
- package/dist/esm/appsec.js +193 -0
- package/dist/esm/buffer.d.ts +49 -0
- package/dist/esm/buffer.js +92 -0
- package/dist/esm/errors.d.ts +17 -0
- package/dist/esm/errors.js +50 -0
- package/dist/esm/index.d.ts +123 -0
- package/dist/esm/index.js +264 -0
- package/dist/esm/integrations/amqp.d.ts +13 -0
- package/dist/esm/integrations/amqp.js +92 -0
- package/dist/esm/integrations/azure-functions.d.ts +18 -0
- package/dist/esm/integrations/azure-functions.js +60 -0
- package/dist/esm/integrations/bullmq.d.ts +19 -0
- package/dist/esm/integrations/bullmq.js +79 -0
- package/dist/esm/integrations/cache.d.ts +13 -0
- package/dist/esm/integrations/cache.js +46 -0
- package/dist/esm/integrations/db.d.ts +62 -0
- package/dist/esm/integrations/db.js +239 -0
- package/dist/esm/integrations/duckdb.d.ts +22 -0
- package/dist/esm/integrations/duckdb.js +29 -0
- package/dist/esm/integrations/express.d.ts +25 -0
- package/dist/esm/integrations/express.js +54 -0
- package/dist/esm/integrations/fastify.d.ts +6 -0
- package/dist/esm/integrations/fastify.js +41 -0
- package/dist/esm/integrations/genai-agents.d.ts +36 -0
- package/dist/esm/integrations/genai-agents.js +118 -0
- package/dist/esm/integrations/genai-stream.d.ts +19 -0
- package/dist/esm/integrations/genai-stream.js +130 -0
- package/dist/esm/integrations/genai.d.ts +18 -0
- package/dist/esm/integrations/genai.js +138 -0
- package/dist/esm/integrations/graphile-worker.d.ts +22 -0
- package/dist/esm/integrations/graphile-worker.js +46 -0
- package/dist/esm/integrations/koa.d.ts +19 -0
- package/dist/esm/integrations/koa.js +31 -0
- package/dist/esm/integrations/lambda.d.ts +11 -0
- package/dist/esm/integrations/lambda.js +32 -0
- package/dist/esm/integrations/level.d.ts +18 -0
- package/dist/esm/integrations/level.js +35 -0
- package/dist/esm/integrations/lmdb.d.ts +26 -0
- package/dist/esm/integrations/lmdb.js +42 -0
- package/dist/esm/integrations/logs.d.ts +10 -0
- package/dist/esm/integrations/logs.js +40 -0
- package/dist/esm/integrations/messaging.d.ts +17 -0
- package/dist/esm/integrations/messaging.js +64 -0
- package/dist/esm/integrations/mqtt.d.ts +7 -0
- package/dist/esm/integrations/mqtt.js +38 -0
- package/dist/esm/integrations/nats.d.ts +17 -0
- package/dist/esm/integrations/nats.js +70 -0
- package/dist/esm/integrations/nestjs.d.ts +16 -0
- package/dist/esm/integrations/nestjs.js +26 -0
- package/dist/esm/integrations/next.d.ts +11 -0
- package/dist/esm/integrations/next.js +22 -0
- package/dist/esm/integrations/orm.d.ts +47 -0
- package/dist/esm/integrations/orm.js +86 -0
- package/dist/esm/integrations/pg-boss.d.ts +8 -0
- package/dist/esm/integrations/pg-boss.js +60 -0
- package/dist/esm/integrations/pools.d.ts +41 -0
- package/dist/esm/integrations/pools.js +89 -0
- package/dist/esm/integrations/postgres.d.ts +12 -0
- package/dist/esm/integrations/postgres.js +55 -0
- package/dist/esm/integrations/prisma.d.ts +3 -0
- package/dist/esm/integrations/prisma.js +30 -0
- package/dist/esm/integrations/sqlite.d.ts +36 -0
- package/dist/esm/integrations/sqlite.js +84 -0
- package/dist/esm/integrations/util.d.ts +9 -0
- package/dist/esm/integrations/util.js +32 -0
- package/dist/esm/jest.d.ts +21 -0
- package/dist/esm/jest.js +41 -0
- package/dist/esm/load.d.ts +1 -0
- package/dist/esm/load.js +5 -0
- package/dist/esm/metrics.d.ts +10 -0
- package/dist/esm/metrics.js +44 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/payload.d.ts +42 -0
- package/dist/esm/payload.js +131 -0
- package/dist/esm/pprof.d.ts +6 -0
- package/dist/esm/pprof.js +85 -0
- package/dist/esm/probes.d.ts +97 -0
- package/dist/esm/probes.js +412 -0
- package/dist/esm/profiling.d.ts +84 -0
- package/dist/esm/profiling.js +251 -0
- package/dist/esm/reporter-core.d.ts +32 -0
- package/dist/esm/reporter-core.js +63 -0
- package/dist/esm/sql.d.ts +19 -0
- package/dist/esm/sql.js +324 -0
- package/dist/esm/test-plan.d.ts +102 -0
- package/dist/esm/test-plan.js +336 -0
- package/dist/esm/vitest.d.ts +27 -0
- package/dist/esm/vitest.js +48 -0
- package/package.json +379 -4
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Response, chunk and stream reading shared by every GenAI wrapper: tokens, finish reasons and tool calls
|
|
2
|
+
// from OpenAI, Anthropic, Mistral (camelCase) and Ollama shapes. Tool calls become `gen_ai.tool.call` span
|
|
3
|
+
// events with the tool name and call id only; prompt, completion and argument text is never recorded.
|
|
4
|
+
import { SpanStatusCode } from '@opentelemetry/api';
|
|
5
|
+
const obj = (v) => (v && typeof v === 'object' ? v : undefined);
|
|
6
|
+
const num = (...v) => v.find((x) => typeof x === 'number');
|
|
7
|
+
const str = (...v) => v.find((x) => typeof x === 'string' && x !== '');
|
|
8
|
+
const list = (v) => (Array.isArray(v) ? v : []);
|
|
9
|
+
function toolsOf(calls, out) {
|
|
10
|
+
for (const tc of list(calls)) {
|
|
11
|
+
const t = obj(tc);
|
|
12
|
+
const name = str(obj(t?.function)?.name);
|
|
13
|
+
if (name)
|
|
14
|
+
out.push([name, str(t?.id)]);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Reads a full response, a stream chunk or a stream event (`{ data }` for Mistral, typed events for Anthropic). */
|
|
18
|
+
export function extract(raw) {
|
|
19
|
+
let o = obj(raw) ?? {};
|
|
20
|
+
if (o.choices === undefined && o.type === undefined && obj(o.data))
|
|
21
|
+
o = obj(o.data);
|
|
22
|
+
const start = o.type === 'message_start' ? obj(o.message) : undefined;
|
|
23
|
+
const u = obj(o.usage) ?? obj(start?.usage) ?? {};
|
|
24
|
+
const g = obj(o.usageMetadata) ?? {};
|
|
25
|
+
const x = {
|
|
26
|
+
id: str(o.id, start?.id, o.responseId),
|
|
27
|
+
model: str(o.model, start?.model, o.modelVersion),
|
|
28
|
+
input: num(u.input_tokens, u.prompt_tokens, u.promptTokens, u.inputTokens, o.prompt_eval_count, g.promptTokenCount),
|
|
29
|
+
output: num(u.output_tokens, u.completion_tokens, u.completionTokens, u.outputTokens, o.eval_count, g.candidatesTokenCount),
|
|
30
|
+
reasons: [],
|
|
31
|
+
tools: [],
|
|
32
|
+
};
|
|
33
|
+
for (const ch of list(o.choices)) {
|
|
34
|
+
const c = obj(ch);
|
|
35
|
+
const r = str(c?.finish_reason, c?.finishReason);
|
|
36
|
+
if (r)
|
|
37
|
+
x.reasons.push(r);
|
|
38
|
+
const m = obj(c?.message) ?? obj(c?.delta);
|
|
39
|
+
toolsOf(m?.tool_calls ?? m?.toolCalls, x.tools);
|
|
40
|
+
}
|
|
41
|
+
for (const r of [o.stop_reason, obj(o.delta)?.stop_reason, o.done_reason, o.stopReason])
|
|
42
|
+
if (str(r))
|
|
43
|
+
x.reasons.push(r);
|
|
44
|
+
for (const b of list(o.content))
|
|
45
|
+
if (obj(b)?.type === 'tool_use')
|
|
46
|
+
x.tools.push([String(obj(b).name), str(obj(b).id)]);
|
|
47
|
+
const cb = obj(o.content_block);
|
|
48
|
+
if (cb?.type === 'tool_use')
|
|
49
|
+
x.tools.push([String(cb.name), str(cb.id)]);
|
|
50
|
+
if (o.choices === undefined)
|
|
51
|
+
toolsOf(obj(o.message)?.tool_calls, x.tools);
|
|
52
|
+
return x;
|
|
53
|
+
}
|
|
54
|
+
/** Sets what `x` holds; `seen` keeps a stream's tool calls from repeating. */
|
|
55
|
+
export function applyExtract(span, x, seen = new Set()) {
|
|
56
|
+
if (x.id)
|
|
57
|
+
span.setAttribute('gen_ai.response.id', x.id);
|
|
58
|
+
if (x.model)
|
|
59
|
+
span.setAttribute('gen_ai.response.model', x.model);
|
|
60
|
+
if (x.input != null)
|
|
61
|
+
span.setAttribute('gen_ai.usage.input_tokens', x.input);
|
|
62
|
+
if (x.output != null)
|
|
63
|
+
span.setAttribute('gen_ai.usage.output_tokens', x.output);
|
|
64
|
+
if (x.reasons.length)
|
|
65
|
+
span.setAttribute('gen_ai.response.finish_reasons', x.reasons);
|
|
66
|
+
for (const [name, id] of x.tools) {
|
|
67
|
+
const k = `${name}\u0000${id ?? ''}`;
|
|
68
|
+
if (seen.has(k))
|
|
69
|
+
continue;
|
|
70
|
+
seen.add(k);
|
|
71
|
+
span.addEvent('gen_ai.tool.call', { 'gen_ai.tool.name': name, ...(id ? { 'gen_ai.tool.call.id': id } : {}) });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Hooks the async iterator of a stream object in place (the caller keeps the SDK's own object): the span records
|
|
76
|
+
* time to first chunk, usage, finish reasons and tool calls, and ends when the stream ends, fails or is closed.
|
|
77
|
+
* False when `res` is not async iterable.
|
|
78
|
+
*/
|
|
79
|
+
export function tapStream(span, res, startedMs) {
|
|
80
|
+
const s = obj(res);
|
|
81
|
+
const orig = s?.[Symbol.asyncIterator];
|
|
82
|
+
if (!s || typeof orig !== 'function')
|
|
83
|
+
return false;
|
|
84
|
+
let first = true, done = false;
|
|
85
|
+
const reasons = [], seen = new Set();
|
|
86
|
+
const end = (e) => {
|
|
87
|
+
if (done)
|
|
88
|
+
return;
|
|
89
|
+
done = true;
|
|
90
|
+
if (e !== undefined) {
|
|
91
|
+
span.recordException(e);
|
|
92
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: e?.message });
|
|
93
|
+
}
|
|
94
|
+
span.end();
|
|
95
|
+
};
|
|
96
|
+
s[Symbol.asyncIterator] = function () {
|
|
97
|
+
const inner = orig.call(s);
|
|
98
|
+
return {
|
|
99
|
+
async next() {
|
|
100
|
+
let r;
|
|
101
|
+
try {
|
|
102
|
+
r = await inner.next();
|
|
103
|
+
}
|
|
104
|
+
catch (e) {
|
|
105
|
+
end(e);
|
|
106
|
+
throw e;
|
|
107
|
+
}
|
|
108
|
+
if (r.done) {
|
|
109
|
+
end();
|
|
110
|
+
return r;
|
|
111
|
+
}
|
|
112
|
+
if (first) {
|
|
113
|
+
first = false;
|
|
114
|
+
span.setAttribute('gen_ai.response.time_to_first_chunk', (Date.now() - startedMs) / 1000);
|
|
115
|
+
}
|
|
116
|
+
const x = extract(r.value);
|
|
117
|
+
for (const q of x.reasons)
|
|
118
|
+
if (!reasons.includes(q))
|
|
119
|
+
reasons.push(q);
|
|
120
|
+
applyExtract(span, { ...x, reasons }, seen);
|
|
121
|
+
return r;
|
|
122
|
+
},
|
|
123
|
+
async return(v) {
|
|
124
|
+
end();
|
|
125
|
+
return inner.return ? inner.return(v) : { done: true, value: v };
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { instrumentLlamaIndex, instrumentOpenAIAgents } from './genai-agents.js';
|
|
2
|
+
/**
|
|
3
|
+
* instrumentOpenAI(new OpenAI()): chat.completions.create and responses.create. Returns the client.
|
|
4
|
+
* For an OpenAI-compatible gateway pass its name: instrumentOpenAI(new OpenAI({ baseURL: litellmUrl }), 'litellm').
|
|
5
|
+
*/
|
|
6
|
+
export declare function instrumentOpenAI<T>(client: T, system?: string): T;
|
|
7
|
+
/** instrumentBedrock(new BedrockRuntimeClient()): `send` of Converse and InvokeModel commands; others pass through untraced. */
|
|
8
|
+
export declare function instrumentBedrock<T>(client: T): T;
|
|
9
|
+
/** instrumentGoogleGenAI(new GoogleGenAI({...})): models.generateContent (Gemini API or Vertex AI). */
|
|
10
|
+
export declare function instrumentGoogleGenAI<T>(client: T): T;
|
|
11
|
+
/** instrumentAnthropic(new Anthropic()): messages.create. Returns the client. */
|
|
12
|
+
export declare function instrumentAnthropic<T>(client: T): T;
|
|
13
|
+
/** instrumentMistral(new Mistral()): chat.complete and chat.stream (the stream span ends with the stream). */
|
|
14
|
+
export declare function instrumentMistral<T>(client: T): T;
|
|
15
|
+
/** instrumentOllama(new Ollama()) or instrumentOllama(ollama): chat and generate, with `stream: true` too. */
|
|
16
|
+
export declare function instrumentOllama<T>(client: T): T;
|
|
17
|
+
/** generateText({ ..., experimental_telemetry: vercelAiTelemetry() }); inputs/outputs off unless overridden. */
|
|
18
|
+
export declare function vercelAiTelemetry(o?: Record<string, unknown>): Record<string, unknown>;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// GenAI spans (OTel GenAI semconv) for the openai, anthropic, AWS Bedrock, Google GenAI, Mistral and Ollama SDKs (and
|
|
2
|
+
// LiteLLM through its OpenAI-compatible proxy), LlamaIndex and the OpenAI Agents SDK (genai-agents.ts), and a
|
|
3
|
+
// telemetry option for the Vercel AI SDK. Streams record time to first chunk, usage and tool calls (genai-stream.ts).
|
|
4
|
+
// Prompt and completion text is never recorded.
|
|
5
|
+
import { context, SpanKind, SpanStatusCode, trace } from '@opentelemetry/api';
|
|
6
|
+
import { applyExtract, extract, tapStream } from './genai-stream.js';
|
|
7
|
+
export { instrumentLlamaIndex, instrumentOpenAIAgents } from './genai-agents.js';
|
|
8
|
+
const DONE = Symbol('wt.instrumented');
|
|
9
|
+
const plain = (b) => (b || {});
|
|
10
|
+
function wrap(target, method, system, req = plain) {
|
|
11
|
+
if (!target || typeof target[method] !== 'function' || target[method][DONE])
|
|
12
|
+
return;
|
|
13
|
+
const orig = target[method];
|
|
14
|
+
const wrapped = function (body, ...rest) {
|
|
15
|
+
const b = req(body);
|
|
16
|
+
const started = Date.now();
|
|
17
|
+
const span = trace.getTracer('watchtower').startSpan(`chat ${b.model || ''}`.trim(), {
|
|
18
|
+
kind: SpanKind.CLIENT,
|
|
19
|
+
attributes: {
|
|
20
|
+
'gen_ai.operation.name': 'chat',
|
|
21
|
+
'gen_ai.system': system,
|
|
22
|
+
...(b.model ? { 'gen_ai.request.model': b.model } : {}),
|
|
23
|
+
...(b.max_tokens != null ? { 'gen_ai.request.max_tokens': b.max_tokens } : {}),
|
|
24
|
+
...(b.temperature != null ? { 'gen_ai.request.temperature': b.temperature } : {}),
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
const p = context.with(trace.setSpan(context.active(), span), () => orig.call(this, body, ...rest));
|
|
28
|
+
// side branch only: the caller keeps the SDK's own promise (APIPromise helpers stay intact)
|
|
29
|
+
Promise.resolve(p).then((res) => {
|
|
30
|
+
// a stream keeps the span open until it ends (this runs before the caller's own continuation)
|
|
31
|
+
if (b.stream && tapStream(span, res, started))
|
|
32
|
+
return;
|
|
33
|
+
// the shared reader adds camelCase usage (Mistral), Ollama counts and tool calls
|
|
34
|
+
if (!b.stream && res) {
|
|
35
|
+
record(span, res);
|
|
36
|
+
applyExtract(span, extract(res));
|
|
37
|
+
}
|
|
38
|
+
span.end();
|
|
39
|
+
}, (e) => {
|
|
40
|
+
span.recordException(e);
|
|
41
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: e?.message });
|
|
42
|
+
span.end();
|
|
43
|
+
});
|
|
44
|
+
return p;
|
|
45
|
+
};
|
|
46
|
+
wrapped[DONE] = true;
|
|
47
|
+
target[method] = wrapped;
|
|
48
|
+
}
|
|
49
|
+
function record(span, res) {
|
|
50
|
+
const u = res.usage || {};
|
|
51
|
+
const g = res.usageMetadata || {};
|
|
52
|
+
const input = u.prompt_tokens ?? u.input_tokens ?? u.inputTokens ?? g.promptTokenCount;
|
|
53
|
+
const output = u.completion_tokens ?? u.output_tokens ?? u.outputTokens ?? g.candidatesTokenCount;
|
|
54
|
+
const one = res.stop_reason ?? res.stopReason;
|
|
55
|
+
const finish = Array.isArray(res.choices) ? res.choices.map((c) => c.finish_reason)
|
|
56
|
+
: Array.isArray(res.candidates) ? res.candidates.map((c) => c.finishReason) : one ? [one] : [];
|
|
57
|
+
const id = res.id ?? res.responseId ?? res.$metadata?.requestId;
|
|
58
|
+
const model = res.model ?? res.modelVersion;
|
|
59
|
+
if (id)
|
|
60
|
+
span.setAttribute('gen_ai.response.id', id);
|
|
61
|
+
if (model)
|
|
62
|
+
span.setAttribute('gen_ai.response.model', model);
|
|
63
|
+
if (finish.length)
|
|
64
|
+
span.setAttribute('gen_ai.response.finish_reasons', finish.filter((f) => Boolean(f)));
|
|
65
|
+
if (input != null)
|
|
66
|
+
span.setAttribute('gen_ai.usage.input_tokens', input);
|
|
67
|
+
if (output != null)
|
|
68
|
+
span.setAttribute('gen_ai.usage.output_tokens', output);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* instrumentOpenAI(new OpenAI()): chat.completions.create and responses.create. Returns the client.
|
|
72
|
+
* For an OpenAI-compatible gateway pass its name: instrumentOpenAI(new OpenAI({ baseURL: litellmUrl }), 'litellm').
|
|
73
|
+
*/
|
|
74
|
+
export function instrumentOpenAI(client, system = 'openai') {
|
|
75
|
+
const c = client;
|
|
76
|
+
wrap(c.chat && c.chat.completions, 'create', system);
|
|
77
|
+
wrap(c.responses, 'create', system);
|
|
78
|
+
return client;
|
|
79
|
+
}
|
|
80
|
+
/** instrumentBedrock(new BedrockRuntimeClient()): `send` of Converse and InvokeModel commands; others pass through untraced. */
|
|
81
|
+
export function instrumentBedrock(client) {
|
|
82
|
+
const c = client;
|
|
83
|
+
const send = c.send;
|
|
84
|
+
if (typeof send !== 'function' || send[DONE])
|
|
85
|
+
return client;
|
|
86
|
+
const traced = { send };
|
|
87
|
+
wrap(traced, 'send', 'aws.bedrock', (cmd) => {
|
|
88
|
+
const x = (cmd || {});
|
|
89
|
+
const name = (x.constructor && x.constructor.name) || '';
|
|
90
|
+
return { model: x.input?.modelId, max_tokens: x.input?.inferenceConfig?.maxTokens, temperature: x.input?.inferenceConfig?.temperature, stream: name.includes('Stream') };
|
|
91
|
+
});
|
|
92
|
+
const wrapped = function (cmd, ...rest) {
|
|
93
|
+
const name = (cmd?.constructor?.name) || '';
|
|
94
|
+
return /^(Converse|InvokeModel)/.test(name) ? traced.send.call(this, cmd, ...rest) : send.call(this, cmd, ...rest);
|
|
95
|
+
};
|
|
96
|
+
wrapped[DONE] = true;
|
|
97
|
+
c.send = wrapped;
|
|
98
|
+
return client;
|
|
99
|
+
}
|
|
100
|
+
/** instrumentGoogleGenAI(new GoogleGenAI({...})): models.generateContent (Gemini API or Vertex AI). */
|
|
101
|
+
export function instrumentGoogleGenAI(client) {
|
|
102
|
+
const c = client;
|
|
103
|
+
wrap(c.models, 'generateContent', c.vertexai ? 'vertex_ai' : 'gemini', (b) => {
|
|
104
|
+
const x = (b || {});
|
|
105
|
+
return { model: x.model, max_tokens: x.config?.maxOutputTokens, temperature: x.config?.temperature };
|
|
106
|
+
});
|
|
107
|
+
return client;
|
|
108
|
+
}
|
|
109
|
+
/** instrumentAnthropic(new Anthropic()): messages.create. Returns the client. */
|
|
110
|
+
export function instrumentAnthropic(client) {
|
|
111
|
+
wrap(client.messages, 'create', 'anthropic');
|
|
112
|
+
return client;
|
|
113
|
+
}
|
|
114
|
+
/** instrumentMistral(new Mistral()): chat.complete and chat.stream (the stream span ends with the stream). */
|
|
115
|
+
export function instrumentMistral(client) {
|
|
116
|
+
const c = client.chat;
|
|
117
|
+
wrap(c, 'complete', 'mistral_ai', mistralReq);
|
|
118
|
+
wrap(c, 'stream', 'mistral_ai', (b) => ({ ...mistralReq(b), stream: true }));
|
|
119
|
+
return client;
|
|
120
|
+
}
|
|
121
|
+
const mistralReq = (b) => {
|
|
122
|
+
const x = (b || {});
|
|
123
|
+
return { model: x.model, max_tokens: x.maxTokens, temperature: x.temperature };
|
|
124
|
+
};
|
|
125
|
+
/** instrumentOllama(new Ollama()) or instrumentOllama(ollama): chat and generate, with `stream: true` too. */
|
|
126
|
+
export function instrumentOllama(client) {
|
|
127
|
+
const ollamaReq = (b) => {
|
|
128
|
+
const x = (b || {});
|
|
129
|
+
return { model: x.model, stream: x.stream, max_tokens: x.options?.num_predict, temperature: x.options?.temperature };
|
|
130
|
+
};
|
|
131
|
+
wrap(client, 'chat', 'ollama', ollamaReq);
|
|
132
|
+
wrap(client, 'generate', 'ollama', ollamaReq);
|
|
133
|
+
return client;
|
|
134
|
+
}
|
|
135
|
+
/** generateText({ ..., experimental_telemetry: vercelAiTelemetry() }); inputs/outputs off unless overridden. */
|
|
136
|
+
export function vercelAiTelemetry(o) {
|
|
137
|
+
return { isEnabled: true, recordInputs: false, recordOutputs: false, tracer: trace.getTracer('watchtower'), ...o };
|
|
138
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface Helpers {
|
|
2
|
+
job?: {
|
|
3
|
+
id?: string | number;
|
|
4
|
+
attempts?: number;
|
|
5
|
+
queue_name?: string | null;
|
|
6
|
+
};
|
|
7
|
+
addJob?: unknown;
|
|
8
|
+
}
|
|
9
|
+
type Task = (payload: unknown, helpers: Helpers) => unknown;
|
|
10
|
+
/**
|
|
11
|
+
* `instrumentAddJob(utils)` (from `makeWorkerUtils`, or the runner from `run`): a PRODUCER span on
|
|
12
|
+
* `addJob` with the trace context in the payload under `_wt` (object payloads only).
|
|
13
|
+
* shortcut: the module-level `quickAddJob` cannot be patched under ESM; use `utils.addJob`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function instrumentAddJob(utilsOrRunner: object): void;
|
|
16
|
+
/**
|
|
17
|
+
* `run({ taskList: wrapTaskList({ send_email: async (payload, helpers) => ... }) })`: each task runs
|
|
18
|
+
* in a CONSUMER span, child of and linked to the `addJob` that queued it; `_wt` is removed from the
|
|
19
|
+
* payload first, and `helpers.addJob` is instrumented so follow-up jobs stay in the trace.
|
|
20
|
+
*/
|
|
21
|
+
export declare function wrapTaskList<T extends Record<string, Task>>(tasks: T): T;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { trace } from '@opentelemetry/api';
|
|
2
|
+
import { patch } from './db.js';
|
|
3
|
+
import { consume, injectData, produce, takeContext } from './messaging.js';
|
|
4
|
+
const attrs = (task, type, op) => ({
|
|
5
|
+
'messaging.system': 'graphile-worker',
|
|
6
|
+
'messaging.destination.name': task,
|
|
7
|
+
'messaging.operation.type': type,
|
|
8
|
+
'messaging.operation.name': op,
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* `instrumentAddJob(utils)` (from `makeWorkerUtils`, or the runner from `run`): a PRODUCER span on
|
|
12
|
+
* `addJob` with the trace context in the payload under `_wt` (object payloads only).
|
|
13
|
+
* shortcut: the module-level `quickAddJob` cannot be patched under ESM; use `utils.addJob`.
|
|
14
|
+
*/
|
|
15
|
+
export function instrumentAddJob(utilsOrRunner) {
|
|
16
|
+
patch(utilsOrRunner, 'addJob', (orig) => function (identifier, payload, ...rest) {
|
|
17
|
+
return produce(`send ${identifier}`, attrs(identifier, 'send', 'addJob'), () => {
|
|
18
|
+
const span = trace.getActiveSpan();
|
|
19
|
+
return orig.call(this, identifier, injectData(payload ?? {}), ...rest).then((job) => {
|
|
20
|
+
if (job?.id !== undefined)
|
|
21
|
+
span?.setAttribute('messaging.message.id', String(job.id));
|
|
22
|
+
return job;
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* `run({ taskList: wrapTaskList({ send_email: async (payload, helpers) => ... }) })`: each task runs
|
|
29
|
+
* in a CONSUMER span, child of and linked to the `addJob` that queued it; `_wt` is removed from the
|
|
30
|
+
* payload first, and `helpers.addJob` is instrumented so follow-up jobs stay in the trace.
|
|
31
|
+
*/
|
|
32
|
+
export function wrapTaskList(tasks) {
|
|
33
|
+
return Object.fromEntries(Object.entries(tasks).map(([name, fn]) => [name, (payload, helpers) => {
|
|
34
|
+
const carrier = takeContext(payload);
|
|
35
|
+
const a = attrs(name, 'process', 'task');
|
|
36
|
+
if (helpers?.job?.id !== undefined)
|
|
37
|
+
a['messaging.message.id'] = String(helpers.job.id);
|
|
38
|
+
if (helpers?.job?.attempts !== undefined)
|
|
39
|
+
a['messaging.graphile_worker.job.attempt'] = helpers.job.attempts;
|
|
40
|
+
if (helpers?.job?.queue_name)
|
|
41
|
+
a['messaging.consumer.group.name'] = helpers.job.queue_name;
|
|
42
|
+
if (helpers)
|
|
43
|
+
instrumentAddJob(helpers);
|
|
44
|
+
return consume(`process ${name}`, a, [carrier], () => fn(payload, helpers));
|
|
45
|
+
}]));
|
|
46
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** The slice of a Koa context this integration reads. */
|
|
2
|
+
export interface KoaContext {
|
|
3
|
+
method: string;
|
|
4
|
+
path: string;
|
|
5
|
+
status: number;
|
|
6
|
+
_matchedRoute?: string;
|
|
7
|
+
/** `body` is set by a body parser (koa-bodyparser, koa-body) for payload capture. */
|
|
8
|
+
request?: {
|
|
9
|
+
body?: unknown;
|
|
10
|
+
type?: string;
|
|
11
|
+
};
|
|
12
|
+
/** The response body the handler set (payload capture; streams are skipped). */
|
|
13
|
+
body?: unknown;
|
|
14
|
+
response?: {
|
|
15
|
+
type?: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** app.use(middleware()) first: server span per request, errors captured then rethrown. */
|
|
19
|
+
export declare function middleware(): (ctx: KoaContext, next: () => Promise<unknown>) => Promise<void>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { context } from '@opentelemetry/api';
|
|
2
|
+
import { captureException } from '../errors.js';
|
|
3
|
+
import { capturePayload, captureActive } from '../payload.js';
|
|
4
|
+
import { serverSpan, statusOf } from './util.js';
|
|
5
|
+
/** app.use(middleware()) first: server span per request, errors captured then rethrown. */
|
|
6
|
+
export function middleware() {
|
|
7
|
+
return async (ctx, next) => {
|
|
8
|
+
const s = serverSpan(ctx.method, ctx.path);
|
|
9
|
+
let status;
|
|
10
|
+
try {
|
|
11
|
+
await context.with(s.ctx, next);
|
|
12
|
+
status = ctx.status;
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
status = statusOf(e);
|
|
16
|
+
if (status >= 500)
|
|
17
|
+
context.with(s.ctx, () => captureException(e));
|
|
18
|
+
throw e;
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
if (captureActive()) {
|
|
22
|
+
capturePayload(s.span, {
|
|
23
|
+
route: ctx._matchedRoute || ctx.path,
|
|
24
|
+
reqType: ctx.request?.type, reqBody: ctx.request?.body,
|
|
25
|
+
resType: ctx.response?.type, resBody: ctx.body,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
s.finish(status, ctx._matchedRoute);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** The slice of the Lambda context this wrapper reads. */
|
|
2
|
+
export interface LambdaContextLike {
|
|
3
|
+
functionName?: string;
|
|
4
|
+
awsRequestId?: string;
|
|
5
|
+
invokedFunctionArn?: string;
|
|
6
|
+
}
|
|
7
|
+
/** exports.handler = wrapHandler(async (event, context) => ...). Async handlers only. Flushes before the
|
|
8
|
+
* runtime freezes, otherwise buffered spans are lost. */
|
|
9
|
+
export declare function wrapHandler<E, R, C extends LambdaContextLike = LambdaContextLike>(handler: (event: E, context: C) => Promise<R>, o?: {
|
|
10
|
+
name?: string;
|
|
11
|
+
}): (event: E, context: C) => Promise<R>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { SpanKind, trace } from '@opentelemetry/api';
|
|
2
|
+
import { captureException } from '../errors.js';
|
|
3
|
+
import * as wt from '../index.js';
|
|
4
|
+
/** exports.handler = wrapHandler(async (event, context) => ...). Async handlers only. Flushes before the
|
|
5
|
+
* runtime freezes, otherwise buffered spans are lost. */
|
|
6
|
+
export function wrapHandler(handler, o) {
|
|
7
|
+
const x = o || {};
|
|
8
|
+
return async (event, context) => {
|
|
9
|
+
const c = context || {};
|
|
10
|
+
return trace.getTracer('watchtower').startActiveSpan(x.name || c.functionName || 'lambda', {
|
|
11
|
+
kind: SpanKind.SERVER,
|
|
12
|
+
attributes: {
|
|
13
|
+
'cloud.provider': 'aws',
|
|
14
|
+
'faas.name': c.functionName,
|
|
15
|
+
'faas.invocation_id': c.awsRequestId,
|
|
16
|
+
'cloud.resource_id': c.invokedFunctionArn,
|
|
17
|
+
},
|
|
18
|
+
}, async (span) => {
|
|
19
|
+
try {
|
|
20
|
+
return await handler(event, context);
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
captureException(e, { handled: false, attributes: { 'error.mechanism': 'lambda' } });
|
|
24
|
+
throw e;
|
|
25
|
+
}
|
|
26
|
+
finally {
|
|
27
|
+
span.end();
|
|
28
|
+
await wt.flush().catch(() => { });
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { DbOptions } from './db.js';
|
|
2
|
+
interface LevelDb {
|
|
3
|
+
location?: string;
|
|
4
|
+
sublevel?(...a: unknown[]): object;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* `instrumentLevel(new ClassicLevel(dir))`, or `instrumentLevel(rocksdb(dir), { system: 'rocksdb' })`:
|
|
8
|
+
* a span per `get`/`getMany`/`put`/`del`/`batch`/`clear` (promise or callback form; keys and values
|
|
9
|
+
* are never recorded), on the db and on its sublevels (`db.collection.name`). With `metrics` (default
|
|
10
|
+
* on) and a `location`, `wt.embedded.file.size` reports the size of the store's directory.
|
|
11
|
+
* Returns `stop()` for the gauge.
|
|
12
|
+
* shortcut: iterators and the chained `batch()` builder are not traced.
|
|
13
|
+
*/
|
|
14
|
+
export declare function instrumentLevel(db: LevelDb, opts?: DbOptions & {
|
|
15
|
+
system?: 'leveldb' | 'rocksdb';
|
|
16
|
+
metrics?: boolean;
|
|
17
|
+
}): () => void;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as nodePath from 'node:path';
|
|
2
|
+
import { observeEmbedded, patch, sizeOf, tracedCb } from './db.js';
|
|
3
|
+
const OPS = ['get', 'getMany', 'put', 'del', 'batch', 'clear'];
|
|
4
|
+
/**
|
|
5
|
+
* `instrumentLevel(new ClassicLevel(dir))`, or `instrumentLevel(rocksdb(dir), { system: 'rocksdb' })`:
|
|
6
|
+
* a span per `get`/`getMany`/`put`/`del`/`batch`/`clear` (promise or callback form; keys and values
|
|
7
|
+
* are never recorded), on the db and on its sublevels (`db.collection.name`). With `metrics` (default
|
|
8
|
+
* on) and a `location`, `wt.embedded.file.size` reports the size of the store's directory.
|
|
9
|
+
* Returns `stop()` for the gauge.
|
|
10
|
+
* shortcut: iterators and the chained `batch()` builder are not traced.
|
|
11
|
+
*/
|
|
12
|
+
export function instrumentLevel(db, opts) {
|
|
13
|
+
const system = opts?.system ?? 'leveldb';
|
|
14
|
+
const namespace = db.location ? nodePath.basename(db.location) : undefined;
|
|
15
|
+
const wrap = (d, collection) => {
|
|
16
|
+
for (const m of OPS) {
|
|
17
|
+
patch(d, m, (orig) => function (...a) {
|
|
18
|
+
if (m === 'batch' && a.length === 0)
|
|
19
|
+
return orig.call(this); // chained builder
|
|
20
|
+
return tracedCb({ system, operation: m, collection, namespace }, opts, a, (args) => orig.apply(this, args));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
patch(d, 'sublevel', (orig) => function (...a) {
|
|
24
|
+
const sub = orig.apply(this, a);
|
|
25
|
+
if (sub)
|
|
26
|
+
wrap(sub, typeof a[0] === 'string' ? a[0] : undefined);
|
|
27
|
+
return sub;
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
wrap(db);
|
|
31
|
+
const dir = db.location;
|
|
32
|
+
if (opts?.metrics === false || !dir)
|
|
33
|
+
return () => { };
|
|
34
|
+
return observeEmbedded(system, namespace, ['file.size'], () => ({ 'file.size': sizeOf(dir) }));
|
|
35
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { DbOptions } from './db.js';
|
|
2
|
+
interface LmdbStats {
|
|
3
|
+
pageSize?: number;
|
|
4
|
+
mapSize?: number;
|
|
5
|
+
lastPageNumber?: number;
|
|
6
|
+
entryCount?: number;
|
|
7
|
+
numReaders?: number;
|
|
8
|
+
}
|
|
9
|
+
interface LmdbDb {
|
|
10
|
+
path?: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
getStats?(): LmdbStats;
|
|
13
|
+
openDB?(...a: unknown[]): object;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* `instrumentLmdb(open({ path }))` (lmdb-js): spans for async writes (`put`, `remove`, `ifVersion`,
|
|
17
|
+
* `ifNoExists`, `clearAsync`, `drop`), `getMany` and `transaction` (calls inside a transaction get
|
|
18
|
+
* child spans), on the root and on every `openDB` sub-database (`db.collection.name`). Sync reads
|
|
19
|
+
* (`get`) are memory-mapped and take microseconds, so they get no span. With `metrics` (default on)
|
|
20
|
+
* it also reports `wt.embedded.map.size`, `.map.used`, `.entries`, `.readers` and `.file.size`.
|
|
21
|
+
* Returns `stop()` for the gauges.
|
|
22
|
+
*/
|
|
23
|
+
export declare function instrumentLmdb(db: LmdbDb, opts?: DbOptions & {
|
|
24
|
+
metrics?: boolean;
|
|
25
|
+
}): () => void;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as nodePath from 'node:path';
|
|
2
|
+
import { observeEmbedded, patch, sizeOf, traced } from './db.js';
|
|
3
|
+
const WRITES = ['put', 'remove', 'ifVersion', 'ifNoExists', 'clearAsync', 'drop', 'getMany', 'prefetch'];
|
|
4
|
+
/**
|
|
5
|
+
* `instrumentLmdb(open({ path }))` (lmdb-js): spans for async writes (`put`, `remove`, `ifVersion`,
|
|
6
|
+
* `ifNoExists`, `clearAsync`, `drop`), `getMany` and `transaction` (calls inside a transaction get
|
|
7
|
+
* child spans), on the root and on every `openDB` sub-database (`db.collection.name`). Sync reads
|
|
8
|
+
* (`get`) are memory-mapped and take microseconds, so they get no span. With `metrics` (default on)
|
|
9
|
+
* it also reports `wt.embedded.map.size`, `.map.used`, `.entries`, `.readers` and `.file.size`.
|
|
10
|
+
* Returns `stop()` for the gauges.
|
|
11
|
+
*/
|
|
12
|
+
export function instrumentLmdb(db, opts) {
|
|
13
|
+
const namespace = db.path ? nodePath.basename(db.path) : undefined;
|
|
14
|
+
const wrap = (d, collection) => {
|
|
15
|
+
for (const m of WRITES) {
|
|
16
|
+
patch(d, m, (orig) => function (...a) {
|
|
17
|
+
return traced({ system: 'lmdb', operation: m, collection, namespace }, opts, () => orig.apply(this, a));
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
patch(d, 'transaction', (orig) => function (...a) {
|
|
21
|
+
return traced({ system: 'lmdb', operation: 'transaction', collection, namespace }, opts, () => orig.apply(this, a), false);
|
|
22
|
+
});
|
|
23
|
+
patch(d, 'openDB', (orig) => function (...a) {
|
|
24
|
+
const sub = orig.apply(this, a);
|
|
25
|
+
const name = typeof a[0] === 'string' ? a[0] : a[0]?.name;
|
|
26
|
+
if (sub)
|
|
27
|
+
wrap(sub, name ?? sub.name);
|
|
28
|
+
return sub;
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
wrap(db);
|
|
32
|
+
if (opts?.metrics === false || typeof db.getStats !== 'function')
|
|
33
|
+
return () => { };
|
|
34
|
+
return observeEmbedded('lmdb', namespace, ['map.size', 'map.used', 'entries', 'readers', 'file.size'], () => {
|
|
35
|
+
const s = db.getStats();
|
|
36
|
+
const used = s.pageSize !== undefined && s.lastPageNumber !== undefined ? (s.lastPageNumber + 1) * s.pageSize : undefined;
|
|
37
|
+
return {
|
|
38
|
+
'map.size': s.mapSize, 'map.used': used, entries: s.entryCount, readers: s.numReaders,
|
|
39
|
+
'file.size': db.path ? sizeOf(db.path) : undefined,
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Anything with a `log` method, e.g. a `Client`. */
|
|
2
|
+
export interface LogSink {
|
|
3
|
+
log(level: string, body: string, attrs?: Record<string, unknown>): void;
|
|
4
|
+
}
|
|
5
|
+
/** pino(opts, pinoStream()) or pino.multistream([{ stream: pinoStream() }, { stream: process.stdout }]). */
|
|
6
|
+
export declare function pinoStream(client?: LogSink): {
|
|
7
|
+
write(line: string): void;
|
|
8
|
+
};
|
|
9
|
+
/** logger.add(watchtowerTransport()). Needs `winston-transport`, which winston always installs. */
|
|
10
|
+
export declare function watchtowerTransport(client?: LogSink): object;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Logs bridges: pino and winston records become OTLP logs carrying the active trace and span ids.
|
|
2
|
+
import * as wt from '../index.js';
|
|
3
|
+
import { load } from '../load.js';
|
|
4
|
+
const defaultSink = { log: (level, body, attrs) => wt.log(level, body, attrs) };
|
|
5
|
+
const PINO = { 10: 'trace', 20: 'debug', 30: 'info', 40: 'warn', 50: 'error', 60: 'fatal' };
|
|
6
|
+
const CORE = new Set(['level', 'time', 'msg', 'pid', 'hostname', 'v']);
|
|
7
|
+
const flat = (o, skip) => {
|
|
8
|
+
const attrs = {};
|
|
9
|
+
for (const [k, v] of Object.entries(o))
|
|
10
|
+
if (!skip || !skip.has(k))
|
|
11
|
+
attrs[k] = typeof v === 'object' ? JSON.stringify(v) : v;
|
|
12
|
+
return attrs;
|
|
13
|
+
};
|
|
14
|
+
/** pino(opts, pinoStream()) or pino.multistream([{ stream: pinoStream() }, { stream: process.stdout }]). */
|
|
15
|
+
export function pinoStream(client) {
|
|
16
|
+
return {
|
|
17
|
+
write(line) {
|
|
18
|
+
let r;
|
|
19
|
+
try {
|
|
20
|
+
r = JSON.parse(line);
|
|
21
|
+
}
|
|
22
|
+
catch (_) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
(client || defaultSink).log(PINO[r.level] || 'info', r.msg || '', flat(r, CORE));
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const WINSTON = { silly: 'trace', debug: 'debug', verbose: 'debug', http: 'info', info: 'info', warn: 'warn', error: 'error' };
|
|
30
|
+
/** logger.add(watchtowerTransport()). Needs `winston-transport`, which winston always installs. */
|
|
31
|
+
export function watchtowerTransport(client) {
|
|
32
|
+
const Transport = load('winston-transport');
|
|
33
|
+
return new (class extends Transport {
|
|
34
|
+
log(info, cb) {
|
|
35
|
+
const { level, message, ...rest } = info;
|
|
36
|
+
(client || defaultSink).log(WINSTON[level] || 'info', String(message), flat(rest));
|
|
37
|
+
cb();
|
|
38
|
+
}
|
|
39
|
+
})();
|
|
40
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Attributes } from '@opentelemetry/api';
|
|
2
|
+
/** Key in job data that carries the producer's trace context (W3C `traceparent`/`tracestate`). */
|
|
3
|
+
export declare const CONTEXT_KEY = "_wt";
|
|
4
|
+
export declare function isPlainObject(v: unknown): v is Record<string, unknown>;
|
|
5
|
+
/** The active context as W3C headers. */
|
|
6
|
+
export declare function headers(): Record<string, string>;
|
|
7
|
+
/** Copy of `data` carrying the active context under `_wt`; non-object data is returned unchanged. */
|
|
8
|
+
export declare function injectData(data: unknown): unknown;
|
|
9
|
+
/** Remove and return the `_wt` carrier from object job data. */
|
|
10
|
+
export declare function takeContext(data: unknown): Record<string, unknown> | undefined;
|
|
11
|
+
/** A PRODUCER span around `fn`, which gets the headers to send (the producer span's context). */
|
|
12
|
+
export declare function produce<T>(name: string, attributes: Attributes, fn: (h: Record<string, string>) => T): T;
|
|
13
|
+
/**
|
|
14
|
+
* A CONSUMER span around `fn`, linked to the producer context of each carrier (header maps whose
|
|
15
|
+
* values may be strings or Buffers). With exactly one carrier the producer span is also the parent.
|
|
16
|
+
*/
|
|
17
|
+
export declare function consume<T>(name: string, attributes: Attributes, carriers: Array<Record<string, unknown> | undefined>, fn: () => T): T;
|