Skip to content

Commit df0d9d3

Browse files
authored
docs(all): clarifications & fixes (#370)
* docs: improve contrast for inline code expressions * docs: format stylesheet & remove invalid declarations * docs: fix typo in connect section * docs: clarified middy's usage & linked to official docs * docs: uniformed main example with tabs for logger * docs: moved aws sdk patching under getting started section * docs: added testing your code section to logger * docs: moved middy tips under middy examples * docs: Moved tip under code example in testing yoru code section
1 parent 61c15ab commit df0d9d3

File tree

5 files changed

+80
-29
lines changed

5 files changed

+80
-29
lines changed

Diff for: docs/core/logger.md

+53-10
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,15 @@ Key | Example
105105
**function_arn**: `string` | `arn:aws:lambda:eu-central-1:123456789012:function:shopping-cart-api-lambda-prod-eu-central-1`
106106
**function_request_id**: `string` | `c6af9ac6-7b61-11e6-9a41-93e812345678`
107107

108-
#### Method 1, using a [Middy](https://github.com/middyjs/middy) middleware:
108+
=== "Middleware"
109109

110-
=== "handler.ts"
110+
!!! note
111+
Middy comes bundled with Logger, so you can just import it when using the middleware.
112+
113+
!!! tip "Using Middy for the first time?"
114+
Learn more about [its usage and lifecycle in the official Middy documentation](https://github.com/middyjs/middy#usage){target="_blank"}.
111115

112-
```typescript hl_lines="1 9-11"
116+
```typescript hl_lines="1-2 10-11"
113117
import { Logger, injectLambdaContext } from "@aws-lambda-powertools/logger";
114118
import middy from '@middy/core';
115119

@@ -123,9 +127,7 @@ Key | Example
123127
.use(injectLambdaContext(logger));
124128
```
125129

126-
#### Method 2, calling the `addContext` method:
127-
128-
=== "handler.ts"
130+
=== "Manual"
129131

130132
```typescript hl_lines="7"
131133
import { Logger } from "@aws-lambda-powertools/logger";
@@ -141,9 +143,7 @@ Key | Example
141143
};
142144
```
143145

144-
#### Method 3, using a class decorator:
145-
146-
=== "handler.ts"
146+
=== "Decorator"
147147

148148
```typescript hl_lines="7"
149149
import { Logger } from "@aws-lambda-powertools/logger";
@@ -621,4 +621,47 @@ This is how the printed log would look:
621621
},
622622
"awsAccountId": "123456789012"
623623
}
624-
```
624+
```
625+
626+
## Testing your code
627+
628+
### Inject Lambda Context
629+
630+
When unit testing your code that makes use of `logger.addContext()` or `injectLambdaContext` middleware and decorator, you can optionally pass a dummy Lambda Context if you want your logs to contain this information.
631+
632+
This is a Jest sample that provides the minimum information necessary for Logger to inject context data:
633+
634+
=== "handler.test.ts"
635+
636+
```typescript
637+
638+
const dummyContext = {
639+
callbackWaitsForEmptyEventLoop: true,
640+
functionVersion: '$LATEST',
641+
functionName: 'foo-bar-function',
642+
memoryLimitInMB: '128',
643+
logGroupName: '/aws/lambda/foo-bar-function',
644+
logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456',
645+
invokedFunctionArn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function',
646+
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
647+
getRemainingTimeInMillis: () => 1234,
648+
done: () => console.log('Done!'),
649+
fail: () => console.log('Failed!'),
650+
succeed: () => console.log('Succeeded!'),
651+
};
652+
653+
describe('MyUnitTest', () => {
654+
655+
test('Lambda invoked successfully', async () => {
656+
657+
const testEvent = { test: 'test' };
658+
await handler(testEvent, dummyContext);
659+
660+
});
661+
662+
});
663+
664+
```
665+
666+
!!! tip
667+
If you don't want to declare your own dummy Lambda Context, you can use [`ContextExamples.helloworldContext`](https://github.com/awslabs/aws-lambda-powertools-typescript/blob/main/packages/commons/src/tests/resources/contexts/hello-world.ts#L3-L16) from [`@aws-lambda-powertools/commons`](https://www.npmjs.com/package/@aws-lambda-powertools/commons).

Diff for: docs/core/metrics.md

+6
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ See examples below:
163163

