Skip to content

Commit 9ac2625

Browse files
committed
fix(tracer): include request pathname in trace data
1 parent 5365440 commit 9ac2625

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Diff for: packages/tracer/src/provider/ProviderService.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';
2828
import type { DiagnosticsChannel } from 'undici-types';
2929
import {
3030
findHeaderAndDecode,
31-
getOriginURL,
31+
getRequestURL,
3232
isHttpSubsegment,
3333
} from './utilities.js';
3434

@@ -107,16 +107,18 @@ class ProviderService implements ProviderServiceInterface {
107107
const { request } = message as DiagnosticsChannel.RequestCreateMessage;
108108

109109
const parentSubsegment = this.getSegment();
110-
if (parentSubsegment && request.origin) {
111-
const origin = getOriginURL(request.origin);
110+
const requestURL = getRequestURL(request);
111+
if (parentSubsegment && requestURL) {
112112
const method = request.method;
113113

114-
const subsegment = parentSubsegment.addNewSubsegment(origin.hostname);
114+
const subsegment = parentSubsegment.addNewSubsegment(
115+
requestURL.hostname
116+
);
115117
subsegment.addAttribute('namespace', 'remote');
116118

117119
(subsegment as HttpSubsegment).http = {
118120
request: {
119-
url: origin.hostname,
121+
url: `${requestURL.protocol}//${requestURL.hostname}${requestURL.pathname}`,
120122
method,
121123
},
122124
};

Diff for: packages/tracer/src/provider/utilities.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { URL } from 'node:url';
22
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
3+
import type { DiagnosticsChannel } from 'undici-types';
34
import type { HttpSubsegment } from '../types/ProviderService.js';
45

56
const decoder = new TextDecoder();
@@ -52,12 +53,24 @@ const isHttpSubsegment = (
5253
};
5354

5455
/**
55-
* Convert the origin url to a URL object when it is a string
56+
* Convert the origin url to a URL object when it is a string and append the path if provided
5657
*
57-
* @param origin The origin url
58+
* @param origin The request object containing the origin url and path
5859
*/
59-
const getOriginURL = (origin: string | URL): URL => {
60-
return origin instanceof URL ? origin : new URL(origin);
60+
const getRequestURL = (
61+
request: DiagnosticsChannel.Request
62+
): URL | undefined => {
63+
if (typeof request.origin === 'string') {
64+
return new URL(`${request.origin}${request.path || ''}`);
65+
}
66+
67+
if (request.origin instanceof URL) {
68+
request.origin.pathname = request.path || '';
69+
70+
return request.origin;
71+
}
72+
73+
return undefined;
6174
};
6275

63-
export { findHeaderAndDecode, isHttpSubsegment, getOriginURL };
76+
export { findHeaderAndDecode, isHttpSubsegment, getRequestURL };

0 commit comments

Comments
 (0)