Exemplo n.º 1
0
def test_timeout_mechanism_too_short_time(monkeypatch, context):
    monkeypatch.setattr(Configuration, "timeout_timer", True)
    monkeypatch.setattr(context, "get_remaining_time_in_millis", lambda: 1000)
    SpansContainer.create_span()
    SpansContainer.get_span().start(context=context)

    assert not TimeoutMechanism.is_activated()
Exemplo n.º 2
0
 def lambda_wrapper(*args, **kwargs):
     if _is_context_already_wrapped(*args):
         return func(*args, **kwargs)
     _add_wrap_flag_to_context(*args)
     executed = False
     ret_val = None
     try:
         SpansContainer.create_span(*args, is_new_invocation=True)
         with lumigo_safe_execute("auto tag"):
             AutoTagEvent.auto_tag_event(args[0])
         SpansContainer.get_span().start(*args)
         try:
             executed = True
             ret_val = func(*args, **kwargs)
         except Exception as e:
             with lumigo_safe_execute("Customer's exception"):
                 SpansContainer.get_span().add_exception_event(
                     e, inspect.trace())
             raise
         finally:
             with lumigo_safe_execute("end"):
                 SpansContainer.get_span().end(ret_val, *args)
         return ret_val
     except Exception:
         # The case where our wrapping raised an exception
         if not executed:
             TimeoutMechanism.stop()
             get_logger().exception("exception in the wrapper",
                                    exc_info=True)
             return func(*args, **kwargs)
         else:
             raise
Exemplo n.º 3
0
def test_timeout_mechanism_timeout_occurred_send_new_spans(monkeypatch, context, dummy_span):
    SpansContainer.create_span()
    SpansContainer.get_span().start(context=context)
    SpansContainer.get_span().add_span(dummy_span)
    SpansContainer.get_span().handle_timeout()

    SpansContainer.get_span().add_span(dummy_span)
    assert SpansContainer.get_span().span_ids_to_send
Exemplo n.º 4
0
def test_malformed_txid(monkeypatch, context):
    monkeypatch.setenv(
        "_X_AMZN_TRACE_ID", f"Root=1-5fd891b8-{MALFORMED_TXID};Parent=0a885f800de045d4;Sampled=0"
    )
    SpansContainer.create_span({}, context)

    assert SpansContainer.get_span().transaction_id != MALFORMED_TXID
    assert SpansContainer.get_span().function_span["isMalformedTransactionId"]
    result = SpansContainer.get_span().get_patched_root()
    output_trace_id = result.split(";")[0].split("=")[1].split("-")[2]
    assert output_trace_id == SpansContainer.get_span().transaction_id
Exemplo n.º 5
0
def only_if_error(dummy_span, monkeypatch, tmpdir):
    extension_dir = tmpdir.mkdir("tmp")
    monkeypatch.setenv("LUMIGO_EXTENSION_SPANS_DIR_KEY", extension_dir)
    monkeypatch.setattr(uuid, "uuid4", lambda *args, **kwargs: "span_name")
    Configuration.send_only_if_error = True
    SpansContainer.create_span()
    SpansContainer.get_span().start()

    SpansContainer.get_span().add_span(dummy_span)
    reported_ttl = SpansContainer.get_span().end({})
    stop_path_path = f"{lumigo_utils.get_extension_dir()}/span_name_stop"
    return reported_ttl, stop_path_path
Exemplo n.º 6
0
def test_get_patched_root(monkeypatch, context):
    monkeypatch.setenv(
        "_X_AMZN_TRACE_ID",
        "Root=1-5fd891b8-252f5de90a085ae04267aa4e;Parent=0a885f800de045d4;Sampled=0",
    )
    SpansContainer.create_span({}, context)
    result = SpansContainer.get_span().get_patched_root()
    root = result.split(";")[0].split("=")[1]
    one, current_time, txid = root.split("-")

    result_time = datetime.fromtimestamp(int(current_time, 16))
    assert one == "1"
    assert (result_time - datetime.now()).total_seconds() < 5
    assert txid == "252f5de90a085ae04267aa4e"
Exemplo n.º 7
0
def test_spans_container_end_function_with_error_double_size_limit(monkeypatch, dummy_span):
    long_string = "v" * int(Configuration.get_max_entry_size() * 1.5)
    monkeypatch.setenv("LONG_STRING", long_string)
    event = {"k": long_string}
    SpansContainer.create_span(event)
    SpansContainer.get_span().start()
    start_span = copy.deepcopy(SpansContainer.get_span().function_span)
    SpansContainer.get_span().add_exception_event(Exception("Some Error"), inspect.trace())

    SpansContainer.get_span().end(event=event)

    end_span = SpansContainer.get_span().function_span
    assert len(end_span["event"]) > len(start_span["event"])
    assert end_span["event"] == json.dumps(event)
Exemplo n.º 8
0
def test_spans_container_timeout_mechanism_send_only_on_errors_mode(
    monkeypatch, context, reporter_mock, dummy_span
):
    monkeypatch.setattr(Configuration, "send_only_if_error", True)

    SpansContainer.create_span()
    SpansContainer.get_span().start()
    SpansContainer.get_span().add_span(dummy_span)

    SpansContainer.get_span().handle_timeout()

    messages = reporter_mock.call_args.kwargs["msgs"]
    assert len(messages) == 2
    assert [m for m in messages if m["type"] == FUNCTION_TYPE and m["id"].endswith("_started")]
    assert [m for m in messages if m["type"] == HTTP_TYPE]
def test_aggregating_response_body():
    """
    This test is here to validate that we're not leaking memory on aggregating response body.
    Unfortunately python doesn't give us better tools, so we must check the problematic member itself.
    """
    SpansContainer.create_span()
    add_request_event(
        HttpRequest(host="dummy",
                    method="dummy",
                    uri="dummy",
                    headers={"dummy": "dummy"},
                    body="dummy"))

    big_response_chunk = b"leak" * DEFAULT_MAX_ENTRY_SIZE
    for _ in range(10):
        update_event_response(host=None,
                              status_code=200,
                              headers=None,
                              body=big_response_chunk)
    assert len(HttpState.previous_response_body) <= len(big_response_chunk)
Exemplo n.º 10
0
def test_aggregating_response_body():
    """
    This test is here to validate that we're not leaking memory on aggregating response body.
    Unfortunately python doesn't give us better tools, so we must check the problematic member itself.
    """
    SpansContainer.create_span()
    span = add_request_event(
        None,
        HttpRequest(
            host="dummy", method="dummy", uri="dummy", headers={"dummy": "dummy"}, body="dummy"
        ),
    )

    big_response_chunk = b'leak"' * DEFAULT_MAX_ENTRY_SIZE
    for _ in range(10):
        update_event_response(
            span["id"], host=None, status_code=200, headers=None, body=big_response_chunk
        )
    body = list(SpansContainer.get_span().spans.values())[0]["info"]["httpInfo"]["response"]["body"]
    assert len(body) <= len(big_response_chunk)
    assert body[: -len(TRUNCATE_SUFFIX)] in json.dumps(big_response_chunk.decode())
Exemplo n.º 11
0
def test_spans_container_end_function_got_none_return_value(monkeypatch):
    SpansContainer.create_span()
    SpansContainer.get_span().start()
    SpansContainer.get_span().end(None)
    assert SpansContainer.get_span().function_span["return_value"] is None