Esempio n. 1
0
    def parse_request(self, parse_params: HttpRequest) -> dict:
        if Configuration.verbose and parse_params and not should_scrub_domain(parse_params.host):
            additional_info = {
                "headers": prepare_large_data(
                    omit_keys(dict(parse_params.headers.items() if parse_params.headers else {}))
                ),
                "body": prepare_large_data(omit_keys(parse_params.body)),
                "method": parse_params.method,
                "uri": parse_params.uri,
            }
        else:
            additional_info = {
                "method": parse_params.method if parse_params else "",
                "body": "The data is not available",
            }

        return {
            "id": str(uuid.uuid4()),
            "type": HTTP_TYPE,
            "info": {
                "httpInfo": {
                    "host": parse_params.host if parse_params else "",
                    "request": additional_info,
                }
            },
            "started": int(time.time() * 1000),
        }
Esempio n. 2
0
    def end(self, ret_val=None) -> Optional[int]:
        TimeoutMechanism.stop()
        reported_rtt = None
        self.previous_request = None, b""
        self.function_span.update({"ended": int(time.time() * 1000)})
        if Configuration.is_step_function:
            self.add_step_end_event(ret_val)
        if Configuration.verbose:
            self.function_span.update(
                {"return_value": prepare_large_data(omit_keys(ret_val))})
        spans_contain_errors: bool = any(
            _is_span_has_error(s)
            for s in self.http_spans + [self.function_span])

        if (not Configuration.send_only_if_error) or spans_contain_errors:
            to_send = [self.function_span] + [
                s for s in self.http_spans
                if s["id"] in self.http_span_ids_to_send
            ]
            reported_rtt = utils.report_json(region=self.region, msgs=to_send)
        else:
            get_logger().debug(
                "No Spans were sent, `Configuration.send_only_if_error` is on and no span has error"
            )
        return reported_rtt
Esempio n. 3
0
    def parse_response(
        self, url: str, status_code: int, headers: Optional[http.client.HTTPMessage], body: bytes
    ) -> dict:
        if Configuration.verbose and not should_scrub_domain(url):
            additional_info = {
                "headers": prepare_large_data(omit_keys(dict(headers.items() if headers else {}))),
                "body": prepare_large_data(omit_keys(body)),
                "statusCode": status_code,
            }
        else:
            additional_info = {"statusCode": status_code, "body": "The data is not available"}

        return {
            "type": HTTP_TYPE,
            "info": {"httpInfo": {"host": url, "response": additional_info}},
            "ended": int(time.time() * 1000),
        }
Esempio n. 4
0
    def create_span(cls,
                    event=None,
                    context=None,
                    force=False) -> "SpansContainer":
        """
        This function creates a span out of a given AWS context.
        The force flag delete any existing span-container (to handle with warm execution of lambdas).
        Note that if lambda will be executed directly (regular pythonic function call and not invoked),
            it will override the container.
        """
        if cls._span and not force:
            return cls._span
        additional_info = {}
        if Configuration.verbose:
            additional_info.update({
                "event":
                prepare_large_data(EventParser.parse_event(omit_keys(event))),
                "envs":
                prepare_large_data(omit_keys(dict(os.environ))),
            })

        trace_root, transaction_id, suffix = parse_trace_id(
            os.environ.get("_X_AMZN_TRACE_ID", ""))
        remaining_time = getattr(context, "get_remaining_time_in_millis",
                                 lambda: MAX_LAMBDA_TIME)()
        cls._span = SpansContainer(
            started=int(time.time() * 1000),
            name=os.environ.get("AWS_LAMBDA_FUNCTION_NAME"),
            runtime=os.environ.get("AWS_EXECUTION_ENV"),
            region=os.environ.get("AWS_REGION"),
            memory_allocated=os.environ.get("AWS_LAMBDA_FUNCTION_MEMORY_SIZE"),
            log_stream_name=os.environ.get("AWS_LAMBDA_LOG_STREAM_NAME"),
            log_group_name=os.environ.get("AWS_LAMBDA_LOG_GROUP_NAME"),
            trace_root=trace_root,
            transaction_id=transaction_id,
            trace_id_suffix=suffix,
            request_id=getattr(context, "aws_request_id", ""),
            account=safe_split_get(
                getattr(context, "invoked_function_arn", ""), ":", 4, ""),
            trigger_by=parse_triggered_by(event),
            max_finish_time=int(time.time() * 1000) + remaining_time,
            **additional_info,
        )
        return cls._span
Esempio n. 5
0
def test_prepare_large_data(value, output):
    assert prepare_large_data(value, 20) == output