Shared runtime library for Wholeshoot services
Project description
wshtlib
Lightweight observability library for AWS Lambda and FastAPI. Zero external dependencies.
A focused alternative to aws-powertools — covers structured logging, CloudWatch metrics (EMF), request context propagation, and Lambda handler boilerplate. Nothing more.
Install
pip install wshtlib
FastAPI/Starlette middleware is optional:
pip install wshtlib[fastapi]
Usage
Lambda handler
from wshtlib import bootstrap, get_logger
logger = get_logger("my-service")
@bootstrap
def handler(event, context):
logger.info("invoked", path=event.get("path"))
return {"statusCode": 200}
The decorator handles:
- Warming events (
"source": "lambda-warming") — returns 200 early - Context init and structured log enrichment
- Unhandled exceptions — logs error, returns 500
Structured logging
from wshtlib import get_logger
logger = get_logger("my-service")
logger.info("user signed in", user_id="u_123", plan="pro")
Output is JSON to stdout, enriched with level, timestamp, service, location, runtime fields, and Lambda context on invocation.
CloudWatch metrics (EMF)
from wshtlib.metrics import metrics
metrics.count("OrderPlaced")
metrics.put("Duration", 142.5, unit="Milliseconds")
metrics.flush()
metrics is a module-level MetricsContext instance. For isolated contexts (e.g. per-request), instantiate MetricsContext() directly.
Namespace defaults to the METRICS_NAMESPACE env var, falling back to "Wholeshoot".
Request context
from wshtlib import get_context, set_user_id
set_user_id(claims["sub"])
ctx = get_context() # {"trace_id": ..., "correlation_id": ..., "user_id": ...}
Context is stored in a ContextVar — safe for concurrent async handlers.
FastAPI middleware
from fastapi import FastAPI
from wshtlib.middleware import WshtlibMiddleware
app = FastAPI()
app.add_middleware(WshtlibMiddleware)
Initialises request context, logs method, path, status, duration_ms per request, and injects X-Trace-Id into the response.
Utilities
from wshtlib import require_env, require_https_url, require_secret
db_url = require_env("DATABASE_URL") # raises RuntimeError if missing/empty
endpoint = require_https_url(require_env("API_URL")) # raises ValueError if not https
api_key = require_secret("api/key") # raises RuntimeError if missing/empty, cached
Environment variables
| Variable | Default | Description |
|---|---|---|
LOG_LEVEL |
INFO |
Logger level |
METRICS_NAMESPACE |
Wholeshoot |
CloudWatch namespace |
ENVIRONMENT |
— | Added as a metrics dimension if set |
Development
uv sync --group dev
uv run pytest
uv run mypy wshtlib
uv run ruff check wshtlib
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wshtlib-0.1.1.tar.gz.
File metadata
- Download URL: wshtlib-0.1.1.tar.gz
- Upload date:
- Size: 43.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26a8919a20c4bee83a993977bd7501a0ec4060e57a68f1f55e7dd8a64f85305d
|
|
| MD5 |
05db62978e13ccf244ee166dc7b087f8
|
|
| BLAKE2b-256 |
d14b60734c841adc26566b05a3a32cd4f5ca75bec7e9a90e376bba4b2bec1535
|
Provenance
The following attestation bundles were made for wshtlib-0.1.1.tar.gz:
Publisher:
ci.yml on pjosols/wshtlib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wshtlib-0.1.1.tar.gz -
Subject digest:
26a8919a20c4bee83a993977bd7501a0ec4060e57a68f1f55e7dd8a64f85305d - Sigstore transparency entry: 1281037189
- Sigstore integration time:
-
Permalink:
pjosols/wshtlib@4fb5518f93c5695fb3e1ed8eb66104ff80c92256 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/pjosols
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@4fb5518f93c5695fb3e1ed8eb66104ff80c92256 -
Trigger Event:
push
-
Statement type:
File details
Details for the file wshtlib-0.1.1-py3-none-any.whl.
File metadata
- Download URL: wshtlib-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a571136314163f5b93fd8de1088d2474974f15b7b0d48fc6d0704df868136fb
|
|
| MD5 |
716063b406e1107a54f486292d2f3ca9
|
|
| BLAKE2b-256 |
3012d31f3abb08147a4a489a3eac7a59ff70c0137f73a7378c041014f0d48550
|
Provenance
The following attestation bundles were made for wshtlib-0.1.1-py3-none-any.whl:
Publisher:
ci.yml on pjosols/wshtlib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wshtlib-0.1.1-py3-none-any.whl -
Subject digest:
8a571136314163f5b93fd8de1088d2474974f15b7b0d48fc6d0704df868136fb - Sigstore transparency entry: 1281037192
- Sigstore integration time:
-
Permalink:
pjosols/wshtlib@4fb5518f93c5695fb3e1ed8eb66104ff80c92256 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/pjosols
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@4fb5518f93c5695fb3e1ed8eb66104ff80c92256 -
Trigger Event:
push
-
Statement type: