autotel-tanstack 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.
- package/LICENSE +21 -0
- package/README.md +294 -0
- package/dist/auto.d.ts +44 -0
- package/dist/auto.js +47 -0
- package/dist/auto.js.map +1 -0
- package/dist/browser/context.d.ts +48 -0
- package/dist/browser/context.js +3 -0
- package/dist/browser/context.js.map +1 -0
- package/dist/browser/debug-headers.d.ts +16 -0
- package/dist/browser/debug-headers.js +3 -0
- package/dist/browser/debug-headers.js.map +1 -0
- package/dist/browser/error-reporting.d.ts +37 -0
- package/dist/browser/error-reporting.js +3 -0
- package/dist/browser/error-reporting.js.map +1 -0
- package/dist/browser/handlers.d.ts +19 -0
- package/dist/browser/handlers.js +3 -0
- package/dist/browser/handlers.js.map +1 -0
- package/dist/browser/index.d.ts +10 -0
- package/dist/browser/index.js +12 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/loaders.d.ts +36 -0
- package/dist/browser/loaders.js +3 -0
- package/dist/browser/loaders.js.map +1 -0
- package/dist/browser/metrics.d.ts +54 -0
- package/dist/browser/metrics.js +3 -0
- package/dist/browser/metrics.js.map +1 -0
- package/dist/browser/middleware.d.ts +39 -0
- package/dist/browser/middleware.js +3 -0
- package/dist/browser/middleware.js.map +1 -0
- package/dist/browser/server-functions.d.ts +19 -0
- package/dist/browser/server-functions.js +3 -0
- package/dist/browser/server-functions.js.map +1 -0
- package/dist/browser/testing.d.ts +45 -0
- package/dist/browser/testing.js +3 -0
- package/dist/browser/testing.js.map +1 -0
- package/dist/browser/types.d.ts +85 -0
- package/dist/browser/types.js +3 -0
- package/dist/browser/types.js.map +1 -0
- package/dist/chunk-4C7T5ZIM.js +20 -0
- package/dist/chunk-4C7T5ZIM.js.map +1 -0
- package/dist/chunk-CSFIPJC2.js +11 -0
- package/dist/chunk-CSFIPJC2.js.map +1 -0
- package/dist/chunk-DTZCOB4W.js +32 -0
- package/dist/chunk-DTZCOB4W.js.map +1 -0
- package/dist/chunk-EGRHWZRV.js +3 -0
- package/dist/chunk-EGRHWZRV.js.map +1 -0
- package/dist/chunk-EUYFVNYE.js +16 -0
- package/dist/chunk-EUYFVNYE.js.map +1 -0
- package/dist/chunk-HIQYW2HB.js +20 -0
- package/dist/chunk-HIQYW2HB.js.map +1 -0
- package/dist/chunk-HKM7LMO6.js +129 -0
- package/dist/chunk-HKM7LMO6.js.map +1 -0
- package/dist/chunk-I4LX3LOG.js +35 -0
- package/dist/chunk-I4LX3LOG.js.map +1 -0
- package/dist/chunk-JSI6QG7M.js +96 -0
- package/dist/chunk-JSI6QG7M.js.map +1 -0
- package/dist/chunk-JXO7H6KO.js +10 -0
- package/dist/chunk-JXO7H6KO.js.map +1 -0
- package/dist/chunk-MFYOV2SF.js +32 -0
- package/dist/chunk-MFYOV2SF.js.map +1 -0
- package/dist/chunk-MNP65ZX7.js +21 -0
- package/dist/chunk-MNP65ZX7.js.map +1 -0
- package/dist/chunk-NTY64BKS.js +38 -0
- package/dist/chunk-NTY64BKS.js.map +1 -0
- package/dist/chunk-OLBHLVLE.js +220 -0
- package/dist/chunk-OLBHLVLE.js.map +1 -0
- package/dist/chunk-TNOQTZ3N.js +92 -0
- package/dist/chunk-TNOQTZ3N.js.map +1 -0
- package/dist/chunk-UMEJU65Q.js +34 -0
- package/dist/chunk-UMEJU65Q.js.map +1 -0
- package/dist/chunk-UTPW3QRT.js +52 -0
- package/dist/chunk-UTPW3QRT.js.map +1 -0
- package/dist/chunk-V3RO5N2M.js +8 -0
- package/dist/chunk-V3RO5N2M.js.map +1 -0
- package/dist/chunk-XXBHZR3M.js +99 -0
- package/dist/chunk-XXBHZR3M.js.map +1 -0
- package/dist/chunk-Z3MJ3GZ6.js +18 -0
- package/dist/chunk-Z3MJ3GZ6.js.map +1 -0
- package/dist/chunk-Z5D2V4DU.js +216 -0
- package/dist/chunk-Z5D2V4DU.js.map +1 -0
- package/dist/context.d.ts +94 -0
- package/dist/context.js +4 -0
- package/dist/context.js.map +1 -0
- package/dist/debug-headers.d.ts +43 -0
- package/dist/debug-headers.js +5 -0
- package/dist/debug-headers.js.map +1 -0
- package/dist/error-reporting.d.ts +118 -0
- package/dist/error-reporting.js +4 -0
- package/dist/error-reporting.js.map +1 -0
- package/dist/handlers.d.ts +70 -0
- package/dist/handlers.js +6 -0
- package/dist/handlers.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/loaders.d.ts +124 -0
- package/dist/loaders.js +6 -0
- package/dist/loaders.js.map +1 -0
- package/dist/metrics.d.ts +113 -0
- package/dist/metrics.js +4 -0
- package/dist/metrics.js.map +1 -0
- package/dist/middleware.d.ts +104 -0
- package/dist/middleware.js +7 -0
- package/dist/middleware.js.map +1 -0
- package/dist/server-functions.d.ts +71 -0
- package/dist/server-functions.js +6 -0
- package/dist/server-functions.js.map +1 -0
- package/dist/testing.d.ts +128 -0
- package/dist/testing.js +110 -0
- package/dist/testing.js.map +1 -0
- package/dist/types-C37KSxMN.d.ts +152 -0
- package/package.json +166 -0
- package/src/auto.ts +86 -0
- package/src/browser/context.ts +88 -0
- package/src/browser/debug-headers.ts +19 -0
- package/src/browser/error-reporting.ts +63 -0
- package/src/browser/handlers.ts +23 -0
- package/src/browser/index.ts +65 -0
- package/src/browser/loaders.ts +62 -0
- package/src/browser/metrics.ts +86 -0
- package/src/browser/middleware.ts +61 -0
- package/src/browser/server-functions.ts +31 -0
- package/src/browser/testing.ts +67 -0
- package/src/browser/types.ts +100 -0
- package/src/context.test.ts +90 -0
- package/src/context.ts +145 -0
- package/src/debug-headers.ts +109 -0
- package/src/env.ts +56 -0
- package/src/error-reporting.ts +204 -0
- package/src/handlers.ts +339 -0
- package/src/index.ts +92 -0
- package/src/loaders.test.ts +123 -0
- package/src/loaders.ts +267 -0
- package/src/metrics.ts +183 -0
- package/src/middleware.test.ts +191 -0
- package/src/middleware.ts +400 -0
- package/src/server-functions.test.ts +86 -0
- package/src/server-functions.ts +184 -0
- package/src/testing.test.ts +72 -0
- package/src/testing.ts +276 -0
- package/src/types.test.ts +46 -0
- package/src/types.ts +182 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { MiddlewareHandler } from './middleware.js';
|
|
2
|
+
import './types-C37KSxMN.js';
|
|
3
|
+
import '@opentelemetry/api';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for debug headers middleware
|
|
7
|
+
*/
|
|
8
|
+
interface DebugHeadersConfig {
|
|
9
|
+
/**
|
|
10
|
+
* Whether to enable debug headers
|
|
11
|
+
* @default process.env.NODE_ENV === 'development'
|
|
12
|
+
*/
|
|
13
|
+
enabled?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Custom headers to add
|
|
16
|
+
*/
|
|
17
|
+
customHeaders?: Record<string, string | (() => string)>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create middleware that adds debug headers to responses in development
|
|
21
|
+
*
|
|
22
|
+
* Adds helpful debug information to response headers:
|
|
23
|
+
* - X-Debug-Timestamp: Request timestamp
|
|
24
|
+
* - X-Debug-Node-Version: Node.js version
|
|
25
|
+
* - X-Debug-Uptime: Process uptime in seconds
|
|
26
|
+
* - X-Debug-Trace-Id: Current trace ID (if available)
|
|
27
|
+
*
|
|
28
|
+
* @param config - Configuration options
|
|
29
|
+
* @returns Middleware handler
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { createStart } from '@tanstack/react-start';
|
|
34
|
+
* import { debugHeadersMiddleware } from 'autotel-tanstack/debug-headers';
|
|
35
|
+
*
|
|
36
|
+
* export const startInstance = createStart(() => ({
|
|
37
|
+
* requestMiddleware: [debugHeadersMiddleware()],
|
|
38
|
+
* }));
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function debugHeadersMiddleware(config?: DebugHeadersConfig): MiddlewareHandler;
|
|
42
|
+
|
|
43
|
+
export { type DebugHeadersConfig, debugHeadersMiddleware };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"debug-headers.js"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import * as _tanstack_react_start from '@tanstack/react-start';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Error reporting utilities for TanStack Start
|
|
5
|
+
*
|
|
6
|
+
* Provides basic error reporting without external dependencies,
|
|
7
|
+
* following the patterns from TanStack Start observability guide.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Error report data structure
|
|
11
|
+
*/
|
|
12
|
+
interface ErrorReport {
|
|
13
|
+
id: string;
|
|
14
|
+
count: number;
|
|
15
|
+
lastSeen: Date;
|
|
16
|
+
error: {
|
|
17
|
+
name: string;
|
|
18
|
+
message: string;
|
|
19
|
+
stack?: string;
|
|
20
|
+
context?: unknown;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Error store for in-memory error tracking
|
|
25
|
+
*
|
|
26
|
+
* Stores error reports with deduplication by error name + message.
|
|
27
|
+
* Thread-safe for concurrent access.
|
|
28
|
+
*/
|
|
29
|
+
declare class ErrorStore {
|
|
30
|
+
private errors;
|
|
31
|
+
private readonly maxErrors;
|
|
32
|
+
/**
|
|
33
|
+
* Report an error
|
|
34
|
+
*/
|
|
35
|
+
reportError(error: Error, context?: unknown): string;
|
|
36
|
+
/**
|
|
37
|
+
* Get all error reports
|
|
38
|
+
*/
|
|
39
|
+
getAllErrors(): ErrorReport[];
|
|
40
|
+
/**
|
|
41
|
+
* Get a specific error by ID
|
|
42
|
+
*/
|
|
43
|
+
getError(id: string): ErrorReport | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Clear all errors
|
|
46
|
+
*/
|
|
47
|
+
clear(): void;
|
|
48
|
+
/**
|
|
49
|
+
* Clear a specific error
|
|
50
|
+
*/
|
|
51
|
+
clearError(id: string): void;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Global error store instance
|
|
55
|
+
*/
|
|
56
|
+
declare const errorStore: ErrorStore;
|
|
57
|
+
/**
|
|
58
|
+
* Report an error to the error store
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* import { reportError } from 'autotel-tanstack/error-reporting';
|
|
63
|
+
*
|
|
64
|
+
* try {
|
|
65
|
+
* await riskyOperation();
|
|
66
|
+
* } catch (error) {
|
|
67
|
+
* reportError(error as Error, {
|
|
68
|
+
* userId: context.userId,
|
|
69
|
+
* operation: 'riskyOperation',
|
|
70
|
+
* });
|
|
71
|
+
* throw error;
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
declare function reportError(error: Error, context?: unknown): string;
|
|
76
|
+
/**
|
|
77
|
+
* Create an error reporting endpoint handler
|
|
78
|
+
*
|
|
79
|
+
* Returns a handler that exposes error reports in JSON format.
|
|
80
|
+
* Use this to create an `/admin/errors` endpoint.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* // routes/admin/errors.ts
|
|
85
|
+
* import { createFileRoute } from '@tanstack/react-router';
|
|
86
|
+
* import { json } from '@tanstack/react-start';
|
|
87
|
+
* import { createErrorReportingHandler } from 'autotel-tanstack/error-reporting';
|
|
88
|
+
*
|
|
89
|
+
* export const Route = createFileRoute('/admin/errors')({
|
|
90
|
+
* server: {
|
|
91
|
+
* handlers: {
|
|
92
|
+
* GET: createErrorReportingHandler(),
|
|
93
|
+
* },
|
|
94
|
+
* },
|
|
95
|
+
* });
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
declare function createErrorReportingHandler(): () => Promise<_tanstack_react_start.JsonResponse<{
|
|
99
|
+
errors: ErrorReport[];
|
|
100
|
+
}>>;
|
|
101
|
+
/**
|
|
102
|
+
* Wrap a function with automatic error reporting
|
|
103
|
+
*
|
|
104
|
+
* Automatically reports errors to the error store.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* import { withErrorReporting } from 'autotel-tanstack/error-reporting';
|
|
109
|
+
*
|
|
110
|
+
* const riskyOperation = createServerFn()
|
|
111
|
+
* .handler(withErrorReporting(async () => {
|
|
112
|
+
* return await performOperation();
|
|
113
|
+
* }, { operation: 'riskyOperation' }));
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
declare function withErrorReporting<TArgs extends unknown[], TReturn>(fn: (...args: TArgs) => Promise<TReturn>, context?: Record<string, unknown>): (...args: TArgs) => Promise<TReturn>;
|
|
117
|
+
|
|
118
|
+
export { type ErrorReport, createErrorReportingHandler, errorStore, reportError, withErrorReporting };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"error-reporting.js"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { W as WrapStartHandlerConfig } from './types-C37KSxMN.js';
|
|
2
|
+
import '@opentelemetry/api';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Request handler type (compatible with TanStack Start handlers)
|
|
6
|
+
*/
|
|
7
|
+
type RequestHandler = (request: Request, opts?: {
|
|
8
|
+
context?: Record<string, unknown>;
|
|
9
|
+
}) => Promise<Response> | Response;
|
|
10
|
+
/**
|
|
11
|
+
* Wrap a TanStack Start handler with OpenTelemetry tracing
|
|
12
|
+
*
|
|
13
|
+
* This function wraps the entire request handler to automatically create
|
|
14
|
+
* spans for all incoming requests. It initializes OpenTelemetry and
|
|
15
|
+
* provides comprehensive request tracing.
|
|
16
|
+
*
|
|
17
|
+
* @param config - Configuration options including OTLP endpoint and headers
|
|
18
|
+
* @returns Function that wraps a request handler
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // server.ts
|
|
23
|
+
* import { createStartHandler, defaultStreamHandler } from '@tanstack/react-start/server';
|
|
24
|
+
* import { wrapStartHandler } from 'autotel-tanstack/handlers';
|
|
25
|
+
*
|
|
26
|
+
* export default wrapStartHandler({
|
|
27
|
+
* service: 'my-app',
|
|
28
|
+
* endpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
|
|
29
|
+
* headers: { 'x-honeycomb-team': process.env.HONEYCOMB_API_KEY },
|
|
30
|
+
* })(createStartHandler(defaultStreamHandler));
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* // With env var configuration (recommended for production)
|
|
36
|
+
* // Set OTEL_SERVICE_NAME, OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_HEADERS
|
|
37
|
+
* export default wrapStartHandler()(createStartHandler(defaultStreamHandler));
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare function wrapStartHandler(config?: WrapStartHandlerConfig): (handler: RequestHandler) => RequestHandler;
|
|
41
|
+
/**
|
|
42
|
+
* Create a traced handler without auto-initialization
|
|
43
|
+
*
|
|
44
|
+
* Use this when you want to initialize autotel separately
|
|
45
|
+
* (e.g., with more advanced configuration).
|
|
46
|
+
*
|
|
47
|
+
* @param config - Configuration options (excluding endpoint/headers)
|
|
48
|
+
* @returns Function that wraps a request handler
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import { init } from 'autotel';
|
|
53
|
+
* import { createTracedHandler } from 'autotel-tanstack/handlers';
|
|
54
|
+
*
|
|
55
|
+
* // Initialize autotel with custom configuration
|
|
56
|
+
* init({
|
|
57
|
+
* service: 'my-app',
|
|
58
|
+
* endpoint: 'https://api.honeycomb.io',
|
|
59
|
+
* instrumentations: [/* custom instrumentations *\/],
|
|
60
|
+
* });
|
|
61
|
+
*
|
|
62
|
+
* // Wrap handler without re-initializing
|
|
63
|
+
* export default createTracedHandler({
|
|
64
|
+
* captureHeaders: ['x-request-id'],
|
|
65
|
+
* })(createStartHandler(defaultStreamHandler));
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare function createTracedHandler(config?: Omit<WrapStartHandlerConfig, 'endpoint' | 'headers' | 'service'>): (handler: RequestHandler) => RequestHandler;
|
|
69
|
+
|
|
70
|
+
export { createTracedHandler, wrapStartHandler };
|
package/dist/handlers.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"handlers.js"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export { D as DEFAULT_CONFIG, S as SPAN_ATTRIBUTES, T as TanStackInstrumentationConfig, c as TraceLoaderConfig, b as TraceServerFnConfig, a as TracingMiddlewareConfig, W as WrapStartHandlerConfig } from './types-C37KSxMN.js';
|
|
2
|
+
export { MiddlewareHandler, createTracingMiddleware, functionTracingMiddleware, tracingMiddleware } from './middleware.js';
|
|
3
|
+
export { createTracedServerFnFactory, traceServerFn } from './server-functions.js';
|
|
4
|
+
export { createTracedRoute, traceBeforeLoad, traceLoader } from './loaders.js';
|
|
5
|
+
export { createTracedHandler, wrapStartHandler } from './handlers.js';
|
|
6
|
+
export { createTracedHeaders, extractContextFromRequest, getActiveContext, injectContextToHeaders, runInContext } from './context.js';
|
|
7
|
+
export { DebugHeadersConfig, debugHeadersMiddleware } from './debug-headers.js';
|
|
8
|
+
export { TimingStats, createMetricsHandler, metricsCollector, recordTiming } from './metrics.js';
|
|
9
|
+
export { ErrorReport, createErrorReportingHandler, errorStore, reportError, withErrorReporting } from './error-reporting.js';
|
|
10
|
+
export { flush, init, shutdown, span, trace } from 'autotel';
|
|
11
|
+
import '@opentelemetry/api';
|
|
12
|
+
import '@tanstack/react-start';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Environment detection utilities
|
|
16
|
+
* Prevents server-only code from running in the browser
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Check if we're running in a browser environment
|
|
20
|
+
* Uses typeof checks to avoid TypeScript DOM type requirements
|
|
21
|
+
*/
|
|
22
|
+
declare function isBrowser(): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Check if we're running in a Node.js environment
|
|
25
|
+
*/
|
|
26
|
+
declare function isNode(): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Check if we're in a server-side context
|
|
29
|
+
* In TanStack Start, middleware runs on the server, but router config
|
|
30
|
+
* might be evaluated on both sides during SSR
|
|
31
|
+
*/
|
|
32
|
+
declare function isServerSide(): boolean;
|
|
33
|
+
|
|
34
|
+
export { isBrowser, isNode, isServerSide };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { debugHeadersMiddleware } from './chunk-UTPW3QRT.js';
|
|
2
|
+
export { createMetricsHandler, metricsCollector, recordTiming } from './chunk-JSI6QG7M.js';
|
|
3
|
+
export { createErrorReportingHandler, errorStore, reportError, withErrorReporting } from './chunk-XXBHZR3M.js';
|
|
4
|
+
export { createTracingMiddleware, functionTracingMiddleware, tracingMiddleware } from './chunk-OLBHLVLE.js';
|
|
5
|
+
export { createTracedServerFnFactory, traceServerFn } from './chunk-TNOQTZ3N.js';
|
|
6
|
+
export { createTracedRoute, traceBeforeLoad, traceLoader } from './chunk-HKM7LMO6.js';
|
|
7
|
+
export { isBrowser, isNode, isServerSide } from './chunk-EUYFVNYE.js';
|
|
8
|
+
export { createTracedHandler, wrapStartHandler } from './chunk-Z5D2V4DU.js';
|
|
9
|
+
export { DEFAULT_CONFIG, SPAN_ATTRIBUTES } from './chunk-I4LX3LOG.js';
|
|
10
|
+
export { createTracedHeaders, extractContextFromRequest, getActiveContext, injectContextToHeaders, runInContext } from './chunk-NTY64BKS.js';
|
|
11
|
+
import './chunk-EGRHWZRV.js';
|
|
12
|
+
export { flush, init, shutdown, span, trace } from 'autotel';
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js","sourcesContent":[]}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { c as TraceLoaderConfig } from './types-C37KSxMN.js';
|
|
2
|
+
import '@opentelemetry/api';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Loader context type (compatible with TanStack router loader context)
|
|
6
|
+
*/
|
|
7
|
+
interface LoaderContext {
|
|
8
|
+
params?: Record<string, string>;
|
|
9
|
+
route?: {
|
|
10
|
+
id?: string;
|
|
11
|
+
};
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Wrap a TanStack route loader with OpenTelemetry tracing
|
|
16
|
+
*
|
|
17
|
+
* This function wraps a loader function to automatically create spans
|
|
18
|
+
* for each invocation. It captures route ID, params (optionally),
|
|
19
|
+
* and errors.
|
|
20
|
+
*
|
|
21
|
+
* @param loaderFn - The loader function to wrap
|
|
22
|
+
* @param config - Configuration options
|
|
23
|
+
* @returns Wrapped loader function with tracing
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { createFileRoute } from '@tanstack/react-router';
|
|
28
|
+
* import { traceLoader } from 'autotel-tanstack/loaders';
|
|
29
|
+
*
|
|
30
|
+
* export const Route = createFileRoute('/users/$userId')({
|
|
31
|
+
* loader: traceLoader(async ({ params }) => {
|
|
32
|
+
* return await db.users.findUnique({ where: { id: params.userId } });
|
|
33
|
+
* }),
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* // With custom name and param capture
|
|
40
|
+
* export const Route = createFileRoute('/products/$category/$productId')({
|
|
41
|
+
* loader: traceLoader(
|
|
42
|
+
* async ({ params }) => {
|
|
43
|
+
* return await db.products.findUnique({
|
|
44
|
+
* where: { id: params.productId, category: params.category },
|
|
45
|
+
* });
|
|
46
|
+
* },
|
|
47
|
+
* {
|
|
48
|
+
* name: 'loadProduct',
|
|
49
|
+
* captureParams: true,
|
|
50
|
+
* captureResult: false,
|
|
51
|
+
* }
|
|
52
|
+
* ),
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
declare function traceLoader<T extends (context: LoaderContext) => Promise<unknown>>(loaderFn: T, config?: TraceLoaderConfig): T;
|
|
57
|
+
/**
|
|
58
|
+
* Wrap a TanStack route beforeLoad function with OpenTelemetry tracing
|
|
59
|
+
*
|
|
60
|
+
* This function wraps a beforeLoad function to automatically create spans.
|
|
61
|
+
* beforeLoad runs before the route component renders and is typically
|
|
62
|
+
* used for auth checks, redirects, or data prefetching.
|
|
63
|
+
*
|
|
64
|
+
* @param beforeLoadFn - The beforeLoad function to wrap
|
|
65
|
+
* @param config - Configuration options
|
|
66
|
+
* @returns Wrapped beforeLoad function with tracing
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* import { createFileRoute } from '@tanstack/react-router';
|
|
71
|
+
* import { traceBeforeLoad } from 'autotel-tanstack/loaders';
|
|
72
|
+
*
|
|
73
|
+
* export const Route = createFileRoute('/dashboard')({
|
|
74
|
+
* beforeLoad: traceBeforeLoad(async ({ context }) => {
|
|
75
|
+
* if (!context.auth.isAuthenticated) {
|
|
76
|
+
* throw redirect({ to: '/login' });
|
|
77
|
+
* }
|
|
78
|
+
* }),
|
|
79
|
+
* loader: async () => {
|
|
80
|
+
* return await fetchDashboardData();
|
|
81
|
+
* },
|
|
82
|
+
* });
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
declare function traceBeforeLoad<T extends (context: LoaderContext) => Promise<unknown>>(beforeLoadFn: T, config?: TraceLoaderConfig): T;
|
|
86
|
+
/**
|
|
87
|
+
* Create a traced route configuration helper
|
|
88
|
+
*
|
|
89
|
+
* This higher-order function helps create route configurations
|
|
90
|
+
* with automatic tracing for both loader and beforeLoad.
|
|
91
|
+
*
|
|
92
|
+
* @param routeId - The route identifier
|
|
93
|
+
* @param config - Tracing configuration
|
|
94
|
+
* @returns Object with traced loader and beforeLoad wrappers
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* import { createFileRoute } from '@tanstack/react-router';
|
|
99
|
+
* import { createTracedRoute } from 'autotel-tanstack/loaders';
|
|
100
|
+
*
|
|
101
|
+
* const traced = createTracedRoute('/users/$userId');
|
|
102
|
+
*
|
|
103
|
+
* export const Route = createFileRoute('/users/$userId')({
|
|
104
|
+
* beforeLoad: traced.beforeLoad(async ({ context }) => {
|
|
105
|
+
* // Auth check
|
|
106
|
+
* }),
|
|
107
|
+
* loader: traced.loader(async ({ params }) => {
|
|
108
|
+
* return await getUser(params.userId);
|
|
109
|
+
* }),
|
|
110
|
+
* });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
declare function createTracedRoute(routeId: string, config?: Omit<TraceLoaderConfig, 'name'>): {
|
|
114
|
+
/**
|
|
115
|
+
* Wrap a loader function with tracing
|
|
116
|
+
*/
|
|
117
|
+
loader<T extends (context: LoaderContext) => Promise<unknown>>(loaderFn: T): T;
|
|
118
|
+
/**
|
|
119
|
+
* Wrap a beforeLoad function with tracing
|
|
120
|
+
*/
|
|
121
|
+
beforeLoad<T extends (context: LoaderContext) => Promise<unknown>>(beforeLoadFn: T): T;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export { createTracedRoute, traceBeforeLoad, traceLoader };
|
package/dist/loaders.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"loaders.js"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import * as _tanstack_react_start from '@tanstack/react-start';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Performance metrics collection for TanStack Start
|
|
5
|
+
*
|
|
6
|
+
* Provides utilities to collect and expose performance metrics
|
|
7
|
+
* following the patterns from TanStack Start observability guide.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Performance timing data
|
|
11
|
+
*/
|
|
12
|
+
interface TimingStats {
|
|
13
|
+
count: number;
|
|
14
|
+
avg: number;
|
|
15
|
+
p50: number;
|
|
16
|
+
p95: number;
|
|
17
|
+
min: number;
|
|
18
|
+
max: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Metrics collector for performance data
|
|
22
|
+
*
|
|
23
|
+
* Collects timing metrics and provides statistical analysis.
|
|
24
|
+
* Thread-safe for concurrent access.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { metricsCollector } from 'autotel-tanstack/metrics';
|
|
29
|
+
*
|
|
30
|
+
* // Record a timing
|
|
31
|
+
* metricsCollector.recordTiming('serverFn.getUser', 150);
|
|
32
|
+
*
|
|
33
|
+
* // Get stats
|
|
34
|
+
* const stats = metricsCollector.getStats('serverFn.getUser');
|
|
35
|
+
* console.log(`Average: ${stats.avg}ms, P95: ${stats.p95}ms`);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
declare class MetricsCollector {
|
|
39
|
+
private metrics;
|
|
40
|
+
private readonly maxSamples;
|
|
41
|
+
/**
|
|
42
|
+
* Record a timing measurement
|
|
43
|
+
*/
|
|
44
|
+
recordTiming(name: string, duration: number): void;
|
|
45
|
+
/**
|
|
46
|
+
* Get statistics for a metric
|
|
47
|
+
*/
|
|
48
|
+
getStats(name: string): TimingStats | null;
|
|
49
|
+
/**
|
|
50
|
+
* Get all collected metrics
|
|
51
|
+
*/
|
|
52
|
+
getAllStats(): Record<string, TimingStats>;
|
|
53
|
+
/**
|
|
54
|
+
* Reset all metrics
|
|
55
|
+
*/
|
|
56
|
+
reset(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Reset a specific metric
|
|
59
|
+
*/
|
|
60
|
+
resetMetric(name: string): void;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Global metrics collector instance
|
|
64
|
+
*/
|
|
65
|
+
declare const metricsCollector: MetricsCollector;
|
|
66
|
+
/**
|
|
67
|
+
* Helper to create a metrics endpoint handler
|
|
68
|
+
*
|
|
69
|
+
* Returns a handler that exposes metrics in JSON format.
|
|
70
|
+
* Use this to create a `/metrics` endpoint.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // routes/metrics.ts
|
|
75
|
+
* import { createFileRoute } from '@tanstack/react-router';
|
|
76
|
+
* import { json } from '@tanstack/react-start';
|
|
77
|
+
* import { createMetricsHandler } from 'autotel-tanstack/metrics';
|
|
78
|
+
*
|
|
79
|
+
* export const Route = createFileRoute('/metrics')({
|
|
80
|
+
* server: {
|
|
81
|
+
* handlers: {
|
|
82
|
+
* GET: createMetricsHandler(),
|
|
83
|
+
* },
|
|
84
|
+
* },
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
declare function createMetricsHandler(): () => Promise<_tanstack_react_start.JsonResponse<{
|
|
89
|
+
system: {
|
|
90
|
+
uptime: number;
|
|
91
|
+
memory: NodeJS.MemoryUsage;
|
|
92
|
+
timestamp: string;
|
|
93
|
+
};
|
|
94
|
+
application: Record<string, TimingStats>;
|
|
95
|
+
}>>;
|
|
96
|
+
/**
|
|
97
|
+
* Auto-record timing from a function execution
|
|
98
|
+
*
|
|
99
|
+
* Wraps a function to automatically record its execution time.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* import { recordTiming } from 'autotel-tanstack/metrics';
|
|
104
|
+
*
|
|
105
|
+
* const getUser = createServerFn({ method: 'GET' })
|
|
106
|
+
* .handler(recordTiming('serverFn.getUser', async ({ data: id }) => {
|
|
107
|
+
* return await db.users.findUnique({ where: { id } });
|
|
108
|
+
* }));
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
declare function recordTiming<T extends (...args: unknown[]) => Promise<unknown>>(metricName: string, fn: T): T;
|
|
112
|
+
|
|
113
|
+
export { type TimingStats, createMetricsHandler, metricsCollector, recordTiming };
|
package/dist/metrics.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"metrics.js"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { a as TracingMiddlewareConfig } from './types-C37KSxMN.js';
|
|
2
|
+
import '@opentelemetry/api';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Generic middleware handler type (compatible with TanStack's middleware pattern)
|
|
6
|
+
*
|
|
7
|
+
* This type represents the shape of TanStack middleware handlers.
|
|
8
|
+
* We use a generic type to avoid direct dependency on TanStack packages.
|
|
9
|
+
*/
|
|
10
|
+
interface MiddlewareHandler<TContext = unknown> {
|
|
11
|
+
(opts: {
|
|
12
|
+
next: (ctx?: Partial<TContext>) => Promise<TContext>;
|
|
13
|
+
context: TContext;
|
|
14
|
+
request?: Request;
|
|
15
|
+
pathname?: string;
|
|
16
|
+
data?: unknown;
|
|
17
|
+
method?: string;
|
|
18
|
+
filename?: string;
|
|
19
|
+
functionId?: string;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
}): Promise<TContext>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Create a TanStack-compatible tracing middleware
|
|
25
|
+
*
|
|
26
|
+
* This creates middleware that automatically traces all requests/server functions
|
|
27
|
+
* with OpenTelemetry spans. Use with TanStack Start's middleware system.
|
|
28
|
+
*
|
|
29
|
+
* @param config - Configuration options
|
|
30
|
+
* @returns Middleware handler compatible with TanStack Start
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // Global request middleware in app/start.ts
|
|
35
|
+
* import { createStart } from '@tanstack/react-start';
|
|
36
|
+
* import { createTracingMiddleware } from 'autotel-tanstack/middleware';
|
|
37
|
+
*
|
|
38
|
+
* export const startInstance = createStart(() => ({
|
|
39
|
+
* requestMiddleware: [
|
|
40
|
+
* createTracingMiddleware({
|
|
41
|
+
* captureHeaders: ['x-request-id', 'user-agent'],
|
|
42
|
+
* excludePaths: ['/health', '/metrics'],
|
|
43
|
+
* }),
|
|
44
|
+
* ],
|
|
45
|
+
* }));
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* // Server function middleware
|
|
51
|
+
* import { createServerFn } from '@tanstack/react-start';
|
|
52
|
+
* import { createTracingMiddleware } from 'autotel-tanstack/middleware';
|
|
53
|
+
*
|
|
54
|
+
* export const getUser = createServerFn({ method: 'GET' })
|
|
55
|
+
* .middleware([createTracingMiddleware({ type: 'function' })])
|
|
56
|
+
* .handler(async ({ data: id }) => {
|
|
57
|
+
* return await db.users.findUnique({ where: { id } });
|
|
58
|
+
* });
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
declare function createTracingMiddleware<TContext = unknown>(config?: TracingMiddlewareConfig): MiddlewareHandler<TContext>;
|
|
62
|
+
/**
|
|
63
|
+
* Pre-configured tracing middleware with sensible defaults
|
|
64
|
+
*
|
|
65
|
+
* Convenience export for quick setup. Uses adaptive sampling,
|
|
66
|
+
* captures x-request-id header, and excludes common health check paths.
|
|
67
|
+
*
|
|
68
|
+
* @param config - Optional configuration overrides
|
|
69
|
+
* @returns Middleware handler
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* import { createStart } from '@tanstack/react-start';
|
|
74
|
+
* import { tracingMiddleware } from 'autotel-tanstack/middleware';
|
|
75
|
+
*
|
|
76
|
+
* export const startInstance = createStart(() => ({
|
|
77
|
+
* requestMiddleware: [tracingMiddleware()],
|
|
78
|
+
* }));
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
declare function tracingMiddleware<TContext = unknown>(config?: TracingMiddlewareConfig): MiddlewareHandler<TContext>;
|
|
82
|
+
/**
|
|
83
|
+
* Create function-specific tracing middleware
|
|
84
|
+
*
|
|
85
|
+
* Convenience wrapper for server function middleware.
|
|
86
|
+
*
|
|
87
|
+
* @param config - Optional configuration
|
|
88
|
+
* @returns Middleware handler for server functions
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* import { createServerFn } from '@tanstack/react-start';
|
|
93
|
+
* import { functionTracingMiddleware } from 'autotel-tanstack/middleware';
|
|
94
|
+
*
|
|
95
|
+
* export const getUser = createServerFn({ method: 'GET' })
|
|
96
|
+
* .middleware([functionTracingMiddleware()])
|
|
97
|
+
* .handler(async ({ data: id }) => {
|
|
98
|
+
* return await db.users.findUnique({ where: { id } });
|
|
99
|
+
* });
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
declare function functionTracingMiddleware<TContext = unknown>(config?: Omit<TracingMiddlewareConfig, 'type'>): MiddlewareHandler<TContext>;
|
|
103
|
+
|
|
104
|
+
export { type MiddlewareHandler, createTracingMiddleware, functionTracingMiddleware, tracingMiddleware };
|