Skip to content

Commit e80abed

Browse files
committed
Added documentation skeleton
1 parent 02b7c9c commit e80abed

File tree

19 files changed

+639
-2
lines changed

19 files changed

+639
-2
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
This project follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) format for changes and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
8+
## [Unreleased]
9+
10+
## [0.1.0] - 2019-11-15
11+
### Added
12+
- Public beta release

docs/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM squidfunk/mkdocs-material
2+
RUN pip install mkdocs-git-revision-date-plugin

docs/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[comment]: <> (Includes Changelog content entire file as a snippet)
2+
--8<-- "CHANGELOG.md"

docs/core/logger.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: Logger
3+
description: Core utility
4+
---
5+
6+
Logger provides an opinionated logger with output structured as JSON.
7+
8+
## Key features
9+
10+
* Capture key fields from Lambda context, cold start and structures logging output as JSON
11+
* Log Lambda event when instructed (disabled by default)
12+
* Log sampling enables DEBUG log level for a percentage of requests (disabled by default)
13+
* Append additional keys to structured log at any point in time
14+
15+
## Getting started
16+
17+
Logger requires two settings:
18+
19+
Setting | Description | Environment variable | Constructor parameter
20+
------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | -------------------------------------------------
21+
**Logging level** | Sets how verbose Logger should be (INFO, by default) | `LOG_LEVEL` | `level`
22+
**Service** | Sets **service** key that will be present across all log statements | `POWERTOOLS_SERVICE_NAME` | `service`
23+
24+
> Example using AWS Serverless Application Model (SAM)
25+
26+
=== "template.yaml"
27+
```yaml hl_lines="9 10"
28+
Resources:
29+
HelloWorldFunction:
30+
Type: AWS::Serverless::Function
31+
Properties:
32+
Runtime: nodejs14.x
33+
Environment:
34+
Variables:
35+
LOG_LEVEL: INFO
36+
POWERTOOLS_SERVICE_NAME: example
37+
```
38+
=== "app.py"
39+
```typescript hl_lines="3 4"
40+
import { Logger } from '@aws-lambda-powertools/logger';
41+
42+
const logger = Logger(); // Sets service via env var
43+
// OR const logger = Logger({ service: 'example' });
44+
```
45+
46+
### Standard structured keys
47+
48+
Your Logger will include the following keys to your structured logging:
49+
50+
Key | Example | Note
51+
------------------------------------------------- | ------------------------------------------------- | ---------------------------------------------------------------------------------
52+
**level**: `str` | `INFO` | Logging level
53+
**location**: `str` | `collect.handler:1` | Source code location where statement was executed
54+
**message**: `Any` | `Collecting payment` | Unserializable JSON values are casted as `str`
55+
**timestamp**: `str` | `2021-05-03 10:20:19,650+0200` | Timestamp with milliseconds, by default uses local timezone
56+
**service**: `str` | `payment` | Service name defined, by default `service_undefined`
57+
**xray_trace_id**: `str` | `1-5759e988-bd862e3fe1be46a994272793` | When [tracing is enabled](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html){target="_blank"}, it shows X-Ray Trace ID
58+
**sampling_rate**: `float` | `0.1` | When enabled, it shows sampling rate in percentage e.g. 10%
59+
**exception_name**: `str` | `ValueError` | When `logger.exception` is used and there is an exception
60+
**exception**: `str` | `Traceback (most recent call last)..` | When `logger.exception` is used and there is an exception
61+
62+
:construction: WIP :construction:
63+
64+
**How do I aggregate and search Powertools logs across accounts?**
65+
66+
As of now, ElasticSearch (ELK) or 3rd party solutions are best suited to this task.
67+
68+
Please see this discussion for more information: https://github.com/awslabs/aws-lambda-powertools-python/issues/460

