Skip to content

test(tracer): remove axios and use https directly #2559

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 7 commits into from
May 27, 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
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/tracer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@aws-sdk/client-xray": "^3.574.0",
"@types/promise-retry": "^1.1.6",
"aws-sdk": "^2.1627.0",
"axios": "^1.6.8",
"promise-retry": "^2.0.1"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Tracer } from '../../src/Tracer.js';
import type { Callback, Context } from 'aws-lambda';
import AWS from 'aws-sdk';
import axios from 'axios';
import { httpRequest } from '../helpers/httpRequest.js';

const serviceName =
process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler';
Expand Down Expand Up @@ -52,8 +52,9 @@ export class MyFunctionBase {
Item: { id: `${serviceName}-${event.invocation}-sdkv2` },
})
.promise(),
axios.get('https://docs.powertools.aws.dev/lambda/typescript/latest/', {
timeout: 5000,
httpRequest({
hostname: 'docs.powertools.aws.dev',
path: '/lambda/typescript/latest/',
}),
new Promise((resolve, reject) => {
setTimeout(() => {
Expand All @@ -66,7 +67,7 @@ export class MyFunctionBase {
}, 2000); // We need to wait for to make sure previous calls are finished
}),
])
.then(([_dynamoDBRes, _axiosRes, promiseRes]) => promiseRes)
.then(([_dynamoDBRes, _httpRes, promiseRes]) => promiseRes)
.catch((err) => {
throw err;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Tracer } from '../../src/index.js';
import type { Context } from 'aws-lambda';
import axios from 'axios';
import AWS from 'aws-sdk';
import type { Subsegment } from 'aws-xray-sdk-core';
import { httpRequest } from '../helpers/httpRequest.js';

const serviceName =
process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler';
Expand Down Expand Up @@ -55,10 +55,10 @@ export const handler = async (
Item: { id: `${serviceName}-${event.invocation}-sdkv2` },
})
.promise();
await axios.get(
'https://docs.powertools.aws.dev/lambda/typescript/latest/',
{ timeout: 5000 }
);
await httpRequest({
hostname: 'docs.powertools.aws.dev',
path: '/lambda/typescript/latest/',
});

const res = customResponseValue;
if (event.throw) {
Expand Down
10 changes: 5 additions & 5 deletions packages/tracer/tests/e2e/allFeatures.middy.test.functionCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Tracer } from '../../src/index.js';
import { captureLambdaHandler } from '../../src/middleware/middy.js';
import type { Context } from 'aws-lambda';
import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb';
import axios from 'axios';
import { httpRequest } from '../helpers/httpRequest.js';

const serviceName =
process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler';
Expand Down Expand Up @@ -46,10 +46,10 @@ const testHandler = async (
Item: { id: { S: `${serviceName}-${event.invocation}-sdkv3` } },
})
);
await axios.get(
'https://docs.powertools.aws.dev/lambda/typescript/latest/',
{ timeout: 5000 }
);
await httpRequest({
hostname: 'docs.powertools.aws.dev',
path: '/lambda/typescript/latest/',
});

const res = customResponseValue;
if (event.throw) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Tracer } from '../../src/index.js';
import type { Context } from 'aws-lambda';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, PutCommand } from '@aws-sdk/lib-dynamodb';
import axios from 'axios';
import { httpRequest } from '../helpers/httpRequest.js';

const serviceName =
process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler';
Expand Down Expand Up @@ -58,7 +58,10 @@ export class MyFunctionBase {
const url = 'https://docs.powertools.aws.dev/lambda/typescript/latest/';
// Add conditional behavior because fetch is not available in Node.js 16 - this can be removed once we drop support for Node.js 16
if (process.version.startsWith('v16')) {
await axios.get(url, { timeout: 5000 });
await httpRequest({
hostname: 'docs.powertools.aws.dev',
path: '/lambda/typescript/latest/',
});
} else {
await fetch(url);
}
Expand Down
52 changes: 52 additions & 0 deletions packages/tracer/tests/helpers/httpRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import https, { type RequestOptions } from 'node:https';

/**
* Make an HTTP request using the built-in `https` module
*
* This helper function is used in Tracer's tests to make HTTP requests
* and assert that the requests are being traced correctly.
*
* If the requests are traced correctly, then all 3rd party libraries
* built on top of the `https` module should also be traced correctly.
*
* @param params - The request options
*/
const httpRequest = (params: RequestOptions): Promise<unknown> =>
new Promise((resolve, reject) => {
if (!params.protocol) {
params.protocol = 'https:';
}
if (!params.timeout) {
params.timeout = 5000;
}

const req = https.request(params, (res) => {
if (
res.statusCode == null ||
res.statusCode < 200 ||
res.statusCode >= 300
) {
return reject(new Error(`statusCode=${res.statusCode || 'unknown'}`));
}
const incomingData: Uint8Array[] = [];
let responseBody: string;
res.on('data', (chunk) => {
incomingData.push(chunk);
});
res.on('end', () => {
try {
responseBody = Buffer.concat(incomingData).toString();
} catch (error) {
reject(error instanceof Error ? error : new Error('Unknown error'));
}
resolve(responseBody);
});
});
req.on('error', (error) => {
reject(error instanceof Error ? error : new Error(error));
});

req.end();
});

export { httpRequest };