@tracewayapp/backend 0.2.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.
- package/README.md +368 -0
- package/dist/index.d.mts +232 -0
- package/dist/index.d.ts +232 -0
- package/dist/index.js +742 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +681 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +28 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { Span, TracewayOptions, MetricRecord, ExceptionStackTrace, Trace } from '@tracewayapp/core';
|
|
2
|
+
export { CollectionFrame, ExceptionStackTrace, MetricRecord, ReportRequest, Span, Trace, TracewayOptions } from '@tracewayapp/core';
|
|
3
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
4
|
+
|
|
5
|
+
declare function init(connectionString: string, options?: TracewayOptions): void;
|
|
6
|
+
declare function captureException(error: Error): void;
|
|
7
|
+
declare function captureExceptionWithAttributes(error: Error, attributes?: Record<string, string>, traceId?: string): void;
|
|
8
|
+
declare function captureMessage(msg: string, attributes?: Record<string, string>): void;
|
|
9
|
+
declare function captureMetric(name: string, value: number): void;
|
|
10
|
+
declare function captureTrace(traceId: string, endpoint: string, durationMs: number, startedAt: Date, statusCode: number, bodySize: number, clientIP: string, attributes?: Record<string, string>, spans?: Span[]): void;
|
|
11
|
+
declare function captureTask(traceId: string, taskName: string, durationMs: number, startedAt: Date, attributes?: Record<string, string>, spans?: Span[]): void;
|
|
12
|
+
/** Handle returned by startSpan for tracking span timing */
|
|
13
|
+
interface SpanHandle {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
startTime: string;
|
|
17
|
+
startedAt: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Start a new span. If within a trace context, the span will be
|
|
21
|
+
* automatically added to the trace when ended.
|
|
22
|
+
*/
|
|
23
|
+
declare function startSpan(name: string): SpanHandle;
|
|
24
|
+
/**
|
|
25
|
+
* End a span and get the completed Span object.
|
|
26
|
+
* If within a trace context, automatically adds the span to the trace.
|
|
27
|
+
*
|
|
28
|
+
* @param addToContext - If true (default), adds span to current trace context
|
|
29
|
+
*/
|
|
30
|
+
declare function endSpan(span: SpanHandle, addToContext?: boolean): Span;
|
|
31
|
+
/**
|
|
32
|
+
* Capture the current trace context as a trace.
|
|
33
|
+
* Call this at the end of a request/task to record the trace.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* // In Express middleware (after response)
|
|
38
|
+
* withTraceContext({ endpoint: `${req.method} ${req.path}` }, async () => {
|
|
39
|
+
* await handleRequest(req, res);
|
|
40
|
+
* setTraceResponseInfo(res.statusCode, contentLength);
|
|
41
|
+
* captureCurrentTrace(); // Records the trace with all spans
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare function captureCurrentTrace(): void;
|
|
46
|
+
declare function shouldSample(isError: boolean): boolean;
|
|
47
|
+
declare function measureTask(title: string, fn: () => void | Promise<void>): void;
|
|
48
|
+
declare function shutdown(): Promise<void>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Trace context stored in AsyncLocalStorage.
|
|
52
|
+
* Automatically propagates through async operations.
|
|
53
|
+
*/
|
|
54
|
+
interface TraceContext {
|
|
55
|
+
/** Unique trace identifier (UUID v4) */
|
|
56
|
+
traceId: string;
|
|
57
|
+
/** Whether this is a background task (vs HTTP request) */
|
|
58
|
+
isTask: boolean;
|
|
59
|
+
/** When the trace started */
|
|
60
|
+
startedAt: Date;
|
|
61
|
+
/** Collected spans within this trace */
|
|
62
|
+
spans: Span[];
|
|
63
|
+
/** Key-value attributes for this trace */
|
|
64
|
+
attributes: Record<string, string>;
|
|
65
|
+
/** For HTTP traces: endpoint like "GET /api/users" */
|
|
66
|
+
endpoint?: string;
|
|
67
|
+
/** For HTTP traces: response status code */
|
|
68
|
+
statusCode?: number;
|
|
69
|
+
/** For HTTP traces: response body size */
|
|
70
|
+
bodySize?: number;
|
|
71
|
+
/** For HTTP traces: client IP address */
|
|
72
|
+
clientIP?: string;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Options for creating a new trace context.
|
|
76
|
+
*/
|
|
77
|
+
interface TraceContextOptions {
|
|
78
|
+
/** Custom trace ID (auto-generated if not provided) */
|
|
79
|
+
traceId?: string;
|
|
80
|
+
/** Mark as background task instead of HTTP trace */
|
|
81
|
+
isTask?: boolean;
|
|
82
|
+
/** Initial attributes */
|
|
83
|
+
attributes?: Record<string, string>;
|
|
84
|
+
/** HTTP endpoint (e.g., "GET /api/users") */
|
|
85
|
+
endpoint?: string;
|
|
86
|
+
/** Client IP address */
|
|
87
|
+
clientIP?: string;
|
|
88
|
+
}
|
|
89
|
+
declare const asyncLocalStorage: AsyncLocalStorage<TraceContext>;
|
|
90
|
+
/**
|
|
91
|
+
* Get the current trace context, if any.
|
|
92
|
+
* Returns undefined if not within a trace context.
|
|
93
|
+
*/
|
|
94
|
+
declare function getTraceContext(): TraceContext | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* Get the current trace ID, if within a trace context.
|
|
97
|
+
*/
|
|
98
|
+
declare function getTraceId(): string | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* Check if currently within a trace context.
|
|
101
|
+
*/
|
|
102
|
+
declare function hasTraceContext(): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Run a function within a new trace context.
|
|
105
|
+
* All async operations within will have access to this context.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* // HTTP request handler
|
|
110
|
+
* withTraceContext({ endpoint: "GET /api/users", clientIP: req.ip }, async () => {
|
|
111
|
+
* const users = await db.query("SELECT * FROM users");
|
|
112
|
+
* captureException(new Error("oops")); // Auto-linked to trace
|
|
113
|
+
* return users;
|
|
114
|
+
* });
|
|
115
|
+
*
|
|
116
|
+
* // Background task
|
|
117
|
+
* withTraceContext({ isTask: true, endpoint: "process-emails" }, async () => {
|
|
118
|
+
* await processEmails();
|
|
119
|
+
* });
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
declare function withTraceContext<T>(options: TraceContextOptions, fn: () => T): T;
|
|
123
|
+
/**
|
|
124
|
+
* Run a function within a trace context, automatically capturing the trace on completion.
|
|
125
|
+
* This is a convenience wrapper that handles timing and capture automatically.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* // In Express middleware
|
|
130
|
+
* app.use((req, res, next) => {
|
|
131
|
+
* runWithTraceContext(
|
|
132
|
+
* {
|
|
133
|
+
* endpoint: `${req.method} ${req.path}`,
|
|
134
|
+
* clientIP: req.ip,
|
|
135
|
+
* attributes: { userId: req.user?.id },
|
|
136
|
+
* },
|
|
137
|
+
* async () => {
|
|
138
|
+
* await next();
|
|
139
|
+
* // Status code and body size set via setTraceResponseInfo()
|
|
140
|
+
* },
|
|
141
|
+
* { captureOnEnd: true }
|
|
142
|
+
* );
|
|
143
|
+
* });
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
declare function runWithTraceContext<T>(options: TraceContextOptions, fn: () => T | Promise<T>): T | Promise<T>;
|
|
147
|
+
/**
|
|
148
|
+
* Add a completed span to the current trace context.
|
|
149
|
+
* No-op if not within a trace context.
|
|
150
|
+
*/
|
|
151
|
+
declare function addSpanToContext(span: Span): void;
|
|
152
|
+
/**
|
|
153
|
+
* Set an attribute on the current trace context.
|
|
154
|
+
* No-op if not within a trace context.
|
|
155
|
+
*/
|
|
156
|
+
declare function setTraceAttribute(key: string, value: string): void;
|
|
157
|
+
/**
|
|
158
|
+
* Set multiple attributes on the current trace context.
|
|
159
|
+
* No-op if not within a trace context.
|
|
160
|
+
*/
|
|
161
|
+
declare function setTraceAttributes(attributes: Record<string, string>): void;
|
|
162
|
+
/**
|
|
163
|
+
* Set HTTP response info on the current trace context.
|
|
164
|
+
* Used by framework adapters after the response is sent.
|
|
165
|
+
*/
|
|
166
|
+
declare function setTraceResponseInfo(statusCode: number, bodySize?: number): void;
|
|
167
|
+
/**
|
|
168
|
+
* Get all spans from the current trace context.
|
|
169
|
+
* Returns empty array if not within a trace context.
|
|
170
|
+
*/
|
|
171
|
+
declare function getTraceSpans(): Span[];
|
|
172
|
+
/**
|
|
173
|
+
* Get the duration in milliseconds since the trace started.
|
|
174
|
+
* Returns 0 if not within a trace context.
|
|
175
|
+
*/
|
|
176
|
+
declare function getTraceDuration(): number;
|
|
177
|
+
/**
|
|
178
|
+
* Fork the current trace context for a sub-operation.
|
|
179
|
+
* Useful for parallel operations that should have isolated spans.
|
|
180
|
+
* Returns undefined if not within a trace context.
|
|
181
|
+
*/
|
|
182
|
+
declare function forkTraceContext<T>(fn: () => T): T | undefined;
|
|
183
|
+
|
|
184
|
+
declare function formatErrorStackTrace(error: Error): string;
|
|
185
|
+
|
|
186
|
+
declare function collectMetrics(): MetricRecord[];
|
|
187
|
+
declare function resetCpuTracking(): void;
|
|
188
|
+
|
|
189
|
+
interface CollectionFrameStoreOptions {
|
|
190
|
+
apiUrl: string;
|
|
191
|
+
token: string;
|
|
192
|
+
debug: boolean;
|
|
193
|
+
maxCollectionFrames: number;
|
|
194
|
+
collectionInterval: number;
|
|
195
|
+
uploadThrottle: number;
|
|
196
|
+
metricsInterval: number;
|
|
197
|
+
version: string;
|
|
198
|
+
serverName: string;
|
|
199
|
+
sampleRate: number;
|
|
200
|
+
errorSampleRate: number;
|
|
201
|
+
}
|
|
202
|
+
declare class CollectionFrameStore {
|
|
203
|
+
private current;
|
|
204
|
+
private currentSetAt;
|
|
205
|
+
private sendQueue;
|
|
206
|
+
private lastUploadStarted;
|
|
207
|
+
private collectionTimer;
|
|
208
|
+
private metricsTimer;
|
|
209
|
+
private readonly apiUrl;
|
|
210
|
+
private readonly token;
|
|
211
|
+
private readonly debug;
|
|
212
|
+
private readonly collectionInterval;
|
|
213
|
+
private readonly uploadThrottle;
|
|
214
|
+
private readonly metricsInterval;
|
|
215
|
+
private readonly version;
|
|
216
|
+
private readonly serverName;
|
|
217
|
+
readonly sampleRate: number;
|
|
218
|
+
readonly errorSampleRate: number;
|
|
219
|
+
constructor(options: CollectionFrameStoreOptions);
|
|
220
|
+
private tick;
|
|
221
|
+
private ensureCurrent;
|
|
222
|
+
addException(exception: ExceptionStackTrace): void;
|
|
223
|
+
addMetric(metric: MetricRecord): void;
|
|
224
|
+
addTrace(trace: Trace): void;
|
|
225
|
+
private rotateCurrentCollectionFrame;
|
|
226
|
+
private processSendQueue;
|
|
227
|
+
private triggerUpload;
|
|
228
|
+
private collectSystemMetrics;
|
|
229
|
+
shutdown(): Promise<void>;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export { CollectionFrameStore, type CollectionFrameStoreOptions, type SpanHandle, type TraceContext, type TraceContextOptions, addSpanToContext, captureCurrentTrace, captureException, captureExceptionWithAttributes, captureMessage, captureMetric, captureTask, captureTrace, collectMetrics, endSpan, forkTraceContext, formatErrorStackTrace, getTraceContext, getTraceDuration, getTraceId, getTraceSpans, hasTraceContext, init, measureTask, resetCpuTracking, runWithTraceContext, setTraceAttribute, setTraceAttributes, setTraceResponseInfo, shouldSample, shutdown, startSpan, asyncLocalStorage as traceContextStorage, withTraceContext };
|