@blaxel/telemetry 0.2.17-dev.131 → 0.2.17-dev.132

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.
@@ -42,10 +42,6 @@ declare class TelemetryManager {
42
42
  flush(): Promise<void>;
43
43
  getLogger(): Promise<Logger>;
44
44
  setupSignalHandler(): void;
45
- /**
46
- * Check if telemetry is properly initialized and active
47
- */
48
- get isActive(): boolean;
49
45
  /**
50
46
  * Get resource attributes for OpenTelemetry.
51
47
  */
package/dist/telemetry.js CHANGED
@@ -142,22 +142,6 @@ class TelemetryManager {
142
142
  });
143
143
  });
144
144
  }
145
- // Handle uncaughtException differently - log but don't shutdown telemetry
146
- process.on("uncaughtException", (error) => {
147
- core_1.logger.error("Uncaught exception:", error);
148
- // Don't shutdown telemetry for uncaught exceptions
149
- });
150
- // Don't listen to 'exit' event as it can be triggered by various things
151
- // and we don't want to shutdown telemetry unless explicitly requested
152
- }
153
- /**
154
- * Check if telemetry is properly initialized and active
155
- */
156
- get isActive() {
157
- return (this.initialized &&
158
- this.configured &&
159
- this.nodeTracerProvider !== null &&
160
- this.meterProvider !== null);
161
145
  }
