@contractspec/lib.logger 1.56.1 → 1.58.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.
Files changed (108) hide show
  1. package/dist/context.browser.d.ts +44 -0
  2. package/dist/context.browser.d.ts.map +1 -0
  3. package/dist/context.browser.js +77 -0
  4. package/dist/context.d.ts +22 -0
  5. package/dist/context.d.ts.map +1 -0
  6. package/dist/context.js +1 -0
  7. package/dist/context.node.d.ts +43 -0
  8. package/dist/context.node.d.ts.map +1 -0
  9. package/dist/context.node.js +62 -0
  10. package/dist/elysia-plugin.d.ts +58 -0
  11. package/dist/elysia-plugin.d.ts.map +1 -0
  12. package/dist/elysia-plugin.js +793 -0
  13. package/dist/formatters.d.ts +25 -0
  14. package/dist/formatters.d.ts.map +1 -0
  15. package/dist/formatters.js +232 -0
  16. package/dist/index.browser.d.ts +8 -0
  17. package/dist/index.browser.d.ts.map +1 -0
  18. package/dist/index.browser.js +728 -0
  19. package/dist/index.d.ts +9 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +800 -0
  22. package/dist/logger.browser.d.ts +48 -0
  23. package/dist/logger.browser.d.ts.map +1 -0
  24. package/dist/logger.browser.js +722 -0
  25. package/dist/logger.d.ts +2 -0
  26. package/dist/logger.d.ts.map +1 -0
  27. package/dist/logger.js +707 -0
  28. package/dist/logger.node.d.ts +48 -0
  29. package/dist/logger.node.d.ts.map +1 -0
  30. package/dist/logger.node.js +707 -0
  31. package/dist/node/context.js +0 -0
  32. package/dist/node/context.node.js +61 -0
  33. package/dist/node/elysia-plugin.js +792 -0
  34. package/dist/node/formatters.js +231 -0
  35. package/dist/node/index.js +799 -0
  36. package/dist/node/logger.js +706 -0
  37. package/dist/node/logger.node.js +706 -0
  38. package/dist/node/timer.js +122 -0
  39. package/dist/node/tracer.js +274 -0
  40. package/dist/node/tracer.node.js +274 -0
  41. package/dist/node/types.js +13 -0
  42. package/dist/timer.d.ts +99 -0
  43. package/dist/timer.d.ts.map +1 -0
  44. package/dist/timer.js +123 -0
  45. package/dist/tracer.browser.d.ts +47 -0
  46. package/dist/tracer.browser.d.ts.map +1 -0
  47. package/dist/tracer.browser.js +290 -0
  48. package/dist/tracer.d.ts +2 -0
  49. package/dist/tracer.d.ts.map +1 -0
  50. package/dist/tracer.js +275 -0
  51. package/dist/tracer.node.d.ts +47 -0
  52. package/dist/tracer.node.d.ts.map +1 -0
  53. package/dist/tracer.node.js +275 -0
  54. package/dist/types.d.ts +68 -0
  55. package/dist/types.d.ts.map +1 -0
  56. package/dist/types.js +14 -0
  57. package/package.json +112 -42
  58. package/dist/context.browser.d.mts +0 -49
  59. package/dist/context.browser.d.mts.map +0 -1
  60. package/dist/context.browser.mjs +0 -89
  61. package/dist/context.browser.mjs.map +0 -1
  62. package/dist/context.d.mts +0 -17
  63. package/dist/context.d.mts.map +0 -1
  64. package/dist/context.mjs +0 -1
  65. package/dist/context.node.d.mts +0 -48
  66. package/dist/context.node.d.mts.map +0 -1
  67. package/dist/context.node.mjs +0 -79
  68. package/dist/context.node.mjs.map +0 -1
  69. package/dist/elysia-plugin.d.mts +0 -63
  70. package/dist/elysia-plugin.d.mts.map +0 -1
  71. package/dist/elysia-plugin.mjs +0 -84
  72. package/dist/elysia-plugin.mjs.map +0 -1
  73. package/dist/formatters.d.mts +0 -29
  74. package/dist/formatters.d.mts.map +0 -1
  75. package/dist/formatters.mjs +0 -180
  76. package/dist/formatters.mjs.map +0 -1
  77. package/dist/index.browser.d.mts +0 -7
  78. package/dist/index.browser.mjs +0 -8
  79. package/dist/index.d.mts +0 -8
  80. package/dist/index.mjs +0 -9
  81. package/dist/logger.browser.d.mts +0 -51
  82. package/dist/logger.browser.d.mts.map +0 -1
  83. package/dist/logger.browser.mjs +0 -190
  84. package/dist/logger.browser.mjs.map +0 -1
  85. package/dist/logger.d.mts +0 -2
  86. package/dist/logger.mjs +0 -3
  87. package/dist/logger.node.d.mts +0 -51
  88. package/dist/logger.node.d.mts.map +0 -1
  89. package/dist/logger.node.mjs +0 -190
  90. package/dist/logger.node.mjs.map +0 -1
  91. package/dist/timer.d.mts +0 -103
  92. package/dist/timer.d.mts.map +0 -1
  93. package/dist/timer.mjs +0 -165
  94. package/dist/timer.mjs.map +0 -1
  95. package/dist/tracer.browser.d.mts +0 -51
  96. package/dist/tracer.browser.d.mts.map +0 -1
  97. package/dist/tracer.browser.mjs +0 -116
  98. package/dist/tracer.browser.mjs.map +0 -1
  99. package/dist/tracer.d.mts +0 -2
  100. package/dist/tracer.mjs +0 -3
  101. package/dist/tracer.node.d.mts +0 -51
  102. package/dist/tracer.node.d.mts.map +0 -1
  103. package/dist/tracer.node.mjs +0 -116
  104. package/dist/tracer.node.mjs.map +0 -1
  105. package/dist/types.d.mts +0 -71
  106. package/dist/types.d.mts.map +0 -1
  107. package/dist/types.mjs +0 -14
  108. package/dist/types.mjs.map +0 -1
