|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | import contextlib |
3 | 3 | import json |
| 4 | +import os |
4 | 5 | import random |
5 | 6 | import string |
6 | 7 | import threading |
|
21 | 22 | from ddtrace.ext.ci import CI_APP_TEST_ORIGIN |
22 | 23 | from ddtrace.internal._encoding import BufferFull |
23 | 24 | from ddtrace.internal._encoding import BufferItemTooLarge |
| 25 | +from ddtrace.internal._encoding import EncodingValidationError |
24 | 26 | from ddtrace.internal._encoding import ListStringTable |
25 | 27 | from ddtrace.internal._encoding import MsgpackStringTable |
26 | 28 | from ddtrace.internal.compat import msgpack_type |
|
33 | 35 | from ddtrace.internal.encoding import _EncoderBase |
34 | 36 | from ddtrace.span import Span |
35 | 37 | from tests.utils import DummyTracer |
| 38 | +from tests.utils import override_global_config |
36 | 39 |
|
37 | 40 |
|
38 | 41 | _ORIGIN_KEY = ORIGIN_KEY.encode() |
@@ -700,3 +703,139 @@ def test_json_encoder_traces_bytes(): |
700 | 703 | assert u"\ufffdspan.a" == span_a["name"], span_a["name"] |
701 | 704 | assert u"\x80span.b" == span_b["name"] |
702 | 705 | assert u"\ufffdspan.b" == span_c["name"] |
| 706 | + |
| 707 | + |
| 708 | +@pytest.mark.skipif( |
| 709 | + os.getenv("PYTHONOPTIMIZE", "").lower() in ("1", "t", "true") or six.PY2, |
| 710 | + reason="Python optimize removes assertions from cython code. PY2 byte string formatting fails assertions", |
| 711 | +) |
| 712 | +def test_verifying_v05_payloads(): |
| 713 | + string_table_size = 4 * (1 << 12) |
| 714 | + encoder = MsgpackEncoderV05(string_table_size, string_table_size) |
| 715 | + |
| 716 | + # Ensure EncodingValidationError is not raised when trace fields are encoded as expected |
| 717 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 718 | + traces = [[Span("name", "service", "resource", "type") for _ in range(5)] for _ in range(100)] |
| 719 | + for trace in traces: |
| 720 | + for s in trace: |
| 721 | + s._meta = { |
| 722 | + "app": "ac_query", |
| 723 | + "language": "python", |
| 724 | + "key_for_long_string": "very_long_string_that_will_be_dropped" * int(string_table_size * 0.1), |
| 725 | + } |
| 726 | + s._metrics = { |
| 727 | + "zqSCqAYiBgjmqYKoBiohcCKwCagB": 7, |
| 728 | + "_sampling_priority_v1": 0, |
| 729 | + "very_long_string_that_will_be_dropped" * int(string_table_size * 0.1): 1, |
| 730 | + } |
| 731 | + |
| 732 | + encoded = [] |
| 733 | + for trace in traces: |
| 734 | + try: |
| 735 | + encoder.put(trace) |
| 736 | + encoded.append(trace) |
| 737 | + except BufferFull: |
| 738 | + pass |
| 739 | + assert 0 < len(encoded) < len(traces), "Ensures BufferFull is raised and only a subset of traces are encoded" |
| 740 | + assert encoder.encode() |
| 741 | + |
| 742 | + # Ensure EncodingValidationError is raised when the encoded span name does not match the span name |
| 743 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 744 | + with pytest.raises(EncodingValidationError) as e: |
| 745 | + og_span = Span("name", "service", "resource", "type") |
| 746 | + encoder.put([og_span]) |
| 747 | + og_span.name = "new_name" |
| 748 | + encoder.encode() |
| 749 | + assert "misencoded name: b'name'" in e.value.args[0] |
| 750 | + |
| 751 | + # Ensure EncodingValidationError is raised when the encoded service does not match the span service |
| 752 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 753 | + with pytest.raises(EncodingValidationError) as e: |
| 754 | + og_span = Span("name", "service", "resource", "type") |
| 755 | + encoder.put([og_span]) |
| 756 | + og_span.service = "new_service" |
| 757 | + encoder.encode() |
| 758 | + assert "misencoded service: b'service'" in e.value.args[0] |
| 759 | + |
| 760 | + # Ensure EncodingValidationError is raised when the encoded resource does not match the span resource |
| 761 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 762 | + with pytest.raises(EncodingValidationError) as e: |
| 763 | + og_span = Span("name", "service", "resource", "type") |
| 764 | + encoder.put([og_span]) |
| 765 | + og_span.resource = "new_resource" |
| 766 | + encoder.encode() |
| 767 | + assert "misencoded resource: b'resource'" in e.value.args[0] |
| 768 | + |
| 769 | + # Ensure EncodingValidationError is raised when the encoded duration does not match |
| 770 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 771 | + with pytest.raises(EncodingValidationError) as e: |
| 772 | + og_span = Span("name", "service", "resource", "type") |
| 773 | + encoder.put([og_span]) |
| 774 | + og_span.duration_ns = 55 |
| 775 | + encoder.encode() |
| 776 | + assert "misencoded duration: 0" in e.value.args[0] |
| 777 | + |
| 778 | + # Ensure EncodingValidationError is raised when the encoded start does not match |
| 779 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 780 | + with pytest.raises(EncodingValidationError) as e: |
| 781 | + og_span = Span("name", "service", "resource", "type", start=10) |
| 782 | + encoder.put([og_span]) |
| 783 | + og_span.start_ns = 100000001 |
| 784 | + encoder.encode() |
| 785 | + assert "misencoded start: 10" in e.value.args[0] |
| 786 | + |
| 787 | + # Ensure EncodingValidationError is raised when the encoded parent_id does not match |
| 788 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 789 | + with pytest.raises(EncodingValidationError) as e: |
| 790 | + og_span = Span("name", "service", "resource", "type", parent_id=1) |
| 791 | + encoder.put([og_span]) |
| 792 | + og_span.parent_id = 2 |
| 793 | + encoder.encode() |
| 794 | + assert "misencoded parent id: 1" in e.value.args[0] |
| 795 | + |
| 796 | + # Ensure EncodingValidationError is raised when the encoded trace id does not match |
| 797 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 798 | + with pytest.raises(EncodingValidationError) as e: |
| 799 | + og_span = Span("name", "service", "resource", "type", trace_id=1) |
| 800 | + encoder.put([og_span]) |
| 801 | + og_span.trace_id = 2 |
| 802 | + encoder.encode() |
| 803 | + assert "misencoded trace id: 1" in e.value.args[0] |
| 804 | + |
| 805 | + # Ensure EncodingValidationError is raised when the encoded span id does not match |
| 806 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 807 | + with pytest.raises(EncodingValidationError) as e: |
| 808 | + og_span = Span("name", "service", "resource", "type", span_id=1) |
| 809 | + encoder.put([og_span]) |
| 810 | + og_span.span_id = 2 |
| 811 | + encoder.encode() |
| 812 | + assert "misencoded span id: 1" in e.value.args[0] |
| 813 | + |
| 814 | + # Ensure EncodingValidationError is raised when the encoded tags do not match the span tag's |
| 815 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 816 | + with pytest.raises(EncodingValidationError) as e: |
| 817 | + og_span = Span("name", "service", "resource", "type") |
| 818 | + og_span._meta["hi"] = "tag" |
| 819 | + encoder.put([og_span]) |
| 820 | + og_span._meta["hi"] = "new tag" |
| 821 | + encoder.encode() |
| 822 | + assert "misencoded tag: k=hi v=tag" in e.value.args[0] |
| 823 | + |
| 824 | + # Ensure EncodingValidationError is raised when the encoded metrics do not match the metrics set on the span |
| 825 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 826 | + with pytest.raises(EncodingValidationError) as e: |
| 827 | + og_span = Span("name", "service", "resource", "type") |
| 828 | + og_span._metrics["hi"] = 1 |
| 829 | + encoder.put([og_span]) |
| 830 | + og_span._metrics["hi"] = 2 |
| 831 | + encoder.encode() |
| 832 | + assert "misencoded metric: k=hi v=1" in e.value.args[0] |
| 833 | + |
| 834 | + # Ensure EncodingValidationError is raised when the encoded span type does not match the span type on the span |
| 835 | + with override_global_config({"_trace_writer_log_err_payload": True}): |
| 836 | + with pytest.raises(EncodingValidationError) as e: |
| 837 | + og_span = Span("name", "service", "resource", "type") |
| 838 | + encoder.put([og_span]) |
| 839 | + og_span.span_type = "new_span_type" |
| 840 | + encoder.encode() |
| 841 | + assert "misencoded span type: b'type'" in e.value.args[0] |
0 commit comments