@senzops/apm-node 1.1.4 → 1.1.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.
@@ -4,27 +4,49 @@ import { normalizePath } from '../core/normalizer';
4
4
  // --- App Router Wrapper ---
5
5
  export const wrapNextRoute = (handler: Function) => {
6
6
  return async (req: Request | any, context?: any) => {
7
+
8
+ // Extract info from Web Standard Request
7
9
  const url = req.url ? new URL(req.url) : { pathname: '/' };
8
10
  const method = req.method || 'GET';
9
- const ua = req.headers.get ? req.headers.get('user-agent') : undefined;
10
- const ip = req.headers.get ? req.headers.get('x-forwarded-for') : undefined;
11
+
12
+ // Header Extraction
13
+ let headers: Record<string, string> = {};
14
+ let ua: string | undefined;
15
+ let ip: string | undefined;
16
+
17
+ if (typeof req.headers.get === 'function') {
18
+ // It's a Web Request Object
19
+ ua = req.headers.get('user-agent');
20
+ ip = req.headers.get('x-forwarded-for');
21
+
22
+ // Convert to plain object for trace context extraction
23
+ req.headers.forEach((value: string, key: string) => {
24
+ headers[key] = value;
25
+ });
26
+ } else {
27
+ // It's a Node Request Object (rare in App router but possible)
28
+ headers = req.headers;
29
+ ua = headers['user-agent'];
30
+ ip = headers['x-forwarded-for'] as string;
31
+ }
11
32
 
12
33
  return client.startTrace({
13
34
  method,
14
35
  path: url.pathname,
15
36
  userAgent: ua,
16
- ip: ip
37
+ ip: ip,
38
+ headers: headers // Pass extracted headers
17
39
  }, async () => {
18
40
  try {
19
41
  const response = await handler(req, context);
20
42
  const status = response?.status || 200;
43
+
21
44
  client.endTrace(status, { route: normalizePath(url.pathname) });
22
45
  return response;
23
46
  } catch (err: any) {
24
- // AUTOMATIC ERROR CAPTURE
25
47
  client.captureError(err);
26
48
  client.endTrace(500, { route: normalizePath(url.pathname) });
27
- throw err; // Re-throw so Next.js handles the error page
49
+ throw err;
28
50
  }
29
51
  });
30
52
  };
@@ -40,6 +62,7 @@ export const wrapNextPages = (handler: Function) => {
40
62
  path: path,
41
63
  userAgent: req.headers['user-agent'],
42
64
  ip: req.headers['x-forwarded-for'] || req.socket?.remoteAddress,
65
+ headers: req.headers // Standard Node headers work fine
43
66
  }, async () => {
44
67
 
45
68
  const done = () => {
@@ -52,10 +75,7 @@ export const wrapNextPages = (handler: Function) => {
52
75
  try {
53
76
  return await handler(req, res);
54
77
  } catch (e: any) {
55
- // AUTOMATIC ERROR CAPTURE
56
78
  client.captureError(e);
57
- // Note: In Pages dir, we rely on 'finish' listener above to close trace,
58
- // but capturing here ensures the error data is attached.
59
79
  throw e;
60
80
  }
61
81
  });