| Annotations | Metadata | |
|---|---|---|
| Indexed | ✅ Searchable | ❌ Not searchable |
| Use case | Filter/search traces | Detailed debug data |
| Types | String, Number, Boolean | Any (objects, arrays) |
Scenario thi: Application generates thousands of trace data each hour. Cần filter results based on custom attributes trong X-Ray console.
Giải pháp:
Lưu ý: Annotations là indexed và searchable, segment fields không indexed.
from aws_xray_sdk.core import xray_recorder
subsegment = xray_recorder.begin_subsegment('process_order')
# Annotations — indexed, searchable in X-Ray console
subsegment.put_annotation('order_id', 'order-123')
subsegment.put_annotation('customer_tier', 'premium')
subsegment.put_annotation('is_retry', False)
# Metadata — not indexed, for detailed data
subsegment.put_metadata('order_details', {'items': [...], 'total': 150.00})
subsegment.put_metadata('request_headers', event.get('headers', {}))
xray_recorder.end_subsegment()
from aws_lambda_powertools import Tracer
tracer = Tracer(service="order-service")
@tracer.capture_lambda_handler
def handler(event, context):
tracer.put_annotation("order_id", event['orderId'])
tracer.put_metadata("event", event)
return process_order(event)
X-Ray Console → Traces → Filter:
annotation.order_id = "order-123"
annotation.customer_tier = "premium"
annotation.is_retry = false
X-Ray compiles and processes segment documents to:
Inferred segments represent:
Exam Tip: