|
5 | 5 | Instrumentation for AWS Lambda functions |
6 | 6 | """ |
7 | 7 |
|
8 | | -import sys |
9 | | -import traceback |
10 | 8 | from typing import TYPE_CHECKING, Any, Callable, Dict, Tuple |
11 | 9 |
|
12 | | -import wrapt |
13 | | -from opentelemetry.semconv.trace import SpanAttributes |
14 | | - |
15 | | -from instana import get_aws_lambda_handler |
16 | | -from instana.instrumentation.aws.triggers import enrich_lambda_span, get_context |
17 | | -from instana.log import logger |
18 | | -from instana.singletons import env_is_aws_lambda, get_agent, get_tracer |
19 | | -from instana.util.ids import define_server_timing |
20 | | - |
21 | 10 | if TYPE_CHECKING: |
22 | 11 | from instana.agent.aws_lambda import AWSLambdaAgent |
23 | 12 |
|
| 13 | +try: |
| 14 | + import sys |
| 15 | + import traceback |
24 | 16 |
|
25 | | -def lambda_handler_with_instana( |
26 | | - wrapped: Callable[..., object], |
27 | | - instance: object, |
28 | | - args: Tuple[object, ...], |
29 | | - kwargs: Dict[str, Any], |
30 | | -) -> object: |
31 | | - event = args[0] |
32 | | - agent: "AWSLambdaAgent" = get_agent() |
33 | | - tracer = get_tracer() |
| 17 | + import wrapt |
| 18 | + from opentelemetry.semconv.trace import SpanAttributes |
34 | 19 |
|
35 | | - agent.collector.collect_snapshot(*args) |
36 | | - incoming_ctx = get_context(tracer, event) |
| 20 | + from instana import get_aws_lambda_handler |
| 21 | + from instana.instrumentation.aws.triggers import enrich_lambda_span, get_context |
| 22 | + from instana.log import logger |
| 23 | + from instana.singletons import env_is_aws_lambda, get_agent, get_tracer |
| 24 | + from instana.util.ids import define_server_timing |
37 | 25 |
|
38 | | - result = None |
39 | | - with tracer.start_as_current_span( |
40 | | - "aws.lambda.entry", span_context=incoming_ctx |
41 | | - ) as span: |
42 | | - enrich_lambda_span(agent, span, *args) |
43 | | - try: |
44 | | - result = wrapped(*args, **kwargs) |
| 26 | + def lambda_handler_with_instana( |
| 27 | + wrapped: Callable[..., object], |
| 28 | + instance: object, |
| 29 | + args: Tuple[object, ...], |
| 30 | + kwargs: Dict[str, Any], |
| 31 | + ) -> object: |
| 32 | + event = args[0] |
| 33 | + agent: "AWSLambdaAgent" = get_agent() |
| 34 | + tracer = get_tracer() |
45 | 35 |
|
46 | | - if isinstance(result, dict): |
47 | | - server_timing_value = define_server_timing(span.context.trace_id) |
48 | | - if "headers" in result: |
49 | | - result["headers"]["Server-Timing"] = server_timing_value |
50 | | - elif "multiValueHeaders" in result: |
51 | | - result["multiValueHeaders"]["Server-Timing"] = [server_timing_value] |
52 | | - if "statusCode" in result and result.get("statusCode"): |
53 | | - status_code = int(result["statusCode"]) |
54 | | - span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code) |
55 | | - if 500 <= status_code: |
56 | | - span.record_exception(f"HTTP status {status_code}") |
57 | | - except Exception as exc: |
58 | | - logger.debug(f"AWS Lambda lambda_handler_with_instana error: {exc}") |
59 | | - if span: |
60 | | - exc = traceback.format_exc() |
61 | | - span.record_exception(exc) |
62 | | - raise |
63 | | - finally: |
64 | | - agent.collector.shutdown() |
| 36 | + agent.collector.collect_snapshot(*args) |
| 37 | + incoming_ctx = get_context(tracer, event) |
| 38 | + |
| 39 | + result = None |
| 40 | + with tracer.start_as_current_span( |
| 41 | + "aws.lambda.entry", span_context=incoming_ctx |
| 42 | + ) as span: |
| 43 | + enrich_lambda_span(agent, span, *args) |
| 44 | + try: |
| 45 | + result = wrapped(*args, **kwargs) |
65 | 46 |
|
66 | | - if agent.collector.started: |
67 | | - agent.collector.shutdown() |
68 | | - |
69 | | - return result |
| 47 | + if isinstance(result, dict): |
| 48 | + server_timing_value = define_server_timing(span.context.trace_id) |
| 49 | + if "headers" in result: |
| 50 | + result["headers"]["Server-Timing"] = server_timing_value |
| 51 | + elif "multiValueHeaders" in result: |
| 52 | + result["multiValueHeaders"]["Server-Timing"] = [ |
| 53 | + server_timing_value |
| 54 | + ] |
| 55 | + if "statusCode" in result and result.get("statusCode"): |
| 56 | + status_code = int(result["statusCode"]) |
| 57 | + span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code) |
| 58 | + if 500 <= status_code: |
| 59 | + span.record_exception(f"HTTP status {status_code}") |
| 60 | + except Exception as exc: |
| 61 | + logger.debug(f"AWS Lambda lambda_handler_with_instana error: {exc}") |
| 62 | + if span: |
| 63 | + exc = traceback.format_exc() |
| 64 | + span.record_exception(exc) |
| 65 | + raise |
| 66 | + finally: |
| 67 | + agent.collector.shutdown() |
70 | 68 |
|
| 69 | + if agent.collector.started: |
| 70 | + agent.collector.shutdown() |
71 | 71 |
|
72 | | -if env_is_aws_lambda: |
73 | | - handler_module, handler_function = get_aws_lambda_handler() |
| 72 | + return result |
74 | 73 |
|
75 | | - if handler_module and handler_function: |
76 | | - try: |
77 | | - logger.debug( |
78 | | - f"Instrumenting AWS Lambda handler ({handler_module}.{handler_function})" |
79 | | - ) |
80 | | - sys.path.insert(0, "/var/runtime") |
81 | | - sys.path.insert(0, "/var/task") |
82 | | - wrapt.wrap_function_wrapper( |
83 | | - handler_module, handler_function, lambda_handler_with_instana |
84 | | - ) |
85 | | - except (ModuleNotFoundError, ImportError) as exc: |
86 | | - logger.debug(f"AWS Lambda error: {exc}") |
| 74 | + if env_is_aws_lambda: |
| 75 | + handler_module, handler_function = get_aws_lambda_handler() |
| 76 | + |
| 77 | + if handler_module and handler_function: |
| 78 | + try: |
| 79 | + logger.debug( |
| 80 | + f"Instrumenting AWS Lambda handler ({handler_module}.{handler_function})" |
| 81 | + ) |
| 82 | + sys.path.insert(0, "/var/runtime") |
| 83 | + sys.path.insert(0, "/var/task") |
| 84 | + wrapt.wrap_function_wrapper( |
| 85 | + handler_module, handler_function, lambda_handler_with_instana |
| 86 | + ) |
| 87 | + except (ModuleNotFoundError, ImportError) as exc: |
| 88 | + logger.debug(f"AWS Lambda error: {exc}") |
| 89 | + logger.warning( |
| 90 | + "Instana: Couldn't instrument AWS Lambda handler. Not monitoring." |
| 91 | + ) |
| 92 | + else: |
87 | 93 | logger.warning( |
88 | | - "Instana: Couldn't instrument AWS Lambda handler. Not monitoring." |
| 94 | + "Instana: Couldn't determine AWS Lambda Handler. Not monitoring." |
89 | 95 | ) |
90 | | - else: |
91 | | - logger.warning( |
92 | | - "Instana: Couldn't determine AWS Lambda Handler. Not monitoring." |
93 | | - ) |
| 96 | +except ImportError: |
| 97 | + pass |
0 commit comments