def _functionPatch(self, original_func, instance, args, kwargs):
        lambda_context = args[1]
        ctx_aws_request_id = lambda_context.aws_request_id
        ctx_invoked_function_arn = lambda_context.invoked_function_arn
        orig_handler = os.environ.get("ORIG_HANDLER",
                                      os.environ.get("_HANDLER"))

        # TODO: enable propagate from AWS by env variable
        xray_trace_id = os.environ.get("_X_AMZN_TRACE_ID", "")

        lambda_name = os.environ.get("AWS_LAMBDA_FUNCTION_NAME")
        function_version = os.environ.get("AWS_LAMBDA_FUNCTION_VERSION")

        propagator = AwsXRayFormat()
        parent_context = propagator.extract({"X-Amzn-Trace-Id": xray_trace_id})

        with self._tracer.start_as_current_span(name=orig_handler,
                                                context=parent_context,
                                                kind=SpanKind.SERVER) as span:
            # Refer: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/faas.md#example
            span.set_attribute("faas.execution", ctx_aws_request_id)
            span.set_attribute("faas.id", ctx_invoked_function_arn)

            # TODO: fix in Collector because they belong resource attrubutes
            span.set_attribute("faas.name", lambda_name)
            span.set_attribute("faas.version", function_version)

            result = original_func(*args, **kwargs)

        # force_flush before function quit in case of Lambda freeze.
        self._tracer_provider.force_flush()

        return result
Ejemplo n.º 2
0
    def _instrumented_lambda_handler_call(call_wrapped, instance, args, kwargs):
        orig_handler_name = ".".join(
            [wrapped_module_name, wrapped_function_name]
        )

        # TODO: enable propagate from AWS by env variable
        xray_trace_id = os.environ.get("_X_AMZN_TRACE_ID", "")
        propagator = AwsXRayFormat()
        parent_context = propagator.extract({"X-Amzn-Trace-Id": xray_trace_id})

        with tracer.start_as_current_span(
            name=orig_handler_name, context=parent_context, kind=SpanKind.SERVER
        ) as span:
            if span.is_recording():
                lambda_context = args[1]
                # Refer: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/faas.md#example
                span.set_attribute(
                    SpanAttributes.FAAS_EXECUTION, lambda_context.aws_request_id
                )
                span.set_attribute(
                    "faas.id", lambda_context.invoked_function_arn
                )

                # TODO: fix in Collector because they belong resource attrubutes
                span.set_attribute(
                    "faas.name", os.environ.get("AWS_LAMBDA_FUNCTION_NAME")
                )
                span.set_attribute(
                    "faas.version",
                    os.environ.get("AWS_LAMBDA_FUNCTION_VERSION"),
                )

            result = call_wrapped(*args, **kwargs)

        # force_flush before function quit in case of Lambda freeze.
        tracer_provider = get_tracer_provider()
        tracer_provider.force_flush()

        return result
Ejemplo n.º 3
0
    def _function_patch(self, original_func, _, args, kwargs):
        lambda_context = args[1]
        ctx_aws_request_id = lambda_context.aws_request_id
        orig_handler = os.environ.get("ORIG_HANDLER",
                                      os.environ.get("_HANDLER"))
        xray_trace_id = os.environ.get("_X_AMZN_TRACE_ID", "")

        propagator = AwsXRayFormat()
        parent_context = propagator.extract(DictGetter(),
                                            {TRACE_HEADER_KEY: xray_trace_id})

        with self._tracer.start_as_current_span(orig_handler,
                                                context=parent_context,
                                                kind=SpanKind.CLIENT) as span:
            # Refer: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/faas.md#example
            span.set_attribute("faas.execution", ctx_aws_request_id)

            result = original_func(*args, **kwargs)

        # force_flush before function quit in case of Lambda freeze.
        self._tracer_provider.force_flush()

        return result