Skip to content

Commit

Permalink
feat: add pre-commit hooks (#64)
Browse files Browse the repository at this point in the history
* chore: pre-commit dep

Signed-off-by: heitorlessa <lessa@amazon.co.uk>

* feat: add initial pre-commit config

Signed-off-by: heitorlessa <lessa@amazon.co.uk>

* feat: additional type annotation, toml, merge, etc

Signed-off-by: heitorlessa <lessa@amazon.co.uk>

* improv: speed up formatting

Signed-off-by: heitorlessa <lessa@amazon.co.uk>

* fix: python version range

Signed-off-by: heitorlessa <lessa@amazon.co.uk>

* chore: pre-commit checks

* chore: update dev deps

* feat: install pre-commit hooks during dev

Signed-off-by: heitorlessa <lessa@amazon.co.uk>
  • Loading branch information
heitorlessa authored Jun 7, 2020
1 parent 58aa099 commit b010833
Show file tree
Hide file tree
Showing 26 changed files with 180 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ assignees: ''

* **Powertools version used**:
* **Packaging format (Layers, PyPi)**:
* **AWS Lambda function runtime:**
* **AWS Lambda function runtime:**
* **Debugging logs**

> [How to enable debug mode](https://awslabs.github.io/aws-lambda-powertools-python/#debug-mode)**
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/rfc.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ assignees: ''
## Key information

* RFC PR: (leave this empty)
* Related issue(s), if known:
* Related issue(s), if known:
* Area: (i.e. Tracer, Metrics, Logger, etc.)
* Meet [tenets](https://awslabs.github.io/aws-lambda-powertools-python/#tenets): (Yes/no)

Expand Down
38 changes: 38 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# We use poetry to run formatting and linting before commit/push
# Longers checks such as tests, security and complexity baseline
# are run as part of CI to prevent slower feedback loop
# All checks can be run locally via `make pr`

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: check-merge-conflict
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-toml
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.5.1
hooks:
- id: python-use-type-annotations
- repo: local
hooks:
- id: black
name: formatting::black
entry: poetry run black
language: system
types: [python]
- id: isort
name: formatting::isort
entry: poetry run isort -rc
language: system
types: [python]
- repo: local
hooks:
- id: flake8
name: linting::flake8
entry: poetry run flake8
language: system
types: [python]
exclude: example
fail_fast: true
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Changelog
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
Expand Down
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ target:
@$(MAKE) pr

dev:
pip install --upgrade pip poetry
pip install --upgrade pip poetry pre-commit
poetry install
pre-commit install

dev-docs:
cd docs && npm install

format:
poetry run isort -rc .
poetry run black aws_lambda_powertools
poetry run black tests
poetry run isort -rc aws_lambda_powertools tests example
poetry run black aws_lambda_powertools tests

lint: format
poetry run flake8
Expand Down
2 changes: 1 addition & 1 deletion aws_lambda_powertools/metrics/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@
}
}
}
}
}
2 changes: 1 addition & 1 deletion aws_lambda_powertools/middleware_factory/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def lambda_handler_decorator(decorator: Callable = None, trace_execution=False):
Environment variables
---------------------
POWERTOOLS_TRACE_MIDDLEWARES : str
uses `aws_lambda_powertools.tracing.Tracer`
uses `aws_lambda_powertools.tracing.Tracer`
to create sub-segments per middleware (e.g. `"true", "True", "TRUE"`)
Parameters
Expand Down
2 changes: 1 addition & 1 deletion bandit.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,4 @@
"test_name": "blacklist"
}
]
}
}
18 changes: 9 additions & 9 deletions docs/content/core/logger.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ You can also explicitly set a service name via `service` param or via `POWERTOOL
```python:title=app.py
from aws_lambda_powertools.logging import Logger
# POWERTOOLS_SERVICE_NAME defined
logger = Logger() # highlight-line
logger = Logger() # highlight-line

# Explicit definition
Logger(service="payment", level="INFO")
Expand All @@ -58,7 +58,7 @@ Key | Type | Example | Description
**sampling_rate** | int | 0.1 | Debug logging sampling rate in percentage e.g. 1% in this case
**message** | any | "Collecting payment" | Log statement value. Unserializable JSON values will be casted to string

## Capturing context Lambda info
## Capturing context Lambda info

You can enrich your structured logs with key Lambda context information via `inject_lambda_context`.

