Skip to content

Commit 5a36723

Browse files
dreamorosiflochazsaragerion
authored
feat: added captureHTTPsRequest feature (#677)
* fix: increased test cases timeout to make it more stable * test: increase timeout to 4min * feat: added captureHTTPsRequest feature * docs: added 3rd party clients notice * fix: removed unused import * chore: add integ tests * fix: increased timeout to 3min * fix: error introduced in test fn * fix: revert changes on e2e test from this PR * fix: reverted e2e test * fix: reintroduced some changes * fix: re-added manual e2e tests * fixtroubleshooting * fixtroubleshooting * fix/troubleshooting * fix/troubleshooting * fix/troubleshooting * Update docs/core/tracer.md Co-authored-by: Florian Chazal <[email protected]> * fix: addressed review comments * build(deps): updated client-dynamodb dev dep * Update docs/core/tracer.md Co-authored-by: Sara Gerion <[email protected]> * Update packages/tracing/tests/unit/ProviderService.test.ts Co-authored-by: Sara Gerion <[email protected]> * Update packages/tracing/src/Tracer.ts Co-authored-by: Sara Gerion <[email protected]> Co-authored-by: Florian Chazal <[email protected]> Co-authored-by: Sara Gerion <[email protected]>
1 parent ff3d153 commit 5a36723

22 files changed

+790
-498
lines changed

Diff for: docs/core/tracer.md

+65-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Tracer is an opinionated thin wrapper for [AWS X-Ray SDK for Node.js](https://gi
1616

1717
* Auto capture cold start and service name as annotations, and responses or full exceptions as metadata
1818
* Auto-disable when not running in AWS Lambda environment
19+
* Automatically trace HTTP(s) clients and generate segments for each request
1920
* Support tracing functions via decorators, middleware, and manual instrumentation
2021
* Support tracing AWS SDK v2 and v3 via AWS X-Ray SDK for Node.js
2122

@@ -49,13 +50,13 @@ The `Tracer` utility must always be instantiated outside of the Lambda handler.
4950

5051
### Utility settings
5152

52-
The library has one optional setting. You can set it as environment variable, or pass it in the constructor.
53-
54-
This setting will be used across all traces emitted:
53+
The library has three optional settings. You can set them as environment variables, or pass them in the constructor:
5554

5655
Setting | Description | Environment variable | Constructor parameter
5756
------------------------------------------------- |------------------------------------------------------------------------------------------------| ------------------------------------------------- | -------------------------------------------------
57+
**Tracing enabled** | Enables or disables tracing. By default tracing is enabled when running in AWS Lambda. | `POWERTOOLS_TRACE_ENABLED` | `enabled`
5858
**Service name** | Sets an annotation with the **name of the service** across all traces e.g. `serverlessAirline` | `POWERTOOLS_SERVICE_NAME` | `serviceName`
59+
**Capture HTTPs Requests** | Defines whether HTTPs requests will be traced or not, enabled by default when tracing is also enabled. | `POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS` | `captureHTTPsRequests`
5960

6061
For a **complete list** of supported environment variables, refer to [this section](./../index.md#environment-variables).
6162

@@ -137,13 +138,9 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
137138

138139
=== "Middy Middleware"
139140

140-
!!! tip "Using Middy for the first time?"
141-
You can install Middy by running `npm i @middy/core`.
142-
Learn more about [its usage and lifecycle in the official Middy documentation](https://github.com/middyjs/middy#usage){target="_blank"}.
143-
144141
```typescript hl_lines="1-2 11 13"
145142
import { Tracer, captureLambdaHandler } from '@aws-lambda-powertools/tracer';
146-
import middy from '@middy/core';
143+
import middy from '@middy/core'; // (1)
147144

148145
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
149146

@@ -157,6 +154,9 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
157154
.use(captureLambdaHandler(tracer));
158155
```
159156

157+
1. Using Middy for the first time? You can install Middy by running `npm i @middy/core`.
158+
Learn more about [its usage and lifecycle in the official Middy documentation](https://github.com/middyjs/middy#usage){target="_blank"}.
159+
160160
=== "Decorator"
161161

162162
!!! info
@@ -326,13 +326,67 @@ If you're looking to shave a few microseconds, or milliseconds depending on your
326326
=== "index.ts"
327327

328328
```typescript hl_lines="5"
329-
import { S3 } from "aws-sdk";
329+
import { S3 } from 'aws-sdk';
330330
import { Tracer } from '@aws-lambda-powertools/tracer';
331331

332332
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
333333
const s3 = tracer.captureAWSClient(new S3());
334334
```
335335

336+
### Tracing HTTP requests
337+
338+
When your function makes calls to HTTP APIs, Tracer automatically traces those calls and add the API to the service graph as a downstream service.
339+
340+
You can opt-out from this feature by setting the **`POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS=false`** environment variable or by passing the `captureHTTPSRequests: false` option to the `Tracer` constructor.
341+
342+
!!! info
343+
The following snippet shows how to trace [axios](https://www.npmjs.com/package/axios) requests, but you can use any HTTP client library built on top of [http](https://nodejs.org/api/http.html) or [https](https://nodejs.org/api/https.html).
344+
Support to 3rd party HTTP clients is provided on a best effort basis.
345+
346+
=== "index.ts"
347+
348+
```typescript hl_lines="2 7"
349+
import { Tracer } from '@aws-lambda-powertools/tracer';
350+
import axios from 'axios'; // (1)
351+
352+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
353+
354+
export const handler = async (event: unknown, context: Context): Promise<void> => {
355+
await axios.get('https://httpbin.org/status/200');
356+
};
357+
```
358+
359+
1. You can install the [axios](https://www.npmjs.com/package/axios) package using `npm i axios`
360+
=== "Example Raw X-Ray Trace excerpt"
361+
362+
```json hl_lines="6 9 12-21"
363+
{
364+
"id": "22883fbc730e3a0b",
365+
"name": "## index.handler",
366+
"start_time": 1647956168.22749,
367+
"end_time": 1647956169.0679862,
368+
"subsegments": [
369+
{
370+
"id": "ab82ab2b7d525d8f",
371+
"name": "httpbin.org",
372+
"start_time": 1647956168.407,
373+
"end_time": 1647956168.945,
374+
"http": {
375+
"request": {
376+
"url": "https://httpbin.org/status/200",
377+
"method": "GET"
378+
},
379+
"response": {
380+
"status": 200,
381+
"content_length": 0
382+
}
383+
},
384+
"namespace": "remote"
385+
}
386+
]
387+
}
388+
```
389+
336390
## Advanced
337391

338392
### Disabling response auto-capture
@@ -361,7 +415,7 @@ This is useful when you need a feature available in X-Ray that is not available
361415

362416
=== "index.ts"
363417

364-
```typescript hl_lines="6"
418+
```typescript hl_lines="7"
365419
import { Logger } from '@aws-lambda-powertools/logger';
366420
import { Tracer } from '@aws-lambda-powertools/tracer';
367421

@@ -379,4 +433,4 @@ Tracer is disabled by default when not running in the AWS Lambda environment - T
379433

380434
* Use annotations on key operations to slice and dice traces, create unique views, and create metrics from it via Trace Groups
381435
* Use a namespace when adding metadata to group data more easily
382-
* Annotations and metadata are added to the current subsegment opened. If you want them in a specific subsegment, [create one](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-subsegments.html#xray-sdk-nodejs-subsegments-lambda) via the escape hatch mechanism
436+
* Annotations and metadata are added to the currently open subsegment. If you want them in a specific subsegment, [create one](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-subsegments.html#xray-sdk-nodejs-subsegments-lambda) via the escape hatch mechanism

Diff for: docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Each TypeScript utility is installed as standalone NPM package.
5454
| **POWERTOOLS_TRACE_ENABLED** | Explicitly disables tracing | [Tracer](./core/tracer) | `true` |
5555
| **POWERTOOLS_TRACER_CAPTURE_RESPONSE** | Captures Lambda or method return as metadata. | [Tracer](./core/tracer) | `true` |
5656
| **POWERTOOLS_TRACER_CAPTURE_ERROR** | Captures Lambda or method exception as metadata. | [Tracer](./core/tracer) | `true` |
57+
| **POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS** | Captures HTTP(s) requests as segments. | [Tracer](./core/tracer) | `true` |
5758
| **POWERTOOLS_LOGGER_LOG_EVENT** | Logs incoming event | [Logger](./core/logger) | `false` |
5859
| **POWERTOOLS_LOGGER_SAMPLE_RATE** | Debug log sampling | [Logger](./core/logger) | `0` |
5960
| **POWERTOOLS_LOG_DEDUPLICATION_DISABLED** | Disables log deduplication filter protection to use Pytest Live Log feature | [Logger](./core/logger) | `false` |

0 commit comments

Comments
 (0)