@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,315 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.shutdown = exports.observe = exports.evaluateResume = exports.evaluate = exports.score = exports.getPrompt = exports.log = exports.flush = exports.Client = exports.DEFAULT_ENDPOINT = exports.capturePayload = exports.setCaptureRules = exports.trace = exports.metrics = exports.captureException = void 0;
|
|
37
|
+
exports.safeResource = safeResource;
|
|
38
|
+
exports.init = init;
|
|
39
|
+
// Thin Watchtower layer over the OpenTelemetry Node SDK: key header, endpoint default,
|
|
40
|
+
// offline buffer, wt.*-safe resource attributes, redaction hook, getPrompt/score/evaluate/observe.
|
|
41
|
+
const node_util_1 = require("node:util");
|
|
42
|
+
const api_1 = require("@opentelemetry/api");
|
|
43
|
+
Object.defineProperty(exports, "metrics", { enumerable: true, get: function () { return api_1.metrics; } });
|
|
44
|
+
Object.defineProperty(exports, "trace", { enumerable: true, get: function () { return api_1.trace; } });
|
|
45
|
+
const resources_1 = require("@opentelemetry/resources");
|
|
46
|
+
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
47
|
+
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
|
|
48
|
+
const sdk_logs_1 = require("@opentelemetry/sdk-logs");
|
|
49
|
+
const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http");
|
|
50
|
+
const exporter_logs_otlp_http_1 = require("@opentelemetry/exporter-logs-otlp-http");
|
|
51
|
+
const buffer_js_1 = require("./buffer.js");
|
|
52
|
+
const metrics_js_1 = require("./metrics.js");
|
|
53
|
+
const nodePath = __importStar(require("node:path"));
|
|
54
|
+
const errors_js_1 = require("./errors.js");
|
|
55
|
+
Object.defineProperty(exports, "captureException", { enumerable: true, get: function () { return errors_js_1.captureException; } });
|
|
56
|
+
const payload_js_1 = require("./payload.js");
|
|
57
|
+
const profiling_js_1 = require("./profiling.js");
|
|
58
|
+
var payload_js_2 = require("./payload.js");
|
|
59
|
+
Object.defineProperty(exports, "setCaptureRules", { enumerable: true, get: function () { return payload_js_2.setCaptureRules; } });
|
|
60
|
+
Object.defineProperty(exports, "capturePayload", { enumerable: true, get: function () { return payload_js_2.capturePayload; } });
|
|
61
|
+
exports.DEFAULT_ENDPOINT = 'http://localhost:4318';
|
|
62
|
+
const SEVERITY = { trace: 1, debug: 5, info: 9, warn: 13, error: 17, fatal: 21 };
|
|
63
|
+
/** Drop caller-supplied wt.* keys: tenant identity comes only from the key. */
|
|
64
|
+
function safeResource(attrs) {
|
|
65
|
+
return (0, errors_js_1.dropReserved)(attrs);
|
|
66
|
+
}
|
|
67
|
+
const errMessage = (e) => String(e?.message || e);
|
|
68
|
+
class Client {
|
|
69
|
+
endpoint;
|
|
70
|
+
apiUrl;
|
|
71
|
+
project;
|
|
72
|
+
headers;
|
|
73
|
+
fetch;
|
|
74
|
+
redact;
|
|
75
|
+
provider;
|
|
76
|
+
tracer;
|
|
77
|
+
loggerProvider;
|
|
78
|
+
logger;
|
|
79
|
+
/** Set when metrics are on; `init` makes it the global meter provider (`metrics.getMeter`). */
|
|
80
|
+
meterProvider;
|
|
81
|
+
stopRuntime = () => { };
|
|
82
|
+
uninstall;
|
|
83
|
+
/** Set when WT_PROFILING=1 started the continuous profiler. */
|
|
84
|
+
profiler;
|
|
85
|
+
constructor(opts) {
|
|
86
|
+
const o = opts || {};
|
|
87
|
+
const env = process.env;
|
|
88
|
+
const apiKey = o.apiKey || env.WT_INGEST_KEY;
|
|
89
|
+
if (!apiKey)
|
|
90
|
+
throw new Error('apiKey is required (or set WT_INGEST_KEY)');
|
|
91
|
+
this.endpoint = (o.endpoint || env.WT_OTLP_ENDPOINT || exports.DEFAULT_ENDPOINT).replace(/\/+$/, '');
|
|
92
|
+
this.apiUrl = (o.apiUrl || env.WT_API_URL || this.endpoint).replace(/\/+$/, '');
|
|
93
|
+
this.project = o.project || env.WT_PROJECT_ID;
|
|
94
|
+
this.headers = { Authorization: `Bearer ${apiKey}` };
|
|
95
|
+
this.fetch = o.fetch || globalThis.fetch;
|
|
96
|
+
const environment = o.environment || env.WT_ENVIRONMENT;
|
|
97
|
+
const release = o.release || env.WT_RELEASE;
|
|
98
|
+
const resource = new resources_1.Resource({
|
|
99
|
+
'service.name': o.serviceName || env.WT_SERVICE || 'app',
|
|
100
|
+
...(environment ? { 'deployment.environment.name': environment } : {}),
|
|
101
|
+
...(release ? { 'service.version': release } : {}),
|
|
102
|
+
...safeResource(o.resource),
|
|
103
|
+
});
|
|
104
|
+
this.redact = o.redact;
|
|
105
|
+
this.provider = new sdk_trace_node_1.NodeTracerProvider({ resource });
|
|
106
|
+
if (o.redact)
|
|
107
|
+
this.provider.addSpanProcessor(redactor(o.redact));
|
|
108
|
+
let exporter = o.exporter;
|
|
109
|
+
if (!exporter) {
|
|
110
|
+
const url = `${this.endpoint}/v1/traces`;
|
|
111
|
+
exporter = new exporter_trace_otlp_http_1.OTLPTraceExporter({ url, headers: this.headers });
|
|
112
|
+
// spans and log records spool to their own subdirectories
|
|
113
|
+
if (o.bufferDir)
|
|
114
|
+
exporter = new buffer_js_1.SpoolExporter(exporter, nodePath.join(o.bufferDir, 'traces'), url, this.headers, this.fetch);
|
|
115
|
+
}
|
|
116
|
+
this.provider.addSpanProcessor(new sdk_trace_base_1.BatchSpanProcessor(exporter));
|
|
117
|
+
this.tracer = this.provider.getTracer('watchtower');
|
|
118
|
+
this.loggerProvider = new sdk_logs_1.LoggerProvider({ resource });
|
|
119
|
+
const logsOn = (0, metrics_js_1.switchOn)('WATCHTOWER_LOGS', 'OTEL_LOGS_EXPORTER', o.logs);
|
|
120
|
+
let logExporter = logsOn ? o.logExporter : undefined;
|
|
121
|
+
if (logsOn && !logExporter && !o.exporter) {
|
|
122
|
+
const url = `${this.endpoint}/v1/logs`;
|
|
123
|
+
logExporter = new exporter_logs_otlp_http_1.OTLPLogExporter({ url, headers: this.headers });
|
|
124
|
+
if (o.bufferDir)
|
|
125
|
+
logExporter = (0, buffer_js_1.spoolLogs)(logExporter, nodePath.join(o.bufferDir, 'logs'), url, this.headers, this.fetch);
|
|
126
|
+
}
|
|
127
|
+
if (logExporter) {
|
|
128
|
+
this.loggerProvider.addLogRecordProcessor(o.logExporter ? new sdk_logs_1.SimpleLogRecordProcessor(logExporter) : new sdk_logs_1.BatchLogRecordProcessor(logExporter));
|
|
129
|
+
}
|
|
130
|
+
this.logger = this.loggerProvider.getLogger('watchtower');
|
|
131
|
+
// with a test span exporter only an explicit metric exporter is used, as for logs
|
|
132
|
+
if ((0, metrics_js_1.switchOn)('WATCHTOWER_METRICS', 'OTEL_METRICS_EXPORTER', o.metrics) && (o.metricExporter || !o.exporter)) {
|
|
133
|
+
const m = (0, metrics_js_1.startMetrics)(this.endpoint, this.headers, resource, o.bufferDir, this.fetch, o.metricExporter);
|
|
134
|
+
this.meterProvider = m.provider;
|
|
135
|
+
this.stopRuntime = m.stop;
|
|
136
|
+
}
|
|
137
|
+
if (o.captureRules ?? env.WT_CAPTURE_RULES === '1') {
|
|
138
|
+
void (0, payload_js_1.loadCaptureRules)({ apiUrl: this.apiUrl, key: apiKey, service: String(resource.attributes['service.name']), fetch: this.fetch });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/** Emit an OTLP log record; trace and span ids come from the active context. */
|
|
142
|
+
log(level, body, attributes) {
|
|
143
|
+
let attrs = (0, errors_js_1.dropReserved)(attributes);
|
|
144
|
+
const redact = this.redact;
|
|
145
|
+
if (redact)
|
|
146
|
+
attrs = Object.fromEntries(Object.entries(attrs).map(([k, v]) => [k, redact(k, v)]));
|
|
147
|
+
this.logger.emit({
|
|
148
|
+
severityNumber: SEVERITY[level] || SEVERITY.info,
|
|
149
|
+
severityText: String(level).toUpperCase(),
|
|
150
|
+
body,
|
|
151
|
+
attributes: attrs,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
forceFlush() {
|
|
155
|
+
return Promise.all([this.provider.forceFlush(), this.loggerProvider.forceFlush(), this.meterProvider?.forceFlush()]);
|
|
156
|
+
}
|
|
157
|
+
// keep the key out of console.log / JSON.stringify
|
|
158
|
+
toJSON() {
|
|
159
|
+
return { endpoint: this.endpoint, project: this.project, headers: { Authorization: 'Bearer ****' } };
|
|
160
|
+
}
|
|
161
|
+
[node_util_1.inspect.custom]() {
|
|
162
|
+
return `Client ${JSON.stringify(this)}`;
|
|
163
|
+
}
|
|
164
|
+
async _call(method, path, body) {
|
|
165
|
+
const url = `${this.apiUrl}/v1/projects/${this.project}/llm/${path}`;
|
|
166
|
+
const res = await this.fetch(url, {
|
|
167
|
+
method,
|
|
168
|
+
headers: { ...this.headers, 'Content-Type': 'application/json' },
|
|
169
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
170
|
+
});
|
|
171
|
+
if (!res.ok)
|
|
172
|
+
throw new Error(`watchtower ${method} ${path}: HTTP ${res.status}`);
|
|
173
|
+
return res.json();
|
|
174
|
+
}
|
|
175
|
+
getPrompt(promptId) {
|
|
176
|
+
return this._call('GET', `prompts/${promptId}`);
|
|
177
|
+
}
|
|
178
|
+
score(traceId, value, o) {
|
|
179
|
+
const x = o || {};
|
|
180
|
+
const body = { trace_id: traceId, value, name: x.name || 'user_feedback', source: 'sdk' };
|
|
181
|
+
if (x.spanId)
|
|
182
|
+
body.span_id = x.spanId;
|
|
183
|
+
if (x.comment)
|
|
184
|
+
body.comment = x.comment;
|
|
185
|
+
return this._call('POST', 'scores', body);
|
|
186
|
+
}
|
|
187
|
+
/** Run task per case under a span; record each scorer result as a score when a project is set. */
|
|
188
|
+
async evaluate(dataset, task, scorers, o) {
|
|
189
|
+
const x = { name: 'evaluate', errorTolerance: true, ...o };
|
|
190
|
+
const rows = [];
|
|
191
|
+
// resume: rows of an earlier run; cases that finished without errors are kept, not re-run
|
|
192
|
+
const done = new Map((x.resume || []).filter((r) => !Object.keys(r.errors || {}).length).map((r) => [r.case, r]));
|
|
193
|
+
for (const [i, c] of dataset.entries()) {
|
|
194
|
+
const kept = done.get(i);
|
|
195
|
+
if (kept) {
|
|
196
|
+
rows.push(kept);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
await this.tracer.startActiveSpan(`${x.name}.case`, async (span) => {
|
|
200
|
+
const traceId = span.spanContext().traceId;
|
|
201
|
+
const row = { case: i, traceId, scores: {}, errors: {} };
|
|
202
|
+
rows.push(row);
|
|
203
|
+
try {
|
|
204
|
+
const input = c !== null && c !== undefined ? c.input : undefined;
|
|
205
|
+
const out = await task(input !== undefined ? input : c);
|
|
206
|
+
for (const [name, fn] of Object.entries(scorers)) {
|
|
207
|
+
try {
|
|
208
|
+
row.scores[name] = await fn(c, out);
|
|
209
|
+
if (this.project)
|
|
210
|
+
await this.score(traceId, row.scores[name], { name });
|
|
211
|
+
}
|
|
212
|
+
catch (e) {
|
|
213
|
+
if (!x.errorTolerance)
|
|
214
|
+
throw e;
|
|
215
|
+
row.errors[name] = errMessage(e);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
catch (e) {
|
|
220
|
+
if (!x.errorTolerance)
|
|
221
|
+
throw e;
|
|
222
|
+
row.errors.task = errMessage(e);
|
|
223
|
+
}
|
|
224
|
+
finally {
|
|
225
|
+
span.end();
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
return rows;
|
|
230
|
+
}
|
|
231
|
+
/** Re-run only the cases that errored or are missing in `previous`. */
|
|
232
|
+
evaluateResume(dataset, task, scorers, previous, o) {
|
|
233
|
+
return this.evaluate(dataset, task, scorers, { ...o, resume: previous });
|
|
234
|
+
}
|
|
235
|
+
/** Wrap fn in a span. */
|
|
236
|
+
observe(name, fn) {
|
|
237
|
+
return (...args) => this.tracer.startActiveSpan(name, (span) => {
|
|
238
|
+
const end = () => span.end();
|
|
239
|
+
try {
|
|
240
|
+
const r = fn(...args);
|
|
241
|
+
if (r && typeof r.then === 'function')
|
|
242
|
+
return r.finally(end);
|
|
243
|
+
end();
|
|
244
|
+
return r;
|
|
245
|
+
}
|
|
246
|
+
catch (e) {
|
|
247
|
+
end();
|
|
248
|
+
throw e;
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
shutdown() {
|
|
253
|
+
this.stopRuntime();
|
|
254
|
+
return Promise.all([this.profiler?.then((p) => p?.stop()), this.provider.shutdown(), this.loggerProvider.shutdown(), this.meterProvider?.shutdown()]).then(() => undefined);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
exports.Client = Client;
|
|
258
|
+
function redactor(hook) {
|
|
259
|
+
return {
|
|
260
|
+
onStart() { },
|
|
261
|
+
onEnd(span) {
|
|
262
|
+
for (const k of Object.keys(span.attributes))
|
|
263
|
+
span.attributes[k] = hook(k, span.attributes[k]);
|
|
264
|
+
},
|
|
265
|
+
shutdown: () => Promise.resolve(),
|
|
266
|
+
forceFlush: () => Promise.resolve(),
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
let current;
|
|
270
|
+
function init(opts) {
|
|
271
|
+
if (current && current.uninstall)
|
|
272
|
+
current.uninstall();
|
|
273
|
+
void current?.profiler?.then((p) => p?.stop());
|
|
274
|
+
const c = new Client(opts);
|
|
275
|
+
current = c;
|
|
276
|
+
c.provider.register(); // global tracer + async context so trace.getActiveSpan() works
|
|
277
|
+
if (c.meterProvider) {
|
|
278
|
+
api_1.metrics.disable(); // a re-init replaces the previous client's provider
|
|
279
|
+
api_1.metrics.setGlobalMeterProvider(c.meterProvider);
|
|
280
|
+
}
|
|
281
|
+
if (process.env.WT_PROFILING === '1') {
|
|
282
|
+
const attrs = c.provider.resource.attributes;
|
|
283
|
+
c.profiler = (0, profiling_js_1.startProfiling)({
|
|
284
|
+
apiKey: c.headers.Authorization.slice('Bearer '.length),
|
|
285
|
+
endpoint: c.endpoint,
|
|
286
|
+
service: String(attrs['service.name']),
|
|
287
|
+
tags: { version: attrs['service.version'] },
|
|
288
|
+
}).catch(() => undefined); // best effort: never break init
|
|
289
|
+
}
|
|
290
|
+
if ((opts || {}).captureProcessErrors !== false) {
|
|
291
|
+
c.uninstall = (0, errors_js_1.installProcessHandlers)(() => c.forceFlush());
|
|
292
|
+
}
|
|
293
|
+
return c;
|
|
294
|
+
}
|
|
295
|
+
function need() {
|
|
296
|
+
if (!current)
|
|
297
|
+
throw new Error('watchtower.init() has not been called');
|
|
298
|
+
return current;
|
|
299
|
+
}
|
|
300
|
+
const flush = () => (current ? current.forceFlush() : Promise.resolve());
|
|
301
|
+
exports.flush = flush;
|
|
302
|
+
const log = (l, b, a) => need().log(l, b, a);
|
|
303
|
+
exports.log = log;
|
|
304
|
+
const getPrompt = (id) => need().getPrompt(id);
|
|
305
|
+
exports.getPrompt = getPrompt;
|
|
306
|
+
const score = (t, v, o) => need().score(t, v, o);
|
|
307
|
+
exports.score = score;
|
|
308
|
+
const evaluate = (d, t, s, o) => need().evaluate(d, t, s, o);
|
|
309
|
+
exports.evaluate = evaluate;
|
|
310
|
+
const evaluateResume = (d, t, s, prev, o) => need().evaluateResume(d, t, s, prev, o);
|
|
311
|
+
exports.evaluateResume = evaluateResume;
|
|
312
|
+
const observe = (n, f) => need().observe(n, f);
|
|
313
|
+
exports.observe = observe;
|
|
314
|
+
const shutdown = () => (current ? current.shutdown() : Promise.resolve());
|
|
315
|
+
exports.shutdown = shutdown;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `instrumentAmqplib(await conn.createChannel())`: patches that channel's class (every channel of
|
|
3
|
+
* it): PRODUCER spans on `publish`/`sendToQueue` with the trace context in `options.headers`, and a
|
|
4
|
+
* CONSUMER span (child of and linked to the producer) around each `consume` callback.
|
|
5
|
+
* shortcut: a ConfirmChannel is its own class; pass one of those too if you use both kinds.
|
|
6
|
+
*/
|
|
7
|
+
export declare function instrumentAmqplib(channel: object): void;
|
|
8
|
+
/**
|
|
9
|
+
* `instrumentRabbitmqClient(new Connection(url))` for rabbitmq-client: publishers made by
|
|
10
|
+
* `createPublisher` get PRODUCER spans on `send` (context in the envelope headers); handlers given
|
|
11
|
+
* to `createConsumer` run in a CONSUMER span. Call it before creating publishers and consumers.
|
|
12
|
+
*/
|
|
13
|
+
export declare function instrumentRabbitmqClient(connection: object): void;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.instrumentAmqplib = instrumentAmqplib;
|
|
4
|
+
exports.instrumentRabbitmqClient = instrumentRabbitmqClient;
|
|
5
|
+
const db_js_1 = require("./db.js");
|
|
6
|
+
const messaging_js_1 = require("./messaging.js");
|
|
7
|
+
const sendAttrs = (dest, routingKey, op, messageId, size) => ({
|
|
8
|
+
'messaging.system': 'rabbitmq',
|
|
9
|
+
'messaging.destination.name': dest,
|
|
10
|
+
'messaging.operation.type': 'send',
|
|
11
|
+
'messaging.operation.name': op,
|
|
12
|
+
'messaging.rabbitmq.destination.routing_key': routingKey,
|
|
13
|
+
...(typeof messageId === 'string' ? { 'messaging.message.id': messageId } : {}),
|
|
14
|
+
...(typeof size === 'number' ? { 'messaging.message.body.size': size } : {}),
|
|
15
|
+
});
|
|
16
|
+
function processAttrs(queue, m) {
|
|
17
|
+
const a = {
|
|
18
|
+
'messaging.system': 'rabbitmq',
|
|
19
|
+
'messaging.destination.name': queue,
|
|
20
|
+
'messaging.operation.type': 'process',
|
|
21
|
+
'messaging.operation.name': 'consume',
|
|
22
|
+
};
|
|
23
|
+
if (m.properties?.messageId)
|
|
24
|
+
a['messaging.message.id'] = m.properties.messageId;
|
|
25
|
+
if (typeof m.content?.length === 'number')
|
|
26
|
+
a['messaging.message.body.size'] = m.content.length;
|
|
27
|
+
if (m.fields?.deliveryTag !== undefined)
|
|
28
|
+
a['messaging.rabbitmq.message.delivery_tag'] = m.fields.deliveryTag;
|
|
29
|
+
if (m.fields?.redelivered !== undefined)
|
|
30
|
+
a['messaging.rabbitmq.message.redelivered'] = m.fields.redelivered;
|
|
31
|
+
if (m.fields?.consumerTag)
|
|
32
|
+
a['messaging.consumer.group.name'] = m.fields.consumerTag;
|
|
33
|
+
return a;
|
|
34
|
+
}
|
|
35
|
+
const withHeaders = (o, h) => ({ ...o, headers: { ...o?.headers, ...h } });
|
|
36
|
+
/**
|
|
37
|
+
* `instrumentAmqplib(await conn.createChannel())`: patches that channel's class (every channel of
|
|
38
|
+
* it): PRODUCER spans on `publish`/`sendToQueue` with the trace context in `options.headers`, and a
|
|
39
|
+
* CONSUMER span (child of and linked to the producer) around each `consume` callback.
|
|
40
|
+
* shortcut: a ConfirmChannel is its own class; pass one of those too if you use both kinds.
|
|
41
|
+
*/
|
|
42
|
+
function instrumentAmqplib(channel) {
|
|
43
|
+
const proto = Object.getPrototypeOf(channel);
|
|
44
|
+
(0, db_js_1.patch)(proto, 'publish', (orig) => function (exchange, routingKey, content, options, ...rest) {
|
|
45
|
+
const dest = exchange || routingKey;
|
|
46
|
+
return (0, messaging_js_1.produce)(`publish ${dest}`, sendAttrs(dest, routingKey, 'publish', options?.messageId, content?.length), (h) => orig.call(this, exchange, routingKey, content, withHeaders(options, h), ...rest));
|
|
47
|
+
});
|
|
48
|
+
// amqplib's sendToQueue calls publish('', queue): that inner call adds no second span
|
|
49
|
+
(0, db_js_1.patch)(proto, 'sendToQueue', (orig) => function (queue, content, options, ...rest) {
|
|
50
|
+
return (0, messaging_js_1.produce)(`publish ${queue}`, sendAttrs(queue, queue, 'sendToQueue', options?.messageId, content?.length), (h) => orig.call(this, queue, content, withHeaders(options, h), ...rest));
|
|
51
|
+
});
|
|
52
|
+
(0, db_js_1.patch)(proto, 'consume', (orig) => function (queue, onMessage, ...rest) {
|
|
53
|
+
if (typeof onMessage !== 'function')
|
|
54
|
+
return orig.call(this, queue, onMessage, ...rest);
|
|
55
|
+
const wrapped = (m) => (m
|
|
56
|
+
? (0, messaging_js_1.consume)(`process ${queue}`, processAttrs(queue, m), [m.properties?.headers], () => onMessage(m))
|
|
57
|
+
: onMessage(m)); // null: the server cancelled the consumer
|
|
58
|
+
return orig.call(this, queue, wrapped, ...rest);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* `instrumentRabbitmqClient(new Connection(url))` for rabbitmq-client: publishers made by
|
|
63
|
+
* `createPublisher` get PRODUCER spans on `send` (context in the envelope headers); handlers given
|
|
64
|
+
* to `createConsumer` run in a CONSUMER span. Call it before creating publishers and consumers.
|
|
65
|
+
*/
|
|
66
|
+
function instrumentRabbitmqClient(connection) {
|
|
67
|
+
(0, db_js_1.patch)(connection, 'createPublisher', (orig) => function (...a) {
|
|
68
|
+
const pub = orig.apply(this, a);
|
|
69
|
+
(0, db_js_1.patch)(pub, 'send', (send) => function (envelope, body, ...rest) {
|
|
70
|
+
const env = typeof envelope === 'string' ? { routingKey: envelope } : { ...envelope };
|
|
71
|
+
const rk = env.routingKey ?? '';
|
|
72
|
+
const dest = env.exchange || rk;
|
|
73
|
+
const size = typeof body === 'string' || body instanceof Uint8Array ? body.length : undefined;
|
|
74
|
+
return (0, messaging_js_1.produce)(`publish ${dest}`, sendAttrs(dest, rk, 'send', env.messageId, size), (h) => send.call(this, { ...env, headers: { ...env.headers, ...h } }, body, ...rest));
|
|
75
|
+
});
|
|
76
|
+
return pub;
|
|
77
|
+
});
|
|
78
|
+
(0, db_js_1.patch)(connection, 'createConsumer', (orig) => function (props, handler, ...rest) {
|
|
79
|
+
if (typeof handler !== 'function')
|
|
80
|
+
return orig.call(this, props, handler, ...rest);
|
|
81
|
+
const queue = (typeof props === 'string' ? props : props?.queue) ?? '';
|
|
82
|
+
const wrapped = function (m, ...more) {
|
|
83
|
+
const a = {
|
|
84
|
+
'messaging.system': 'rabbitmq',
|
|
85
|
+
'messaging.destination.name': queue,
|
|
86
|
+
'messaging.operation.type': 'process',
|
|
87
|
+
'messaging.operation.name': 'consume',
|
|
88
|
+
...(m?.messageId ? { 'messaging.message.id': m.messageId } : {}),
|
|
89
|
+
...(m?.deliveryTag !== undefined ? { 'messaging.rabbitmq.message.delivery_tag': m.deliveryTag } : {}),
|
|
90
|
+
...(m?.redelivered !== undefined ? { 'messaging.rabbitmq.message.redelivered': m.redelivered } : {}),
|
|
91
|
+
};
|
|
92
|
+
return (0, messaging_js_1.consume)(`process ${queue}`, a, [m?.headers], () => handler.call(this, m, ...more));
|
|
93
|
+
};
|
|
94
|
+
return orig.call(this, props, wrapped, ...rest);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** The slice of the Azure Functions v4 InvocationContext this wrapper reads. */
|
|
2
|
+
export interface AzureContextLike {
|
|
3
|
+
invocationId?: string;
|
|
4
|
+
functionName?: string;
|
|
5
|
+
options?: {
|
|
6
|
+
trigger?: {
|
|
7
|
+
type?: string;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/** Maps an Azure binding type to the OTel `faas.trigger` value. */
|
|
12
|
+
export declare function faasTrigger(type: string | undefined): string;
|
|
13
|
+
/** app.http('fn', { handler: wrapFunction(async (request, context) => ...) }). One span per
|
|
14
|
+
* invocation with cold start, invocation id and trigger; errors captured; HTTP 5xx results mark the
|
|
15
|
+
* span failed; spans flushed before the host may freeze the worker. */
|
|
16
|
+
export declare function wrapFunction<T, R, C extends AzureContextLike = AzureContextLike>(handler: (trigger: T, context: C) => Promise<R>, o?: {
|
|
17
|
+
name?: string;
|
|
18
|
+
}): (trigger: T, context: C) => Promise<R>;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.faasTrigger = faasTrigger;
|
|
37
|
+
exports.wrapFunction = wrapFunction;
|
|
38
|
+
const api_1 = require("@opentelemetry/api");
|
|
39
|
+
const errors_js_1 = require("../errors.js");
|
|
40
|
+
const wt = __importStar(require("../index.js"));
|
|
41
|
+
const PUBSUB = ['serviceBusTrigger', 'eventHubTrigger', 'queueTrigger', 'eventGridTrigger', 'kafkaTrigger', 'rabbitMQTrigger'];
|
|
42
|
+
const DATASOURCE = ['blobTrigger', 'cosmosDBTrigger', 'sqlTrigger'];
|
|
43
|
+
/** Maps an Azure binding type to the OTel `faas.trigger` value. */
|
|
44
|
+
function faasTrigger(type) {
|
|
45
|
+
if (type === 'httpTrigger')
|
|
46
|
+
return 'http';
|
|
47
|
+
if (type === 'timerTrigger')
|
|
48
|
+
return 'timer';
|
|
49
|
+
if (type && PUBSUB.includes(type))
|
|
50
|
+
return 'pubsub';
|
|
51
|
+
if (type && DATASOURCE.includes(type))
|
|
52
|
+
return 'datasource';
|
|
53
|
+
return 'other';
|
|
54
|
+
}
|
|
55
|
+
let cold = true;
|
|
56
|
+
/** app.http('fn', { handler: wrapFunction(async (request, context) => ...) }). One span per
|
|
57
|
+
* invocation with cold start, invocation id and trigger; errors captured; HTTP 5xx results mark the
|
|
58
|
+
* span failed; spans flushed before the host may freeze the worker. */
|
|
59
|
+
function wrapFunction(handler, o) {
|
|
60
|
+
return async (input, context) => {
|
|
61
|
+
const c = context || {};
|
|
62
|
+
const coldstart = cold;
|
|
63
|
+
cold = false;
|
|
64
|
+
const kind = faasTrigger(c.options?.trigger?.type);
|
|
65
|
+
return api_1.trace.getTracer('watchtower').startActiveSpan(o?.name || c.functionName || 'function', {
|
|
66
|
+
kind: kind === 'http' ? api_1.SpanKind.SERVER : api_1.SpanKind.CONSUMER,
|
|
67
|
+
attributes: {
|
|
68
|
+
'cloud.provider': 'azure',
|
|
69
|
+
'cloud.platform': 'azure_functions',
|
|
70
|
+
'faas.name': c.functionName,
|
|
71
|
+
'faas.invocation_id': c.invocationId,
|
|
72
|
+
'faas.trigger': kind,
|
|
73
|
+
'faas.coldstart': coldstart,
|
|
74
|
+
},
|
|
75
|
+
}, async (span) => {
|
|
76
|
+
try {
|
|
77
|
+
const r = await handler(input, context);
|
|
78
|
+
const status = r?.status;
|
|
79
|
+
if (kind === 'http' && typeof status === 'number') {
|
|
80
|
+
span.setAttribute('http.response.status_code', status);
|
|
81
|
+
if (status >= 500)
|
|
82
|
+
span.setStatus({ code: api_1.SpanStatusCode.ERROR });
|
|
83
|
+
}
|
|
84
|
+
return r;
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
span.setStatus({ code: api_1.SpanStatusCode.ERROR });
|
|
88
|
+
(0, errors_js_1.captureException)(e, { handled: false, attributes: { 'error.mechanism': 'azure_functions' } });
|
|
89
|
+
throw e;
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
span.end();
|
|
93
|
+
await wt.flush().catch(() => { });
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CONTEXT_KEY } from './messaging.js';
|
|
2
|
+
export { CONTEXT_KEY };
|
|
3
|
+
interface BullMQLike {
|
|
4
|
+
Queue: {
|
|
5
|
+
prototype: Record<string | symbol, unknown>;
|
|
6
|
+
};
|
|
7
|
+
Worker: {
|
|
8
|
+
prototype: Record<string | symbol, unknown>;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* `import * as bullmq from 'bullmq'; instrumentBullMQ(bullmq)` (ESM or CJS): PRODUCER spans on
|
|
13
|
+
* `Queue.add`/`addBulk`, CONSUMER spans around each job a `Worker` processes, parented by the
|
|
14
|
+
* producer's context carried in job data under `_wt` (stripped before your processor runs; object
|
|
15
|
+
* data only). Call it once, before creating queues and workers. Returns false when this bullmq
|
|
16
|
+
* has no `Worker.callProcessJob` (consumer spans off; producer spans still on).
|
|
17
|
+
* shortcut: sandboxed (file path) processors run in a child process and get no consumer span.
|
|
18
|
+
*/
|
|
19
|
+
export declare function instrumentBullMQ(bullmq: BullMQLike): boolean;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CONTEXT_KEY = void 0;
|
|
4
|
+
exports.instrumentBullMQ = instrumentBullMQ;
|
|
5
|
+
const api_1 = require("@opentelemetry/api");
|
|
6
|
+
const messaging_js_1 = require("./messaging.js");
|
|
7
|
+
Object.defineProperty(exports, "CONTEXT_KEY", { enumerable: true, get: function () { return messaging_js_1.CONTEXT_KEY; } });
|
|
8
|
+
const PATCHED = Symbol.for('watchtower.bullmq');
|
|
9
|
+
const tracer = () => api_1.trace.getTracer('watchtower');
|
|
10
|
+
const base = (queue, op, name) => ({
|
|
11
|
+
'messaging.system': 'bullmq',
|
|
12
|
+
'messaging.destination.name': queue,
|
|
13
|
+
'messaging.operation.type': op,
|
|
14
|
+
'messaging.operation.name': name,
|
|
15
|
+
});
|
|
16
|
+
async function run(span, fn) {
|
|
17
|
+
try {
|
|
18
|
+
return await fn();
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
span.recordException(e);
|
|
22
|
+
span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: e?.message });
|
|
23
|
+
throw e;
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
span.end();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* `import * as bullmq from 'bullmq'; instrumentBullMQ(bullmq)` (ESM or CJS): PRODUCER spans on
|
|
31
|
+
* `Queue.add`/`addBulk`, CONSUMER spans around each job a `Worker` processes, parented by the
|
|
32
|
+
* producer's context carried in job data under `_wt` (stripped before your processor runs; object
|
|
33
|
+
* data only). Call it once, before creating queues and workers. Returns false when this bullmq
|
|
34
|
+
* has no `Worker.callProcessJob` (consumer spans off; producer spans still on).
|
|
35
|
+
* shortcut: sandboxed (file path) processors run in a child process and get no consumer span.
|
|
36
|
+
*/
|
|
37
|
+
function instrumentBullMQ(bullmq) {
|
|
38
|
+
const q = bullmq.Queue.prototype;
|
|
39
|
+
const w = bullmq.Worker.prototype;
|
|
40
|
+
if (q[PATCHED])
|
|
41
|
+
return typeof w.callProcessJob === 'function';
|
|
42
|
+
q[PATCHED] = true;
|
|
43
|
+
const add = q.add;
|
|
44
|
+
q.add = function (name, data, opts) {
|
|
45
|
+
const attrs = { ...base(this.name, 'send', 'add'), 'messaging.bullmq.job.name': name };
|
|
46
|
+
if (opts?.delay)
|
|
47
|
+
attrs['messaging.bullmq.job.delay_ms'] = opts.delay;
|
|
48
|
+
return tracer().startActiveSpan(`send ${this.name}`, { kind: api_1.SpanKind.PRODUCER, attributes: attrs }, (span) => run(span, async () => {
|
|
49
|
+
const job = (await add.call(this, name, (0, messaging_js_1.injectData)(data), opts));
|
|
50
|
+
if (job?.id)
|
|
51
|
+
span.setAttribute('messaging.message.id', job.id);
|
|
52
|
+
return job;
|
|
53
|
+
}));
|
|
54
|
+
};
|
|
55
|
+
const addBulk = q.addBulk;
|
|
56
|
+
if (addBulk) {
|
|
57
|
+
q.addBulk = function (jobs) {
|
|
58
|
+
const attrs = { ...base(this.name, 'send', 'addBulk'), 'messaging.batch.message_count': jobs.length };
|
|
59
|
+
return tracer().startActiveSpan(`send ${this.name}`, { kind: api_1.SpanKind.PRODUCER, attributes: attrs }, (span) => run(span, () => addBulk.call(this, jobs.map((j) => ({ ...j, data: (0, messaging_js_1.injectData)(j.data) })))));
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const call = w.callProcessJob;
|
|
63
|
+
if (typeof call !== 'function')
|
|
64
|
+
return false;
|
|
65
|
+
w.callProcessJob = function (job, ...rest) {
|
|
66
|
+
let parent = api_1.context.active();
|
|
67
|
+
if ((0, messaging_js_1.isPlainObject)(job.data) && (0, messaging_js_1.isPlainObject)(job.data[messaging_js_1.CONTEXT_KEY])) {
|
|
68
|
+
parent = api_1.propagation.extract(parent, job.data[messaging_js_1.CONTEXT_KEY]);
|
|
69
|
+
delete job.data[messaging_js_1.CONTEXT_KEY];
|
|
70
|
+
}
|
|
71
|
+
const attrs = {
|
|
72
|
+
...base(this.name, 'process', 'process'),
|
|
73
|
+
'messaging.bullmq.job.name': job.name,
|
|
74
|
+
'messaging.bullmq.job.attempt': (job.attemptsMade ?? 0) + 1,
|
|
75
|
+
};
|
|
76
|
+
if (job.id)
|
|
77
|
+
attrs['messaging.message.id'] = job.id;
|
|
78
|
+
if (job.opts?.delay)
|
|
79
|
+
attrs['messaging.bullmq.job.delay_ms'] = job.opts.delay;
|
|
80
|
+
return tracer().startActiveSpan(`process ${this.name}`, { kind: api_1.SpanKind.CONSUMER, attributes: attrs }, parent, (span) => run(span, () => call.call(this, job, ...rest)));
|
|
81
|
+
};
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Hit/miss counters and entry count for a node-cache instance (`get`, `mget`). Returns `stop()`. */
|
|
2
|
+
export declare function instrumentNodeCache(cache: {
|
|
3
|
+
getStats?(): {
|
|
4
|
+
keys?: number;
|
|
5
|
+
};
|
|
6
|
+
}, name?: string): () => void;
|
|
7
|
+
/** Hit/miss counters and entry count for an lru-cache instance (`get`). Returns `stop()`.
|
|
8
|
+
* shortcut: `fetch()` (the async loader) is not counted. */
|
|
9
|
+
export declare function instrumentLruCache(cache: {
|
|
10
|
+
size?: number;
|
|
11
|
+
}, name?: string): () => void;
|
|
12
|
+
/** Hit/miss counters for a Keyv instance (`get` with one key or an array; also `getMany`). */
|
|
13
|
+
export declare function instrumentKeyv(keyv: object, name?: string): void;
|