|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from typing_extensions import override |
| 6 | + |
| 7 | +from opentelemetry.context import Context |
| 8 | +from opentelemetry.sdk.trace import ReadableSpan, Span, SpanProcessor |
| 9 | +from opentelemetry.trace import SpanKind |
| 10 | +from amazon.opentelemetry.distro._aws_attribute_keys import AWS_TRACE_LAMBDA_FLAG_MULTIPLE_SERVER |
| 11 | + |
| 12 | +class AwsLambdaSpanProcessor(SpanProcessor): |
| 13 | + def __init__(self, instrumentation_names=None): |
| 14 | + """ |
| 15 | + :param instrumentation_names: A set or list of instrumentation scope names |
| 16 | + for which we want to mark as SERVER spans if they are INTERNAL. |
| 17 | + """ |
| 18 | + self.instrumentation_names = set(instrumentation_names or ["opentelemetry.instrumentation.flask"]) |
| 19 | + self.parent_lambda_span = None |
| 20 | + |
| 21 | + @override |
| 22 | + def on_start(self, span: Span, parent_context: Optional[Context] = None) -> None: |
| 23 | + scope = getattr(span, "instrumentation_scope", None) |
| 24 | + if span.kind == SpanKind.SERVER and scope.name == "opentelemetry.instrumentation.aws_lambda": |
| 25 | + self.parent_lambda_span = span |
| 26 | + |
| 27 | + if span.kind == SpanKind.INTERNAL and scope.name in self.instrumentation_names: |
| 28 | + span._kind = SpanKind.SERVER |
| 29 | + self.parent_lambda_span.set_attribute(AWS_TRACE_LAMBDA_FLAG_MULTIPLE_SERVER, True) |
| 30 | + return |
| 31 | + |
| 32 | + @override |
| 33 | + def on_end(self, span: ReadableSpan) -> None: |
| 34 | + return |
| 35 | + |
| 36 | + @override |
| 37 | + def force_flush(self, timeout_millis: int = 30000) -> bool: |
| 38 | + """Flush any buffered data.""" |
| 39 | + return True |
| 40 | + |
| 41 | + @override |
| 42 | + def shutdown(self) -> None: |
| 43 | + """Clean up.""" |
| 44 | + self.force_flush() |
0 commit comments