Skip to content

Commit 0c65763

Browse files
dreamorosiam29d
andauthored
chore(docs): miscellaneous updates and clarifications on docs (#2494)
* chore(docs): update banner in docs * chore(docs): update layer arn & externalModules * chore(docs): add eslint section to upgrade guide * chore(docs): add testing your code section to metrics docs --------- Co-authored-by: Alexander Schueren <[email protected]>
1 parent a151bb5 commit 0c65763

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

docs/core/metrics.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ title: Metrics
33
description: Core utility
44
---
55

6-
<!-- markdownlint-disable MD043 -->
7-
86
Metrics creates custom metrics asynchronously by logging metrics to standard output following [Amazon CloudWatch Embedded Metric Format (EMF)](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format.html).
97

108
These metrics can be visualized through [Amazon CloudWatch Console](https://console.aws.amazon.com/cloudwatch/).
@@ -460,3 +458,32 @@ CloudWatch EMF uses the same dimensions across all your metrics. Use `singleMetr
460458
```
461459

462460
1. Binding your handler method allows your handler to access `this` within the class methods.
461+
462+
## Testing your code
463+
464+
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.
465+
466+
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.
467+
468+
This allows you to spy on the logs emitted by the utility and assert that the metrics are being emitted correctly.
469+
470+
```typescript title="Spying on emitted metrics"
471+
describe('Metrics tests', () => {
472+
beforeAll(() => {
473+
process.env.POWERTOOLS_DEV = 'true';
474+
})
475+
476+
it('function metrics properly', async () => {
477+
// Prepare
478+
const metricsSpy = jest.spyOn(console, 'log').mockImplementation();
479+
480+
// Act & Assess
481+
});
482+
});
483+
```
484+
485+
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.
486+
487+
```bash title="Disabling logs while testing"
488+
export POWERTOOLS_DEV=true && npx vitest --silent
489+
```

docs/index.md

+10-14
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ You can use Powertools for AWS Lambda (TypeScript) by installing it with your fa
8989
const powertoolsLayer = LayerVersion.fromLayerVersionArn(
9090
this,
9191
'PowertoolsLayer',
92-
`arn:aws:lambda:${Stack.of(this).region}:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:3`
92+
`arn:aws:lambda:${Stack.of(this).region}:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:5`
9393
);
9494
9595
new Function(this, 'Function', {
@@ -103,7 +103,7 @@ You can use Powertools for AWS Lambda (TypeScript) by installing it with your fa
103103
}
104104
```
105105

106-
If you use `esbuild` to bundle your code, make sure to exclude `@aws-lambda-powertools` from being bundled since the packages will be already present the Layer:
106+
If you use `esbuild` to bundle your code, make sure to exclude `@aws-lambda-powertools/*` and `@aws-sdk/*` from being bundled since the packages are already present the layer:
107107

108108
```typescript
109109
new NodejsFunction(this, 'Function', {
@@ -129,7 +129,7 @@ You can use Powertools for AWS Lambda (TypeScript) by installing it with your fa
129129
- !Sub arn:aws:lambda:${AWS::Region}:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:5
130130
```
131131

132-
If you use `esbuild` to bundle your code, make sure to exclude `@aws-lambda-powertools` from being bundled since the packages will be already present the Layer:
132+
If you use `esbuild` to bundle your code, make sure to exclude `@aws-lambda-powertools/*` and `@aws-sdk/*` from being bundled since the packages are already present the layer:
133133

134134
```yaml hl_lines="5-14"
135135
MyLambdaFunction:
@@ -142,10 +142,8 @@ You can use Powertools for AWS Lambda (TypeScript) by installing it with your fa
142142
BuildProperties:
143143
Minify: true
144144
External:
145-
- '@aws-lambda-powertools/commons'
146-
- '@aws-lambda-powertools/logger'
147-
- '@aws-lambda-powertools/metrics'
148-
- '@aws-lambda-powertools/tracer'
145+
- '@aws-lambda-powertools/*'
146+
- '@aws-sdk/*'
149147
```
150148

151149
Check the [documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-build-typescript.html) for more details.
@@ -160,16 +158,14 @@ You can use Powertools for AWS Lambda (TypeScript) by installing it with your fa
160158
- arn:aws:lambda:${aws:region}:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:5
161159
```
162160

163-
If you use `esbuild` to bundle your code, make sure to exclude `@aws-lambda-powertools` from being bundled since the packages will be already present the Layer:
161+
If you use `esbuild` to bundle your code, make sure to exclude `@aws-lambda-powertools/*` and `@aws-sdk/*` from being bundled since the packages are already present the layer:
164162

165163
```yaml
166164
custom:
167165
esbuild:
168166
external:
169-
- '@aws-lambda-powertools/commons'
170-
- '@aws-lambda-powertools/logger'
171-
- '@aws-lambda-powertools/metrics'
172-
- '@aws-lambda-powertools/tracer'
167+
- '@aws-lambda-powertools/*'
168+
- '@aws-sdk/*'
173169
```
174170

175171
Check the [documentation](https://floydspace.github.io/serverless-esbuild/) for more details.
@@ -193,7 +189,7 @@ You can use Powertools for AWS Lambda (TypeScript) by installing it with your fa
193189
function_name = "lambda_function_name"
194190
role = ...
195191
handler = "index.handler"
196-
runtime = "nodejs16.x"
192+
runtime = "nodejs20.x"
197193
layers = ["arn:aws:lambda:{aws::region}:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:5"]
198194
source_code_hash = filebase64sha256("lambda_function_payload.zip")
199195
}
@@ -218,7 +214,7 @@ You can use Powertools for AWS Lambda (TypeScript) by installing it with your fa
218214
tracingConfig: {
219215
mode: 'Active'
220216
},
221-
runtime: aws.lambda.Runtime.NodeJS16dX,
217+
runtime: aws.lambda.Runtime.NodeJS20dX,
222218
handler: 'index.handler',
223219
role: role.arn,
224220
architectures: ['x86_64']

docs/overrides/main.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
{% endblock %}
99

1010
{% block announce %}
11-
Version 2 is generally available 🔥 Check out the <a href="{{ '../' ~ base_url ~ '/latest/upgrade' }}">upgrade guide</a> to see
12-
what's new.
11+
Starting from July 1, 2024 we will end support for Node.js 16. Learn more <a
12+
href="https://github.com/aws-powertools/powertools-lambda-typescript/issues/2223" target="_blank">here</a>.
1313
{% endblock %}

docs/upgrade.md

+21
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ In v2, you can now directly import from the `types` subpath export, e.g., `@aws-
173173
import { LogAttributes, UnformattedAttributes } from '@aws-lambda-powertools/logger/types';
174174
```
175175

176+
### Using eslint?
177+
178+
When using `eslint`, you might need to use [`@typescript-eslint/parser`](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/parser) and [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-import-resolver-typescript) to resolve the new subpath imports.
179+
180+
Below is an example of how to configure your `.eslintrc.json` file:
181+
182+
```json
183+
{
184+
"parser": "@typescript-eslint/parser",
185+
"settings": {
186+
"import/resolver": {
187+
"node": {},
188+
"typescript": {
189+
"project": "./tsconfig.json",
190+
"alwaysTryTypes": true,
191+
},
192+
},
193+
},
194+
}
195+
```
196+
176197
## Logger
177198

178199
### Log sampling

0 commit comments

Comments
 (0)