@blaxel/telemetry 0.2.70-preview.99 → 0.2.71-dev.101

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