Expand Down Expand Up @@ -109,7 +109,7 @@ Key | Type | Example
<summary><strong>Exerpt output in CloudWatch Logs</strong></summary>

```json:title=cloudwatch_logs.json
{
{
"timestamp":"2020-05-24 18:17:33,774",
"level":"INFO",
"location":"collect.handler:1",
Expand All @@ -125,7 +125,7 @@ Key | Type | Example
"message": "Collecting payment"
}

{
{
"timestamp":"2020-05-24 18:17:33,774",
"level":"INFO",
"location":"collect.handler:15",
Expand All @@ -137,7 +137,7 @@ Key | Type | Example
"cold_start": true,
"sampling_rate": 0.0,
// highlight-start
"message":{
"message":{
"operation":"collect_payment",
"charge_id": "ch_AZFlk2345C0"
}
Expand All @@ -148,7 +148,7 @@ Key | Type | Example

## Appending additional keys

You can append your own keys to your existing Logger via `structure_logs` with **append** param.
You can append your own keys to your existing Logger via `structure_logs` with **append** param.

```python:title=collect.py
from aws_lambda_powertools.logging import Logger
Expand All @@ -166,7 +166,7 @@ def handler(event, context)
<summary><strong>Exerpt output in CloudWatch Logs</strong></summary>

```json:title=cloudwatch_logs.jsonn
{
{
"timestamp": "2020-05-24 18:17:33,774",
"level": "INFO",
"location": "collect.handler:1",
Expand Down Expand Up @@ -209,7 +209,7 @@ def handler(event, context)
<summary><strong>Exerpt output in CloudWatch Logs</strong></summary>

```json:title=cloudwatch_logs.json
{
{
"timestamp": "2020-05-24 18:17:33,774",
"level": "INFO",
"location": "collect.handler:1",
Expand All @@ -223,4 +223,4 @@ def handler(event, context)
"message": "Collecting payment"
}
```
</details>
</details>
8 changes: 4 additions & 4 deletions docs/content/core/metrics.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ You can also pass a service name via `service` param or `POWERTOOLS_SERVICE_NAME
from aws_lambda_powertools.metrics import Metrics, MetricUnit

# POWERTOOLS_METRICS_NAMESPACE and POWERTOOLS_SERVICE_NAME defined
metrics = Metrics() # highlight-line
metrics = Metrics() # highlight-line

# Explicit definition
Metrics(namespace="ServerlessAirline", service="orders") # creates a default dimension {"service": "orders"} under the namespace "ServerlessAirline"
Expand Down Expand Up @@ -69,7 +69,7 @@ CloudWatch EMF supports a max of 100 metrics. Metrics will automatically flush a

## Creating a metric with a different dimension

CloudWatch EMF uses the same dimensions across all your metrics. Use `single_metric` if you have a metric that should have different dimensions.
CloudWatch EMF uses the same dimensions across all your metrics. Use `single_metric` if you have a metric that should have different dimensions.

<Note type="info">
Generally, this would be an edge case since you <a href="https://aws.amazon.com/cloudwatch/pricing/">pay for unique metric</a>. Keep the following formula in mind:
Expand Down Expand Up @@ -105,7 +105,7 @@ def lambda_handler(evt, ctx):

`log_metrics` decorator **validates**, **serializes**, and **flushes** all your metrics. During metrics validation, if any of the following criteria is met, `SchemaValidationError` exception will be raised:

* At least of one Metric and Dimension
* At least of one Metric and Dimension
* Maximum of 9 dimensions
* Namespace is set, and no more than one
* Metric units must be [supported by CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html)
Expand Down Expand Up @@ -141,7 +141,7 @@ metrics = Metrics(namespace="ExampleApplication", service="booking")
metrics.add_metric(name="ColdStart", unit="Count", value=1)

# highlight-start
your_metrics_object = metrics.serialize_metric_set()
your_metrics_object = metrics.serialize_metric_set()
metrics.clear_metrics()
print(json.dumps(your_metrics_object))
# highlight-end
Expand Down
16 changes: 8 additions & 8 deletions docs/content/core/tracer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ You can either explicitly pass using `service` param or via `POWERTOOLS_SERVICE_
```python:title=app.py
from aws_lambda_powertools.tracing import Tracer
# POWERTOOLS_SERVICE_NAME defined
tracer = Tracer() # highlight-line
tracer = Tracer() # highlight-line

# Explicit definition
tracer = Tracer(service="booking")
```

## Lambda handler

You can trace your Lambda function handler via `capture_lambda_handler`.
You can trace your Lambda function handler via `capture_lambda_handler`.

When using this decorator, Tracer performs these additional tasks to ease operations:

Expand All @@ -56,7 +56,7 @@ When using this decorator, Tracer performs these additional tasks to ease operat

```python:title=lambda_handler.py
from aws_lambda_powertools.tracing import Tracer
tracer = Tracer()
tracer = Tracer()

@tracer.capture_lambda_handler # highlight-line
def handler(event, context)
Expand All @@ -73,7 +73,7 @@ You can add annotations using `put_annotation` method from Tracer.

```python:title=app.py
from aws_lambda_powertools.tracing import Tracer
tracer = Tracer()
tracer = Tracer()

@tracer.capture_lambda_handler
def handler(event, context):
Expand All @@ -89,7 +89,7 @@ You can add metadata using `put_metadata` method from Tracer.

```python:title=app.py
from aws_lambda_powertools.tracing import Tracer
tracer = Tracer()
tracer = Tracer()

@tracer.capture_lambda_handler
def handler(event, context):
Expand Down Expand Up @@ -125,7 +125,7 @@ from aws_lambda_powertools.tracing import Tracer
tracer = Tracer()

# highlight-start
@tracer.capture_method
@tracer.capture_method
async def collect_payment():
...
# highlight-end
Expand Down Expand Up @@ -194,7 +194,7 @@ async def collect_payment(charge_id):
...
```

## Reusing Tracer across your code
## Reusing Tracer across your code

Tracer keeps a copy of its configuration after the first initialization. This is useful for scenarios where you want to use Tracer in more than one location across your code base.

Expand All @@ -214,7 +214,7 @@ def handler(event, context)
from aws_lambda_powertools.tracing import Tracer
# highlight-start
# new instance using existing configuration with auto patching overriden
tracer = Tracer(auto_patch=False)
tracer = Tracer(auto_patch=False)
# highlight-end
```

Expand Down
2 changes: 1 addition & 1 deletion docs/content/dummy.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ description: Dummy description

## Test

dummy content
dummy content
2 changes: 1 addition & 1 deletion docs/content/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Powertools is available in PyPi. You can use your favourite dependency managemen

* **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.
* **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.
* **Keep it lean** – Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
* **Keep it lean** – Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
* **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.
* **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)
* **Idiomatic** – Utilities follow programming language idioms and language-specific best practices.
Expand Down
2 changes: 1 addition & 1 deletion docs/content/utilities/middleware_factory.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ from aws_lambda_powertools.middleware_factory import lambda_handler_decorator