docs/core/metrics.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: Metrics
3+
description: Core utility
4+
---
5+
6+
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).
7+
8+
These metrics can be visualized through [Amazon CloudWatch Console](https://console.aws.amazon.com/cloudwatch/).
9+
10+
## Key features
11+
12+
* Aggregate up to 100 metrics using a single CloudWatch EMF object (large JSON blob)
13+
* Validate against common metric definitions mistakes (metric unit, values, max dimensions, max metrics, etc)
14+
* Metrics are created asynchronously by CloudWatch service, no custom stacks needed
15+
* Context manager to create a one off metric with a different dimension
16+
17+
## Terminologies
18+
19+
If you're new to Amazon CloudWatch, there are two terminologies you must be aware of before using this utility:
20+
21+
* **Namespace**. It's the highest level container that will group multiple metrics from multiple services for a given application, for example `ServerlessEcommerce`.
22+
* **Dimensions**. Metrics metadata in key-value format. They help you slice and dice metrics visualization, for example `ColdStart` metric by Payment `service`.
23+
24+
<figure>
25+
<img src="../../media/metrics_terminology.png" />
26+
<figcaption>Metric terminology, visually explained</figcaption>
27+
</figure>
28+
29+
30+
## Getting started
31+
32+
Metric has two global settings that will be used across all metrics emitted:
33+
34+
Setting | Description | Environment variable | Constructor parameter
35+
------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | -------------------------------------------------
36+
**Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace`
37+
**Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service`
38+
39+
!!! tip "Use your application or main service as the metric namespace to easily group all metrics"
40+
41+
> Example using AWS Serverless Application Model (SAM)
42+
43+
=== "template.yml"
44+
45+
```yaml hl_lines="9 10"
46+
Resources:
47+
HelloWorldFunction:
48+
Type: AWS::Serverless::Function
49+
Properties:
50+
Runtime: nodejs14.x
51+
Environment:
52+
Variables:
53+
POWERTOOLS_SERVICE_NAME: payment
54+
POWERTOOLS_METRICS_NAMESPACE: ServerlessAirline
55+
```
56+
57+
:construction: WIP :construction:
58+
59+
!!! tip "For more elaborate assertions and comparisons, check out [our functional testing for Metrics utility](https://github.com/awslabs/aws-lambda-powertools-python/blob/develop/tests/functional/test_metrics.py)"

docs/core/tracer.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
title: Tracer
3+
description: Core utility
4+
---
5+
6+
Tracer is an opinionated thin wrapper for [AWS X-Ray Python SDK](https://github.com/aws/aws-xray-sdk-node/).
7+
8+
![Tracer showcase](../media/tracer_utility_showcase.png)
9+
10+
## Key features
11+
12+
* Auto capture cold start as annotation, and responses or full exceptions as metadata
13+
* Run functions locally with SAM CLI without code change to disable tracing
14+
* Explicitly disable tracing via env var `POWERTOOLS_TRACE_ENABLED="false"`
15+
* Support tracing async methods, generators, and context managers
16+
* Auto patch supported modules by AWS X-Ray
17+
18+
## Getting started
19+
20+
### Permissions
21+
22+
Before your use this utility, your AWS Lambda function [must have permissions](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html#services-xray-permissions) to send traces to AWS X-Ray.
23+
24+
> Example using AWS Serverless Application Model (SAM)
25+
26+
=== "template.yml"
27+
```yaml hl_lines="6 9"
28+
Resources:
29+
HelloWorldFunction:
30+
Type: AWS::Serverless::Function
31+
Properties:
32+
Runtime: nodejs14.x
33+
Tracing: Active
34+
Environment:
35+
Variables:
36+
POWERTOOLS_SERVICE_NAME: example
37+
```
38+
39+
### Lambda handler
40+
41+
You can quickly start by importing the `Tracer` class, initialize it outside the Lambda handler, and use `capture_lambda_handler` decorator.
42+
43+
=== "app.py"
44+
```typescript hl_lines="1 3 6"
45+
import { Tracer } from '@aws-lambda-powertools/tracer';
46+
47+
const tracer = Tracer(); // Sets service via env var
48+
// OR tracer = Tracer({ service: 'example' });
49+
50+
@tracer.capture_lambda_handler
51+
const handler = (event, context) => {
52+
const chargeId = event.chargeId;
53+
const payment = collectPayment(chargeId);
54+
...
55+
}
56+
```
57+
58+
When using this `capture_lambda_handler` decorator, Tracer performs these additional tasks to ease operations:
59+
60+
* Creates a `ColdStart` annotation to easily filter traces that have had an initialization overhead
61+
* Captures any response, or full exceptions generated by the handler, and include as tracing metadata
62+
63+
### Annotations & Metadata
64+
65+
**Annotations** are key-values associated with traces and indexed by AWS X-Ray. You can use them to filter traces and to create [Trace Groups](https://aws.amazon.com/about-aws/whats-new/2018/11/aws-xray-adds-the-ability-to-group-traces/) to slice and dice your transactions.
66+
67+
**Metadata** are key-values also associated with traces but not indexed by AWS X-Ray. You can use them to add additional context for an operation using any native object.
68+
69+
=== "Annotations"
70+
You can add annotations using `put_annotation` method.
71+
72+
```typescript hl_lines="7"
73+
import { Tracer } from '@aws-lambda-powertools/tracer';
74+
75+
const tracer = Tracer()
76+
77+
@tracer.capture_lambda_handler
78+
const handler = (event, context) => {
79+
...
80+
const res = some_logic();
81+
tracer.putAnnotation(key='payment_response', value=res);
82+
}
83+
```
84+
=== "Metadata"
85+
You can add metadata using `put_metadata` method.
86+
87+
```typescript hl_lines="9"
88+
import { Tracer } from '@aws-lambda-powertools/tracer';
89+
90+
const tracer = Tracer()
91+
92+
@tracer.capture_lambda_handler
93+
const handler = (event, context) => {
94+
...
95+
const res = some_logic();
96+
tracer.putMetadata(key='payment_response', value=res);
97+
}
98+
```
99+
100+
:construction: WIP :construction:
101+
102+
## Testing your code
103+
104+
Powertools should be able to detect that it's not running inside a Lambda execution environment and disable tracing for you. If that doesn't work you can also disable it explicitly using `POWERTOOLS_TRACE_ENABLED` environment variable.
105+
106+
```bash
107+
POWERTOOLS_TRACE_ENABLED=0 npm run test
108+
```
109+
110+
## Tips
111+
112+
* Use annotations on key operations to slice and dice traces, create unique views, and create metrics from it via Trace Groups
113+
* Use a namespace when adding metadata to group data more easily
114+
* Annotations and metadata are added to the current subsegment opened. If you want them in a specific subsegment, use a [context manager](https://github.com/aws/aws-xray-sdk-python/#start-a-custom-segmentsubsegment) via the escape hatch mechanism

docs/diagram_src/.gitignore

Whitespace-only changes.

docs/index.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
title: Homepage
3+
description: AWS Lambda Powertools Typescript
4+
---
5+
6+
A suite of utilities for AWS Lambda functions to ease adopting best practices such as tracing, structured logging, custom metrics, and more.
7+
8+
!!! tip "Looking for a quick read through how the core features are used?"
9+
Check out [this detailed blog post](https://aws.amazon.com/blogs/opensource/simplifying-serverless-best-practices-with-lambda-powertools/) with a practical example.
10+
11+
## Tenets
12+
13+
This project separates core utilities that will be available in other runtimes vs general utilities that might not be available across all runtimes.
14+
15+
* **AWS Lambda only**. We optimise for AWS Lambda function environments and supported runtimes only. Utilities might work with web frameworks and non-Lambda environments, though they are not officially supported.
16+
* **Eases the adoption of best practices**. The main priority of the utilities is to facilitate best practices adoption, as defined in the AWS Well-Architected Serverless Lens; all other functionality is optional.
17+
* **Keep it lean**. Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
18+
* **We strive for backwards compatibility**. New features and changes should keep backwards compatibility. If a breaking change cannot be avoided, the deprecation and migration process should be clearly defined.
19+
* **We work backwards from the community**. We aim to strike a balance of what would work best for 80% of customers. Emerging practices are considered and discussed via Requests for Comment (RFCs)
20+
* **Idiomatic**. Utilities follow programming language idioms and language-specific best practices.
21+
22+
## Install
23+
24+
:construction:
25+
26+
## Features
27+
28+
| Utility | Description
29+
| ------------------------------------------------- | ---------------------------------------------------------------------------------
30+
[Tracing](./core/tracer.md) | Decorators and utilities to trace Lambda function handlers, and both synchronous and asynchronous functions
31+
[Logger](./core/logger.md) | Structured logging made easier, and decorator to enrich structured logging with key Lambda context details
32+
[Metrics](./core/metrics.md) | Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF)
33+
34+
## Environment variables
35+
36+
!!! info
37+
**Explicit parameters take precedence over environment variables.**
38+
39+
| Environment variable | Description | Utility | Default |
40+
| ------------------------------------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- | ------------------------------------------------- |
41+
| **POWERTOOLS_SERVICE_NAME** | Sets service name used for tracing namespace, metrics dimension and structured logging | All | `"service_undefined"` |
42+
| **POWERTOOLS_METRICS_NAMESPACE** | Sets namespace used for metrics | [Metrics](./core/metrics) | `None` |
43+
| **POWERTOOLS_TRACE_DISABLED** | Disables tracing | [Tracing](./core/tracer) | `false` |
44+
| **POWERTOOLS_TRACER_CAPTURE_RESPONSE** | Captures Lambda or method return as metadata. | [Tracing](./core/tracer) | `true` |
45+
| **POWERTOOLS_TRACER_CAPTURE_ERROR** | Captures Lambda or method exception as metadata. | [Tracing](./core/tracer) | `true` |
46+
| **POWERTOOLS_LOGGER_LOG_EVENT** | Logs incoming event | [Logging](./core/logger) | `false` |
47+
| **POWERTOOLS_LOGGER_SAMPLE_RATE** | Debug log sampling | [Logging](./core/logger) | `0` |
48+
| **POWERTOOLS_LOG_DEDUPLICATION_DISABLED** | Disables log deduplication filter protection to use Pytest Live Log feature | [Logging](./core/logger) | `false` |
49+
| **LOG_LEVEL** | Sets logging level | [Logging](./core/logger) | `INFO` |

0 commit comments

Comments
 (0)