Skip to content

Commit 95ebfbb

Browse files
authored
docs: multiple minor improvements (#3387)
1 parent 93a19a5 commit 95ebfbb

File tree

11 files changed

+70
-56
lines changed

11 files changed

+70
-56
lines changed

Diff for: docs/core/logger.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ This functionality will include the following keys in your structured logs:
111111
=== "Middy Middleware"
112112

113113
!!! tip "A note about Middy"
114-
We guarantee support for both Middy.js `v4.x` & `v5.x` with the latter being available only if you are using ES modules.
114+
We guarantee support for Middy.js `v4.x` through `v6.x` versions.
115115
Check their docs to learn more about [Middy and its middleware stack](https://middy.js.org/docs/intro/getting-started){target="_blank"} as well as [best practices when working with Powertools](https://middy.js.org/docs/integrations/lambda-powertools#best-practices){target="_blank"}.
116116

117117
```typescript hl_lines="2 14"

Diff for: docs/core/metrics.md

+16-11
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ These settings will be used across all metrics emitted:
7070
| -------------------- | ---------------------------------------------------------------- | ------------------------------ | ------------------- | -------------- | ------------------- | --------------------- |
7171
| **Service** | Optionally, sets **service** metric dimension across all metrics | `POWERTOOLS_SERVICE_NAME` | `service_undefined` | Any string | `serverlessAirline` | `serviceName` |
7272
| **Metric namespace** | Logical container where all metrics will be placed | `POWERTOOLS_METRICS_NAMESPACE` | `default_namespace` | Any string | `serverlessAirline` | `default_namespace` |
73+
| **Enabled** | Whether to emit metrics to standard output or not | `POWERTOOLS_METRICS_ENABLED` | `true` | Boolean | `false` | |
7374

7475
!!! tip
7576
Use your application name or main service as the metric namespace to easily group all metrics
@@ -199,7 +200,7 @@ You can add default dimensions to your metrics by passing them as parameters in
199200
=== "Middy middleware"
200201

201202
!!! tip "A note about Middy"
202-
We guarantee support for both Middy.js `v4.x` & `v5.x` with the latter being available only if you are using ES modules.
203+
We guarantee support for Middy.js `v4.x` through `v6.x` versions.
203204
Check their docs to learn more about [Middy and its middleware stack](https://middy.js.org/docs/intro/getting-started){target="_blank"} as well as [best practices when working with Powertools](https://middy.js.org/docs/integrations/lambda-powertools#best-practices){target="_blank"}.
204205

205206
```typescript hl_lines="24-26"
@@ -465,22 +466,26 @@ Keep the following formula in mind: `unique metric = (metric_name + dimension_na
465466
1. Metadata should be added before calling `addMetric()` to ensure it's included in the same EMF blob.
466467
2. Single metrics are emitted as soon as `addMetric()` is called, so you don't need to call `publishStoredMetrics()`.
467468

469+
### Customizing the logger
470+
471+
You can customize how Metrics logs warnings and debug messages to standard output by passing a custom logger as a constructor parameter. This is useful when you want to silence warnings or debug messages, or when you want to log them to a different output.
472+
473+
=== "Custom logger"
474+
475+
```typescript hl_lines="4 9"
476+
--8<-- "examples/snippets/metrics/customLogger.ts"
477+
```
478+
468479
## Testing your code
469480

470-
When unit testing your code that uses the `Metrics` utility, you may want to silence the logs emitted by the utility or assert that metrics are being emitted correctly. By default, the utility manages its own `console` instance, which means that you can't easily access or mock the logs emitted by the utility.
481+
When unit testing your code that uses the Metrics utility, you may want to silence the logs emitted by the utility. To do so, you can set the `POWERTOOLS_DEV` environment variable to `true`. This instructs the utility to not emit any logs to standard output.
471482

472-
To make it easier to test your code, you can set the `POWERTOOLS_DEV` environment variable to `true` to instruct the utility to use the global `console` object instead of its own.
483+
If instead you want to spy on the logs emitted by the utility, you must set the `POWERTOOLS_DEV` environment variable to `true` in conjunction with the `POWERTOOLS_METRICS_ENABLED` environment variable also set to `true`.
473484

474-
This allows you to spy on the logs emitted by the utility and assert that the metrics are being emitted correctly.
485+
When `POWERTOOLS_DEV` is enabled, Metrics uses the global `console` to emit metrics to standard out. This allows you to easily spy on the logs emitted and make assertions on them.
475486

476487
=== "Spying on emitted metrics"
477488

478-
```typescript hl_lines="4 10"
489+
```typescript hl_lines="4-5 12"
479490
--8<-- "examples/snippets/metrics/testingMetrics.ts"
480491
```
481-
482-
When running your tests with both [Jest](https://jestjs.io) and [Vitest](http://vitest.dev), you can use the `--silent` flag to silence the logs emitted by the utility.
483-
484-
```bash title="Disabling logs while testing"
485-
export POWERTOOLS_DEV=true && npx vitest --silent
486-
```

Diff for: docs/core/tracer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
154154
=== "Middy Middleware"
155155

156156
!!! tip "A note about Middy"
157-
We guarantee support for both Middy.js `v4.x` & `v5.x` with the latter being available only if you are using ES modules.
157+
We guarantee support for Middy.js `v4.x` through `v6.x` versions.
158158
Check their docs to learn more about [Middy and its middleware stack](https://middy.js.org/docs/intro/getting-started){target="_blank"} as well as [best practices when working with Powertools](https://middy.js.org/docs/integrations/lambda-powertools#best-practices){target="_blank"}.
159159

160160
```typescript hl_lines="2 15 17"

Diff for: docs/index.md

+31-27
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ You can use Powertools for AWS Lambda (TypeScript) by installing it with your fa
5656

5757
| Feature | Install | Default dependency |
5858
| --------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- | ------------------- |
59-
| **[Tracer](./core/tracer.md#install)** | **`npm i @aws-lambda-powertools/tracer`**{.copyMe}:clipboard: | `aws-xray-sdk-core` |
60-
| **[Idempotency](./utilities/idempotency.md#install)** | **`npm i @aws-lambda-powertools/idempotency @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb`**{.copyMe}:clipboard: | `jmespath` |
61-
| **[Parameters (SSM)](./utilities/parameters.md#install)** | **`npm i @aws-lambda-powertools/parameters @aws-sdk/client-ssm`**{.copyMe}:clipboard: | |
62-
| **[Parameters (Secrets Manager)](./utilities/parameters.md#install)** | **`npm i @aws-lambda-powertools/parameters @aws-sdk/client-secrets-manager`**{.copyMe}:clipboard: | |
63-
| **[Parameters (AppConfig)](./utilities/parameters.md#install)** | **`npm i @aws-lambda-powertools/parameters @aws-sdk/client-appconfigdata`**{.copyMe}:clipboard: | |
64-
| **[Parser](./utilities/parser.md#install)** | **`npm i @aws-lambda-powertools/parser zod@~3`**{.copyMe}:clipboard: | |
59+
| **[Tracer](./core/tracer.md)** | **`npm i @aws-lambda-powertools/tracer`**{.copyMe}:clipboard: | `aws-xray-sdk-core` |
60+
| **[Idempotency](./utilities/idempotency.md)** | **`npm i @aws-lambda-powertools/idempotency @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb`**{.copyMe}:clipboard: | |
61+
| **[Parameters (SSM)](./utilities/parameters.md)** | **`npm i @aws-lambda-powertools/parameters @aws-sdk/client-ssm`**{.copyMe}:clipboard: | |
62+
| **[Parameters (Secrets Manager)](./utilities/parameters.md)** | **`npm i @aws-lambda-powertools/parameters @aws-sdk/client-secrets-manager`**{.copyMe}:clipboard: | |
63+
| **[Parameters (AppConfig)](./utilities/parameters.md)** | **`npm i @aws-lambda-powertools/parameters @aws-sdk/client-appconfigdata`**{.copyMe}:clipboard: | |
64+
| **[Parser](./utilities/parser.md)** | **`npm i @aws-lambda-powertools/parser zod@~3`**{.copyMe}:clipboard: | |
6565

6666
=== "Lambda Layer"
6767

@@ -223,27 +223,18 @@ You can use Powertools for AWS Lambda (TypeScript) by installing it with your fa
223223

224224
=== "Amplify"
225225

226-
```zsh hl_lines="9 19"
227-
# Create a new one with the layer
228-
❯ amplify add function
229-
? Select which capability you want to add: Lambda function (serverless function)
230-
? Provide an AWS Lambda function name: <NAME-OF-FUNCTION>
231-
? Choose the runtime that you want to use: NodeJS
232-
? Do you want to configure advanced settings? Yes
233-
...
234-
? Do you want to enable Lambda layers for this function? Yes
235-
? Enter up to 5 existing Lambda layer ARNs (comma-separated): arn:aws:lambda:{aws::region}:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:16
236-
❯ amplify push -y
237-
238-
# Updating an existing function and add the layer
239-
❯ amplify update function
240-
? Select the Lambda function you want to update test2
241-
General information
242-
- Name: <NAME-OF-FUNCTION>
243-
? Which setting do you want to update? Lambda layers configuration
244-
? Do you want to enable Lambda layers for this function? Yes
245-
? Enter up to 5 existing Lambda layer ARNs (comma-separated): arn:aws:lambda:{aws::region}:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:16
246-
? Do you want to edit the local lambda function now? No
226+
Remember to replace the region with your AWS region, e.g., `eu-west-1`. Amplify Gen 2 currently does not support obtaining the region dynamically.
227+
228+
```typescript hl_lines="9 19"
229+
import { defineFunction } from "@aws-amplify/backend";
230+
231+
export const myFunction = defineFunction({
232+
name: "my-function",
233+
layers: {
234+
"@aws-lambda-powertools/*":
235+
"arn:aws:lambda:${AWS::Region}:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:16",
236+
},
237+
});
247238
```
248239

249240
### Lambda Layer
@@ -334,6 +325,7 @@ Core utilities such as Tracing, Logging, and Metrics will be available across al
334325
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------------- | --------------------------------------- | ------------------- |
335326
| **POWERTOOLS_SERVICE_NAME** | Set service name used for tracing namespace, metrics dimension and structured logging | All | `service_undefined` |
336327
| **POWERTOOLS_METRICS_NAMESPACE** | Set namespace used for metrics | [Metrics](core/metrics.md) | `default_namespace` |
328+
| **POWERTOOLS_METRICS_ENABLED** | Explicitly disables emitting metrics to stdout | [Metrics](core/metrics.md) | `true` |
337329
| **POWERTOOLS_TRACE_ENABLED** | Explicitly disables tracing | [Tracer](core/tracer.md) | `true` |
338330
| **POWERTOOLS_TRACER_CAPTURE_RESPONSE** | Capture Lambda or method return as metadata. | [Tracer](core/tracer.md) | `true` |
339331
| **POWERTOOLS_TRACER_CAPTURE_ERROR** | Capture Lambda or method exception as metadata. | [Tracer](core/tracer.md) | `true` |
@@ -348,6 +340,18 @@ Core utilities such as Tracing, Logging, and Metrics will be available across al
348340

349341
Each Utility page provides information on example values and allowed values.
350342

343+
### Optimizing for non-production environments
344+
345+
Whether you're prototyping locally or against a non-production environment, you can use `POWERTOOLS_DEV` to increase verbosity across multiple utilities.
346+
347+
When `POWERTOOLS_DEV` is set to a truthy value (`1`, `true`), it'll have the following effects:
348+
349+
| Utility | Effect |
350+
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
351+
| __Logger__ | Increase JSON indentation to 4 and uses global `console` to emit logs to ease testing and local debugging when running functions locally. However, Amazon CloudWatch Logs view will degrade as each new line is treated as a new message |
352+
| __Tracer__ | Disables tracing operations in non-Lambda environments. This already happens automatically in the Tracer utility |
353+
| __Metrics__ | Disables emitting metrics to stdout. Can be overridden by setting `POWERTOOLS_METRICS_ENABLED` to `true` |
354+
351355
## Support Powertools for AWS Lambda (TypeScript)
352356

353357
There are many ways you can help us gain future investments to improve everyone's experience:

Diff for: docs/overrides/main.html

-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,4 @@
55
<a href="{{ '../' ~ base_url }}">
66
<strong>Click here to go to latest.</strong>
77
</a>
8-
{% endblock %}
9-
10-
{% block announce %}
11-
On September 1st, 2024 v1 of Powertools for AWS Lambda (TypeScript) <a
12-
href="https://github.com/aws-powertools/powertools-lambda-typescript/issues/2224" target="_blank">has reached
13-
End-of-Life</a>. We recommend you to <a href="https://docs.powertools.aws.dev/lambda/typescript/latest/upgrade/"
14-
target="_blank">upgrade to v2</a>.
158
{% endblock %}

Diff for: docs/upgrade.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ Below is an example of how to configure your `.eslintrc.json` file:
198198

199199
### Log sampling
200200

201-
!!! note "Disregard if you are not using the [log sampling feature](./core/logger.md#sampling-logs)."
201+
!!! note "Disregard if you are not using the [log sampling feature](./core/logger.md#sampling-debug-logs)."
202202

203203
In v1, log sampling implementation was inconsistent from other Powertools for AWS Lambda languages _(Python, .NET, and Java)_.
204204

@@ -217,7 +217,7 @@ Logger `sampleRateValue` **continues** to determine the percentage of concurrent
217217

218218
In v1, `Logger` exposed the [standard](./core/logger.md#standard-structured-keys) as a single argument, _e.g., `formatAttributes(attributes: UnformattedAttributes)`_. It expected a plain object with keys and values you wanted in the final log output.
219219

220-
In v2, you have more control over **standard** (`attributes`) and [**custom keys**](./core/logger.md#appending-persistent-additional-log-keys-and-values) (`additionalLogAttributes`) in the `formatAttributes` method. Also, you now return a `LogItem` object to increase type safety when defining the final log output.
220+
In v2, you have more control over **standard** (`attributes`) and [**custom keys**](./core/logger.md#appending-additional-keys) (`additionalLogAttributes`) in the `formatAttributes` method. Also, you now return a `LogItem` object to increase type safety when defining the final log output.
221221

222222
=== "Before"
223223

Diff for: docs/utilities/idempotency.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ The configuration options for the `@idempotent` decorator are the same as the on
187187
### MakeHandlerIdempotent Middy middleware
188188

189189
!!! tip "A note about Middy"
190-
We guarantee support for both Middy.js `v4.x` & `v5.x` with the latter being available only if you are using ES modules.
190+
We guarantee support for Middy.js `v4.x` through `v6.x` versions.
191191
Check their docs to learn more about [Middy and its middleware stack](https://middy.js.org/docs/intro/getting-started){target="_blank"} as well as [best practices when working with Powertools](https://middy.js.org/docs/integrations/lambda-powertools#best-practices){target="_blank"}.
192192

193193
If you are using [Middy.js](https://middy.js.org){target="_blank"} as your middleware engine, you can use the `makeHandlerIdempotent` middleware to make your Lambda handler idempotent.

Diff for: docs/utilities/parser.md

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
title: Parser (Zod)
33
descrition: Utility
4-
status: new
54
---
65

76
<!-- markdownlint-disable MD043 --->
@@ -18,8 +17,6 @@ This utility provides data validation and parsing using [Zod](https://zod.dev){t
1817

1918
## Getting started
2019

21-
### Install
22-
2320
```bash
2421
npm install @aws-lambda-powertools/parser zod@~3
2522
```

Diff for: examples/snippets/metrics/customLogger.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { LogLevel, Logger } from '@aws-lambda-powertools/logger';
2+
import { MetricUnit, Metrics } from '@aws-lambda-powertools/metrics';
3+
4+
const logger = new Logger({ logLevel: LogLevel.CRITICAL });
5+
const metrics = new Metrics({
6+
serviceName: 'serverless-airline',
7+
namespace: 'orders',
8+
singleMetric: true,
9+
logger,
10+
});
11+
12+
metrics.addMetric('successfulBooking', MetricUnit.Count, 1);

Diff for: examples/snippets/metrics/testingMetrics.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ import { describe, expect, it, vi } from 'vitest';
22

33
vi.hoisted(() => {
44
process.env.POWERTOOLS_DEV = 'true';
5+
process.env.POWERTOOLS_METRICS_ENABLED = 'true';
56
});
67

78
describe('Metrics tests', () => {
89
it('emits metrics properly', async () => {
910
// Prepare
10-
const metricsSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
11+
const metricsEmittedSpy = vi
12+
.spyOn(console, 'log')
13+
.mockImplementation(() => {});
1114

1215
// Act
1316
// ...
1417

1518
// Assess
16-
expect(metricsSpy).toHaveBeenCalledOnce();
19+
expect(metricsEmittedSpy).toHaveBeenCalledOnce();
1720
});
1821
});

Diff for: mkdocs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ nav:
1111
- API reference: api/" target="_blank
1212
- Upgrade guide: upgrade.md
1313
- We Made This (Community): we_made_this.md
14-
- Workshop 🆕: https://s12d.com/powertools-for-aws-lambda-workshop" target="_blank
14+
- Workshop: https://s12d.com/powertools-for-aws-lambda-workshop" target="_blank
1515
- Roadmap: roadmap.md
1616
- Features:
1717
- core/tracer.md

0 commit comments

Comments
 (0)