@statly/observe 0.1.2 → 1.1.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.
@@ -3,6 +3,9 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __esm = (fn, res) => function __init() {
7
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
8
+ };
6
9
  var __export = (target, all) => {
7
10
  for (var name in all)
8
11
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -17,6 +20,155 @@ var __copyProps = (to, from, except, desc) => {
17
20
  };
18
21
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
22
 
23
+ // src/span.ts
24
+ var Span, TraceContext;
25
+ var init_span = __esm({
26
+ "src/span.ts"() {
27
+ "use strict";
28
+ Span = class {
29
+ constructor(name, context, tags) {
30
+ this._status = "ok" /* OK */;
31
+ this._tags = {};
32
+ this._metadata = {};
33
+ this._finished = false;
34
+ this.name = name;
35
+ this.context = context;
36
+ this.startTime = Date.now();
37
+ if (tags) this._tags = { ...tags };
38
+ }
39
+ /**
40
+ * Finish the span and calculate duration
41
+ */
42
+ finish(endTime) {
43
+ if (this._finished) return;
44
+ this._endTime = endTime || Date.now();
45
+ this._durationMs = this._endTime - this.startTime;
46
+ this._finished = true;
47
+ }
48
+ setTag(key, value) {
49
+ this._tags[key] = value;
50
+ return this;
51
+ }
52
+ setMetadata(key, value) {
53
+ this._metadata[key] = value;
54
+ return this;
55
+ }
56
+ setStatus(status) {
57
+ this._status = status;
58
+ return this;
59
+ }
60
+ get status() {
61
+ return this._status;
62
+ }
63
+ get tags() {
64
+ return { ...this._tags };
65
+ }
66
+ get durationMs() {
67
+ return this._durationMs;
68
+ }
69
+ toDict() {
70
+ return {
71
+ name: this.name,
72
+ traceId: this.context.traceId,
73
+ spanId: this.context.spanId,
74
+ parentId: this.context.parentId,
75
+ startTime: this.startTime,
76
+ endTime: this._endTime,
77
+ durationMs: this._durationMs,
78
+ status: this._status,
79
+ tags: this._tags,
80
+ metadata: this._metadata
81
+ };
82
+ }
83
+ };
84
+ TraceContext = class {
85
+ static getActiveSpan() {
86
+ return this.currentSpan;
87
+ }
88
+ static setActiveSpan(span) {
89
+ this.currentSpan = span;
90
+ }
91
+ };
92
+ TraceContext.currentSpan = null;
93
+ }
94
+ });
95
+
96
+ // src/telemetry.ts
97
+ var telemetry_exports = {};
98
+ __export(telemetry_exports, {
99
+ TelemetryProvider: () => TelemetryProvider,
100
+ trace: () => trace
101
+ });
102
+ async function trace(name, operation, tags) {
103
+ const provider = TelemetryProvider.getInstance();
104
+ const span = provider.startSpan(name, tags);
105
+ try {
106
+ const result = await operation(span);
107
+ return result;
108
+ } catch (error) {
109
+ span.setStatus("error" /* ERROR */);
110
+ span.setTag("error", "true");
111
+ if (error instanceof Error) {
112
+ span.setTag("exception.type", error.name);
113
+ span.setTag("exception.message", error.message);
114
+ }
115
+ throw error;
116
+ } finally {
117
+ provider.finishSpan(span);
118
+ }
119
+ }
120
+ var TelemetryProvider;
121
+ var init_telemetry = __esm({
122
+ "src/telemetry.ts"() {
123
+ "use strict";
124
+ init_span();
125
+ TelemetryProvider = class _TelemetryProvider {
126
+ constructor() {
127
+ this.client = null;
128
+ }
129
+ static getInstance() {
130
+ if (!_TelemetryProvider.instance) {
131
+ _TelemetryProvider.instance = new _TelemetryProvider();
132
+ }
133
+ return _TelemetryProvider.instance;
134
+ }
135
+ setClient(client2) {
136
+ this.client = client2;
137
+ }
138
+ /**
139
+ * Start a new span
140
+ */
141
+ startSpan(name, tags) {
142
+ const parent = TraceContext.getActiveSpan();
143
+ const traceId = parent ? parent.context.traceId : this.generateId();
144
+ const parentId = parent ? parent.context.spanId : null;
145
+ const span = new Span(name, {
146
+ traceId,
147
+ spanId: this.generateId(),
148
+ parentId
149
+ }, tags);
150
+ TraceContext.setActiveSpan(span);
151
+ return span;
152
+ }
153
+ /**
154
+ * Finish and report a span
155
+ */
156
+ finishSpan(span) {
157
+ span.finish();
158
+ if (TraceContext.getActiveSpan() === span) {
159
+ TraceContext.setActiveSpan(null);
160
+ }
161
+ if (this.client) {
162
+ this.client.captureSpan(span);
163
+ }
164
+ }
165
+ generateId() {
166
+ return Math.random().toString(16).substring(2, 18);
167
+ }
168
+ };
169
+ }
170
+ });
171
+
20
172
  // src/integrations/express.ts
21
173
  var express_exports = {};
22
174
  __export(express_exports, {
@@ -369,6 +521,7 @@ var ConsoleIntegration = class {
369
521
  };
370
522
 
371
523
  // src/client.ts
524
+ init_telemetry();
372
525
  var SDK_NAME = "@statly/observe-sdk";
373
526
  var SDK_VERSION = "0.1.0";
374
527
  var StatlyClient = class {
@@ -383,6 +536,7 @@ var StatlyClient = class {
383
536
  this.breadcrumbs = new BreadcrumbManager(this.options.maxBreadcrumbs);
384
537
  this.globalHandlers = new GlobalHandlers();
385
538
  this.consoleIntegration = new ConsoleIntegration();
539
+ TelemetryProvider.getInstance().setClient(this);
386
540
  }
387
541
  mergeOptions(options) {
388
542
  return {
@@ -468,6 +622,30 @@ var StatlyClient = class {
468
622
  });
469
623
  return this.sendEvent(event);
470
624
  }
625
+ /**
626
+ * Capture a completed span
627
+ */
628
+ captureSpan(span) {
629
+ const event = this.buildEvent({
630
+ message: `Span: ${span.name}`,
631
+ level: "span",
632
+ span: span.toDict()
633
+ });
634
+ return this.sendEvent(event);
635
+ }
636
+ /**
637
+ * Start a new tracing span
638
+ */
639
+ startSpan(name, tags) {
640
+ return TelemetryProvider.getInstance().startSpan(name, tags);
641
+ }
642
+ /**
643
+ * Execute a function within a trace span
644
+ */
645
+ async trace(name, operation, tags) {
646
+ const { trace: traceFn } = await Promise.resolve().then(() => (init_telemetry(), telemetry_exports));
647
+ return traceFn(name, operation, tags);
648
+ }
471
649
  /**
472
650
  * Internal method to capture an error
473
651
  */
@@ -768,6 +946,20 @@ async function close() {
768
946
  function getClient() {
769
947
  return client;
770
948
  }
949
+ async function trace2(name, operation, tags) {
950
+ if (!client) {
951
+ return operation(null);
952
+ }
953
+ return client.trace(name, operation, tags);
954
+ }
955
+ function startSpan(name, tags) {
956
+ if (!client) return null;
957
+ return client.startSpan(name, tags);
958
+ }
959
+ function captureSpan(span) {
960
+ if (!client) return "";
961
+ return client.captureSpan(span);
962
+ }
771
963
  var Statly = {
772
964
  init,
773
965
  captureException,
@@ -778,7 +970,10 @@ var Statly = {
778
970
  addBreadcrumb,
779
971
  flush,
780
972
  close,
781
- getClient
973
+ getClient,
974
+ trace: trace2,
975
+ startSpan,
976
+ captureSpan
782
977
  };
783
978
 
784
979
  // src/integrations/express.ts
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  expressErrorHandler,
3
3
  requestHandler
4
- } from "../chunk-UNDSALI5.mjs";
4
+ } from "../chunk-HYFH22G6.mjs";
5
+ import "../chunk-J5AHUFP2.mjs";
5
6
  export {
6
7
  expressErrorHandler,
7
8
  requestHandler
@@ -3,6 +3,9 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __esm = (fn, res) => function __init() {
7
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
8
+ };
6
9
  var __export = (target, all) => {
7
10
  for (var name in all)
8
11
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -17,6 +20,155 @@ var __copyProps = (to, from, except, desc) => {
17
20
  };
18
21
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
22
 
23
+ // src/span.ts
24
+ var Span, TraceContext;
25
+ var init_span = __esm({
26
+ "src/span.ts"() {
27
+ "use strict";
28
+ Span = class {
29
+ constructor(name, context, tags) {
30
+ this._status = "ok" /* OK */;
31
+ this._tags = {};
32
+ this._metadata = {};
33
+ this._finished = false;
34
+ this.name = name;
35
+ this.context = context;
36
+ this.startTime = Date.now();
37
+ if (tags) this._tags = { ...tags };
38
+ }
39
+ /**
40
+ * Finish the span and calculate duration
41
+ */
42
+ finish(endTime) {
43
+ if (this._finished) return;
44
+ this._endTime = endTime || Date.now();
45
+ this._durationMs = this._endTime - this.startTime;
46
+ this._finished = true;
47
+ }
48
+ setTag(key, value) {
49
+ this._tags[key] = value;
50
+ return this;
51
+ }
52
+ setMetadata(key, value) {
53
+ this._metadata[key] = value;
54
+ return this;
55
+ }
56
+ setStatus(status) {
57
+ this._status = status;
58
+ return this;
59
+ }
60
+ get status() {
61
+ return this._status;
62
+ }
63
+ get tags() {
64
+ return { ...this._tags };
65
+ }
66
+ get durationMs() {
67
+ return this._durationMs;
68
+ }
69
+ toDict() {
70
+ return {
71
+ name: this.name,
72
+ traceId: this.context.traceId,
73
+ spanId: this.context.spanId,
74
+ parentId: this.context.parentId,
75
+ startTime: this.startTime,
76
+ endTime: this._endTime,
77
+ durationMs: this._durationMs,
78
+ status: this._status,
79
+ tags: this._tags,
80
+ metadata: this._metadata
81
+ };
82
+ }
83
+ };
84
+ TraceContext = class {
85
+ static getActiveSpan() {
86
+ return this.currentSpan;
87
+ }
88
+ static setActiveSpan(span) {
89
+ this.currentSpan = span;
90
+ }
91
+ };
92
+ TraceContext.currentSpan = null;
93
+ }
94
+ });
95
+
96
+ // src/telemetry.ts
97
+ var telemetry_exports = {};
98
+ __export(telemetry_exports, {
99
+ TelemetryProvider: () => TelemetryProvider,
100
+ trace: () => trace
101
+ });
102
+ async function trace(name, operation, tags) {
103
+ const provider = TelemetryProvider.getInstance();
104
+ const span = provider.startSpan(name, tags);
105
+ try {
106
+ const result = await operation(span);
107
+ return result;
108
+ } catch (error) {
109
+ span.setStatus("error" /* ERROR */);
110
+ span.setTag("error", "true");
111
+ if (error instanceof Error) {
112
+ span.setTag("exception.type", error.name);
113
+ span.setTag("exception.message", error.message);
114
+ }
115
+ throw error;
116
+ } finally {
117
+ provider.finishSpan(span);
118
+ }
119
+ }
120
+ var TelemetryProvider;
121
+ var init_telemetry = __esm({
122
+ "src/telemetry.ts"() {
123
+ "use strict";
124
+ init_span();
125
+ TelemetryProvider = class _TelemetryProvider {
126
+ constructor() {
127
+ this.client = null;
128
+ }
129
+ static getInstance() {
130
+ if (!_TelemetryProvider.instance) {
131
+ _TelemetryProvider.instance = new _TelemetryProvider();
132
+ }
133
+ return _TelemetryProvider.instance;
134
+ }
135
+ setClient(client2) {
136
+ this.client = client2;
137
+ }
138
+ /**
139
+ * Start a new span
140
+ */
141
+ startSpan(name, tags) {
142
+ const parent = TraceContext.getActiveSpan();
143
+ const traceId = parent ? parent.context.traceId : this.generateId();
144
+ const parentId = parent ? parent.context.spanId : null;
145
+ const span = new Span(name, {
146
+ traceId,
147
+ spanId: this.generateId(),
148
+ parentId
149
+ }, tags);
150
+ TraceContext.setActiveSpan(span);
151
+ return span;
152
+ }
153
+ /**
154
+ * Finish and report a span
155
+ */
156
+ finishSpan(span) {
157
+ span.finish();
158
+ if (TraceContext.getActiveSpan() === span) {
159
+ TraceContext.setActiveSpan(null);
160
+ }
161
+ if (this.client) {
162
+ this.client.captureSpan(span);
163
+ }
164
+ }
165
+ generateId() {
166
+ return Math.random().toString(16).substring(2, 18);
167
+ }
168
+ };
169
+ }
170
+ });
171
+
20
172
  // src/integrations/fastify.ts
21
173
  var fastify_exports = {};
22
174
  __export(fastify_exports, {
@@ -370,6 +522,7 @@ var ConsoleIntegration = class {
370
522
  };
371
523
 
372
524
  // src/client.ts
525
+ init_telemetry();
373
526
  var SDK_NAME = "@statly/observe-sdk";
374
527
  var SDK_VERSION = "0.1.0";
375
528
  var StatlyClient = class {
@@ -384,6 +537,7 @@ var StatlyClient = class {
384
537
  this.breadcrumbs = new BreadcrumbManager(this.options.maxBreadcrumbs);
385
538
  this.globalHandlers = new GlobalHandlers();
386
539
  this.consoleIntegration = new ConsoleIntegration();
540
+ TelemetryProvider.getInstance().setClient(this);
387
541
  }
388
542
  mergeOptions(options) {
389
543
  return {
@@ -469,6 +623,30 @@ var StatlyClient = class {
469
623
  });
470
624
  return this.sendEvent(event);
471
625
  }
626
+ /**
627
+ * Capture a completed span
628
+ */
629
+ captureSpan(span) {
630
+ const event = this.buildEvent({
631
+ message: `Span: ${span.name}`,
632
+ level: "span",
633
+ span: span.toDict()
634
+ });
635
+ return this.sendEvent(event);
636
+ }
637
+ /**
638
+ * Start a new tracing span
639
+ */
640
+ startSpan(name, tags) {
641
+ return TelemetryProvider.getInstance().startSpan(name, tags);
642
+ }
643
+ /**
644
+ * Execute a function within a trace span
645
+ */
646
+ async trace(name, operation, tags) {
647
+ const { trace: traceFn } = await Promise.resolve().then(() => (init_telemetry(), telemetry_exports));
648
+ return traceFn(name, operation, tags);
649
+ }
472
650
  /**
473
651
  * Internal method to capture an error
474
652
  */
@@ -769,6 +947,20 @@ async function close() {
769
947
  function getClient() {
770
948
  return client;
771
949
  }
950
+ async function trace2(name, operation, tags) {
951
+ if (!client) {
952
+ return operation(null);
953
+ }
954
+ return client.trace(name, operation, tags);
955
+ }
956
+ function startSpan(name, tags) {
957
+ if (!client) return null;
958
+ return client.startSpan(name, tags);
959
+ }
960
+ function captureSpan(span) {
961
+ if (!client) return "";
962
+ return client.captureSpan(span);
963
+ }
772
964
  var Statly = {
773
965
  init,
774
966
  captureException,
@@ -779,7 +971,10 @@ var Statly = {
779
971
  addBreadcrumb,
780
972
  flush,
781
973
  close,
782
- getClient
974
+ getClient,
975
+ trace: trace2,
976
+ startSpan,
977
+ captureSpan
783
978
  };
784
979
 
785
980
  // src/integrations/fastify.ts
@@ -2,7 +2,8 @@ import {
2
2
  createRequestCapture,
3
3
  statlyFastifyPlugin,
4
4
  statlyPlugin
5
- } from "../chunk-UNDSALI5.mjs";
5
+ } from "../chunk-HYFH22G6.mjs";
6
+ import "../chunk-J5AHUFP2.mjs";
6
7
  export {
7
8
  createRequestCapture,
8
9
  statlyFastifyPlugin,