@senzops/apm-node 1.2.4 → 1.2.6

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.
@@ -1,7 +1,7 @@
1
- import { client } from '../core/client';
2
- import { normalizePath } from '../core/normalizer';
3
- import { invokeWithFrameworkSpan } from '../instrumentation/framework';
4
- import { getClientIp } from '../utils/getClientIp';
1
+ import { client } from '../core/client';
2
+ import { normalizePath } from '../core/normalizer';
3
+ import { invokeWithFrameworkSpan } from '../instrumentation/framework';
4
+ import { getClientIp } from '../utils/getClientIp';
5
5
 
6
6
  // --- App Router Wrapper ---
7
7
  export const wrapNextRoute = (handler: Function) => {
@@ -38,39 +38,40 @@ export const wrapNextRoute = (handler: Function) => {
38
38
  userAgent: ua,
39
39
  ip: ip || getClientIp(req),
40
40
  headers: headers // Pass extracted headers
41
- }, async () => {
42
- try {
43
- const route = normalizePath(url.pathname);
44
- const response = await invokeWithFrameworkSpan(
45
- handler,
46
- undefined,
47
- [req, context],
48
- {
49
- framework: 'next',
50
- type: 'route_handler',
51
- name: `next.app_route_handler ${method} ${route}`,
52
- route,
53
- method,
54
- request: req,
55
- attributes: {
56
- 'next.router': 'app',
57
- 'http.route': route,
58
- 'url.path': url.pathname
59
- }
60
- },
61
- undefined,
62
- {
63
- callbackCompletesSpan: false,
64
- responseEndsSpan: false
65
- }
66
- );
67
- const status = response?.status || 200;
68
-
69
- client.endTrace(status, { route });
70
- return response;
71
- } catch (err: any) {
72
- client.captureError(err);
73
- client.endTrace(500, { route: normalizePath(url.pathname) });
41
+ }, async () => {
42
+ try {
43
+ const route = normalizePath(url.pathname);
44
+ const response = await invokeWithFrameworkSpan(
45
+ handler,
46
+ undefined,
47
+ [req, context],
48
+ {
49
+ framework: 'next',
50
+ type: 'route_handler',
51
+ name: `next.app_route_handler ${method} ${route}`,
52
+ route,
53
+ method,
54
+ handlerName: (handler as any).name || 'handler',
55
+ request: req,
56
+ attributes: {
57
+ 'next.router': 'app',
58
+ 'http.route': route,
59
+ 'url.path': url.pathname
60
+ }
61
+ },
62
+ undefined,
63
+ {
64
+ callbackCompletesSpan: false,
65
+ responseEndsSpan: false
66
+ }
67
+ );
68
+ const status = response?.status || 200;
69
+
70
+ client.endTrace(status, { route });
71
+ return response;
72
+ } catch (err: any) {
73
+ client.captureError(err);
74
+ client.endTrace(500, { route: normalizePath(url.pathname) });
74
75
  throw err;
75
76
  }
76
77
  });
@@ -95,37 +96,38 @@ export const wrapNextPages = (handler: Function) => {
95
96
  };
96
97
 
97
98
  res.once('finish', done);
98
- res.once('close', done);
99
-
100
- try {
101
- const route = normalizePath(path);
102
- return await invokeWithFrameworkSpan(
103
- handler,
104
- undefined,
105
- [req, res],
106
- {
107
- framework: 'next',
108
- type: 'route_handler',
109
- name: `next.pages_api_handler ${req.method || 'GET'} ${route}`,
110
- route,
111
- method: req.method || 'GET',
112
- request: req,
113
- response: res,
114
- attributes: {
115
- 'next.router': 'pages',
116
- 'http.route': route
117
- }
118
- },
119
- undefined,
120
- {
121
- callbackCompletesSpan: false,
122
- responseEndsSpan: true
123
- }
124
- );
125
- } catch (e: any) {
126
- client.captureError(e);
127
- throw e;
99
+ res.once('close', done);
100
+
101
+ try {
102
+ const route = normalizePath(path);
103
+ return await invokeWithFrameworkSpan(
104
+ handler,
105
+ undefined,
106
+ [req, res],
107
+ {
108
+ framework: 'next',
109
+ type: 'route_handler',
110
+ name: `next.pages_api_handler ${req.method || 'GET'} ${route}`,
111
+ route,
112
+ method: req.method || 'GET',
113
+ handlerName: (handler as any).name || 'handler',
114
+ request: req,
115
+ response: res,
116
+ attributes: {
117
+ 'next.router': 'pages',
118
+ 'http.route': route
119
+ }
120
+ },
121
+ undefined,
122
+ {
123
+ callbackCompletesSpan: false,
124
+ responseEndsSpan: true
125
+ }
126
+ );
127
+ } catch (e: any) {
128
+ client.captureError(e);
129
+ throw e;
128
130
  }
129
131
  });
130
132
  };
