Skip to content

fix(tracer): include request pathname in trace data #2955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/tracer/src/provider/ProviderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';
import type { DiagnosticsChannel } from 'undici-types';
import {
findHeaderAndDecode,
getOriginURL,
getRequestURL,
isHttpSubsegment,
} from './utilities.js';

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

const parentSubsegment = this.getSegment();
if (parentSubsegment && request.origin) {
const origin = getOriginURL(request.origin);
const requestURL = getRequestURL(request);
if (parentSubsegment && requestURL) {
const method = request.method;

const subsegment = parentSubsegment.addNewSubsegment(origin.hostname);
const subsegment = parentSubsegment.addNewSubsegment(
requestURL.hostname
);
subsegment.addAttribute('namespace', 'remote');

(subsegment as HttpSubsegment).http = {
request: {
url: origin.hostname,
url: `${requestURL.protocol}//${requestURL.hostname}${requestURL.pathname}`,
method,
},
};
Expand Down
23 changes: 18 additions & 5 deletions packages/tracer/src/provider/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { URL } from 'node:url';
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
import type { DiagnosticsChannel } from 'undici-types';
import type { HttpSubsegment } from '../types/ProviderService.js';

const decoder = new TextDecoder();
Expand Down Expand Up @@ -52,12 +53,24 @@ const isHttpSubsegment = (
};

/**
* Convert the origin url to a URL object when it is a string
* Convert the origin url to a URL object when it is a string and append the path if provided
*
* @param origin The origin url
* @param origin The request object containing the origin url and path
*/
const getOriginURL = (origin: string | URL): URL => {
return origin instanceof URL ? origin : new URL(origin);
const getRequestURL = (
request: DiagnosticsChannel.Request
): URL | undefined => {
if (typeof request.origin === 'string') {
return new URL(`${request.origin}${request.path || ''}`);
}

if (request.origin instanceof URL) {
request.origin.pathname = request.path || '';

return request.origin;
}

return undefined;
};

export { findHeaderAndDecode, isHttpSubsegment, getOriginURL };
export { findHeaderAndDecode, isHttpSubsegment, getRequestURL };
2 changes: 1 addition & 1 deletion packages/tracer/tests/e2e/middy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('Tracer E2E tests, middy instrumentation', () => {
const httpSubsegment = subsegments.get('docs.powertools.aws.dev');
expect(httpSubsegment?.namespace).toBe('remote');
expect(httpSubsegment?.http?.request?.url).toEqual(
'docs.powertools.aws.dev'
'https://docs.powertools.aws.dev/lambda/typescript/latest/'
);
expect(httpSubsegment?.http?.request?.method).toBe('GET');
expect(httpSubsegment?.http?.response?.status).toEqual(expect.any(Number));
Expand Down
5 changes: 4 additions & 1 deletion packages/tracer/tests/helpers/mockRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { channel } from 'node:diagnostics_channel';
import type { URL } from 'node:url';

type MockFetchOptions = {
origin: string | URL;
origin?: string | URL;
path?: string;
method?: string;
headers?: { [key: string]: string };
} & (
Expand All @@ -25,6 +26,7 @@ type MockFetchOptions = {
*/
const mockFetch = ({
origin,
path,
method,
statusCode,
headers,
Expand All @@ -37,6 +39,7 @@ const mockFetch = ({
const request = {
origin,
method: method ?? 'GET',
path,
};

requestCreateChannel.publish({
Expand Down
60 changes: 56 additions & 4 deletions packages/tracer/tests/unit/ProviderService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ describe('Class: ProviderService', () => {
// Act
provider.instrumentFetch();
mockFetch({
origin: 'https://aws.amazon.com/blogs',
origin: 'https://aws.amazon.com',
path: '/blogs',
headers: {
'content-length': '100',
},
Expand All @@ -407,7 +408,7 @@ describe('Class: ProviderService', () => {
expect(segment.addNewSubsegment).toHaveBeenCalledWith('aws.amazon.com');
expect((subsegment as HttpSubsegment).http).toEqual({
request: {
url: 'aws.amazon.com',
url: 'https://aws.amazon.com/blogs',
method: 'GET',
},
response: {
Expand Down Expand Up @@ -438,7 +439,8 @@ describe('Class: ProviderService', () => {
// Act
provider.instrumentFetch();
mockFetch({
origin: new URL('https://aws.amazon.com/blogs'),
origin: new URL('https://aws.amazon.com'),
path: '/blogs',
headers: {
'content-type': 'application/json',
},
Expand All @@ -447,7 +449,7 @@ describe('Class: ProviderService', () => {
// Assess
expect((subsegment as HttpSubsegment).http).toEqual({
request: {
url: 'aws.amazon.com',
url: 'https://aws.amazon.com/blogs',
method: 'GET',
},
response: {
Expand Down Expand Up @@ -568,6 +570,56 @@ describe('Class: ProviderService', () => {
expect(subsegment.close).toHaveBeenCalledTimes(1);
expect(provider.setSegment).toHaveBeenLastCalledWith(segment);
});

it('skips the segment creation when the request has no origin', async () => {
// Prepare
const provider: ProviderService = new ProviderService();
const segment = new Subsegment('## dummySegment');
jest.spyOn(segment, 'addNewSubsegment');
jest.spyOn(provider, 'getSegment').mockImplementation(() => segment);
jest.spyOn(provider, 'setSegment');

// Act
provider.instrumentFetch();
mockFetch({});

// Assess
expect(segment.addNewSubsegment).toHaveBeenCalledTimes(0);
expect(provider.setSegment).toHaveBeenCalledTimes(0);
});

it('does not add any path to the segment when the request has no path', async () => {
// Prepare
const provider: ProviderService = new ProviderService();
const segment = new Subsegment('## dummySegment');
const subsegment = segment.addNewSubsegment('aws.amazon.com');
jest
.spyOn(segment, 'addNewSubsegment')
.mockImplementationOnce(() => subsegment);
jest
.spyOn(provider, 'getSegment')
.mockImplementationOnce(() => segment)
.mockImplementationOnce(() => subsegment)
.mockImplementationOnce(() => subsegment);
jest.spyOn(subsegment, 'close');
jest.spyOn(provider, 'setSegment');

// Act
provider.instrumentFetch();
mockFetch({
origin: new URL('https://aws.amazon.com'),
});

// Assess
expect((subsegment as HttpSubsegment).http).toEqual(
expect.objectContaining({
request: {
url: 'https://aws.amazon.com/',
method: 'GET',
},
})
);
});
});

it('closes the segment and adds a fault flag when the connection fails', async () => {
Expand Down