|
| 1 | +import builtins |
| 2 | +import json |
| 3 | +import os |
| 4 | +import pytest |
| 5 | + |
| 6 | +import ddtrace |
| 7 | + |
| 8 | +from datadog_lambda import metric |
| 9 | +from datadog_lambda import tag_object |
| 10 | +from datadog_lambda import tracing |
| 11 | +from datadog_lambda import trigger |
| 12 | +from datadog_lambda import xray |
| 13 | + |
| 14 | +from datadog_lambda.constants import XrayDaemon, XraySubsegment |
| 15 | + |
| 16 | +from tests.utils import get_mock_context |
| 17 | + |
| 18 | + |
| 19 | +event_samples_dir = "tests/event_samples" |
| 20 | +event_samples = [f[:-5] for f in os.listdir(event_samples_dir) if f.endswith(".json")] |
| 21 | + |
| 22 | + |
| 23 | +def test_metric_write_metric_point_to_stdout(benchmark, monkeypatch): |
| 24 | + monkeypatch.setattr(builtins, "print", lambda *a, **k: None) |
| 25 | + benchmark( |
| 26 | + metric.write_metric_point_to_stdout, |
| 27 | + "metric_name", |
| 28 | + 1, |
| 29 | + tags=[ |
| 30 | + "tag1:value1", |
| 31 | + "tag2:value2", |
| 32 | + "tag3:value3", |
| 33 | + ], |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize("event", event_samples) |
| 38 | +def test_tag_object_tag_object(event, benchmark): |
| 39 | + with open(f"{event_samples_dir}/{event}.json") as f: |
| 40 | + event = json.load(f) |
| 41 | + span = ddtrace.tracer.start_span("test") |
| 42 | + benchmark(tag_object.tag_object, span, "function.request", event) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize("event", event_samples) |
| 46 | +def test_tracing_create_inferred_span(event, benchmark): |
| 47 | + with open(f"{event_samples_dir}/{event}.json") as f: |
| 48 | + event = json.load(f) |
| 49 | + context = get_mock_context() |
| 50 | + benchmark(tracing.create_inferred_span, event, context) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize("event", event_samples) |
| 54 | +def test_tracing_extract_dd_trace_context(event, benchmark): |
| 55 | + with open(f"{event_samples_dir}/{event}.json") as f: |
| 56 | + event = json.load(f) |
| 57 | + context = get_mock_context() |
| 58 | + benchmark(tracing.extract_dd_trace_context, event, context) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize("event", event_samples) |
| 62 | +def test_trigger_parse_event_source(event, benchmark): |
| 63 | + with open(f"{event_samples_dir}/{event}.json") as f: |
| 64 | + event = json.load(f) |
| 65 | + benchmark(trigger.parse_event_source, event) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("event", event_samples) |
| 69 | +def test_trigger_extract_trigger_tags(event, benchmark): |
| 70 | + with open(f"{event_samples_dir}/{event}.json") as f: |
| 71 | + event = json.load(f) |
| 72 | + context = get_mock_context() |
| 73 | + benchmark(trigger.extract_trigger_tags, event, context) |
| 74 | + |
| 75 | + |
| 76 | +def test_xray_send_segment(benchmark, monkeypatch): |
| 77 | + monkeypatch.setenv(XrayDaemon.XRAY_DAEMON_ADDRESS, "localhost:9000") |
| 78 | + monkeypatch.setenv( |
| 79 | + XrayDaemon.XRAY_TRACE_ID_HEADER_NAME, |
| 80 | + "Root=1-5e272390-8c398be037738dc042009320;Parent=94ae789b969f1cc5;Sampled=1;Lineage=c6c5b1b9:0", |
| 81 | + ) |
| 82 | + key = { |
| 83 | + "trace-id": "12345678901234567890123456789012", |
| 84 | + "parent-id": "1234567890123456", |
| 85 | + "sampling-priority": "1", |
| 86 | + } |
| 87 | + benchmark(xray.send_segment, XraySubsegment.TRACE_KEY, key) |
0 commit comments