# highlight-start
@lambda_handler_decorator
def middleware_before_after(handler, event, context):
def middleware_before_after(handler, event, context):
# highlight-end
# logic_before_handler_execution()
response = handler(event, context)
Expand Down
2 changes: 1 addition & 1 deletion docs/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = {
'AWS SAM Docs': {
href: 'https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html',
}
}
}
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/src/assets/aws-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/src/components/Note/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ const Note = ({ type = 'info', title = '', children }) => {
)
}

export default Note
export default Note
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Shadow Header built-in component and remove `Launch Graph Manager`
export default () => null;
export default () => null;
2 changes: 1 addition & 1 deletion example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,4 @@ $RECYCLE.BIN/

*/build/*

# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
4 changes: 2 additions & 2 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ This project contains source code and supporting files for a serverless applicat

- hello_world - Code for the application's Lambda function.
- events - Invocation events that you can use to invoke the function.
- tests - Unit tests for the application code.
- tests - Unit tests for the application code.
- template.yaml - A template that defines the application's AWS resources.
- requirements-dev.txt - Dev dependencies to run unit tests successfully

The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code.

If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit.
If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit.
The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started.

* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
Expand Down
2 changes: 1 addition & 1 deletion example/hello_world/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

_cold_start = True

metrics.add_dimension(name="operation", value="example") # added at cold start only
metrics.add_dimension(name="operation", value="example") # added at cold start only


async def aioboto_task():
Expand Down
2 changes: 1 addition & 1 deletion example/pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[pytest]
addopts = -ra -v
testpaths = ./tests
testpaths = ./tests
Loading

0 comments on commit b010833

Please sign in to comment.