Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Minimal AWS Lambda starter with **structured JSON logging**, a small **metrics h
## Features

- Structured logger that emits one JSON object per line (CloudWatch-friendly)
- Simple EMF-style metrics buffer you can flush as a log line
- Correlation IDs for tracing a single invocation across log lines
- Simple EMF-style metrics buffer you can flush as a log line (counters, gauges, timings)
- Example Lambda handler wiring logger + metrics
- Pure unit tests (no network, no AWS credentials)

Expand All @@ -23,21 +24,61 @@ pytest -q
```python
from lambda_obs.handler import handler

result = handler({"name": "world"}, None)
print(result) # {"message": "hello world", "ok": true}
result = handler({"name": "world", "correlation_id": "demo-1"}, None)
print(result) # {"message": "hello world", "ok": true, "correlation_id": "demo-1"}
```

Pass a correlation id via event field, `headers.x-correlation-id`, or Lambda `context.aws_request_id`.

## Layout

```
src/lambda_obs/
logging.py # structured JSON logger
metrics.py # metrics helper
logging.py # structured JSON logger + correlation ids
metrics.py # metrics helper (count / set / timing)
handler.py # example Lambda entrypoint
tests/
.github/workflows/ci.yml
```

## Operations

### Structured logs in CloudWatch

Each log line is a single JSON object. Filter and explore with CloudWatch Logs Insights, for example:

```
fields @timestamp, level, message, correlation_id
| filter ispresent(correlation_id)
| sort @timestamp desc
| limit 50
```

### Metrics (EMF)

`Metrics.flush()` writes an Embedded Metric Format line to stdout. In Lambda, CloudWatch extracts the namespace, dimensions, and metric values automatically—no extra agent required.

Useful starter metrics from the example handler:

- `Invocations` / `Success` / `Errors` (Count)
- `HandlerDuration` (Milliseconds)

### Correlation

Prefer one correlation id per request. Propagate it to downstream calls and keep it on every log line via `Logger.with_correlation_id(...)`.

### Local vs deployed

| Concern | Local | Deployed Lambda |
| --- | --- | --- |
| Credentials | Not required for unit tests | Execution role only |
| Logs | stdout / captured in tests | CloudWatch Logs |
| Metrics | JSON line on stdout | EMF → CloudWatch Metrics |

### CI

GitHub Actions runs `pytest` across Python 3.10–3.12 on pushes and pull requests to `main` and `develop`.

## Design notes

Inspired by the *ideas* behind AWS Lambda Powertools (structured logs + custom metrics), but this is an original, tiny teaching scaffold — not a fork or copy of AWS Powertools code.
Expand Down
Loading