164164
=== "Middy middleware"
165165

166+
!!! note
167+
Middy comes bundled with Metrics, so you can just import it when using the middleware.
168+
169+
!!! tip "Using Middy for the first time?"
170+
Learn more about [its usage and lifecycle in the official Middy documentation](https://github.com/middyjs/middy#usage){target="_blank"}.
171+
166172
```typescript hl_lines="5"
167173
import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
168174
import { Context } from 'aws-lambda';

Diff for: docs/core/tracer.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
7272

7373
=== "Middleware"
7474

75+
!!! note
76+
Middy comes bundled with Tracer, so you can just import it when using the middleware.
77+
78+
!!! tip "Using Middy for the first time?"
79+
Learn more about [its usage and lifecycle in the official Middy documentation](https://github.com/middyjs/middy#usage){target="_blank"}.
80+
7581
```typescript hl_lines="1-2 4 7 9"
7682
import { Tracer } from '@aws-lambda-powertools/tracer';
7783
import middy from '@middy/core';
@@ -105,7 +111,7 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
105111

106112
=== "Manual"
107113

108-
```typescript hl_lines="1 3 7 9 11 17 20 24"
114+
```typescript hl_lines="1 3 7 9-10 13-14 20 23 27 29"
109115
import { Tracer } from '@aws-lambda-powertools/tracer';
110116

111117
const tracer = Tracer(); // Sets service via env var
@@ -252,8 +258,6 @@ You can trace other methods using the `captureMethod` decorator or manual instru
252258
}
253259
```
254260

255-
## Advanced
256-
257261
### Patching AWS SDK clients
258262

259263
Tracer can patch [AWS SDK clients](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-awssdkclients.html) and create traces when your application makes calls to AWS services.
@@ -300,6 +304,8 @@ If you're looking to shave a few microseconds, or milliseconds depending on your
300304
const s3 = tracer.captureAWSClient(new S3({ apiVersion: "2006-03-01" }));
301305
```
302306

307+
## Advanced
308+
303309
### Disabling response auto-capture
304310

305311
Use **`POWERTOOLS_TRACER_CAPTURE_RESPONSE=false`** environment variable to instruct Tracer **not** to serialize function responses as metadata.

Diff for: docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,5 @@ Each TypeScript utility is installed as standalone NPM package.
7878

7979
## Connect
8080

81-
* **AWS Developers Slack**: `#lambda-powertools`** - **[Invite, if you don't have an account](https://join.slack.com/t/awsdevelopers/shared_invite/zt-yryddays-C9fkWrmguDv0h2EEDzCqvw){target="_blank"}**
81+
* **AWS Developers Slack**: `#lambda-powertools` - [Invite, if you don't have an account](https://join.slack.com/t/awsdevelopers/shared_invite/zt-yryddays-C9fkWrmguDv0h2EEDzCqvw){target="_blank"}
8282
* **Email**: [email protected]

Diff for: docs/stylesheets/extra.css

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
11
.md-grid {
2-
max-width: 81vw
3-
}
4-
5-
.highlight .hll {
6-
background-color: lavender
7-
8-
[data-md-color-scheme="slate"] {
9-
background-color: rgb(69, 48, 164)
10-
}
2+
max-width: 81vw;
113
}
124

135
.md-typeset table:not([class]) {
14-
font-size: 0.75rem
6+
font-size: 0.75rem;
157
}
168

179
.md-typeset a {
18-
border-bottom: 0.1px dashed black
10+
border-bottom: 0.1px dashed black;
1911
}
2012

2113
.md-typeset table:not([class]) {
22-
font-size: 0.75rem
14+
font-size: 0.75rem;
2315
}
2416

2517
.md-nav__link--active {
26-
font-weight: bold
18+
font-weight: bold;
2719
}
2820

2921
.md-typeset .admonition, .md-typeset details {
30-
font-size: 0.70rem
22+
font-size: 0.70rem;
3123
}
3224

3325
[data-md-color-scheme="slate"] {
34-
--md-typeset-a-color: rgb(28, 152, 152)
26+
--md-typeset-a-color: rgb(28, 152, 152);
3527
}
3628

3729
.copyMe {
3830
cursor: pointer;
3931
border-bottom: 0.1px dashed black;
4032
}
33+
34+
p code {
35+
font-weight: bolder;
36+
}

0 commit comments

Comments
 (0)