162
146
  /**
163
147
  * Get resource attributes for OpenTelemetry.
@@ -208,119 +192,15 @@ class TelemetryManager {
208
192
  });
209
193
  }
210
194
  instrumentApp() {
211
- core_1.logger.debug("Available propagation fields before setup:", api_1.propagation.fields());
212
195
  core_1.telemetryRegistry.registerProvider(new telemetry_provider_1.OtelTelemetryProvider());
213
196
  const httpInstrumentation = new instrumentation_http_1.HttpInstrumentation({
214
197
  requireParentforOutgoingSpans: true,
215
- requireParentforIncomingSpans: false, // Allow root spans for incoming requests
216
- ignoreIncomingRequestHook: () => false, // Don't ignore any requests
217
- ignoreOutgoingRequestHook: () => false, // Don't ignore any requests
218
- requestHook: (span, request) => {
219
- core_1.logger.debug("Available propagation fields before setup:", api_1.propagation.fields());
220
- // Log incoming headers for debugging
221
- if ("headers" in request && request.headers) {
222
- // Specifically log trace context headers
223
- const headers = request.headers;
224
- const traceHeaders = {
225
- traceparent: headers.traceparent,
226
- tracestate: headers.tracestate,
227
- "x-blaxel-authorization": headers["x-blaxel-authorization"],
228
- "x-blaxel-workspace": headers["x-blaxel-workspace"],
229
- };
230
- core_1.logger.debug("Trace context headers:", JSON.stringify(traceHeaders));
231
- // Manual trace context extraction for debugging
232
- if (headers.traceparent) {
233
- const extractedContext = api_1.propagation.extract(api_1.context.active(), headers);
234
- core_1.logger.debug("Active context keys:", Object.keys(api_1.context.active()));
235
- core_1.logger.debug("Extracted context keys:", Object.keys(extractedContext));
236
- // const extractedSpan = trace.getSpan(extractedContext);
237
- // if (extractedSpan) {
238
- // // Force set the extracted span as active
239
- // context.with(
240
- // trace.setSpan(context.active(), extractedSpan),
241
- // () => {
242
- // logger.debug("Forced context activation from traceparent");
243
- // }
244
- // );
245
- // }
246
- try {
247
- const traceparentValue = Array.isArray(headers.traceparent)
248
- ? headers.traceparent[0]
249
- : headers.traceparent;
250
- core_1.logger.debug("Manual traceparent parsing:", traceparentValue);
251
- // Try to manually parse the traceparent header
252
- const parts = traceparentValue.split("-");
253
- if (parts.length === 4) {
254
- core_1.logger.debug("Traceparent parts:", JSON.stringify({
255
- version: parts[0],
256
- traceId: parts[1],
257
- spanId: parts[2],
258
- flags: parts[3],
259
- }));
260
- // Check if this looks like a valid traceparent
261
- if (parts[1] !== "00000000000000000000000000000000" &&
262
- parts[2] !== "0000000000000000") {
263
- core_1.logger.debug("Traceparent appears valid - extraction should work");
264
- }
265
- else {
266
- core_1.logger.debug("Traceparent contains invalid IDs");
267
- }
268
- }
269
- // // Extract trace context manually to see what should be extracted
270
- // const extractedContext = propagation.extract(
271
- // context.active(),
272
- // headers
273
- // );
274
- const extractedSpan = api_1.trace.getSpan(extractedContext);
275
- if (extractedSpan) {
276
- const extractedSpanContext = extractedSpan.spanContext();
277
- core_1.logger.debug("Manual context extraction result:", JSON.stringify({
278
- traceId: extractedSpanContext.traceId,
279
- spanId: extractedSpanContext.spanId,
280
- traceFlags: extractedSpanContext.traceFlags,
281
- }));
282
- }
283
- else {
284
- core_1.logger.debug("Manual context extraction failed - no span found");
285
- core_1.logger.debug("Available propagation fields:", api_1.propagation.fields());
286
- }
287
- }
288
- catch (error) {
289
- core_1.logger.debug("Manual context extraction error:", error);
290
- }
291
- }
292
- // Safe logging of span information without circular references
293
- const spanContext = span.spanContext();
294
- core_1.logger.debug("Span created:", {
295
- spanId: spanContext.spanId,
296
- traceId: spanContext.traceId,
297
- traceFlags: spanContext.traceFlags,
298
- });
299
- core_1.logger.debug("Span context:", spanContext);
300
- // Log the span context that was created from the incoming request
301
- core_1.logger.debug("HTTP span context:", JSON.stringify({
302
- traceId: spanContext.traceId,
303
- spanId: spanContext.spanId,
304
- traceFlags: spanContext.traceFlags,
305
- }));
306
- }
307
- },
308
- responseHook: (span) => {
309
- const spanContext = span.spanContext();
310
- core_1.logger.debug("HTTP response span context:", JSON.stringify({
311
- traceId: spanContext.traceId,
312
- spanId: spanContext.spanId,
313
- traceFlags: spanContext.traceFlags,
314
- }));
315
- },
316
198
  });
317
199
  (0, instrumentation_1.registerInstrumentations)({
318
200
  instrumentations: [httpInstrumentation],
319
201
  });
320
202
  }
321
203
  setExporters() {
322
- // Log current propagators for debugging
323
- core_1.logger.debug("Current propagators:", api_1.propagation.fields());
324
204
  const resource = new BlaxelResource(this.resourceAttributes);
325
205
  const logExporter = this.getLogExporter();
326
206
  this.loggerProvider = new sdk_logs_1.LoggerProvider({
@@ -343,8 +223,6 @@ class TelemetryManager {
343
223
  ],
344
224
  });
345
225
  this.nodeTracerProvider.register();
346
- // Ensure W3C trace context propagation is working
347
- core_1.logger.debug("Propagation fields after tracer registration:", api_1.propagation.fields());
348
226
  const metricExporter = this.getMetricExporter();
349
227
  this.meterProvider = new sdk_metrics_1.MeterProvider({
350
228
  resource,
@@ -1,5 +1,7 @@
1
1
  import { BlaxelSpan, BlaxelSpanOptions, BlaxelTelemetryProvider } from "@blaxel/core";
2
2
  export declare class OtelTelemetryProvider implements BlaxelTelemetryProvider {
3
+ private spans;
4
+ retrieveActiveSpanContext(): import("@opentelemetry/api").Context;
3
5
  startSpan(name: string, options?: BlaxelSpanOptions): BlaxelSpan;
4
6
  flush(): Promise<void>;
5
7
  }
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OtelTelemetryProvider = void 0;
4
4
  const api_1 = require("@opentelemetry/api");
5
5
  const telemetry_1 = require("./telemetry");
6
- const core_1 = require("@blaxel/core");
7
6
  class OtelSpan {
8
7
  span;
8
+ closed = false;
9
9
  constructor(span) {
10
10
  this.span = span;
11
11
  }
@@ -21,11 +21,12 @@ class OtelSpan {
21
21
  }
22
22
  setStatus(status, message) {
23
23
  this.span.setStatus({
24
- code: status === "ok" ? api_1.SpanStatusCode.OK : api_1.SpanStatusCode.ERROR,
24
+ code: status === 'ok' ? api_1.SpanStatusCode.OK : api_1.SpanStatusCode.ERROR,
25
25
  message,
26
26
  });
27
27
  }
28
28
  end() {
29
+ this.closed = true;
29
30
  this.span.end();
30
31
  }
31
32
  getContext() {
@@ -33,79 +34,26 @@ class OtelSpan {
33
34
  }
34
35
  }
35
36
  class OtelTelemetryProvider {
36
- startSpan(name, options) {
37
- // Check if telemetry is active, reinitialize if needed
38
- core_1.logger.debug("Telemetry is active:", telemetry_1.blaxelTelemetry.isActive);
39
- if (!telemetry_1.blaxelTelemetry.isActive) {
40
- core_1.logger.debug("Telemetry not active, reinitializing...");
41
- // Synchronous reinitialize - just call initialize, setConfiguration will happen async
42
- telemetry_1.blaxelTelemetry.initialize();
37
+ spans = [];
38
+ retrieveActiveSpanContext() {
39
+ for (let i = this.spans.length - 1; i >= 0; i--) {
40
+ const span = this.spans[i];
41
+ if (!span.closed) {
42
+ return api_1.trace.setSpanContext(api_1.ROOT_CONTEXT, span.getContext());
43
+ }
43
44
  }
44
- // Use the tracer from the registered NodeTracerProvider
45
+ return api_1.context.active();
46
+ }
47
+ startSpan(name, options) {
45
48
  const tracer = api_1.trace.getTracer("blaxel");
46
- // Prepare OpenTelemetry span options
47
49
  const otelOptions = {
48
50
  attributes: options?.attributes,
49
51
  root: options?.isRoot,
50
52
  };
51
- core_1.logger.debug("Options:", options
52
- ? {
53
- attributes: options.attributes,
54
- isRoot: options.isRoot,
55
- hasParentContext: !!options.parentContext,
56
- }
57
- : null);
58
- core_1.logger.debug("OTel span options:", otelOptions);
59
- // Handle parent context properly with debugging
60
- let ctx = api_1.context.active();
61
- const activeSpan = api_1.trace.getActiveSpan();
62
- core_1.logger.debug("Active context keys:", Object.keys(ctx));
63
- core_1.logger.debug("Active span:", activeSpan
64
- ? {
65
- spanId: activeSpan.spanContext().spanId,
66
- traceId: activeSpan.spanContext().traceId,
67
- }
68
- : null);
69
- core_1.logger.debug("Active span context:", activeSpan ? activeSpan.spanContext() : null);
70
- // Debug logging for context issues
71
- core_1.logger.info(`Creating span "${name}":`, {
72
- hasActiveSpan: !!activeSpan,
73
- activeSpanId: activeSpan?.spanContext().spanId,
74
- isRoot: options?.isRoot,
75
- hasParentContext: !!options?.parentContext,
76
- parentContext: options?.parentContext,
77
- contextKeys: Object.keys(ctx),
78
- telemetryActive: telemetry_1.blaxelTelemetry.isActive,
79
- activeTraceId: activeSpan?.spanContext().traceId,
80
- otelOptions: otelOptions,
81
- });
82
- if (options?.parentContext) {
83
- // If explicit parent context is provided, use it
84
- ctx = api_1.trace.setSpanContext(api_1.ROOT_CONTEXT, options.parentContext);
85
- }
86
- else if (options?.isRoot) {
87
- // If explicitly marked as root, use ROOT_CONTEXT
88
- ctx = api_1.ROOT_CONTEXT;
89
- }
90
- // Otherwise, use the active context (default behavior)
91
- // Start the span with proper context
92
- const span = tracer.startSpan(name, otelOptions, ctx);
93
- const otelSpan = new OtelSpan(span);
94
- // Safe logging of span information without circular references
95
- const spanContext = span.spanContext();
96
- core_1.logger.debug("Span created:", {
97
- spanId: spanContext.spanId,
98
- traceId: spanContext.traceId,
99
- traceFlags: spanContext.traceFlags,
100
- });
101
- core_1.logger.debug("Span context:", spanContext);
102
- // Additional debugging
103
- core_1.logger.info(`Created span "${name}":`, {
104
- spanId: spanContext.spanId,
105
- traceId: spanContext.traceId,
106
- parentSpanId: activeSpan?.spanContext().spanId || "none",
107
- });
108
- return otelSpan;
53
+ const ctx = this.retrieveActiveSpanContext();
54
+ const span = new OtelSpan(tracer.startSpan(name, otelOptions, ctx));
55
+ this.spans.push(span);
56
+ return span;
109
57
  }
110
58
  async flush() {
111
59
  await telemetry_1.blaxelTelemetry.flush();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaxel/telemetry",
3
- "version": "0.2.17-dev.131",
3
+ "version": "0.2.17-dev.132",
4
4
  "description": "Blaxel SDK for TypeScript",
5
5
  "license": "MIT",
6
6
  "author": "Blaxel, INC (https://blaxel.ai)",
@@ -71,7 +71,7 @@
71
71
  "@opentelemetry/sdk-trace-base": "^2.0.0",
72
72
  "@opentelemetry/sdk-trace-node": "^2.0.0",
73
73
  "ai": "^4.3.13",
74
- "@blaxel/core": "0.2.17-dev.131"
74
+ "@blaxel/core": "0.2.17-dev.132"
75
75
  },
76
76
  "devDependencies": {
77
77
  "@eslint/js": "^9.26.0",