Пример #1
0
def _after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
    with lumigo_safe_execute("handle sqlalchemy after execute"):
        span = SpansContainer.get_span().get_last_span()
        if not span:
            get_logger().warning("Redis span ended without a record on its start")
            return
        span.update({"ended": get_current_ms_time(), "response": ""})
Пример #2
0
 def started(self, event):
     with lumigo_safe_execute("pymongo started"):
         span_id = str(uuid.uuid4())
         LumigoMongoMonitoring.request_to_span_id[
             event.request_id] = span_id
         SpansContainer.get_span().add_span({
             "id":
             span_id,
             "type":
             self.MONGO_SPAN,
             "started":
             get_current_ms_time(),
             "databaseName":
             event.database_name,
             "commandName":
             event.command_name,
             "request":
             lumigo_dumps(event.command),
             "mongoRequestId":
             event.request_id,
             "mongoOperationId":
             event.operation_id,
             "mongoConnectionId":
             event.connection_id,
         })
Пример #3
0
    def parse_response(self, url: str, status_code: int, headers: dict,
                       body: bytes) -> dict:
        max_size = Configuration.get_max_entry_size(
            has_error=is_error_code(status_code))
        if Configuration.verbose and not should_scrub_domain(url):
            additional_info = {
                "headers": lumigo_dumps(headers, max_size),
                "body": lumigo_dumps(body, max_size) if body else "",
                "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": get_current_ms_time(),
        }
Пример #4
0
    def parse_request(self, parse_params: HttpRequest) -> dict:
        if Configuration.verbose and parse_params and not should_scrub_domain(
                parse_params.host):
            HttpState.omit_skip_path = self.get_omit_skip_path()
            additional_info = {
                "headers":
                lumigo_dumps(parse_params.headers),
                "body":
                lumigo_dumps(parse_params.body,
                             omit_skip_path=HttpState.omit_skip_path)
                if parse_params.body else "",
                "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": get_current_ms_time(),
        }
Пример #5
0
 def update_event_end_time(self) -> None:
     """
     This function assumes synchronous execution - we update the last http event.
     """
     if self.spans:
         span = self.spans[-1]
         span["ended"] = get_current_ms_time()
         self.span_ids_to_send.add(span["id"])
Пример #6
0
    def create_span(cls,
                    event=None,
                    context=None,
                    is_new_invocation=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 is_new_invocation:
            return cls._span
        # copy the event to ensure that we will not change it
        event = copy.deepcopy(event)
        additional_info = {}
        if Configuration.verbose:
            additional_info.update({
                "event": EventDumper.dump_event(event),
                "envs": _get_envs_for_span()
            })

        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=get_current_ms_time(),
            name=os.environ.get("AWS_LAMBDA_FUNCTION_NAME"),
            runtime=os.environ.get("AWS_EXECUTION_ENV"),
            region=get_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=get_current_ms_time() + remaining_time,
            is_new_invocation=is_new_invocation,
            **additional_info,
        )
        return cls._span
Пример #7
0
def command_failed(exception: Exception):
    with lumigo_safe_execute("redis command failed"):
        span = SpansContainer.get_span().get_last_span()
        if not span:
            get_logger().warning(
                "Redis span ended without a record on its start")
            return
        span.update({
            "ended": get_current_ms_time(),
            "error": exception.args[0] if exception.args else None
        })
Пример #8
0
def command_finished(ret_val: Dict):
    with lumigo_safe_execute("redis command finished"):
        span = SpansContainer.get_span().get_last_span()
        if not span:
            get_logger().warning(
                "Redis span ended without a record on its start")
            return
        span.update({
            "ended": get_current_ms_time(),
            "response": lumigo_dumps(copy.deepcopy(ret_val))
        })
Пример #9
0
def _handle_error(context):
    with lumigo_safe_execute("handle sqlalchemy error"):
        span = SpansContainer.get_span().get_span_by_id(_last_span_id)
        if not span:
            get_logger().warning(
                "Redis span ended without a record on its start")
            return
        span.update({
            "ended":
            get_current_ms_time(),
            "error":
            lumigo_dumps({
                "type": context.original_exception.__class__.__name__,
                "args": context.original_exception.args,
            }),
        })
Пример #10
0
def _before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
    with lumigo_safe_execute("handle sqlalchemy before execute"):
        SpansContainer.get_span().add_span(
            {
                "id": str(uuid.uuid4()),
                "type": SQL_SPAN,
                "started": get_current_ms_time(),
                "connectionParameters": {
                    "host": conn.engine.url.host or conn.engine.url.database,
                    "port": conn.engine.url.port,
                    "database": conn.engine.url.database,
                    "user": conn.engine.url.username,
                },
                "query": lumigo_dumps(statement),
                "values": lumigo_dumps(parameters),
            }
        )
Пример #11
0
    def end(self,
            ret_val=None,
            event: Optional[dict] = None,
            context=None) -> Optional[int]:
        TimeoutMechanism.stop()
        reported_rtt = None
        self.previous_request = None
        self.function_span.update({"ended": get_current_ms_time()})
        if Configuration.is_step_function:
            self.add_step_end_event(ret_val)
        parsed_ret_val = None
        if Configuration.verbose:
            try:
                if ret_val is not None:
                    parsed_ret_val = lumigo_dumps(ret_val,
                                                  enforce_jsonify=True,
                                                  decimal_safe=True)
            except Exception as err:
                suffix = ""
                if err.args:
                    suffix = f'Original message: "{err.args[0]}"'
                self.function_span["error"] = self._create_exception_event(
                    "ReturnValueError",
                    "The lambda will probably fail due to bad return value. " +
                    suffix,
                )
        self.function_span.update({"return_value": parsed_ret_val})
        if _is_span_has_error(self.function_span):
            self._set_error_extra_data(event)
        spans_contain_errors: bool = any(
            _is_span_has_error(s) for s in self.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.spans if s["id"] in self.span_ids_to_send
            ]
            reported_rtt = lumigo_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
Пример #12
0
def command_started(command: str, request_args: Union[Dict, List[Dict]],
                    connection_options: Optional[Dict]):
    span_id = str(uuid.uuid4())
    host = (connection_options or {}).get("host")
    port = (connection_options or {}).get("port")
    SpansContainer.get_span().add_span({
        "id":
        span_id,
        "type":
        REDIS_SPAN,
        "started":
        get_current_ms_time(),
        "requestCommand":
        command,
        "requestArgs":
        lumigo_dumps(copy.deepcopy(request_args)),
        "connectionOptions": {
            "host": host,
            "port": port
        },
    })