Skip to content

Commit 93e605f

Browse files
authored
chore(docs): move code snippets under examples (aws-powertools#2436)
* chore(docs): move code snippets under examples * chore: remove unused variable * chore
1 parent b28a4c1 commit 93e605f

File tree

185 files changed

+287
-315
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+287
-315
lines changed

.github/workflows/reusable-run-linting-check-and-unit-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ jobs:
8989
- name: Setup dependencies
9090
uses: ./.github/actions/cached-node-modules
9191
- name: Run linting
92-
run: npm run lint -w docs/snippets
92+
run: npm run lint -w examples/snippets
9393
check-docs:
9494
runs-on: ubuntu-latest
9595
env:

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ You can use Powertools for AWS Lambda (TypeScript) by installing it with your fa
4444

4545
### Examples
4646

47-
You can find examples of how to use Powertools for AWS Lambda (TypeScript) in the [examples](https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/examples/app) directory. The application is a simple REST API that can be deployed via either AWS CDK or AWS SAM.
47+
You can find examples of how to use Powertools for AWS Lambda (TypeScript) in the [examples](https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/examples) directory. The directory contains code snippets around certain features as well as an is a simple REST API application that can be deployed via either AWS CDK or AWS SAM.
4848

4949
Community-contributed examples:
5050

docs/contributing/conventions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Additionally, we try to share common runtime code between utilities to reduce ma
3434

3535
There are also a few other workspaces that are not utilities published to npm, but that still share dependencies and/or runtime code with the utilities. These workspaces are:
3636

37-
* `docs/snippets`: contains the documentation code snippets
37+
* `examples/snippets`: contains the documentation code snippets
3838
* `examples/app`: contains an example project that can be deployed via AWS CDK or AWS SAM
3939
* `layers`: contains the code used to build and publish the [Lambda layers](../index.md#lambda-layer)
4040

docs/core/logger.md

+18-18
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The `Logger` utility must always be instantiated outside the Lambda handler. By
3737
=== "handler.ts"
3838

3939
```typescript hl_lines="1 3"
40-
--8<-- "docs/snippets/logger/basicUsage.ts"
40+
--8<-- "examples/snippets/logger/basicUsage.ts"
4141
```
4242

4343
### Utility settings
@@ -60,7 +60,7 @@ Check API docs to learn more about [Logger constructor options](https://docs.pow
6060
=== "handler.ts"
6161

6262
```typescript hl_lines="1 4"
63-
--8<-- "docs/snippets/logger/sam.ts"
63+
--8<-- "examples/snippets/logger/sam.ts"
6464
```
6565

6666
=== "template.yaml"
@@ -115,7 +115,7 @@ This functionality will include the following keys in your structured logs:
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"
118-
--8<-- "docs/snippets/logger/middy.ts"
118+
--8<-- "examples/snippets/logger/middy.ts"
119119
```
120120

121121
=== "Decorator"
@@ -128,15 +128,15 @@ This functionality will include the following keys in your structured logs:
128128
If this is not the desired behavior, you can call the `logger.injectLambdaContext()` method directly in your handler.
129129

130130
```typescript hl_lines="8"
131-
--8<-- "docs/snippets/logger/decorator.ts"
131+
--8<-- "examples/snippets/logger/decorator.ts"
132132
```
133133

134134
1. Binding your handler method allows your handler to access `this` within the class methods.
135135

136136
=== "Manual"
137137

138138
```typescript hl_lines="10"
139-
--8<-- "docs/snippets/logger/manual.ts"
139+
--8<-- "examples/snippets/logger/manual.ts"
140140
```
141141

142142
In each case, the printed log will look like this:
@@ -168,13 +168,13 @@ When debugging in non-production environments, you can instruct Logger to log th
168168
=== "Middy Middleware"
169169

170170
```typescript hl_lines="15"
171-
--8<-- "docs/snippets/logger/eventMiddy.ts"
171+
--8<-- "examples/snippets/logger/eventMiddy.ts"
172172
```
173173

174174
=== "Decorator"
175175

176176
```typescript hl_lines="8"
177-
--8<-- "docs/snippets/logger/eventDecorator.ts"
177+
--8<-- "examples/snippets/logger/eventDecorator.ts"
178178
```
179179

180180
1. Binding your handler method allows your handler to access `this` within the class methods.
@@ -193,7 +193,7 @@ To remove the keys you added, you can use the `removeKeys` method.
193193
=== "handler.ts"
194194

195195
```typescript hl_lines="5-13 17-25 32"
196-
--8<-- "docs/snippets/logger/appendKeys.ts"
196+
--8<-- "examples/snippets/logger/appendKeys.ts"
197197
```
198198
=== "Example CloudWatch Logs excerpt"
199199

@@ -239,13 +239,13 @@ If you want to make sure that persistent attributes added **inside the handler f
239239
=== "Middy Middleware"
240240

241241
```typescript hl_lines="31"
242-
--8<-- "docs/snippets/logger/clearStateMiddy.ts"
242+
--8<-- "examples/snippets/logger/clearStateMiddy.ts"
243243
```
244244

245245
=== "Decorator"
246246

247247
```typescript hl_lines="16"
248-
--8<-- "docs/snippets/logger/clearStateDecorator.ts"
248+
--8<-- "examples/snippets/logger/clearStateDecorator.ts"
249249
```
250250

251251
1. Binding your handler method allows your handler to access `this` within the class methods.
@@ -303,7 +303,7 @@ You can append additional data to a single log item by passing objects as additi
303303
=== "handler.ts"
304304

305305
```typescript hl_lines="16-18 23-25 37"
306-
--8<-- "docs/snippets/logger/extraData.ts"
306+
--8<-- "examples/snippets/logger/extraData.ts"
307307
```
308308
=== "Example CloudWatch Logs excerpt"
309309

@@ -356,7 +356,7 @@ The error will be logged with default key name `error`, but you can also pass yo
356356
=== "handler.ts"
357357

358358
```typescript hl_lines="13 20-22"
359-
--8<-- "docs/snippets/logger/logError.ts"
359+
--8<-- "examples/snippets/logger/logError.ts"
360360
```
361361

362362
=== "Example CloudWatch Logs excerpt"
@@ -414,7 +414,7 @@ We support the following log levels:
414414
You can access the current log level by using the `getLevelName()` method. This method returns the name of the current log level as a string. If you want to change the log level at runtime, you can use the `setLogLevel()` method. This method accepts a string value that represents the log level you want to set, both lower and upper case values are supported.
415415

416416
```typescript
417-
--8<-- "docs/snippets/logger/logLevel.ts"
417+
--8<-- "examples/snippets/logger/logLevel.ts"
418418
```
419419

420420
If you want to access the numeric value of the current log level, you can use the `level` property. For example, if the current log level is `INFO`, `logger.level` property will return `12`.
@@ -480,7 +480,7 @@ Once a child logger is created, the logger and its parent will act as separate i
480480
=== "handler.ts"
481481

482482
```typescript hl_lines="16-18"
483-
--8<-- "docs/snippets/logger/createChild.ts"
483+
--8<-- "examples/snippets/logger/createChild.ts"
484484
```
485485

486486
=== "Example CloudWatch Logs excerpt"
@@ -534,7 +534,7 @@ Sampling decision happens at the Logger initialization. This means sampling may
534534
=== "handler.ts"
535535

536536
```typescript hl_lines="6"
537-
--8<-- "docs/snippets/logger/logSampling.ts"
537+
--8<-- "examples/snippets/logger/logSampling.ts"
538538
```
539539

540540
=== "Example CloudWatch Logs excerpt - Invocation #1"
@@ -644,15 +644,15 @@ You can customize the structure (keys and values) of your log items by passing a
644644
=== "handler.ts"
645645

646646
```typescript hl_lines="2 6"
647-
--8<-- "docs/snippets/logger/bringYourOwnFormatterHandler.ts"
647+
--8<-- "examples/snippets/logger/bringYourOwnFormatterHandler.ts"
648648
```
649649

650650
This is how the `MyCompanyLogFormatter` (dummy name) would look like:
651651

652652
=== "utils/formatters/MyCompanyLogFormatter.ts"
653653

654654
```typescript
655-
--8<-- "docs/snippets/logger/bringYourOwnFormatterClass.ts"
655+
--8<-- "examples/snippets/logger/bringYourOwnFormatterClass.ts"
656656
```
657657

658658
This is how the printed log would look:
@@ -701,7 +701,7 @@ This is a Jest sample that provides the minimum information necessary for Logger
701701
=== "handler.test.ts"
702702

703703
```typescript
704-
--8<-- "docs/snippets/logger/unitTesting.ts"
704+
--8<-- "examples/snippets/logger/unitTesting.ts"
705705
```
706706

707707
### Suppress logs with Jest

docs/core/metrics.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ The `Metrics` utility must always be instantiated outside of the Lambda handler.
5959
=== "handler.ts"
6060

6161
```typescript hl_lines="1 3-6"
62-
--8<-- "docs/snippets/metrics/basicUsage.ts"
62+
--8<-- "examples/snippets/metrics/basicUsage.ts"
6363
```
6464

6565
### Utility settings
@@ -83,7 +83,7 @@ The `Metrics` utility is instantiated outside of the Lambda handler. In doing th
8383
=== "handler.ts"
8484

8585
```typescript hl_lines="1 4"
86-
--8<-- "docs/snippets/metrics/sam.ts"
86+
--8<-- "examples/snippets/metrics/sam.ts"
8787
```
8888

8989
=== "template.yml"
@@ -109,13 +109,13 @@ You can create metrics using the `addMetric` method, and you can create dimensio
109109
=== "Metrics"
110110

111111
```typescript hl_lines="12"
112-
--8<-- "docs/snippets/metrics/createMetrics.ts"
112+
--8<-- "examples/snippets/metrics/createMetrics.ts"
113113
```
114114

115115
=== "Metrics with custom dimensions"
116116

117117
```typescript hl_lines="12-13"
118-
--8<-- "docs/snippets/metrics/customDimensions.ts"
118+
--8<-- "examples/snippets/metrics/customDimensions.ts"
119119
```
120120

121121
!!! tip "Autocomplete Metric Units"
@@ -137,7 +137,7 @@ You can create [high-resolution metrics](https://aws.amazon.com/about-aws/whats-
137137
=== "Metrics with high resolution"
138138

139139
```typescript hl_lines="4 20"
140-
--8<-- "docs/snippets/metrics/addHighResolutionMetric.ts"
140+
--8<-- "examples/snippets/metrics/addHighResolutionMetric.ts"
141141
```
142142

143143
!!! tip "Autocomplete Metric Resolutions"
@@ -150,7 +150,7 @@ You can call `addMetric()` with the same name multiple times. The values will be
150150
=== "addMetric() with the same name"
151151

152152
```typescript hl_lines="12 14"
153-
--8<-- "docs/snippets/metrics/multiValueMetrics.ts"
153+
--8<-- "examples/snippets/metrics/multiValueMetrics.ts"
154154
```
155155
=== "Example CloudWatch Logs excerpt"
156156

@@ -195,7 +195,7 @@ You can add default dimensions to your metrics by passing them as parameters in
195195
=== "constructor"
196196

197197
```typescript hl_lines="6"
198-
--8<-- "docs/snippets/metrics/defaultDimensions.ts"
198+
--8<-- "examples/snippets/metrics/defaultDimensions.ts"
199199
```
200200

201201
=== "Middy middleware"
@@ -205,13 +205,13 @@ You can add default dimensions to your metrics by passing them as parameters in
205205
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"}.
206206

207207
```typescript hl_lines="24-26"
208-
--8<-- "docs/snippets/metrics/defaultDimensionsMiddy.ts"
208+
--8<-- "examples/snippets/metrics/defaultDimensionsMiddy.ts"
209209
```
210210

211211
=== "setDefaultDimensions method"
212212

213213
```typescript hl_lines="7"
214-
--8<-- "docs/snippets/metrics/setDefaultDimensions.ts"
214+
--8<-- "examples/snippets/metrics/setDefaultDimensions.ts"
215215
```
216216

217217
=== "with logMetrics decorator"
@@ -224,7 +224,7 @@ You can add default dimensions to your metrics by passing them as parameters in
224224
If this is not the desired behavior, you can use the `logMetrics` middleware instead.
225225

226226
```typescript hl_lines="12"
227-
--8<-- "docs/snippets/metrics/defaultDimensionsDecorator.ts"
227+
--8<-- "examples/snippets/metrics/defaultDimensionsDecorator.ts"
228228
```
229229

230230
1. Binding your handler method allows your handler to access `this` within the class methods.
@@ -258,7 +258,7 @@ See below an example of how to automatically flush metrics with the Middy-compat
258258
=== "handler.ts"
259259

260260
```typescript hl_lines="2 17"
261-
--8<-- "docs/snippets/metrics/middy.ts"
261+
--8<-- "examples/snippets/metrics/middy.ts"
262262
```
263263

264264
=== "Example CloudWatch Logs excerpt"
@@ -297,7 +297,7 @@ The `logMetrics` decorator of the metrics utility can be used when your Lambda h
297297
=== "handler.ts"
298298

299299
```typescript hl_lines="10"
300-
--8<-- "docs/snippets/metrics/decorator.ts"
300+
--8<-- "examples/snippets/metrics/decorator.ts"
301301
```
302302

303303
1. Binding your handler method allows your handler to access `this` within the class methods.
@@ -334,7 +334,7 @@ You can manually flush the metrics with `publishStoredMetrics` as follows:
334334
=== "handler.ts"
335335

336336
```typescript hl_lines="13"
337-
--8<-- "docs/snippets/metrics/manual.ts"
337+
--8<-- "examples/snippets/metrics/manual.ts"
338338
```
339339

340340
=== "Example CloudWatch Logs excerpt"
@@ -366,7 +366,7 @@ If you want to ensure that at least one metric is emitted before you flush them,
366366
=== "handler.ts"
367367

368368
```typescript hl_lines="21"
369-
--8<-- "docs/snippets/metrics/throwOnEmptyMetrics.ts"
369+
--8<-- "examples/snippets/metrics/throwOnEmptyMetrics.ts"
370370
```
371371

372372
### Capturing a cold start invocation as metric
@@ -376,13 +376,13 @@ You can optionally capture cold start metrics with the `logMetrics` middleware o
376376
=== "Middy Middleware"
377377

378378
```typescript hl_lines="18"
379-
--8<-- "docs/snippets/metrics/captureColdStartMetricMiddy.ts"
379+
--8<-- "examples/snippets/metrics/captureColdStartMetricMiddy.ts"
380380
```
381381

382382
=== "logMetrics decorator"
383383

384384
```typescript hl_lines="10"
385-
--8<-- "docs/snippets/metrics/captureColdStartMetricDecorator.ts"
385+
--8<-- "examples/snippets/metrics/captureColdStartMetricDecorator.ts"
386386
```
387387

388388
If it's a cold start invocation, this feature will:
@@ -406,7 +406,7 @@ You can add high-cardinality data as part of your Metrics log with the `addMetad
406406
=== "handler.ts"
407407

408408
```typescript hl_lines="15"
409-
--8<-- "docs/snippets/metrics/addMetadata.ts"
409+
--8<-- "examples/snippets/metrics/addMetadata.ts"
410410
```
411411

412412
=== "Example CloudWatch Logs excerpt"
@@ -450,13 +450,13 @@ CloudWatch EMF uses the same dimensions across all your metrics. Use `singleMetr
450450
=== "Middy Middleware"
451451

452452
```typescript hl_lines="21 23-24"
453-
--8<-- "docs/snippets/metrics/singleMetricDifferentDimsMiddy.ts"
453+
--8<-- "examples/snippets/metrics/singleMetricDifferentDimsMiddy.ts"
454454
```
455455

456456
=== "logMetrics decorator"
457457

458458
```typescript hl_lines="16 18-19"
459-
--8<-- "docs/snippets/metrics/singleMetricDifferentDimsDecorator.ts"
459+
--8<-- "examples/snippets/metrics/singleMetricDifferentDimsDecorator.ts"
460460
```
461461

462462
1. Binding your handler method allows your handler to access `this` within the class methods.

0 commit comments

Comments
 (0)