@blaxel/telemetry 0.2.70 → 0.2.71-dev.105
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/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/instrumentation/blaxel_core.js +200 -0
- package/dist/cjs/telemetry.js +3 -0
- package/dist/cjs/types/instrumentation/blaxel_core.d.ts +5 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/instrumentation/blaxel_core.js +197 -0
- package/dist/esm/telemetry.js +3 -0
- package/package.json +2 -2
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { BlAgent, BlaxelMcpServerTransport, BlJob, McpTool, startSpan, } from "@blaxel/core";
|
|
2
|
+
/**
|
|
3
|
+
* Monkey-patches BlAgent.prototype.run to wrap with telemetry spans.
|
|
4
|
+
*/
|
|
5
|
+
function patchBlAgent() {
|
|
6
|
+
const origRun = BlAgent.prototype.run;
|
|
7
|
+
BlAgent.prototype.run = async function (input, headers = {}, params = {}) {
|
|
8
|
+
const span = startSpan(this.agentName, {
|
|
9
|
+
attributes: {
|
|
10
|
+
"agent.name": this.agentName,
|
|
11
|
+
"agent.args": JSON.stringify(input),
|
|
12
|
+
"span.type": "agent.run",
|
|
13
|
+
},
|
|
14
|
+
isRoot: false,
|
|
15
|
+
});
|
|
16
|
+
try {
|
|
17
|
+
const result = await origRun.call(this, input, headers, params);
|
|
18
|
+
span.setAttribute("agent.run.result", result);
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
if (err instanceof Error) {
|
|
23
|
+
span.setAttribute("agent.run.error", err.stack);
|
|
24
|
+
}
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
span.end();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Monkey-patches BlJob.prototype.run to wrap with telemetry spans.
|
|
34
|
+
*/
|
|
35
|
+
function patchBlJob() {
|
|
36
|
+
const origRun = BlJob.prototype.run;
|
|
37
|
+
BlJob.prototype.run = async function (tasks, options) {
|
|
38
|
+
const span = startSpan(this.jobName, {
|
|
39
|
+
attributes: {
|
|
40
|
+
"job.name": this.jobName,
|
|
41
|
+
"span.type": "job.run",
|
|
42
|
+
},
|
|
43
|
+
isRoot: false,
|
|
44
|
+
});
|
|
45
|
+
try {
|
|
46
|
+
const result = await origRun.call(this, tasks, options);
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
if (err instanceof Error) {
|
|
51
|
+
span.setAttribute("job.run.error", err.stack);
|
|
52
|
+
}
|
|
53
|
+
throw err;
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
span.end();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Monkey-patches McpTool.prototype.listTools and McpTool.prototype.call
|
|
62
|
+
* to wrap with telemetry spans.
|
|
63
|
+
*/
|
|
64
|
+
function patchMcpTool() {
|
|
65
|
+
const origListTools = McpTool.prototype.listTools;
|
|
66
|
+
const origCall = McpTool.prototype.call;
|
|
67
|
+
McpTool.prototype.listTools = async function () {
|
|
68
|
+
const span = startSpan(this.name, {
|
|
69
|
+
attributes: {
|
|
70
|
+
"span.type": "tool.list",
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
try {
|
|
74
|
+
const result = await origListTools.call(this);
|
|
75
|
+
span.setAttribute("tool.list.result", JSON.stringify(result));
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
span.setStatus("error");
|
|
80
|
+
span.recordException(err);
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
span.end();
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
McpTool.prototype.call = async function (toolName, args) {
|
|
88
|
+
const span = startSpan(this.name + "." + toolName, {
|
|
89
|
+
attributes: {
|
|
90
|
+
"span.type": "tool.call",
|
|
91
|
+
"tool.name": toolName,
|
|
92
|
+
"tool.args": JSON.stringify(args),
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
try {
|
|
96
|
+
const result = await origCall.call(this, toolName, args);
|
|
97
|
+
span.setAttribute("tool.call.result", JSON.stringify(result));
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
catch (err) {
|
|
101
|
+
span.setStatus("error");
|
|
102
|
+
span.recordException(err);
|
|
103
|
+
throw err;
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
span.end();
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Monkey-patches BlaxelMcpServerTransport to add telemetry spans
|
|
112
|
+
* for incoming messages (via onmessage setter) and outgoing messages (via send).
|
|
113
|
+
*/
|
|
114
|
+
function patchMcpServer() {
|
|
115
|
+
// Patch the onmessage setter to wrap the handler with span tracking
|
|
116
|
+
const origDescriptor = Object.getOwnPropertyDescriptor(BlaxelMcpServerTransport.prototype, "onmessage");
|
|
117
|
+
if (origDescriptor?.set) {
|
|
118
|
+
const origSetter = origDescriptor.set;
|
|
119
|
+
Object.defineProperty(BlaxelMcpServerTransport.prototype, "onmessage", {
|
|
120
|
+
...origDescriptor,
|
|
121
|
+
set(handler) {
|
|
122
|
+
if (handler) {
|
|
123
|
+
const tracedHandler = async (message) => {
|
|
124
|
+
const messageId = message.id ? String(message.id) : "";
|
|
125
|
+
const [clientId] = messageId.includes(":")
|
|
126
|
+
? messageId.split(":")
|
|
127
|
+
: [undefined];
|
|
128
|
+
const span = startSpan("mcp.message", {
|
|
129
|
+
attributes: {
|
|
130
|
+
"span.type": "mcp.message",
|
|
131
|
+
...(clientId ? { "mcp.client.id": clientId } : {}),
|
|
132
|
+
...(message.method
|
|
133
|
+
? { "mcp.method": message.method }
|
|
134
|
+
: {}),
|
|
135
|
+
...(message.params?.name
|
|
136
|
+
? {
|
|
137
|
+
"mcp.toolName": message.params.name,
|
|
138
|
+
}
|
|
139
|
+
: {}),
|
|
140
|
+
},
|
|
141
|
+
isRoot: false,
|
|
142
|
+
});
|
|
143
|
+
try {
|
|
144
|
+
await Promise.resolve(handler(message));
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
span.setStatus("error");
|
|
148
|
+
span.recordException(err);
|
|
149
|
+
throw err;
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
span.end();
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
origSetter.call(this, tracedHandler);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
origSetter.call(this, handler);
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
// Patch the send method to wrap with span tracking
|
|
164
|
+
const origSend = BlaxelMcpServerTransport.prototype.send;
|
|
165
|
+
BlaxelMcpServerTransport.prototype.send = async function (msg) {
|
|
166
|
+
const span = startSpan("mcp.send", {
|
|
167
|
+
attributes: {
|
|
168
|
+
"span.type": "mcp.send",
|
|
169
|
+
},
|
|
170
|
+
isRoot: false,
|
|
171
|
+
});
|
|
172
|
+
try {
|
|
173
|
+
await origSend.call(this, msg);
|
|
174
|
+
span.setAttributes({
|
|
175
|
+
"mcp.message.response_sent": true,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
span.setStatus("error");
|
|
180
|
+
span.recordException(err);
|
|
181
|
+
throw err;
|
|
182
|
+
}
|
|
183
|
+
finally {
|
|
184
|
+
span.end();
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Instruments all @blaxel/core classes with telemetry via monkey patching.
|
|
190
|
+
* This should be called during telemetry initialization.
|
|
191
|
+
*/
|
|
192
|
+
export function instrumentBlaxelCore() {
|
|
193
|
+
patchBlAgent();
|
|
194
|
+
patchBlJob();
|
|
195
|
+
patchMcpTool();
|
|
196
|
+
patchMcpServer();
|
|
197
|
+
}
|
package/dist/esm/telemetry.js
CHANGED
|
@@ -6,6 +6,7 @@ import { envDetector } from "@opentelemetry/resources";
|
|
|
6
6
|
import { MeterProvider, PeriodicExportingMetricReader, } from "@opentelemetry/sdk-metrics";
|
|
7
7
|
import { AlwaysOnSampler, BatchSpanProcessor, NodeTracerProvider, } from "@opentelemetry/sdk-trace-node";
|
|
8
8
|
import { AuthRefreshingMetricExporter, AuthRefreshingSpanExporter, createMetricExporter, createTraceExporter } from "./auth_refresh_exporters.js";
|
|
9
|
+
import { instrumentBlaxelCore } from "./instrumentation/blaxel_core.js";
|
|
9
10
|
import { OtelTelemetryProvider } from "./telemetry_provider.js";
|
|
10
11
|
export class BlaxelResource {
|
|
11
12
|
attributes;
|
|
@@ -196,6 +197,8 @@ class TelemetryManager {
|
|
|
196
197
|
registerInstrumentations({
|
|
197
198
|
instrumentations: [httpInstrumentation],
|
|
198
199
|
});
|
|
200
|
+
// Instrument @blaxel/core classes via monkey patching
|
|
201
|
+
instrumentBlaxelCore();
|
|
199
202
|
}
|
|
200
203
|
setExporters() {
|
|
201
204
|
const resource = new BlaxelResource(this.resourceAttributes);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blaxel/telemetry",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.71-dev.105",
|
|
4
4
|
"description": "Blaxel SDK for TypeScript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Blaxel, INC (https://blaxel.ai)",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"@ai-sdk/google": "^2.0.25",
|
|
55
55
|
"@ai-sdk/groq": "^2.0.26",
|
|
56
56
|
"@ai-sdk/openai": "^2.0.57",
|
|
57
|
-
"@blaxel/core": "0.2.
|
|
57
|
+
"@blaxel/core": "0.2.71-dev.105",
|
|
58
58
|
"@opentelemetry/api": "^1.9.0",
|
|
59
59
|
"@opentelemetry/api-logs": "^0.200.0",
|
|
60
60
|
"@opentelemetry/exporter-logs-otlp-http": "^0.200.0",
|