|
1 | 1 | import datadog.trace.bootstrap.instrumentation.api.AgentSpan |
2 | | -import datadog.trace.bootstrap.instrumentation.api.InstrumentationTags |
| 2 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpanLink |
| 3 | +import datadog.trace.bootstrap.instrumentation.api.SpanPointerUtils |
3 | 4 | import datadog.trace.instrumentation.aws.v2.dynamodb.DynamoDbUtil |
4 | 5 | import org.junit.jupiter.api.Test |
5 | 6 | import software.amazon.awssdk.core.SdkBytes |
6 | 7 | import software.amazon.awssdk.services.dynamodb.model.AttributeValue |
7 | 8 |
|
8 | 9 | class DynamoDbUtilTest { |
9 | 10 | static createMockSpan() { |
10 | | - def tags = [:] |
| 11 | + def links = [] |
11 | 12 |
|
12 | 13 | def mockSpan = [ |
13 | | - setTag: { String key, String value -> |
14 | | - tags[key] = value |
15 | | - return null |
| 14 | + addLink: { AgentSpanLink link -> |
| 15 | + links.add(link) |
16 | 16 | } |
17 | 17 | ] as AgentSpan |
18 | 18 |
|
19 | | - return [span: mockSpan, tags: tags] |
| 19 | + return [span: mockSpan, links: links] |
20 | 20 | } |
21 | 21 |
|
22 | 22 | @Test |
23 | | - void testExportTagsWithNullKeys() { |
| 23 | + void testAddSpanPointerWithNullKeys() { |
24 | 24 | def mockData = createMockSpan() |
25 | 25 | def mockSpan = mockData.span |
26 | | - def tags = mockData.tags |
| 26 | + def links = mockData.links |
27 | 27 |
|
28 | | - DynamoDbUtil.exportTagsWithKnownKeys(mockSpan, null) |
| 28 | + DynamoDbUtil.addSpanPointer(mockSpan, "table", null) |
29 | 29 |
|
30 | | - assert tags.isEmpty() |
| 30 | + assert links.isEmpty() |
31 | 31 | } |
32 | 32 |
|
33 | 33 | @Test |
34 | | - void testExportTagsWithEmptyKeys() { |
| 34 | + void testAddSpanPointerWithEmptyKeys() { |
35 | 35 | def mockData = createMockSpan() |
36 | 36 | def mockSpan = mockData.span |
37 | | - def tags = mockData.tags |
| 37 | + def links = mockData.links |
38 | 38 |
|
39 | | - DynamoDbUtil.exportTagsWithKnownKeys(mockSpan, [:]) |
| 39 | + DynamoDbUtil.addSpanPointer(mockSpan, "table", [:]) |
40 | 40 |
|
41 | | - assert tags.isEmpty() |
| 41 | + assert links.isEmpty() |
42 | 42 | } |
43 | 43 |
|
44 | 44 | @Test |
45 | | - void testExportTagsWithSingleStringKey() { |
| 45 | + void testAddSpanPointerWithNullTableName() { |
46 | 46 | def mockData = createMockSpan() |
47 | 47 | def mockSpan = mockData.span |
48 | | - def tags = mockData.tags |
| 48 | + def links = mockData.links |
49 | 49 |
|
50 | 50 | def keys = [ |
51 | 51 | "id": AttributeValue.builder().s("12345").build() |
52 | 52 | ] |
53 | 53 |
|
54 | | - DynamoDbUtil.exportTagsWithKnownKeys(mockSpan, keys) |
| 54 | + DynamoDbUtil.addSpanPointer(mockSpan, null, keys) |
55 | 55 |
|
56 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_1] == "id" |
57 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_1_VALUE] == "12345" |
| 56 | + assert links.isEmpty() |
58 | 57 | } |
59 | 58 |
|
60 | 59 | @Test |
61 | | - void testExportTagsWithSingleNumberKey() { |
| 60 | + void testAddSpanPointerWithSingleStringKey() { |
62 | 61 | def mockData = createMockSpan() |
63 | 62 | def mockSpan = mockData.span |
64 | | - def tags = mockData.tags |
| 63 | + def links = mockData.links |
| 64 | + |
| 65 | + def keys = [ |
| 66 | + "id": AttributeValue.builder().s("12345").build() |
| 67 | + ] |
| 68 | + |
| 69 | + DynamoDbUtil.addSpanPointer(mockSpan, "my-table", keys) |
| 70 | + |
| 71 | + assert links.size() == 1 |
| 72 | + def link = links[0] |
| 73 | + assert link.attributes().asMap().get("ptr.kind") == SpanPointerUtils.DYNAMODB_PTR_KIND |
| 74 | + assert link.attributes().asMap().get("ptr.dir") == SpanPointerUtils.DOWN_DIRECTION |
| 75 | + assert link.attributes().asMap().get("link.kind") == SpanPointerUtils.LINK_KIND |
| 76 | + assert link.attributes().asMap().get("ptr.hash") != null |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testAddSpanPointerWithSingleNumberKey() { |
| 81 | + def mockData = createMockSpan() |
| 82 | + def mockSpan = mockData.span |
| 83 | + def links = mockData.links |
65 | 84 |
|
66 | 85 | def keys = [ |
67 | 86 | "count": AttributeValue.builder().n("42").build() |
68 | 87 | ] |
69 | 88 |
|
70 | | - DynamoDbUtil.exportTagsWithKnownKeys(mockSpan, keys) |
| 89 | + DynamoDbUtil.addSpanPointer(mockSpan, "my-table", keys) |
71 | 90 |
|
72 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_1] == "count" |
73 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_1_VALUE] == "42" |
| 91 | + assert links.size() == 1 |
| 92 | + def link = links[0] |
| 93 | + assert link.attributes().asMap().get("ptr.kind") == SpanPointerUtils.DYNAMODB_PTR_KIND |
74 | 94 | } |
75 | 95 |
|
76 | 96 | @Test |
77 | | - void testExportTagsWithSingleBinaryKey() { |
| 97 | + void testAddSpanPointerWithSingleBinaryKey() { |
78 | 98 | def mockData = createMockSpan() |
79 | 99 | def mockSpan = mockData.span |
80 | | - def tags = mockData.tags |
| 100 | + def links = mockData.links |
81 | 101 |
|
82 | 102 | def binaryData = "binary-data".getBytes() |
83 | 103 | def keys = [ |
84 | 104 | "data": AttributeValue.builder().b(SdkBytes.fromByteArray(binaryData)).build() |
85 | 105 | ] |
86 | 106 |
|
87 | | - DynamoDbUtil.exportTagsWithKnownKeys(mockSpan, keys) |
| 107 | + DynamoDbUtil.addSpanPointer(mockSpan, "my-table", keys) |
88 | 108 |
|
89 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_1] == "data" |
90 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_1_VALUE] == "binary-data" |
| 109 | + assert links.size() == 1 |
| 110 | + def link = links[0] |
| 111 | + assert link.attributes().asMap().get("ptr.kind") == SpanPointerUtils.DYNAMODB_PTR_KIND |
91 | 112 | } |
92 | 113 |
|
93 | 114 | @Test |
94 | | - void testExportTagsWithTwoKeys() { |
| 115 | + void testAddSpanPointerWithTwoKeys() { |
95 | 116 | def mockData = createMockSpan() |
96 | 117 | def mockSpan = mockData.span |
97 | | - def tags = mockData.tags |
| 118 | + def links = mockData.links |
98 | 119 |
|
99 | 120 | def keys = [ |
100 | 121 | "id": AttributeValue.builder().s("12345").build(), |
101 | 122 | "name": AttributeValue.builder().s("item-name").build() |
102 | 123 | ] |
103 | 124 |
|
104 | | - DynamoDbUtil.exportTagsWithKnownKeys(mockSpan, keys) |
| 125 | + DynamoDbUtil.addSpanPointer(mockSpan, "my-table", keys) |
105 | 126 |
|
106 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_1] == "id" |
107 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_1_VALUE] == "12345" |
108 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_2] == "name" |
109 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_2_VALUE] == "item-name" |
| 127 | + assert links.size() == 1 |
| 128 | + def link = links[0] |
| 129 | + assert link.attributes().asMap().get("ptr.kind") == SpanPointerUtils.DYNAMODB_PTR_KIND |
| 130 | + assert link.attributes().asMap().get("ptr.hash") != null |
110 | 131 | } |
111 | 132 |
|
112 | 133 | @Test |
113 | | - void testExportTagsWithTwoKeysSortsAlphabetically() { |
| 134 | + void testAddSpanPointerWithTwoKeysSortsAlphabetically() { |
114 | 135 | def mockData = createMockSpan() |
115 | | - def mockSpan = mockData.span |
116 | | - def tags = mockData.tags |
| 136 | + def mockSpan1 = mockData.span |
| 137 | + def links1 = mockData.links |
117 | 138 |
|
118 | | - def keys = [ |
| 139 | + // Keys in order bKey, aKey |
| 140 | + def keys1 = [ |
119 | 141 | "bKey": AttributeValue.builder().s("abc").build(), |
120 | 142 | "aKey": AttributeValue.builder().s("zxy").build() |
121 | 143 | ] |
122 | 144 |
|
123 | | - DynamoDbUtil.exportTagsWithKnownKeys(mockSpan, keys) |
| 145 | + DynamoDbUtil.addSpanPointer(mockSpan1, "my-table", keys1) |
| 146 | + |
| 147 | + // Reverse order: aKey, bKey — should produce the same hash |
| 148 | + def mockData2 = createMockSpan() |
| 149 | + def mockSpan2 = mockData2.span |
| 150 | + def links2 = mockData2.links |
| 151 | + |
| 152 | + def keys2 = [ |
| 153 | + "aKey": AttributeValue.builder().s("zxy").build(), |
| 154 | + "bKey": AttributeValue.builder().s("abc").build() |
| 155 | + ] |
| 156 | + |
| 157 | + DynamoDbUtil.addSpanPointer(mockSpan2, "my-table", keys2) |
124 | 158 |
|
125 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_1] == "aKey" |
126 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_1_VALUE] == "zxy" |
127 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_2] == "bKey" |
128 | | - assert tags[InstrumentationTags.DYNAMO_PRIMARY_KEY_2_VALUE] == "abc" |
| 159 | + assert links1.size() == 1 |
| 160 | + assert links2.size() == 1 |
| 161 | + // Both should produce the same hash regardless of input order |
| 162 | + assert links1[0].attributes().asMap().get("ptr.hash") == links2[0].attributes().asMap().get("ptr.hash") |
129 | 163 | } |
130 | 164 | } |
0 commit comments