コード例 #1
0
def test_logging_filter_no_span(elasticapm_client):
    transaction = elasticapm_client.begin_transaction("test")
    f = LoggingFilter()
    record = logging.LogRecord(__name__, logging.DEBUG, __file__, 252, "dummy_msg", [], None)
    f.filter(record)
    assert record.elasticapm_transaction_id == transaction.id
    assert record.elasticapm_trace_id == transaction.trace_parent.trace_id
    assert record.elasticapm_span_id is None
    assert record.elasticapm_labels
コード例 #2
0
def test_elastic_apm_stdlib_with_filter_log_correlation_ecs_fields():
    apm = elasticapm.Client({
        "SERVICE_NAME": "apm-service",
        "DISABLE_SEND": True
    })
    stream = StringIO()
    logger = logging.getLogger("apm-logger")
    handler = logging.StreamHandler(stream)
    handler.setFormatter(
        ecs_logging.StdlibFormatter(
            exclude_fields=["@timestamp", "process", "log.origin.file.line"]))
    handler.addFilter(LoggingFilter())
    logger.addHandler(handler)
    logger.setLevel(logging.DEBUG)

    apm.begin_transaction("test-transaction")
    try:
        with elasticapm.capture_span("test-span"):
            span_id = elasticapm.get_span_id()
            trace_id = elasticapm.get_trace_id()
            transaction_id = elasticapm.get_transaction_id()

            logger.info("test message")
    finally:
        apm.end_transaction("test-transaction")

    ecs = json.loads(stream.getvalue().rstrip())
    assert ecs == {
        "ecs": {
            "version": "1.5.0"
        },
        "log": {
            "level": "info",
            "logger": "apm-logger",
            "origin": {
                "file": {
                    "name": "test_apm.py"
                },
                "function":
                "test_elastic_apm_stdlib_with_filter_log_correlation_ecs_fields",
            },
            "original": "test message",
        },
        "message": "test message",
        "span": {
            "id": span_id
        },
        "trace": {
            "id": trace_id
        },
        "transaction": {
            "id": transaction_id
        },
    }
コード例 #3
0
def test_logging_filter_span(elasticapm_client):
    transaction = elasticapm_client.begin_transaction("test")
    with capture_span("test") as span:
        f = LoggingFilter()
        record = logging.LogRecord(__name__, logging.DEBUG, __file__, 252, "dummy_msg", [], None)
        f.filter(record)
        assert record.elasticapm_transaction_id == transaction.id
        assert record.elasticapm_service_name == transaction.tracer.config.service_name
        assert record.elasticapm_trace_id == transaction.trace_parent.trace_id
        assert record.elasticapm_span_id == span.id
        assert record.elasticapm_labels

    # Capture too many spans so we start dropping
    for i in range(10):
        with capture_span("drop"):
            pass

    # Test logging with DroppedSpan
    with capture_span("drop") as span:
        record = logging.LogRecord(__name__, logging.DEBUG, __file__, 252, "dummy_msg2", [], None)
        f.filter(record)
        assert record.elasticapm_transaction_id == transaction.id
        assert record.elasticapm_service_name == transaction.tracer.config.service_name
        assert record.elasticapm_trace_id == transaction.trace_parent.trace_id
        assert record.elasticapm_span_id is None
        assert record.elasticapm_labels