|
| 1 | +import json |
| 2 | +import elasticapm |
| 3 | +from elasticapm.handlers.structlog import structlog_processor |
| 4 | +import ecs_logging |
| 5 | +import logging |
| 6 | +import structlog |
| 7 | +from .compat import StringIO |
| 8 | + |
| 9 | + |
| 10 | +def test_elasticapm_structlog_log_correlation_ecs_fields(): |
| 11 | + apm = elasticapm.Client({"SERVICE_NAME": "apm-service"}) |
| 12 | + stream = StringIO() |
| 13 | + logger = structlog.PrintLogger(stream) |
| 14 | + logger = structlog.wrap_logger( |
| 15 | + logger, processors=[structlog_processor, ecs_logging.StructlogFormatter()] |
| 16 | + ) |
| 17 | + log = logger.new() |
| 18 | + |
| 19 | + apm.begin_transaction("test-transaction") |
| 20 | + try: |
| 21 | + with elasticapm.capture_span("test-span"): |
| 22 | + span_id = elasticapm.get_span_id() |
| 23 | + trace_id = elasticapm.get_trace_id() |
| 24 | + transaction_id = elasticapm.get_transaction_id() |
| 25 | + |
| 26 | + log.info("test message") |
| 27 | + finally: |
| 28 | + apm.end_transaction("test-transaction") |
| 29 | + |
| 30 | + ecs = json.loads(stream.getvalue().rstrip()) |
| 31 | + ecs.pop("@timestamp") |
| 32 | + assert ecs == { |
| 33 | + "ecs": {"version": "1.5.0"}, |
| 34 | + "log": {"level": "info"}, |
| 35 | + "message": "test message", |
| 36 | + "span": {"id": span_id}, |
| 37 | + "trace": {"id": trace_id}, |
| 38 | + "transaction": {"id": transaction_id}, |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +def test_elastic_apm_stdlib_log_correlation_ecs_fields(): |
| 43 | + apm = elasticapm.Client({"SERVICE_NAME": "apm-service"}) |
| 44 | + stream = StringIO() |
| 45 | + logger = logging.getLogger("apm-logger") |
| 46 | + handler = logging.StreamHandler(stream) |
| 47 | + handler.setFormatter( |
| 48 | + ecs_logging.StdlibFormatter( |
| 49 | + exclude_fields=["@timestamp", "process", "log.origin.file.line"] |
| 50 | + ) |
| 51 | + ) |
| 52 | + logger.addHandler(handler) |
| 53 | + logger.setLevel(logging.DEBUG) |
| 54 | + |
| 55 | + apm.begin_transaction("test-transaction") |
| 56 | + try: |
| 57 | + with elasticapm.capture_span("test-span"): |
| 58 | + span_id = elasticapm.get_span_id() |
| 59 | + trace_id = elasticapm.get_trace_id() |
| 60 | + transaction_id = elasticapm.get_transaction_id() |
| 61 | + |
| 62 | + logger.info("test message") |
| 63 | + finally: |
| 64 | + apm.end_transaction("test-transaction") |
| 65 | + |
| 66 | + ecs = json.loads(stream.getvalue().rstrip()) |
| 67 | + assert ecs == { |
| 68 | + "ecs": {"version": "1.5.0"}, |
| 69 | + "log": { |
| 70 | + "level": "info", |
| 71 | + "logger": "apm-logger", |
| 72 | + "origin": { |
| 73 | + "file": {"name": "test_apm.py"}, |
| 74 | + "function": "test_elastic_apm_stdlib_log_correlation_ecs_fields", |
| 75 | + }, |
| 76 | + "original": "test message", |
| 77 | + }, |
| 78 | + "message": "test message", |
| 79 | + "span": {"id": span_id}, |
| 80 | + "trace": {"id": trace_id}, |
| 81 | + "transaction": {"id": transaction_id}, |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | +def test_elastic_apm_stdlib_exclude_fields(): |
| 86 | + apm = elasticapm.Client({"SERVICE_NAME": "apm-service"}) |
| 87 | + stream = StringIO() |
| 88 | + logger = logging.getLogger("apm-logger") |
| 89 | + handler = logging.StreamHandler(stream) |
| 90 | + handler.setFormatter( |
| 91 | + ecs_logging.StdlibFormatter( |
| 92 | + exclude_fields=[ |
| 93 | + "@timestamp", |
| 94 | + "process", |
| 95 | + "log.origin.file.line", |
| 96 | + "span", |
| 97 | + "transaction.id", |
| 98 | + ] |
| 99 | + ) |
| 100 | + ) |
| 101 | + logger.addHandler(handler) |
| 102 | + logger.setLevel(logging.DEBUG) |
| 103 | + |
| 104 | + apm.begin_transaction("test-transaction") |
| 105 | + try: |
| 106 | + with elasticapm.capture_span("test-span"): |
| 107 | + trace_id = elasticapm.get_trace_id() |
| 108 | + |
| 109 | + logger.info("test message") |
| 110 | + finally: |
| 111 | + apm.end_transaction("test-transaction") |
| 112 | + |
| 113 | + ecs = json.loads(stream.getvalue().rstrip()) |
| 114 | + assert ecs == { |
| 115 | + "ecs": {"version": "1.5.0"}, |
| 116 | + "log": { |
| 117 | + "level": "info", |
| 118 | + "logger": "apm-logger", |
| 119 | + "origin": { |
| 120 | + "file": {"name": "test_apm.py"}, |
| 121 | + "function": "test_elastic_apm_stdlib_exclude_fields", |
| 122 | + }, |
| 123 | + "original": "test message", |
| 124 | + }, |
| 125 | + "message": "test message", |
| 126 | + "trace": {"id": trace_id}, |
| 127 | + } |
0 commit comments