-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathotel_logger_example.py
More file actions
67 lines (51 loc) · 2.57 KB
/
Copy pathotel_logger_example.py
File metadata and controls
67 lines (51 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Demonstrates OTel-enriched logging in a durable execution.
The DurableExecutionOtelPlugin wraps the execution logger (enrich_logger=True
by default) so every log line emitted through context.logger / step_context.logger
is automatically enriched with the active OpenTelemetry trace context
(otel.trace_id, otel.span_id, otel.trace_sampled). This lets logs correlate to
the spans the plugin emits without any user code changes.
Logs emitted:
- at the top level correlate to the invocation span
- inside a step correlate to that step's span
- inside a child context correlate to the child-context span
"""
from typing import Any
from aws_durable_execution_sdk_python_otel import DurableExecutionOtelPlugin
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from aws_durable_execution_sdk_python import StepContext
from aws_durable_execution_sdk_python.context import (
DurableContext,
durable_step,
durable_with_child_context,
)
from aws_durable_execution_sdk_python.execution import durable_execution
tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)
# enrich_logger defaults to True, so the execution logger is wrapped with OTel
# trace context injection (otel.trace_id, otel.span_id, otel.trace_sampled).
otel = DurableExecutionOtelPlugin(tracer_provider)
@durable_step
def greet(step_context: StepContext, name: str) -> str:
# Logged inside a step: enriched with this step's span_id.
# Note: avoid reserved LogRecord keys (e.g. "name") in extra.
step_context.logger.info("Greeting inside step", extra={"greeting_name": name})
return f"hello {name}"
@durable_with_child_context
def greet_in_child(child_context: DurableContext, name: str) -> str:
# Logged inside a child context: enriched with the child-context span_id.
child_context.logger.info("Entering child context")
result: str = child_context.step(greet(name), name="child-greet")
child_context.logger.info("Leaving child context", extra={"result": result})
return result
@durable_execution(plugins=[otel])
def handler(_event: Any, context: DurableContext) -> str:
# Logged at the top level: enriched with the invocation span_id.
context.logger.info("Workflow started")
top: str = context.step(greet("world"), name="top-greet")
nested: str = context.run_in_child_context(
greet_in_child("nested"), name="child-context"
)
context.logger.info("Workflow completed", extra={"top": top, "nested": nested})
return f"{top} | {nested}"