|
15 | 15 | # pylint: disable=too-many-lines |
16 | 16 | # pylint: disable=no-member |
17 | 17 |
|
| 18 | +import copy |
18 | 19 | import shutil |
19 | 20 | import subprocess |
20 | 21 | import unittest |
|
58 | 59 | ParentBased, |
59 | 60 | StaticSampler, |
60 | 61 | ) |
61 | | -from opentelemetry.sdk.util import BoundedDict, ns_to_iso_str |
| 62 | +from opentelemetry.sdk.util import BoundedDict, BoundedList, ns_to_iso_str |
62 | 63 | from opentelemetry.sdk.util.instrumentation import InstrumentationInfo |
63 | 64 | from opentelemetry.test.spantestutil import ( |
64 | 65 | get_span_with_dropped_attributes_events_links, |
@@ -708,6 +709,69 @@ def test_link_dropped_attributes(self): |
708 | 709 | ) |
709 | 710 | self.assertEqual(link2.dropped_attributes, 0) |
710 | 711 |
|
| 712 | + def test_deepcopy(self): |
| 713 | + context = trace_api.SpanContext( |
| 714 | + trace_id=0x000000000000000000000000DEADBEEF, |
| 715 | + span_id=0x00000000DEADBEF0, |
| 716 | + is_remote=False, |
| 717 | + ) |
| 718 | + attributes = BoundedAttributes( |
| 719 | + 10, {"key1": "value1", "key2": 42}, immutable=False |
| 720 | + ) |
| 721 | + events = BoundedList(10) |
| 722 | + events.extend( |
| 723 | + ( |
| 724 | + trace.Event("event1", {"ekey": "evalue"}), |
| 725 | + trace.Event("event2", {"ekey2": "evalue2"}), |
| 726 | + ) |
| 727 | + ) |
| 728 | + |
| 729 | + links = [ |
| 730 | + trace_api.Link( |
| 731 | + context=trace_api.INVALID_SPAN_CONTEXT, |
| 732 | + attributes={"lkey": "lvalue"}, |
| 733 | + ) |
| 734 | + ] |
| 735 | + |
| 736 | + span = trace.ReadableSpan( |
| 737 | + name="test-span", |
| 738 | + context=context, |
| 739 | + attributes=attributes, |
| 740 | + events=events, |
| 741 | + links=links, |
| 742 | + status=Status(StatusCode.OK), |
| 743 | + ) |
| 744 | + |
| 745 | + span_copy = copy.deepcopy(span) |
| 746 | + |
| 747 | + self.assertEqual(span_copy.name, span.name) |
| 748 | + self.assertEqual(span_copy.status.status_code, span.status.status_code) |
| 749 | + self.assertEqual(span_copy.context.trace_id, span.context.trace_id) |
| 750 | + self.assertEqual(span_copy.context.span_id, span.context.span_id) |
| 751 | + |
| 752 | + self.assertEqual(dict(span_copy.attributes), dict(span.attributes)) |
| 753 | + attributes["key1"] = "mutated" |
| 754 | + self.assertNotEqual( |
| 755 | + span_copy.attributes["key1"], span.attributes["key1"] |
| 756 | + ) |
| 757 | + |
| 758 | + self.assertEqual(len(span_copy.events), len(span.events)) |
| 759 | + self.assertIsNot(span_copy.events, span.events) |
| 760 | + self.assertEqual(span_copy.events[0].name, span.events[0].name) |
| 761 | + self.assertEqual( |
| 762 | + span_copy.events[0].attributes, span.events[0].attributes |
| 763 | + ) |
| 764 | + |
| 765 | + self.assertEqual(len(span_copy.links), len(span.links)) |
| 766 | + self.assertEqual( |
| 767 | + span_copy.links[0].attributes, span.links[0].attributes |
| 768 | + ) |
| 769 | + links[0] = trace_api.Link( |
| 770 | + context=trace_api.INVALID_SPAN_CONTEXT, |
| 771 | + attributes={"mutated": "link"}, |
| 772 | + ) |
| 773 | + self.assertNotIn("mutated", span_copy.links[0].attributes) |
| 774 | + |
711 | 775 |
|
712 | 776 | class DummyError(Exception): |
713 | 777 | pass |
|
0 commit comments