@sentienguard/apm 1.0.8 → 1.0.9

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/src/errors.js CHANGED
@@ -1,132 +1,132 @@
1
- /**
2
- * Error Capture
3
- * Captures unhandled exceptions and associates them with error counters.
4
- *
5
- * Stack traces are captured locally but NOT sent to backend in v1.
6
- * Only error counters are aggregated and sent.
7
- */
8
-
9
- import { getAggregator } from './aggregator.js';
10
- import { debug, warn } from './config.js';
11
-
12
- let isCapturing = false;
13
- let originalUncaughtException = null;
14
- let originalUnhandledRejection = null;
15
-
16
- /**
17
- * Handle uncaught exception
18
- */
19
- function handleUncaughtException(error) {
20
- debug('Captured uncaught exception:', error.message);
21
-
22
- const aggregator = getAggregator();
23
- aggregator.recordError();
24
-
25
- // Log locally but don't crash (let the original handler decide)
26
- if (originalUncaughtException) {
27
- // If there was a previous handler, call it
28
- originalUncaughtException(error);
29
- } else {
30
- // Default behavior: log and continue
31
- // We don't re-throw to avoid double handling
32
- warn('Uncaught exception:', error.message);
33
- }
34
- }
35
-
36
- /**
37
- * Handle unhandled promise rejection
38
- */
39
- function handleUnhandledRejection(reason, promise) {
40
- const message = reason instanceof Error ? reason.message : String(reason);
41
- debug('Captured unhandled rejection:', message);
42
-
43
- const aggregator = getAggregator();
44
- aggregator.recordError();
45
-
46
- // Call previous handler if exists
47
- if (originalUnhandledRejection) {
48
- originalUnhandledRejection(reason, promise);
49
- } else {
50
- warn('Unhandled rejection:', message);
51
- }
52
- }
53
-
54
- /**
55
- * Start capturing unhandled errors
56
- */
57
- export function startErrorCapture() {
58
- if (isCapturing) {
59
- debug('Error capture already active, skipping');
60
- return;
61
- }
62
-
63
- // Store any existing handlers
64
- const existingUncaught = process.listeners('uncaughtException');
65
- const existingRejection = process.listeners('unhandledRejection');
66
-
67
- if (existingUncaught.length > 0) {
68
- originalUncaughtException = existingUncaught[existingUncaught.length - 1];
69
- }
70
- if (existingRejection.length > 0) {
71
- originalUnhandledRejection = existingRejection[existingRejection.length - 1];
72
- }
73
-
74
- // Add our handlers
75
- process.on('uncaughtException', handleUncaughtException);
76
- process.on('unhandledRejection', handleUnhandledRejection);
77
-
78
- isCapturing = true;
79
- debug('Error capture enabled');
80
- }
81
-
82
- /**
83
- * Stop capturing errors (for cleanup/testing)
84
- */
85
- export function stopErrorCapture() {
86
- if (!isCapturing) return;
87
-
88
- process.removeListener('uncaughtException', handleUncaughtException);
89
- process.removeListener('unhandledRejection', handleUnhandledRejection);
90
-
91
- originalUncaughtException = null;
92
- originalUnhandledRejection = null;
93
-
94
- isCapturing = false;
95
- debug('Error capture disabled');
96
- }
97
-
98
- /**
99
- * Create Express error middleware
100
- * Use this as the last middleware to capture Express errors
101
- */
102
- export function expressErrorMiddleware() {
103
- return function sentienguardErrorMiddleware(err, req, res, next) {
104
- debug('Captured Express error:', err.message);
105
-
106
- const aggregator = getAggregator();
107
- aggregator.recordError();
108
-
109
- // Pass to next error handler
110
- next(err);
111
- };
112
- }
113
-
114
- /**
115
- * Create Fastify error handler hook
116
- */
117
- export function fastifyErrorHandler(error, request, reply) {
118
- debug('Captured Fastify error:', error.message);
119
-
120
- const aggregator = getAggregator();
121
- aggregator.recordError();
122
-
123
- // Re-throw to let Fastify handle it
124
- throw error;
125
- }
126
-
127
- export default {
128
- startErrorCapture,
129
- stopErrorCapture,
130
- expressErrorMiddleware,
131
- fastifyErrorHandler
132
- };
1
+ /**
2
+ * Error Capture
3
+ * Captures unhandled exceptions and associates them with error counters.
4
+ *
5
+ * Stack traces are captured locally but NOT sent to backend in v1.
6
+ * Only error counters are aggregated and sent.
7
+ */
8
+
9
+ import { getAggregator } from './aggregator.js';
10
+ import { debug, warn } from './config.js';
11
+
12
+ let isCapturing = false;
13
+ let originalUncaughtException = null;
14
+ let originalUnhandledRejection = null;
15
+
16
+ /**
17
+ * Handle uncaught exception
18
+ */
19
+ function handleUncaughtException(error) {
20
+ debug('Captured uncaught exception:', error.message);
21
+
22
+ const aggregator = getAggregator();
23
+ aggregator.recordError();
24
+
25
+ // Log locally but don't crash (let the original handler decide)
26
+ if (originalUncaughtException) {
27
+ // If there was a previous handler, call it
28
+ originalUncaughtException(error);
29
+ } else {
30
+ // Default behavior: log and continue
31
+ // We don't re-throw to avoid double handling
32
+ warn('Uncaught exception:', error.message);
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Handle unhandled promise rejection
38
+ */
39
+ function handleUnhandledRejection(reason, promise) {
40
+ const message = reason instanceof Error ? reason.message : String(reason);
41
+ debug('Captured unhandled rejection:', message);
42
+
43
+ const aggregator = getAggregator();
44
+ aggregator.recordError();
45
+
46
+ // Call previous handler if exists
47
+ if (originalUnhandledRejection) {
48
+ originalUnhandledRejection(reason, promise);
49
+ } else {
50
+ warn('Unhandled rejection:', message);
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Start capturing unhandled errors
56
+ */
57
+ export function startErrorCapture() {
58
+ if (isCapturing) {
59
+ debug('Error capture already active, skipping');
60
+ return;
61
+ }
62
+
63
+ // Store any existing handlers
64
+ const existingUncaught = process.listeners('uncaughtException');
65
+ const existingRejection = process.listeners('unhandledRejection');
66
+
67
+ if (existingUncaught.length > 0) {
68
+ originalUncaughtException = existingUncaught[existingUncaught.length - 1];
69
+ }
70
+ if (existingRejection.length > 0) {
71
+ originalUnhandledRejection = existingRejection[existingRejection.length - 1];
72
+ }
73
+
74
+ // Add our handlers
75
+ process.on('uncaughtException', handleUncaughtException);
76
+ process.on('unhandledRejection', handleUnhandledRejection);
77
+
78
+ isCapturing = true;
79
+ debug('Error capture enabled');
80
+ }
81
+
82
+ /**
83
+ * Stop capturing errors (for cleanup/testing)
84
+ */
85
+ export function stopErrorCapture() {
86
+ if (!isCapturing) return;
87
+
88
+ process.removeListener('uncaughtException', handleUncaughtException);
89
+ process.removeListener('unhandledRejection', handleUnhandledRejection);
90
+
91
+ originalUncaughtException = null;
92
+ originalUnhandledRejection = null;
93
+
94
+ isCapturing = false;
95
+ debug('Error capture disabled');
96
+ }
97
+
98
+ /**
99
+ * Create Express error middleware
100
+ * Use this as the last middleware to capture Express errors
101
+ */
102
+ export function expressErrorMiddleware() {
103
+ return function sentienguardErrorMiddleware(err, req, res, next) {
104
+ debug('Captured Express error:', err.message);
105
+
106
+ const aggregator = getAggregator();
107
+ aggregator.recordError();
108
+
109
+ // Pass to next error handler
110
+ next(err);
111
+ };
112
+ }
113
+
114
+ /**
115
+ * Create Fastify error handler hook
116
+ */
117
+ export function fastifyErrorHandler(error, request, reply) {
118
+ debug('Captured Fastify error:', error.message);
119
+
120
+ const aggregator = getAggregator();
121
+ aggregator.recordError();
122
+
123
+ // Re-throw to let Fastify handle it
124
+ throw error;
125
+ }
126
+
127
+ export default {
128
+ startErrorCapture,
129
+ stopErrorCapture,
130
+ expressErrorMiddleware,
131
+ fastifyErrorHandler
132
+ };
package/src/index.d.ts CHANGED
@@ -1,102 +1,113 @@
1
- /**
2
- * SentienGuard APM SDK TypeScript Declarations
3
- */
4
-
5
- // Configuration types
6
- export interface ApmConfig {
7
- apiKey?: string;
8
- service?: string;
9
- environment?: string;
10
- endpoint?: string;
11
- flushInterval?: number;
12
- maxRoutes?: number;
13
- maxPayloadSize?: number;
14
- enabled?: boolean;
15
- debug?: boolean;
16
- }
17
-
18
- export interface BrowserApmConfig {
19
- apiKey: string;
20
- service: string;
21
- endpoint?: string;
22
- environment?: string;
23
- flushInterval?: number;
24
- debug?: boolean;
25
- }
26
-
27
- export interface ApmStatus {
28
- enabled: boolean;
29
- initialized: boolean;
30
- config: {
31
- service: string;
32
- environment: string;
33
- flushInterval: number;
34
- };
35
- stats: {
36
- requestMetrics?: number;
37
- dependencyMetrics?: number;
38
- webVitals?: number;
39
- jsErrors?: number;
40
- };
41
- }
42
-
43
- // Core functions
44
- export function initialize(options?: BrowserApmConfig): void;
45
- export function init(options?: BrowserApmConfig): void;
46
- export function shutdown(): Promise<void>;
47
- export function getStatus(): ApmStatus;
48
- export function flush(): Promise<void>;
49
- export function getConfig(): ApmConfig;
50
- export function isEnabled(): boolean;
51
-
52
- // Express middleware (no-ops in browser)
53
- export function expressMiddleware(): (req: any, res: any, next: any) => void;
54
- export function expressErrorMiddleware(): (err: any, req: any, res: any, next: any) => void;
55
-
56
- // Fastify plugin (no-ops in browser)
57
- export function fastifyPlugin(): any;
58
- export function fastifyErrorHandler(): any;
59
-
60
- // Route utilities (no-ops in browser)
61
- export function normalizeRoute(route: string): string;
62
- export function extractRoute(req: any): string;
63
- export const RouteRegistry: any;
64
-
65
- // Aggregator
66
- export function getAggregator(): any;
67
-
68
- // MongoDB instrumentation (no-ops in browser)
69
- export function instrumentMongoDB(): void;
70
-
71
- // OpenAI instrumentation (no-ops in browser)
72
- export function instrumentOpenAI(): void;
73
-
74
- // Circuit breaker (no-ops in browser)
75
- export function createBreaker(fn: any): any;
76
- export function wrapMongoOperation(fn: any): any;
77
- export function getBreakerStats(): any;
78
-
79
- // Default export
80
- declare const SentienGuard: {
81
- init: typeof init;
82
- initialize: typeof initialize;
83
- shutdown: typeof shutdown;
84
- getStatus: typeof getStatus;
85
- flush: typeof flush;
86
- getConfig: typeof getConfig;
87
- isEnabled: typeof isEnabled;
88
- getAggregator: typeof getAggregator;
89
- expressMiddleware: typeof expressMiddleware;
90
- expressErrorMiddleware: typeof expressErrorMiddleware;
91
- fastifyPlugin: typeof fastifyPlugin;
92
- fastifyErrorHandler: typeof fastifyErrorHandler;
93
- normalizeRoute: typeof normalizeRoute;
94
- extractRoute: typeof extractRoute;
95
- instrumentMongoDB: typeof instrumentMongoDB;
96
- instrumentOpenAI: typeof instrumentOpenAI;
97
- createBreaker: typeof createBreaker;
98
- wrapMongoOperation: typeof wrapMongoOperation;
99
- getBreakerStats: typeof getBreakerStats;
100
- };
101
-
102
- export default SentienGuard;
1
+ /**
2
+ * SentienGuard APM SDK TypeScript Declarations
3
+ */
4
+
5
+ // Configuration types
6
+ export interface ApmConfig {
7
+ apiKey?: string;
8
+ service?: string;
9
+ environment?: string;
10
+ endpoint?: string;
11
+ flushInterval?: number;
12
+ maxRoutes?: number;
13
+ maxPayloadSize?: number;
14
+ enabled?: boolean;
15
+ debug?: boolean;
16
+ tracing?: {
17
+ enabled?: boolean;
18
+ traceLocalHttp?: boolean;
19
+ peerServiceMap?: Record<string, string>;
20
+ };
21
+ }
22
+
23
+ export interface BrowserApmConfig {
24
+ apiKey: string;
25
+ service: string;
26
+ endpoint?: string;
27
+ environment?: string;
28
+ flushInterval?: number;
29
+ debug?: boolean;
30
+ }
31
+
32
+ export interface ApmStatus {
33
+ enabled: boolean;
34
+ initialized: boolean;
35
+ /** True when OpenTelemetry tracing is active (W3C trace context) */
36
+ tracing?: boolean;
37
+ config: {
38
+ service: string;
39
+ environment: string;
40
+ flushInterval: number;
41
+ };
42
+ stats: {
43
+ requestMetrics?: number;
44
+ dependencyMetrics?: number;
45
+ webVitals?: number;
46
+ jsErrors?: number;
47
+ };
48
+ }
49
+
50
+ // Core functions
51
+ export function initialize(options?: BrowserApmConfig): void;
52
+ export function init(options?: BrowserApmConfig): void;
53
+ export function shutdown(): Promise<void>;
54
+ export function getStatus(): ApmStatus;
55
+ export function flush(): Promise<void>;
56
+ export function getConfig(): ApmConfig;
57
+ export function isEnabled(): boolean;
58
+
59
+ /** Active OpenTelemetry trace id (hex) for log correlation; undefined if none */
60
+ export function getActiveTraceId(): string | undefined;
61
+
62
+ // Express middleware (no-ops in browser)
63
+ export function expressMiddleware(): (req: any, res: any, next: any) => void;
64
+ export function expressErrorMiddleware(): (err: any, req: any, res: any, next: any) => void;
65
+
66
+ // Fastify plugin (no-ops in browser)
67
+ export function fastifyPlugin(): any;
68
+ export function fastifyErrorHandler(): any;
69
+
70
+ // Route utilities (no-ops in browser)
71
+ export function normalizeRoute(route: string): string;
72
+ export function extractRoute(req: any): string;
73
+ export const RouteRegistry: any;
74
+
75
+ // Aggregator
76
+ export function getAggregator(): any;
77
+
78
+ // MongoDB instrumentation (no-ops in browser)
79
+ export function instrumentMongoDB(): void;
80
+
81
+ // OpenAI instrumentation (no-ops in browser)
82
+ export function instrumentOpenAI(): void;
83
+
84
+ // Circuit breaker (no-ops in browser)
85
+ export function createBreaker(fn: any): any;
86
+ export function wrapMongoOperation(fn: any): any;
87
+ export function getBreakerStats(): any;
88
+
89
+ // Default export
90
+ declare const SentienGuard: {
91
+ init: typeof init;
92
+ initialize: typeof initialize;
93
+ shutdown: typeof shutdown;
94
+ getStatus: typeof getStatus;
95
+ flush: typeof flush;
96
+ getConfig: typeof getConfig;
97
+ isEnabled: typeof isEnabled;
98
+ getAggregator: typeof getAggregator;
99
+ expressMiddleware: typeof expressMiddleware;
100
+ expressErrorMiddleware: typeof expressErrorMiddleware;
101
+ fastifyPlugin: typeof fastifyPlugin;
102
+ fastifyErrorHandler: typeof fastifyErrorHandler;
103
+ normalizeRoute: typeof normalizeRoute;
104
+ extractRoute: typeof extractRoute;
105
+ instrumentMongoDB: typeof instrumentMongoDB;
106
+ instrumentOpenAI: typeof instrumentOpenAI;
107
+ createBreaker: typeof createBreaker;
108
+ wrapMongoOperation: typeof wrapMongoOperation;
109
+ getBreakerStats: typeof getBreakerStats;
110
+ getActiveTraceId: typeof getActiveTraceId;
111
+ };
112
+
113
+ export default SentienGuard;