diff --git a/package-lock.json b/package-lock.json index d13c4fd6ac..3f7433ce58 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18884,7 +18884,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": { diff --git a/packages/tracer/package.json b/packages/tracer/package.json index 282e349f27..4c9a8192fd 100644 --- a/packages/tracer/package.json +++ b/packages/tracer/package.json @@ -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": { diff --git a/packages/tracer/tests/e2e/allFeatures.decorator.test.functionCode.ts b/packages/tracer/tests/e2e/allFeatures.decorator.test.functionCode.ts index ca2f6ace93..c6f1734ff7 100644 --- a/packages/tracer/tests/e2e/allFeatures.decorator.test.functionCode.ts +++ b/packages/tracer/tests/e2e/allFeatures.decorator.test.functionCode.ts @@ -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'; @@ -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(() => { @@ -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; }); diff --git a/packages/tracer/tests/e2e/allFeatures.manual.test.functionCode.ts b/packages/tracer/tests/e2e/allFeatures.manual.test.functionCode.ts index 95cdc3fd33..74983fe5f6 100644 --- a/packages/tracer/tests/e2e/allFeatures.manual.test.functionCode.ts +++ b/packages/tracer/tests/e2e/allFeatures.manual.test.functionCode.ts @@ -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'; @@ -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) { diff --git a/packages/tracer/tests/e2e/allFeatures.middy.test.functionCode.ts b/packages/tracer/tests/e2e/allFeatures.middy.test.functionCode.ts index f55af0bdd4..61e5a9198c 100644 --- a/packages/tracer/tests/e2e/allFeatures.middy.test.functionCode.ts +++ b/packages/tracer/tests/e2e/allFeatures.middy.test.functionCode.ts @@ -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'; @@ -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) { diff --git a/packages/tracer/tests/e2e/asyncHandler.decorator.test.functionCode.ts b/packages/tracer/tests/e2e/asyncHandler.decorator.test.functionCode.ts index 05668a7b9f..65d9cbf937 100644 --- a/packages/tracer/tests/e2e/asyncHandler.decorator.test.functionCode.ts +++ b/packages/tracer/tests/e2e/asyncHandler.decorator.test.functionCode.ts @@ -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'; @@ -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); } diff --git a/packages/tracer/tests/helpers/httpRequest.ts b/packages/tracer/tests/helpers/httpRequest.ts new file mode 100644 index 0000000000..579f56ac3f --- /dev/null +++ b/packages/tracer/tests/helpers/httpRequest.ts @@ -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 => + 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 };