-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtammy-test-logging-factory.py
50 lines (40 loc) · 1.77 KB
/
tammy-test-logging-factory.py
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
import json
import logging
from opentelemetry.trace.propagation import get_current_span
def lambda_handler(event, context):
logger = logging.getLogger()
# Removes presumably the only handler (LambdaLoggerHandler)
# that was set by AWS Lambda Python runtime
all_handlers = logger.handlers
logger.removeHandler(all_handlers[0])
# Get current span from Otel SDK to use in formatting
current_span = get_current_span()
ctx = current_span.get_span_context()
# Set up new log record factory to accept RequestId, span
# context for formatting
old_factory = logging.getLogRecordFactory()
def record_factory(*args, **kwargs):
record = old_factory(*args, **kwargs)
record.aws_request_id = str(context.aws_request_id)
record.otelSpanID = format(ctx.span_id, "016x")
record.otelTraceID = format(ctx.trace_id, "032x")
record.otelTraceSampled = ctx.trace_flags.sampled
return record
logging.setLogRecordFactory(record_factory)
# Set up new handler with format that has RequestId, span context
test_formatter = logging.Formatter(
"%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] " \
"[RequestId %(aws_request_id)s] " \
"[trace_id=%(otelTraceID)s span_id=%(otelSpanID)s " \
"trace_flags=%(otelTraceSampled)02d resource.service.name=%(otelServiceName)s] - %(message)s"
)
test_handler = logging.StreamHandler()
test_handler.setFormatter(test_formatter)
logger.addHandler(test_handler)
# Test logs and response
logger.warning("Here is a logger warning message")
logger.warning("Here is context.aws_request_id: %s", context.aws_request_id)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}