예제 #1
0
def test_elasticapm_structlog_log_correlation_ecs_fields(spec_validator):
    apm = elasticapm.Client({"SERVICE_NAME": "apm-service", "DISABLE_SEND": True})
    stream = StringIO()
    logger = structlog.PrintLogger(stream)
    logger = structlog.wrap_logger(
        logger, processors=[structlog_processor, ecs_logging.StructlogFormatter()]
    )
    log = logger.new()

    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()

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

    ecs = json.loads(spec_validator(stream.getvalue().rstrip()))
    ecs.pop("@timestamp")
    assert ecs == {
        "ecs": {"version": "1.6.0"},
        "log.level": "info",
        "message": "test message",
        "span": {"id": span_id},
        "trace": {"id": trace_id},
        "transaction": {"id": transaction_id},
        "service": {"name": "apm-service"},
    }
예제 #2
0
def get_extra_logging_payload():
    payload = {
        "transaction.id": elasticapm.get_transaction_id(),
        "span.id": elasticapm.get_span_id(),
        "trace.id": elasticapm.get_trace_id(),
        "event.dataset": event_dataset
    }
    return payload
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
        },
    }
예제 #4
0
def checkout(request: Purchase = Body(...)):
    """Signup user.
    """
    result = {
        'id': time.time(),
        'username': request.username,
        'email': request.email,
        'cost_spend': request.cost_spend,
        'item_count': request.item_count,
        'billing_amount': request.cost_spend * request.item_count,
    }

    #Different logging levels
    logger.debug('This is a debug message')
    logger.info('This is an info message')
    logger.warning('This is a warning message')
    logger.error('This is an error message')
    logger.critical('This is a critical message')
    logger.exception("This is an exception")

    # add extra field to logstash message
    extra = {
        'test_string': 'python version: ' + repr(sys.version_info),
        'test_boolean': True,
        'test_dict': {
            'a': 1,
            'b': 'c'
        },
        'test_float': 1.23,
        'test_integer': 123,
        'test_list': [1, 2, '3'],
    }
    logger.info('test extra fields', extra=extra)

    #Capture an arbitrary exception by calling capture_exception:
    try:
        1 / 0
    except ZeroDivisionError:
        apm_client.capture_exception()

    # Log a generic message with capture_message:
    apm_client.capture_message('hello, world!')

    # Alternatively, a parameterized message as a dictionary.
    apm_client.capture_message(
        param_message={
            'message': 'Billing process for %s succeeded. Amount: %s',
            'params': (result['id'], result['billing_amount']),
        })

    # Get the id of the current transaction.
    transaction_id = elasticapm.get_transaction_id()
    logger.info('Current transaction_id: ' + str(transaction_id))

    # Get the trace_id of the current transaction’s trace.
    trace_id = elasticapm.get_trace_id()
    logger.info('Current trace_id: ' + str(trace_id))

    # Get the id of the current span.
    span_id = elasticapm.get_span_id()
    logger.info('Current span_id: ' + str(span_id))

    return result
예제 #5
0
def test_get_transaction_id(elasticapm_client):
    transaction = elasticapm_client.begin_transaction("test")
    assert transaction.id == elasticapm.get_transaction_id()