Skip to content

Commit 9eb127a

Browse files
authored
docs(tracer): split and lint code snippets (#1260)
1 parent b45d382 commit 9eb127a

File tree

6 files changed

+57
-26
lines changed

6 files changed

+57
-26
lines changed

Diff for: .github/workflows/python_docs.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
- "docs/**"
99
- "CHANGELOG.md"
1010
- "mkdocs.yml"
11+
- "examples/**"
1112

1213
jobs:
1314
docs:
@@ -20,10 +21,15 @@ jobs:
2021
uses: actions/setup-python@v4
2122
with:
2223
python-version: "3.8"
24+
# Maintenance: temporarily until we drop Python 3.6 and make cfn-lint a dev dependency
25+
- name: Setup Cloud Formation Linter with Latest Version
26+
uses: scottbrenner/cfn-lint-action@v2
2327
- name: Install dependencies
2428
run: make dev
2529
- name: Lint documentation
26-
run: make lint-docs
30+
run: |
31+
make lint-docs
32+
cfn-lint examples/**/*.yaml
2733
- name: Setup doc deploy
2834
run: |
2935
git config --global user.name Docs deploy

Diff for: .pre-commit-config.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ repos:
3030
entry: poetry run flake8
3131
language: system
3232
types: [python]
33-
exclude: example
3433
- repo: https://github.com/igorshubovych/markdownlint-cli
3534
rev: "11c08644ce6df850480d98f628596446a526cbc6" # frozen: v0.31.1
3635
hooks:
3736
- id: markdownlint
3837
args: ["--fix"]
38+
- repo: https://github.com/aws-cloudformation/cfn-python-lint
39+
rev: v0.61.1
40+
hooks:
41+
- id: cfn-python-lint
42+
files: examples/.*\.(yaml|yml)$

Diff for: Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ dev:
1010
pre-commit install
1111

1212
format:
13-
poetry run isort aws_lambda_powertools tests
14-
poetry run black aws_lambda_powertools tests
13+
poetry run isort aws_lambda_powertools tests examples
14+
poetry run black aws_lambda_powertools tests examples
1515

1616
lint: format
17-
poetry run flake8 aws_lambda_powertools/* tests/*
17+
poetry run flake8 aws_lambda_powertools tests examples
1818

1919
lint-docs:
2020
docker run -v ${PWD}:/markdown 06kellyjac/markdownlint-cli "docs"

Diff for: docs/core/tracer.md

+4-21
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,16 @@ Tracer is an opinionated thin wrapper for [AWS X-Ray Python SDK](https://github.
2020

2121
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.
2222

23-
```yaml hl_lines="6 9" title="AWS Serverless Application Model (SAM) example"
24-
Resources:
25-
HelloWorldFunction:
26-
Type: AWS::Serverless::Function
27-
Properties:
28-
Runtime: python3.8
29-
Tracing: Active
30-
Environment:
31-
Variables:
32-
POWERTOOLS_SERVICE_NAME: example
23+
```yaml hl_lines="9 12" title="AWS Serverless Application Model (SAM) example"
24+
--8<-- "examples/tracer/template.yaml"
3325
```
3426

3527
### Lambda handler
3628

3729
You can quickly start by initializing `Tracer` and use `capture_lambda_handler` decorator for your Lambda handler.
3830

39-
```python hl_lines="1 3 6" title="Tracing Lambda handler with capture_lambda_handler"
40-
from aws_lambda_powertools import Tracer
41-
42-
tracer = Tracer() # Sets service via env var
43-
# OR tracer = Tracer(service="example")
44-
45-
@tracer.capture_lambda_handler
46-
def handler(event, context):
47-
charge_id = event.get('charge_id')
48-
payment = collect_payment(charge_id)
49-
...
31+
```python hl_lines="1 4 12" title="Tracing Lambda handler with capture_lambda_handler"
32+
--8<-- "examples/tracer/src/capture_lambda_handler.py"
5033
```
5134

5235
`capture_lambda_handler` performs these additional tasks to ease operations:

Diff for: examples/tracer/src/capture_lambda_handler.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from aws_lambda_powertools import Tracer
2+
from aws_lambda_powertools.utilities.typing import LambdaContext
3+
4+
tracer = Tracer() # Sets service via POWERTOOLS_SERVICE_NAME env var
5+
# OR tracer = Tracer(service="example")
6+
7+
8+
def collect_payment(charge_id: str) -> str:
9+
return f"dummy payment collected for charge: {charge_id}"
10+
11+
12+
@tracer.capture_lambda_handler
13+
def handler(event: dict, context: LambdaContext) -> str:
14+
charge_id = event.get("charge_id")
15+
return collect_payment(charge_id)

Diff for: examples/tracer/template.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
AWSTemplateFormatVersion: "2010-09-09"
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: AWS Lambda Powertools Tracer doc examples
4+
5+
Globals:
6+
Function:
7+
Timeout: 5
8+
Runtime: python3.9
9+
Tracing: Active
10+
Environment:
11+
Variables:
12+
POWERTOOLS_SERVICE_NAME: example
13+
Layers:
14+
# Find the latest Layer version in the official documentation
15+
# https://awslabs.github.io/aws-lambda-powertools-python/latest/#lambda-layer
16+
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPython:21
17+
18+
Resources:
19+
CaptureLambdaHandlerExample:
20+
Type: AWS::Serverless::Function
21+
Properties:
22+
CodeUri: src
23+
Handler: capture_lambda_handler.handler

0 commit comments

Comments
 (0)