Last active
April 12, 2021 22:36
-
-
Save michaelbrewer/6a8529610de3ca543938ec09d3c47373 to your computer and use it in GitHub Desktop.
02 - Tracing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import time | |
from aws_lambda_powertools import Logger, Tracer | |
from aws_lambda_powertools.logging import correlation_paths | |
logger = Logger() | |
tracer = Tracer() | |
@tracer.capture_method | |
def create_payment(payment): | |
order_key = payment["order_key"] | |
logger.info(f"Order key: %s", order_key) | |
# Simulate failure | |
if "FAILURE" in order_key: | |
tracer.put_annotation("PAYMENT_STATUS", "ERROR") | |
raise RuntimeError("Failed to make payment") | |
# Simulate a slow transaction | |
time.sleep(2) | |
tracer.put_annotation(key="ORDER_KEY", value=order_key) | |
tracer.put_annotation(key="PAYMENT_STATUS", value="SUCCESS") | |
return {"order_id": "1111111", "status": "SUCCESS"} | |
@tracer.capture_lambda_handler | |
@logger.inject_lambda_context(log_event=True, correlation_id_path=correlation_paths.API_GATEWAY_REST) | |
def lambda_handler(event, context): | |
payment = create_payment(json.loads(event["body"])) | |
return { | |
"statusCode": 200, | |
"headers": {"Content-Type": "application/json"}, | |
"body": json.dumps(payment), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment