@ydking0911/observr 0.3.1 → 0.4.7

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/index.d.mts CHANGED
@@ -4,6 +4,7 @@ interface ObservrEvent {
4
4
  level: "debug" | "info" | "warn" | "error";
5
5
  trace_id?: string;
6
6
  span_id?: string;
7
+ parent_span_id?: string;
7
8
  message: string;
8
9
  service?: string;
9
10
  duration_ms?: number;
@@ -39,10 +40,11 @@ declare class Span {
39
40
  readonly name: string;
40
41
  readonly spanId: string;
41
42
  readonly traceId: string;
43
+ readonly parentSpanId: string | undefined;
42
44
  private readonly transport;
43
45
  private readonly attributes;
44
46
  private startTime;
45
- constructor(name: string, transport: Transport, attributes?: Record<string, unknown>);
47
+ constructor(name: string, transport: Transport, attributes?: Record<string, unknown>, parentSpanId?: string);
46
48
  setAttribute(key: string, value: unknown): this;
47
49
  /** Run an async function inside this span, emit on completion. */
48
50
  run<T>(fn: (span: this) => Promise<T>): Promise<T>;
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ interface ObservrEvent {
4
4
  level: "debug" | "info" | "warn" | "error";
5
5
  trace_id?: string;
6
6
  span_id?: string;
7
+ parent_span_id?: string;
7
8
  message: string;
8
9
  service?: string;
9
10
  duration_ms?: number;
@@ -39,10 +40,11 @@ declare class Span {
39
40
  readonly name: string;
40
41
  readonly spanId: string;
41
42
  readonly traceId: string;
43
+ readonly parentSpanId: string | undefined;
42
44
  private readonly transport;
43
45
  private readonly attributes;
44
46
  private startTime;
45
- constructor(name: string, transport: Transport, attributes?: Record<string, unknown>);
47
+ constructor(name: string, transport: Transport, attributes?: Record<string, unknown>, parentSpanId?: string);
46
48
  setAttribute(key: string, value: unknown): this;
47
49
  /** Run an async function inside this span, emit on completion. */
48
50
  run<T>(fn: (span: this) => Promise<T>): Promise<T>;
package/dist/index.js CHANGED
@@ -285,15 +285,27 @@ function unpatchConsole() {
285
285
  }
286
286
 
287
287
  // src/span.ts
288
+ var import_node_async_hooks = require("async_hooks");
288
289
  var import_node_crypto = require("crypto");
290
+ var _activeSpan = new import_node_async_hooks.AsyncLocalStorage();
289
291
  var Span = class {
290
- constructor(name, transport, attributes = {}) {
292
+ constructor(name, transport, attributes = {}, parentSpanId) {
291
293
  this.startTime = 0;
292
294
  this.name = name;
293
295
  this.spanId = (0, import_node_crypto.randomBytes)(8).toString("hex");
294
- this.traceId = (0, import_node_crypto.randomBytes)(16).toString("hex");
295
296
  this.transport = transport;
296
297
  this.attributes = { ...attributes };
298
+ const active = _activeSpan.getStore();
299
+ if (parentSpanId !== void 0) {
300
+ this.parentSpanId = parentSpanId;
301
+ this.traceId = active !== void 0 ? active.traceId : (0, import_node_crypto.randomBytes)(16).toString("hex");
302
+ } else if (active !== void 0) {
303
+ this.parentSpanId = active.spanId;
304
+ this.traceId = active.traceId;
305
+ } else {
306
+ this.parentSpanId = void 0;
307
+ this.traceId = (0, import_node_crypto.randomBytes)(16).toString("hex");
308
+ }
297
309
  }
298
310
  setAttribute(key, value) {
299
311
  this.attributes[key] = value;
@@ -302,31 +314,34 @@ var Span = class {
302
314
  /** Run an async function inside this span, emit on completion. */
303
315
  async run(fn) {
304
316
  this.startTime = performance.now();
305
- let level = "info";
306
- let exceptionMsg;
307
- try {
308
- const result = await fn(this);
309
- return result;
310
- } catch (err) {
311
- level = "error";
312
- exceptionMsg = err instanceof Error ? err.stack ?? err.message : String(err);
313
- throw err;
314
- } finally {
315
- const durationMs = parseFloat(
316
- (performance.now() - this.startTime).toFixed(2)
317
- );
318
- if (exceptionMsg) this.attributes["exception"] = exceptionMsg;
319
- this.transport.send({
320
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
321
- type: "span",
322
- level,
323
- trace_id: this.traceId,
324
- span_id: this.spanId,
325
- message: this.name,
326
- duration_ms: durationMs,
327
- attributes: this.attributes
328
- });
329
- }
317
+ return _activeSpan.run(this, async () => {
318
+ let level = "info";
319
+ let exceptionMsg;
320
+ try {
321
+ const result = await fn(this);
322
+ return result;
323
+ } catch (err) {
324
+ level = "error";
325
+ exceptionMsg = err instanceof Error ? err.stack ?? err.message : String(err);
326
+ throw err;
327
+ } finally {
328
+ const durationMs = parseFloat(
329
+ (performance.now() - this.startTime).toFixed(2)
330
+ );
331
+ if (exceptionMsg) this.attributes["exception"] = exceptionMsg;
332
+ this.transport.send({
333
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
334
+ type: "span",
335
+ level,
336
+ trace_id: this.traceId,
337
+ span_id: this.spanId,
338
+ ...this.parentSpanId !== void 0 && { parent_span_id: this.parentSpanId },
339
+ message: this.name,
340
+ duration_ms: durationMs,
341
+ attributes: { ...this.attributes }
342
+ });
343
+ }
344
+ });
330
345
  }
331
346
  };
332
347
 
package/dist/index.mjs CHANGED
@@ -264,15 +264,27 @@ function unpatchConsole() {
264
264
  }
265
265
 
266
266
  // src/span.ts
267
+ import { AsyncLocalStorage } from "async_hooks";
267
268
  import { randomBytes } from "crypto";
269
+ var _activeSpan = new AsyncLocalStorage();
268
270
  var Span = class {
269
- constructor(name, transport, attributes = {}) {
271
+ constructor(name, transport, attributes = {}, parentSpanId) {
270
272
  this.startTime = 0;
271
273
  this.name = name;
272
274
  this.spanId = randomBytes(8).toString("hex");
273
- this.traceId = randomBytes(16).toString("hex");
274
275
  this.transport = transport;
275
276
  this.attributes = { ...attributes };
277
+ const active = _activeSpan.getStore();
278
+ if (parentSpanId !== void 0) {
279
+ this.parentSpanId = parentSpanId;
280
+ this.traceId = active !== void 0 ? active.traceId : randomBytes(16).toString("hex");
281
+ } else if (active !== void 0) {
282
+ this.parentSpanId = active.spanId;
283
+ this.traceId = active.traceId;
284
+ } else {
285
+ this.parentSpanId = void 0;
286
+ this.traceId = randomBytes(16).toString("hex");
287
+ }
276
288
  }
277
289
  setAttribute(key, value) {
278
290
  this.attributes[key] = value;
@@ -281,31 +293,34 @@ var Span = class {
281
293
  /** Run an async function inside this span, emit on completion. */
282
294
  async run(fn) {
283
295
  this.startTime = performance.now();
284
- let level = "info";
285
- let exceptionMsg;
286
- try {
287
- const result = await fn(this);
288
- return result;
289
- } catch (err) {
290
- level = "error";
291
- exceptionMsg = err instanceof Error ? err.stack ?? err.message : String(err);
292
- throw err;
293
- } finally {
294
- const durationMs = parseFloat(
295
- (performance.now() - this.startTime).toFixed(2)
296
- );
297
- if (exceptionMsg) this.attributes["exception"] = exceptionMsg;
298
- this.transport.send({
299
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
300
- type: "span",
301
- level,
302
- trace_id: this.traceId,
303
- span_id: this.spanId,
304
- message: this.name,
305
- duration_ms: durationMs,
306
- attributes: this.attributes
307
- });
308
- }
296
+ return _activeSpan.run(this, async () => {
297
+ let level = "info";
298
+ let exceptionMsg;
299
+ try {
300
+ const result = await fn(this);
301
+ return result;
302
+ } catch (err) {
303
+ level = "error";
304
+ exceptionMsg = err instanceof Error ? err.stack ?? err.message : String(err);
305
+ throw err;
306
+ } finally {
307
+ const durationMs = parseFloat(
308
+ (performance.now() - this.startTime).toFixed(2)
309
+ );
310
+ if (exceptionMsg) this.attributes["exception"] = exceptionMsg;
311
+ this.transport.send({
312
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
313
+ type: "span",
314
+ level,
315
+ trace_id: this.traceId,
316
+ span_id: this.spanId,
317
+ ...this.parentSpanId !== void 0 && { parent_span_id: this.parentSpanId },
318
+ message: this.name,
319
+ duration_ms: durationMs,
320
+ attributes: { ...this.attributes }
321
+ });
322
+ }
323
+ });
309
324
  }
310
325
  };
311
326
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ydking0911/observr",
3
- "version": "0.3.1",
3
+ "version": "0.4.7",
4
4
  "description": "Zero-config observability for AI agents and developers",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -60,4 +60,4 @@
60
60
  "dist",
61
61
  "README.md"
62
62
  ]
63
- }
63
+ }