def test_trace_parent_wrong_version(caplog): header = "xx-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-03" with caplog.at_level("DEBUG", "zuqa.utils"): trace_parent = TraceParent.from_string(header) assert trace_parent is None assert_any_record_contains(caplog.records, "Invalid version field, value xx")
def test_trace_parent_wrong_trace_options_field(caplog): header = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-xx" with caplog.at_level("DEBUG", "elasticapm.utils"): trace_parent = TraceParent.from_string(header) assert trace_parent is None assert_any_record_contains(caplog.records, "Invalid trace-options field, value xx")
def test_send_remote_failover_sync_non_transport_exception_error( should_try, http_send, caplog): should_try.return_value = True client = Client( server_url="http://example.com", service_name="app_name", secret_token="secret", transport_class="elasticapm.transport.http.Transport", metrics_interval="0ms", metrics_sets=[], ) # test error http_send.side_effect = ValueError("oopsie") with caplog.at_level("ERROR", "elasticapm.transport"): client.capture_message("foo", handled=False) client._transport.flush() assert client._transport.state.did_fail() assert_any_record_contains(caplog.records, "oopsie", "elasticapm.transport") # test recovery http_send.side_effect = None client.capture_message("foo", handled=False) client.close() assert not client._transport.state.did_fail() client.close()
def test_trace_parent_wrong_version_255(caplog): """Version FF or 255 is explicitly forbidden""" header = "ff-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-03" with caplog.at_level("DEBUG", "elasticapm.utils"): trace_parent = TraceParent.from_string(header) assert trace_parent is None assert_any_record_contains(caplog.records, "Invalid version field, value 255")
def test_trace_parent_wrong_format(caplog): header = "00" with caplog.at_level("DEBUG", "elasticapm.utils"): trace_parent = TraceParent.from_string(header) assert trace_parent is None assert_any_record_contains(caplog.records, "Invalid traceparent header format, value 00")
def test_send_timer(sending_elasticapm_client, caplog): with caplog.at_level("DEBUG", "elasticapm.transport"): assert sending_elasticapm_client.config.api_request_time == 2000 sending_elasticapm_client.begin_transaction("test_type") sending_elasticapm_client.end_transaction("test") sending_elasticapm_client._transport.flush() assert_any_record_contains(caplog.records, "Sent request")
def test_skip_instrument_env_var(caplog): instrumentation = _TestDummyInstrumentation() with mock.patch.dict("os.environ", {"SKIP_INSTRUMENT_TEST_DUMMY_INSTRUMENT": "foo" }), caplog.at_level(logging.DEBUG, "elasticapm.instrument"): instrumentation.instrument() assert_any_record_contains(caplog.records, "Skipping", "elasticapm.instrument") assert not instrumentation.instrumented
def test_flush_time(mock_send, caplog, elasticapm_client): with caplog.at_level("DEBUG", "elasticapm.transport"): transport = Transport(client=elasticapm_client) transport.start_thread() # let first run finish time.sleep(0.2) transport.close() assert_any_record_contains(caplog.records, "due to time since last flush", "elasticapm.transport") assert mock_send.call_count == 0
def test_drop_events_in_failing_processor(elasticapm_client, caplog): with caplog.at_level(logging.WARNING, logger="elasticapm.transport"): elasticapm_client.begin_transaction("test") elasticapm_client.end_transaction("test", "FAIL") assert elasticapm_client._transport.events[TRANSACTION][0] is None assert_any_record_contains( caplog.records, "Dropped event of type transaction due to exception in processor tests.processors.tests.dummy_processor_failing", "elasticapm.transport", )
def test_interval_timer_exception(caplog): def my_func(): return 1 / 0 with caplog.at_level("ERROR", "elasticapm.utils.threading"): timer = IntervalTimer(function=my_func, interval=0.1) timer.start() time.sleep(0.25) assert timer.is_alive() timer.cancel() assert_any_record_contains(caplog.records, "Exception in interval timer function")
def test_end_nonexisting_span(caplog, elasticapm_client): with caplog.at_level(logging.DEBUG, "elasticapm.traces"): t = elasticapm_client.begin_transaction("test") # we're purposefully creating a case where we don't begin a span # and then try to end the non-existing span t.is_sampled = False with elasticapm.capture_span("test_name", "test_type"): t.is_sampled = True elasticapm_client.end_transaction("test", "") assert_any_record_contains( caplog.records, "ended non-existing span test_name of type test_type", "elasticapm.traces")
def test_drop_events_in_processor(elasticapm_client, caplog): dropping_processor = mock.MagicMock(return_value=None, event_types=[SPAN], __name__="dropper") shouldnt_be_called_processor = mock.Mock(event_types=[]) elasticapm_client._transport._processors = [dropping_processor, shouldnt_be_called_processor] with caplog.at_level(logging.DEBUG, logger="elasticapm.transport"): elasticapm_client.queue(SPAN, {"some": "data"}) assert dropping_processor.call_count == 1 assert shouldnt_be_called_processor.call_count == 0 assert elasticapm_client._transport.events[SPAN][0] is None assert_any_record_contains( caplog.records, "Dropped event of type span due to processor mock.mock.dropper", "elasticapm.transport" )
def test_fetch_server_info_flat_string(waiting_httpserver, caplog, elasticapm_client): waiting_httpserver.serve_content( code=200, content=b'"8.0.0-alpha1"', ) url = waiting_httpserver.url transport = Transport(url + "/" + constants.EVENTS_API_PATH, client=elasticapm_client) with caplog.at_level("WARNING"): transport.fetch_server_info() assert elasticapm_client.server_version is None assert_any_record_contains(caplog.records, "No version key found in server response")
def test_fetch_server_info_no_json(waiting_httpserver, caplog, elasticapm_client): waiting_httpserver.serve_content( code=200, content=b'"version": "8.0.0-alpha1"', ) url = waiting_httpserver.url transport = Transport(url + "/" + constants.EVENTS_API_PATH, client=elasticapm_client) with caplog.at_level("WARNING"): transport.fetch_server_info() assert elasticapm_client.server_version is None assert_any_record_contains( caplog.records, "JSON decoding error while fetching server information")
def test_transaction_outcome(elasticapm_client, caplog, outcome, http_status_code, log_message, result): transaction = elasticapm_client.begin_transaction("test") with caplog.at_level(logging.INFO, "elasticapm.traces"): elasticapm.set_transaction_outcome(outcome=outcome, http_status_code=http_status_code) assert transaction.outcome == result if log_message is None: assert not [ True for record in caplog.records if record.name == "elasticapm.traces" ] else: assert_any_record_contains(caplog.records, log_message, "elasticapm.traces")
def test_metric_limit(caplog, elasticapm_client): m = MetricsSet(MetricsRegistry(elasticapm_client)) with caplog.at_level(logging.WARNING, logger="elasticapm.metrics"): for i in range(2): counter = m.counter("counter", some_label=i) gauge = m.gauge("gauge", some_label=i) timer = m.timer("timer", some_label=i) if i == 0: assert isinstance(timer, Timer) assert isinstance(gauge, Gauge) assert isinstance(counter, Counter) else: assert isinstance(timer, NoopMetric) assert isinstance(gauge, NoopMetric) assert isinstance(counter, NoopMetric) assert_any_record_contains(caplog.records, "The limit of 3 metricsets has been reached", "elasticapm.metrics")
def test_send_remote_failover_sync(should_try, sending_elasticapm_client, caplog): sending_elasticapm_client.httpserver.code = 400 sending_elasticapm_client.httpserver.content = "go away" should_try.return_value = True # test error with caplog.at_level("ERROR", "elasticapm.transport"): sending_elasticapm_client.capture_message("foo", handled=False) sending_elasticapm_client._transport.flush() assert sending_elasticapm_client._transport.state.did_fail() assert_any_record_contains(caplog.records, "go away") # test recovery sending_elasticapm_client.httpserver.code = 202 sending_elasticapm_client.capture_message("bar", handled=False) sending_elasticapm_client.close() assert not sending_elasticapm_client._transport.state.did_fail()
def test_drop_events_in_processor(elasticapm_client, caplog): dropping_processor = mock.MagicMock(return_value=None, event_types=[TRANSACTION], __name__="dropper") shouldnt_be_called_processor = mock.Mock(event_types=[]) elasticapm_client._transport._processors = [ dropping_processor, shouldnt_be_called_processor ] with caplog.at_level(logging.DEBUG, logger="elasticapm.transport"): elasticapm_client.begin_transaction("test") elasticapm_client.end_transaction("test", "FAIL") assert dropping_processor.call_count == 1 assert shouldnt_be_called_processor.call_count == 0 assert elasticapm_client._transport.events[TRANSACTION][0] is None assert_any_record_contains( caplog.records, "Dropped event of type transaction due to processor mock.mock.dropper", "elasticapm.transport")
def test_api_request_time_dynamic(mock_send, caplog, elasticapm_client): elasticapm_client.config.update(version="1", api_request_time="1s") with caplog.at_level("DEBUG", "elasticapm.transport"): transport = Transport(client=elasticapm_client) transport.start_thread() # let first run finish time.sleep(0.2) transport.close() assert not caplog.records assert mock_send.call_count == 0 elasticapm_client.config.update(version="1", api_request_time="100ms") with caplog.at_level("DEBUG", "elasticapm.transport"): transport = Transport(client=elasticapm_client) transport.start_thread() # let first run finish time.sleep(0.2) transport.close() assert_any_record_contains(caplog.records, "due to time since last flush", "elasticapm.transport") assert mock_send.call_count == 0
def test_sqs_send_too_many_attributes_for_disttracing(instrument, elasticapm_client, sqs_client_and_queue, caplog): sqs, queue_url = sqs_client_and_queue attributes = {str(i): {"DataType": "String", "StringValue": str(i)} for i in range(SQS_MAX_ATTRIBUTES)} elasticapm_client.begin_transaction("test") with caplog.at_level("INFO"): sqs.send_message( QueueUrl=queue_url, MessageAttributes=attributes, MessageBody=("bar"), ) elasticapm_client.end_transaction("test", "test") messages = sqs.receive_message( QueueUrl=queue_url, AttributeNames=["All"], MessageAttributeNames=[ "All", ], ) message = messages["Messages"][0] assert "traceparent" not in message["MessageAttributes"] assert_any_record_contains(caplog.records, "Not adding disttracing headers")
def test_uninstrument_py3(caplog): original = Dummy.dummy assert not isinstance(Dummy.dummy, wrapt.BoundFunctionWrapper) instrumentation = _TestDummyInstrumentation() with caplog.at_level(logging.DEBUG, "elasticapm.instrument"): instrumentation.instrument() assert_any_record_contains( caplog.records, "Instrumented test_dummy_instrument, tests.instrumentation.base_tests.Dummy.dummy", "elasticapm.instrument", ) assert Dummy.dummy is not original assert isinstance(Dummy.dummy, wrapt.BoundFunctionWrapper) with caplog.at_level(logging.DEBUG, "elasticapm.instrument"): instrumentation.uninstrument() assert_any_record_contains( caplog.records, "Uninstrumented test_dummy_instrument, tests.instrumentation.base_tests.Dummy.dummy", "elasticapm.instrument", ) assert Dummy.dummy is original assert not isinstance(Dummy.dummy, wrapt.BoundFunctionWrapper)
def test_instrument_nonexisting_method(caplog): with caplog.at_level(logging.DEBUG, "elasticapm.instrument"): _TestInstrumentNonExistingMethod().instrument() assert_any_record_contains(caplog.records, "has no attribute", "elasticapm.instrument")
def test_label_while_no_transaction(caplog): with caplog.at_level(logging.WARNING, "elasticapm.errors"): elasticapm.label(foo="bar") assert_any_record_contains(caplog.records, "foo", "elasticapm.errors")