@@ -0,0 +1,122 @@
1
+ // src/timer.ts
2
+ class Timer {
3
+ id;
4
+ startTime;
5
+ laps = [];
6
+ stopped = false;
7
+ stopTime;
8
+ constructor(id) {
9
+ this.id = id || crypto.randomUUID();
10
+ this.startTime = performance.now();
11
+ }
12
+ stop() {
13
+ if (this.stopped) {
14
+ return this.getElapsed();
15
+ }
16
+ this.stopTime = performance.now();
17
+ this.stopped = true;
18
+ const elapsed = this.stopTime - this.startTime;
19
+ this.laps.push({
20
+ label: "stop",
21
+ time: this.stopTime,
22
+ elapsed
23
+ });
24
+ return elapsed;
25
+ }
26
+ lap(label) {
27
+ if (this.stopped) {
28
+ return this.getElapsed();
29
+ }
30
+ const now = performance.now();
31
+ const elapsed = now - this.startTime;
32
+ this.laps.push({
33
+ label: label || `lap-${this.laps.length + 1}`,
34
+ time: now,
35
+ elapsed
36
+ });
37
+ return elapsed;
38
+ }
39
+ getElapsed() {
40
+ if (this.stopped && this.stopTime) {
41
+ return this.stopTime - this.startTime;
42
+ }
43
+ return performance.now() - this.startTime;
44
+ }
45
+ getLaps() {
46
+ return [...this.laps];
47
+ }
48
+ getSummary() {
49
+ return {
50
+ id: this.id,
51
+ totalTime: this.getElapsed(),
52
+ isRunning: !this.stopped,
53
+ laps: this.getLaps()
54
+ };
55
+ }
56
+ reset() {
57
+ this.startTime = performance.now();
58
+ this.laps = [];
59
+ this.stopped = false;
60
+ this.stopTime = undefined;
61
+ }
62
+ }
63
+
64
+ class TimerManager {
65
+ timers = new Map;
66
+ start(id) {
67
+ const timer = new Timer(id);
68
+ this.timers.set(timer.id, timer);
69
+ return timer;
70
+ }
71
+ get(id) {
72
+ return this.timers.get(id);
73
+ }
74
+ stop(id) {
75
+ const timer = this.timers.get(id);
76
+ if (timer) {
77
+ const elapsed = timer.stop();
78
+ this.timers.delete(id);
79
+ return elapsed;
80
+ }
81
+ return;
82
+ }
83
+ getActive() {
84
+ return Array.from(this.timers.values()).filter((timer) => !timer.getSummary().isRunning === false);
85
+ }
86
+ clear() {
87
+ this.timers.clear();
88
+ }
89
+ getSummary() {
90
+ return Array.from(this.timers.values()).map((timer) => timer.getSummary());
91
+ }
92
+ }
93
+ async function timed(operation, options) {
94
+ const timer = new Timer(options?.id);
95
+ try {
96
+ const result = await operation();
97
+ const elapsed = timer.stop();
98
+ options?.onComplete?.(elapsed);
99
+ return { result, elapsed, timer };
100
+ } catch (error) {
101
+ timer.stop();
102
+ throw error;
103
+ }
104
+ }
105
+ function timedSync(operation, options) {
106
+ const timer = new Timer(options?.id);
107
+ try {
108
+ const result = operation();
109
+ const elapsed = timer.stop();
110
+ options?.onComplete?.(elapsed);
111
+ return { result, elapsed, timer };
112
+ } catch (error) {
113
+ timer.stop();
114
+ throw error;
115
+ }
116
+ }
117
+ export {
118
+ timedSync,
119
+ timed,
120
+ TimerManager,
121
+ Timer
122
+ };
@@ -0,0 +1,274 @@
1
+ // src/context.node.ts
2
+ import { AsyncLocalStorage } from "node:async_hooks";
3
+
4
+ class LogContext {
5
+ static instance;
6
+ storage;
7
+ constructor() {
8
+ this.storage = new AsyncLocalStorage;
9
+ }
10
+ static getInstance() {
11
+ if (!LogContext.instance) {
12
+ LogContext.instance = new LogContext;
13
+ }
14
+ return LogContext.instance;
15
+ }
16
+ run(context, fn) {
17
+ const contextData = {
18
+ context: { ...context },
19
+ trace: this.getCurrentTrace()
20
+ };
21
+ return this.storage.run(contextData, fn);
22
+ }
23
+ extend(additionalContext, fn) {
24
+ const currentContext = this.getContext();
25
+ const mergedContext = {
26
+ ...currentContext,
27
+ ...additionalContext
28
+ };
29
+ return this.run(mergedContext, fn);
30
+ }
31
+ set(key, value) {
32
+ const current = this.storage.getStore();
33
+ if (current) {
34
+ current.context[key] = value;
35
+ }
36
+ }
37
+ get(key) {
38
+ const current = this.storage.getStore();
39
+ return current?.context?.[key];
40
+ }
41
+ getContext() {
42
+ const current = this.storage.getStore();
43
+ return current?.context || {};
44
+ }
45
+ setTrace(trace) {
46
+ const current = this.storage.getStore();
47
+ if (current) {
48
+ current.trace = trace;
49
+ }
50
+ }
51
+ getCurrentTrace() {
52
+ const current = this.storage.getStore();
53
+ return current?.trace;
54
+ }
55
+ generateId() {
56
+ return crypto.randomUUID();
57
+ }
58
+ }
59
+
60
+ // src/timer.ts
61
+ class Timer {
62
+ id;
63
+ startTime;
64
+ laps = [];
65
+ stopped = false;
66
+ stopTime;
67
+ constructor(id) {
68
+ this.id = id || crypto.randomUUID();
69
+ this.startTime = performance.now();
70
+ }
71
+ stop() {
72
+ if (this.stopped) {
73
+ return this.getElapsed();
74
+ }
75
+ this.stopTime = performance.now();
76
+ this.stopped = true;
77
+ const elapsed = this.stopTime - this.startTime;
78
+ this.laps.push({
79
+ label: "stop",
80
+ time: this.stopTime,
81
+ elapsed
82
+ });
83
+ return elapsed;
84
+ }
85
+ lap(label) {
86
+ if (this.stopped) {
87
+ return this.getElapsed();
88
+ }
89
+ const now = performance.now();
90
+ const elapsed = now - this.startTime;
91
+ this.laps.push({
92
+ label: label || `lap-${this.laps.length + 1}`,
93
+ time: now,
94
+ elapsed
95
+ });
96
+ return elapsed;
97
+ }
98
+ getElapsed() {
99
+ if (this.stopped && this.stopTime) {
100
+ return this.stopTime - this.startTime;
101
+ }
102
+ return performance.now() - this.startTime;
103
+ }
104
+ getLaps() {
105
+ return [...this.laps];
106
+ }
107
+ getSummary() {
108
+ return {
109
+ id: this.id,
110
+ totalTime: this.getElapsed(),
111
+ isRunning: !this.stopped,
112
+ laps: this.getLaps()
113
+ };
114
+ }
115
+ reset() {
116
+ this.startTime = performance.now();
117
+ this.laps = [];
118
+ this.stopped = false;
119
+ this.stopTime = undefined;
120
+ }
121
+ }
122
+
123
+ class TimerManager {
124
+ timers = new Map;
125
+ start(id) {
126
+ const timer = new Timer(id);
127
+ this.timers.set(timer.id, timer);
128
+ return timer;
129
+ }
130
+ get(id) {
131
+ return this.timers.get(id);
132
+ }
133
+ stop(id) {
134
+ const timer = this.timers.get(id);
135
+ if (timer) {
136
+ const elapsed = timer.stop();
137
+ this.timers.delete(id);
138
+ return elapsed;
139
+ }
140
+ return;
141
+ }
142
+ getActive() {
143
+ return Array.from(this.timers.values()).filter((timer) => !timer.getSummary().isRunning === false);
144
+ }
145
+ clear() {
146
+ this.timers.clear();
147
+ }
148
+ getSummary() {
149
+ return Array.from(this.timers.values()).map((timer) => timer.getSummary());
150
+ }
151
+ }
152
+ async function timed(operation, options) {
153
+ const timer = new Timer(options?.id);
154
+ try {
155
+ const result = await operation();
156
+ const elapsed = timer.stop();
157
+ options?.onComplete?.(elapsed);
158
+ return { result, elapsed, timer };
159
+ } catch (error) {
160
+ timer.stop();
161
+ throw error;
162
+ }
163
+ }
164
+ function timedSync(operation, options) {
165
+ const timer = new Timer(options?.id);
166
+ try {
167
+ const result = operation();
168
+ const elapsed = timer.stop();
169
+ options?.onComplete?.(elapsed);
170
+ return { result, elapsed, timer };
171
+ } catch (error) {
172
+ timer.stop();
173
+ throw error;
174
+ }
175
+ }
176
+
177
+ // src/tracer.node.ts
178
+ class Tracer {
179
+ context;
180
+ activeSpans = new Map;
181
+ constructor() {
182
+ this.context = LogContext.getInstance();
183
+ }
184
+ startSpan(options) {
185
+ const parentTrace = this.context.getCurrentTrace();
186
+ const traceId = parentTrace?.traceId || this.generateTraceId();
187
+ const span = {
188
+ traceId,
189
+ parentId: parentTrace?.spanId,
190
+ spanId: this.generateSpanId(),
191
+ operationType: options.operationType,
192
+ operationName: options.operationName,
193
+ startTime: performance.now(),
194
+ metadata: { ...options.metadata },
195
+ tags: [...options.tags || []]
196
+ };
197
+ this.activeSpans.set(span.spanId, span);
198
+ this.context.setTrace(span);
199
+ return span;
200
+ }
201
+ finishSpan(spanId) {
202
+ const span = this.activeSpans.get(spanId);
203
+ if (!span) {
204
+ return;
205
+ }
206
+ const duration = performance.now() - span.startTime;
207
+ this.activeSpans.delete(spanId);
208
+ const currentTrace = this.context.getCurrentTrace();
209
+ if (currentTrace?.spanId === spanId && span.parentId) {
210
+ const parentSpan = this.findSpanById(span.parentId);
211
+ if (parentSpan) {
212
+ this.context.setTrace(parentSpan);
213
+ }
214
+ }
215
+ return duration;
216
+ }
217
+ async trace(options, fn) {
218
+ const span = this.startSpan(options);
219
+ const timer = options.autoTiming !== false ? new Timer(`trace-${span.spanId}`) : undefined;
220
+ try {
221
+ const result = await fn();
222
+ const duration = this.finishSpan(span.spanId);
223
+ if (timer) {
224
+ timer.stop();
225
+ }
226
+ if (duration !== undefined) {
227
+ span.metadata.duration = duration;
228
+ }
229
+ return result;
230
+ } catch (error) {
231
+ span.metadata.error = {
232
+ name: error.name || "Unknown",
233
+ message: error.message || "Unknown error",
234
+ stack: error.stack
235
+ };
236
+ const duration = this.finishSpan(span.spanId);
237
+ span.metadata.duration = duration;
238
+ if (timer) {
239
+ timer.stop();
240
+ }
241
+ throw error;
242
+ }
243
+ }
244
+ addMetadata(key, value) {
245
+ const currentTrace = this.context.getCurrentTrace();
246
+ if (currentTrace) {
247
+ currentTrace.metadata[key] = value;
248
+ }
249
+ }
250
+ addTags(...tags) {
251
+ const currentTrace = this.context.getCurrentTrace();
252
+ if (currentTrace) {
253
+ currentTrace.tags.push(...tags);
254
+ }
255
+ }
256
+ getCurrentTrace() {
257
+ return this.context.getCurrentTrace();
258
+ }
259
+ getActiveSpans() {
260
+ return Array.from(this.activeSpans.values());
261
+ }
262
+ findSpanById(spanId) {
263
+ return this.activeSpans.get(spanId);
264
+ }
265
+ generateTraceId() {
266
+ return crypto.randomUUID().replace(/-/g, "");
267
+ }
268
+ generateSpanId() {
269
+ return crypto.randomUUID().replace(/-/g, "").substring(0, 16);
270
+ }
271
+ }
272
+ export {
273
+ Tracer
274
+ };
@@ -0,0 +1,274 @@
1
+ // src/context.node.ts
2
+ import { AsyncLocalStorage } from "node:async_hooks";
3
+
4
+ class LogContext {
5
+ static instance;
6
+ storage;
7
+ constructor() {
8
+ this.storage = new AsyncLocalStorage;
9
+ }
10
+ static getInstance() {
11
+ if (!LogContext.instance) {
12
+ LogContext.instance = new LogContext;
13
+ }
14
+ return LogContext.instance;
15
+ }
16
+ run(context, fn) {
17
+ const contextData = {
18
+ context: { ...context },
19
+ trace: this.getCurrentTrace()
20
+ };
21
+ return this.storage.run(contextData, fn);
22
+ }
23
+ extend(additionalContext, fn) {
24
+ const currentContext = this.getContext();
25
+ const mergedContext = {
26
+ ...currentContext,
27
+ ...additionalContext
28
+ };
29
+ return this.run(mergedContext, fn);
30
+ }
31
+ set(key, value) {
32
+ const current = this.storage.getStore();
33
+ if (current) {
34
+ current.context[key] = value;
35
+ }
36
+ }
37
+ get(key) {
38
+ const current = this.storage.getStore();
39
+ return current?.context?.[key];
40
+ }
41
+ getContext() {
42
+ const current = this.storage.getStore();
43
+ return current?.context || {};
44
+ }
45
+ setTrace(trace) {
46
+ const current = this.storage.getStore();
47
+ if (current) {
48
+ current.trace = trace;
49
+ }
50
+ }
51
+ getCurrentTrace() {
52
+ const current = this.storage.getStore();
53
+ return current?.trace;
54
+ }
55
+ generateId() {
56
+ return crypto.randomUUID();
57
+ }
58
+ }
59
+
60
+ // src/timer.ts
61
+ class Timer {
62
+ id;
63
+ startTime;
64
+ laps = [];
65
+ stopped = false;
66
+ stopTime;
67
+ constructor(id) {
68
+ this.id = id || crypto.randomUUID();
69
+ this.startTime = performance.now();
70
+ }
71
+ stop() {
72
+ if (this.stopped) {
73
+ return this.getElapsed();
74
+ }
75
+ this.stopTime = performance.now();
76
+ this.stopped = true;
77
+ const elapsed = this.stopTime - this.startTime;
78
+ this.laps.push({
79
+ label: "stop",
80
+ time: this.stopTime,
81
+ elapsed
82
+ });
83
+ return elapsed;
84
+ }
85
+ lap(label) {
86
+ if (this.stopped) {
87
+ return this.getElapsed();
88
+ }
89
+ const now = performance.now();
90
+ const elapsed = now - this.startTime;
91
+ this.laps.push({
92
+ label: label || `lap-${this.laps.length + 1}`,
93
+ time: now,
94
+ elapsed
95
+ });
96
+ return elapsed;
97
+ }
98
+ getElapsed() {
99
+ if (this.stopped && this.stopTime) {
100
+ return this.stopTime - this.startTime;
101
+ }
102
+ return performance.now() - this.startTime;
103
+ }
104
+ getLaps() {
105
+ return [...this.laps];
106
+ }
107
+ getSummary() {
108
+ return {
109
+ id: this.id,
110
+ totalTime: this.getElapsed(),
111
+ isRunning: !this.stopped,
112
+ laps: this.getLaps()
113
+ };
114
+ }
115
+ reset() {
116
+ this.startTime = performance.now();
117
+ this.laps = [];
118
+ this.stopped = false;
119
+ this.stopTime = undefined;
120
+ }
121
+ }
122
+
123
+ class TimerManager {
124
+ timers = new Map;
125
+ start(id) {
126
+ const timer = new Timer(id);
127
+ this.timers.set(timer.id, timer);
128
+ return timer;
129
+ }
130
+ get(id) {
131
+ return this.timers.get(id);
132
+ }
133
+ stop(id) {
134
+ const timer = this.timers.get(id);
135
+ if (timer) {
136
+ const elapsed = timer.stop();
137
+ this.timers.delete(id);
138
+ return elapsed;
139
+ }
140
+ return;
141
+ }
142
+ getActive() {
143
+ return Array.from(this.timers.values()).filter((timer) => !timer.getSummary().isRunning === false);
144
+ }
145
+ clear() {
146
+ this.timers.clear();
147
+ }
148
+ getSummary() {
149
+ return Array.from(this.timers.values()).map((timer) => timer.getSummary());
150
+ }
151
+ }
152
+ async function timed(operation, options) {
153
+ const timer = new Timer(options?.id);
154
+ try {
155
+ const result = await operation();
156
+ const elapsed = timer.stop();
157
+ options?.onComplete?.(elapsed);
158
+ return { result, elapsed, timer };
159
+ } catch (error) {
160
+ timer.stop();
161
+ throw error;
162
+ }
163
+ }
164
+ function timedSync(operation, options) {
165
+ const timer = new Timer(options?.id);
166
+ try {
167
+ const result = operation();
168
+ const elapsed = timer.stop();
169
+ options?.onComplete?.(elapsed);
170
+ return { result, elapsed, timer };
171
+ } catch (error) {
172
+ timer.stop();
173
+ throw error;
174
+ }
175
+ }
176
+
177
+ // src/tracer.node.ts
178
+ class Tracer {
179
+ context;
180
+ activeSpans = new Map;
181
+ constructor() {
182
+ this.context = LogContext.getInstance();
183
+ }
184
+ startSpan(options) {
185
+ const parentTrace = this.context.getCurrentTrace();
186
+ const traceId = parentTrace?.traceId || this.generateTraceId();
187
+ const span = {
188
+ traceId,
189
+ parentId: parentTrace?.spanId,
190
+ spanId: this.generateSpanId(),
191
+ operationType: options.operationType,
192
+ operationName: options.operationName,
193
+ startTime: performance.now(),
194
+ metadata: { ...options.metadata },
195
+ tags: [...options.tags || []]
196
+ };
197
+ this.activeSpans.set(span.spanId, span);
198
+ this.context.setTrace(span);
199
+ return span;
200
+ }
201
+ finishSpan(spanId) {
202
+ const span = this.activeSpans.get(spanId);
203
+ if (!span) {
204
+ return;
205
+ }
206
+ const duration = performance.now() - span.startTime;
207
+ this.activeSpans.delete(spanId);
208
+ const currentTrace = this.context.getCurrentTrace();
209
+ if (currentTrace?.spanId === spanId && span.parentId) {
210
+ const parentSpan = this.findSpanById(span.parentId);
211
+ if (parentSpan) {
212
+ this.context.setTrace(parentSpan);
213
+ }
214
+ }
215
+ return duration;
216
+ }
217
+ async trace(options, fn) {
218
+ const span = this.startSpan(options);
219
+ const timer = options.autoTiming !== false ? new Timer(`trace-${span.spanId}`) : undefined;
220
+ try {
221
+ const result = await fn();
222
+ const duration = this.finishSpan(span.spanId);
223
+ if (timer) {
224
+ timer.stop();
225
+ }
226
+ if (duration !== undefined) {
227
+ span.metadata.duration = duration;
228
+ }
229
+ return result;
230
+ } catch (error) {
231
+ span.metadata.error = {
232
+ name: error.name || "Unknown",
233
+ message: error.message || "Unknown error",
234
+ stack: error.stack
235
+ };
236
+ const duration = this.finishSpan(span.spanId);
237
+ span.metadata.duration = duration;
238
+ if (timer) {
239
+ timer.stop();
240
+ }
241
+ throw error;
242
+ }
243
+ }
244
+ addMetadata(key, value) {
245
+ const currentTrace = this.context.getCurrentTrace();
246
+ if (currentTrace) {
247
+ currentTrace.metadata[key] = value;
248
+ }
249
+ }
250
+ addTags(...tags) {
251
+ const currentTrace = this.context.getCurrentTrace();
252
+ if (currentTrace) {
253
+ currentTrace.tags.push(...tags);
254
+ }
255
+ }
256
+ getCurrentTrace() {
257
+ return this.context.getCurrentTrace();
258
+ }
259
+ getActiveSpans() {
260
+ return Array.from(this.activeSpans.values());
261
+ }
262
+ findSpanById(spanId) {
263
+ return this.activeSpans.get(spanId);
264
+ }
265
+ generateTraceId() {
266
+ return crypto.randomUUID().replace(/-/g, "");
267
+ }
268
+ generateSpanId() {
269
+ return crypto.randomUUID().replace(/-/g, "").substring(0, 16);
270
+ }
271
+ }
272
+ export {
273
+ Tracer
274
+ };
@@ -0,0 +1,13 @@
1
+ // src/types.ts
2
+ var LogLevel;
3
+ ((LogLevel2) => {
4
+ LogLevel2[LogLevel2["TRACE"] = 0] = "TRACE";
5
+ LogLevel2[LogLevel2["DEBUG"] = 1] = "DEBUG";
6
+ LogLevel2[LogLevel2["INFO"] = 2] = "INFO";
7
+ LogLevel2[LogLevel2["WARN"] = 3] = "WARN";
8
+ LogLevel2[LogLevel2["ERROR"] = 4] = "ERROR";
9
+ LogLevel2[LogLevel2["FATAL"] = 5] = "FATAL";
10
+ })(LogLevel ||= {});
11
+ export {
12
+ LogLevel
13
+ };