131
- };
133
+ };
@@ -0,0 +1,60 @@
1
+ import { client } from '../core/client';
2
+ import { getRoute, normalizePath } from '../core/normalizer';
3
+ import { getClientIp } from '../utils/getClientIp';
4
+
5
+ export interface NitroApp {
6
+ h3App: {
7
+ handler: (event: any) => Promise<any>;
8
+ stack: any[];
9
+ };
10
+ hooks: any;
11
+ [key: string]: any;
12
+ }
13
+
14
+ export const nitroPlugin = (nitroApp: NitroApp) => {
15
+ if (!nitroApp.h3App || !nitroApp.h3App.handler) return;
16
+
17
+ const originalHandler = nitroApp.h3App.handler;
18
+
19
+ nitroApp.h3App.handler = async (event: any) => {
20
+ const req = event.node?.req || event.req;
21
+ const path = req?.originalUrl || req?.url || (event.path as string) || '/';
22
+ const method = req?.method || (event.method as string) || 'GET';
23
+
24
+ const headers = req?.headers || {};
25
+
26
+ return client.startTrace({
27
+ method,
28
+ path,
29
+ route: normalizePath(path),
30
+ ip: getClientIp({ headers, socket: req?.socket }),
31
+ userAgent: headers['user-agent'],
32
+ headers
33
+ }, async () => {
34
+ let status = 200;
35
+ try {
36
+ const response = await originalHandler(event);
37
+
38
+ if (event.node?.res?.statusCode) status = event.node.res.statusCode;
39
+ if (response?.status) status = response.status;
40
+
41
+ client.endTrace(status, { route: getRoute(event, path) });
42
+ return response;
43
+ } catch (err: any) {
44
+ status = err.statusCode || err.status || 500;
45
+ client.captureError(err);
46
+ client.endTrace(status, { route: getRoute(event, path) });
47
+ throw err;
48
+ } finally {
49
+ const cfCtx = event.context?.cloudflare?.context || event.context?.cf || event.context;
50
+ const waitUntil = cfCtx?.waitUntil || event.waitUntil;
51
+
52
+ if (waitUntil && typeof waitUntil === 'function') {
53
+ waitUntil(client.flush());
54
+ } else {
55
+ client.flush().catch(() => {});
56
+ }
57
+ }
58
+ });
59
+ };
60
+ };
@@ -0,0 +1,45 @@
1
+ import { client } from '../core/client';
2
+ import { normalizePath } from '../core/normalizer';
3
+ import { getClientIp } from '../utils/getClientIp';
4
+
5
+ type WorkerHandler = (request: Request, env: any, ctx: any) => Promise<Response>;
6
+
7
+ export const wrapWorker = (handler: WorkerHandler) => {
8
+ return async (request: Request, env: any, ctx: any) => {
9
+ const url = new URL(request.url);
10
+ const path = url.pathname;
11
+ const method = request.method || 'GET';
12
+
13
+ const headers: Record<string, string> = {};
14
+ request.headers.forEach((value, key) => {
15
+ headers[key] = value;
16
+ });
17
+
18
+ return client.startTrace({
19
+ method,
20
+ path,
21
+ route: normalizePath(path),
22
+ ip: getClientIp({ headers }),
23
+ userAgent: headers['user-agent'],
24
+ headers
25
+ }, async () => {
26
+ let status = 500;
27
+ try {
28
+ const response = await handler(request, env, ctx);
29
+ status = response.status;
30
+ return response;
31
+ } catch (err: any) {
32
+ client.captureError(err);
33
+ throw err;
34
+ } finally {
35
+ client.endTrace(status, { route: normalizePath(path) });
36
+
37
+ if (ctx && typeof ctx.waitUntil === 'function') {
38
+ ctx.waitUntil(client.flush());
39
+ } else {
40
+ await client.flush();
41
+ }
42
+ }
43
+ });
44
+ };
45
+ };
package/tsup.config.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import { defineConfig } from 'tsup';
2
2
 
3
+ const NODE_BUILTINS = [
4
+ 'http', 'https', 'url', 'net', 'module', 'crypto', 'async_hooks',
5
+ 'node:http', 'node:https', 'node:url', 'node:net', 'node:module',
6
+ 'node:crypto', 'node:async_hooks'
7
+ ];
8
+
3
9
  export default defineConfig([
4
10
  {
5
11
  entry: ['src/index.ts', 'src/register.ts'],
@@ -9,6 +15,8 @@ export default defineConfig([
9
15
  minify: true,
10
16
  sourcemap: true,
11
17
  splitting: false,
18
+ external: NODE_BUILTINS,
19
+ noExternal: [],
12
20
  },
13
21
  {
14
22
  entry: ['src/index.ts'],
@@ -17,5 +25,6 @@ export default defineConfig([
17
25
  minify: true,
18
26
  sourcemap: true,
19
27
  splitting: false,
28
+ external: NODE_BUILTINS,
20
29
  }
21
30
  ]);