Skip to content

Commit 5c744a7

Browse files
committed
Fix tests
1 parent cc38328 commit 5c744a7

6 files changed

Lines changed: 41 additions & 31 deletions

File tree

dd-trace-core/src/main/java/datadog/trace/common/metrics/ConflatingMetricsAggregator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ public final class ConflatingMetricsAggregator implements MetricsAggregator, Eve
5151
private static final DDCache<String, UTF8BytesString> SERVICE_NAMES =
5252
DDCaches.newFixedSizeCache(32);
5353

54-
private static final DDCache<String, UTF8BytesString> SPAN_KINDS = DDCaches.newFixedSizeCache(16);
54+
private static final DDCache<CharSequence, UTF8BytesString> SPAN_KINDS =
55+
DDCaches.newFixedSizeCache(16);
5556
private static final DDCache<
5657
String, Pair<DDCache<String, UTF8BytesString>, Function<String, UTF8BytesString>>>
5758
PEER_TAGS_CACHE =
58-
DDCaches.newUnboundedCache(
59+
DDCaches.newFixedSizeCache(
5960
64); // it can be unbounded since those values are returned by the agent and should be
60-
// under control.
61+
// under control. 64 entries is enough in this case to contain all the peer tags.
6162
private static final Function<
6263
String, Pair<DDCache<String, UTF8BytesString>, Function<String, UTF8BytesString>>>
6364
PEER_TAGS_CACHE_ADDER =
@@ -268,7 +269,7 @@ private boolean publish(CoreSpan<?> span, boolean isTopLevel) {
268269
span.getHttpStatusCode(),
269270
isSynthetic(span),
270271
span.isTopLevel(),
271-
SPAN_KINDS.computeIfAbsent(span.getTag(SPAN_KIND, ""), UTF8_ENCODE),
272+
SPAN_KINDS.computeIfAbsent(span.getTag(SPAN_KIND, ""), UTF8BytesString::create),
272273
getPeerTags(span));
273274
boolean isNewKey = false;
274275
MetricKey key = keys.putIfAbsent(newKey, newKey);

dd-trace-core/src/main/java/datadog/trace/common/metrics/MetricKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public MetricKey(
2727
int httpStatusCode,
2828
boolean synthetics,
2929
boolean isTraceRoot,
30-
UTF8BytesString spanKind,
30+
CharSequence spanKind,
3131
List<UTF8BytesString> peerTags) {
3232
this.resource = null == resource ? EMPTY : UTF8BytesString.create(resource);
3333
this.service = null == service ? EMPTY : UTF8BytesString.create(service);
@@ -36,7 +36,7 @@ public MetricKey(
3636
this.httpStatusCode = httpStatusCode;
3737
this.synthetics = synthetics;
3838
this.isTraceRoot = isTraceRoot;
39-
this.spanKind = spanKind;
39+
this.spanKind = null == spanKind ? EMPTY : UTF8BytesString.create(spanKind);
4040
this.peerTags = peerTags == null ? Collections.emptyList() : peerTags;
4141

4242
// Unrolled polynomial hashcode to avoid varargs allocation

dd-trace-core/src/test/groovy/datadog/trace/common/metrics/AggregateMetricTest.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.common.metrics
22

3+
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString
34
import datadog.trace.test.util.DDSpecification
45

56
import java.util.concurrent.BlockingDeque
@@ -51,7 +52,7 @@ class AggregateMetricTest extends DDSpecification {
5152
given:
5253
AggregateMetric aggregate = new AggregateMetric().recordDurations(3, new AtomicLongArray(0L, 0L, 0L | ERROR_TAG | TOP_LEVEL_TAG))
5354

54-
Batch batch = new Batch().reset(new MetricKey("foo", "bar", "qux", "type", 0, false, true, "corge", ["grault":"quux"]))
55+
Batch batch = new Batch().reset(new MetricKey("foo", "bar", "qux", "type", 0, false, true, "corge", [UTF8BytesString.create("grault:quux")]))
5556
batch.add(0L, 10)
5657
batch.add(0L, 10)
5758
batch.add(0L, 10)
@@ -126,7 +127,7 @@ class AggregateMetricTest extends DDSpecification {
126127
def "consistent under concurrent attempts to read and write"() {
127128
given:
128129
AggregateMetric aggregate = new AggregateMetric()
129-
MetricKey key = new MetricKey("foo", "bar", "qux", "type", 0, false, true, "corge", ["grault":"quux"])
130+
MetricKey key = new MetricKey("foo", "bar", "qux", "type", 0, false, true, "corge", [UTF8BytesString.create("grault:quux")])
130131
BlockingDeque<Batch> queue = new LinkedBlockingDeque<>(1000)
131132
ExecutorService reader = Executors.newSingleThreadExecutor()
132133
int writerCount = 10

dd-trace-core/src/test/groovy/datadog/trace/common/metrics/ConflatingMetricAggregatorTest.groovy

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
125125
false,
126126
true,
127127
"baz",
128-
[:]
128+
[]
129129
), _) >> { MetricKey key, AggregateMetric value ->
130130
value.getHitCount() == 1 && value.getTopLevelCount() == 1 && value.getDuration() == 100
131131
}
@@ -168,7 +168,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
168168
false,
169169
false,
170170
kind,
171-
[:]
171+
[]
172172
), { AggregateMetric aggregateMetric ->
173173
aggregateMetric.getHitCount() == 1 && aggregateMetric.getTopLevelCount() == 0 && aggregateMetric.getDuration() == 100
174174
})
@@ -221,7 +221,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
221221
false,
222222
false,
223223
"grault",
224-
["country":"france"]
224+
[UTF8BytesString.create("country:france")]
225225
), { AggregateMetric aggregateMetric ->
226226
aggregateMetric.getHitCount() == 1 && aggregateMetric.getTopLevelCount() == 0 && aggregateMetric.getDuration() == 100
227227
})
@@ -235,7 +235,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
235235
false,
236236
false,
237237
"grault",
238-
["country":"france", "georegion":"europe"]
238+
[UTF8BytesString.create("country:france"), UTF8BytesString.create("georegion:europe")]
239239
), { AggregateMetric aggregateMetric ->
240240
aggregateMetric.getHitCount() == 1 && aggregateMetric.getTopLevelCount() == 0 && aggregateMetric.getDuration() == 100
241241
})
@@ -277,7 +277,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
277277
false,
278278
topLevel,
279279
"baz",
280-
[:]
280+
[]
281281
), { AggregateMetric value ->
282282
value.getHitCount() == 1 && value.getTopLevelCount() == topLevelCount && value.getDuration() == 100
283283
})
@@ -333,7 +333,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
333333
false,
334334
false,
335335
"baz",
336-
[:]
336+
[]
337337
), { AggregateMetric value ->
338338
value.getHitCount() == count && value.getDuration() == count * duration
339339
})
@@ -346,7 +346,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
346346
false,
347347
false,
348348
"baz",
349-
[:]
349+
[]
350350
), { AggregateMetric value ->
351351
value.getHitCount() == count && value.getDuration() == count * duration * 2
352352
})
@@ -396,7 +396,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
396396
false,
397397
true,
398398
"baz",
399-
[:]
399+
[]
400400
), _) >> { MetricKey key, AggregateMetric value ->
401401
value.getHitCount() == 1 && value.getDuration() == duration
402402
}
@@ -410,7 +410,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
410410
false,
411411
true,
412412
"baz",
413-
[:]
413+
[]
414414
), _)
415415
1 * writer.finishBucket() >> { latch.countDown() }
416416

@@ -456,7 +456,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
456456
false,
457457
true,
458458
"baz",
459-
[:]
459+
[]
460460
), { AggregateMetric value ->
461461
value.getHitCount() == 1 && value.getDuration() == duration
462462
})
@@ -487,7 +487,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
487487
false,
488488
true,
489489
"baz",
490-
[:]
490+
[]
491491
),{ AggregateMetric value ->
492492
value.getHitCount() == 1 && value.getDuration() == duration
493493
})
@@ -501,7 +501,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
501501
false,
502502
true,
503503
"baz",
504-
[:]
504+
[]
505505
), _)
506506
1 * writer.finishBucket() >> { latch.countDown() }
507507

@@ -547,7 +547,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
547547
false,
548548
true,
549549
"quux",
550-
[:]
550+
[]
551551
), { AggregateMetric value ->
552552
value.getHitCount() == 1 && value.getDuration() == duration
553553
})
@@ -603,7 +603,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
603603
false,
604604
true,
605605
"garply",
606-
[:]
606+
[]
607607
), { AggregateMetric value ->
608608
value.getHitCount() == 1 && value.getDuration() == duration
609609
})

dd-trace-core/src/test/groovy/datadog/trace/common/metrics/SerializingMetricWriterTest.groovy

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import datadog.trace.api.Config
44
import datadog.trace.api.ProcessTags
55
import datadog.trace.api.WellKnownTags
66
import datadog.trace.api.Pair
7+
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString
78
import datadog.trace.test.util.DDSpecification
89
import org.msgpack.core.MessagePack
910
import org.msgpack.core.MessageUnpacker
@@ -53,7 +54,11 @@ class SerializingMetricWriterTest extends DDSpecification {
5354
false,
5455
false,
5556
"client",
56-
["country":"canada", "georegion":"amer", "peer.service":"remote-service"]
57+
[
58+
UTF8BytesString.create("country:canada"),
59+
UTF8BytesString.create("georegion:amer"),
60+
UTF8BytesString.create("peer.service:remote-service")
61+
]
5762
),
5863
new AggregateMetric().recordDurations(10, new AtomicLongArray(1L))
5964
),
@@ -67,7 +72,11 @@ class SerializingMetricWriterTest extends DDSpecification {
6772
true,
6873
false,
6974
"producer",
70-
["country":"canada", "georegion":"amer", "peer.service":"remote-service"]
75+
[
76+
UTF8BytesString.create("country:canada"),
77+
UTF8BytesString.create("georegion:amer"),
78+
UTF8BytesString.create("peer.service:remote-service")
79+
],
7180
),
7281
new AggregateMetric().recordDurations(9, new AtomicLongArray(1L))
7382
)
@@ -83,7 +92,7 @@ class SerializingMetricWriterTest extends DDSpecification {
8392
false,
8493
false,
8594
"producer",
86-
["messaging.destination" : "dest" + i]
95+
[UTF8BytesString.create("messaging.destination:dest" + i)]
8796
),
8897
new AggregateMetric().recordDurations(10, new AtomicLongArray(1L))
8998
)
@@ -177,10 +186,8 @@ class SerializingMetricWriterTest extends DDSpecification {
177186
int peerTagsLength = unpacker.unpackArrayHeader()
178187
assert peerTagsLength == key.getPeerTags().size()
179188
for (int i = 0; i < peerTagsLength; i++) {
180-
def string = unpacker.unpackString()
181-
def separatorPos = string.indexOf(':')
182-
def tagVal = key.getPeerTags()[string.substring(0, separatorPos)]
183-
assert tagVal == string.substring(separatorPos + 1)
189+
def unpackedPeerTag = unpacker.unpackString()
190+
assert unpackedPeerTag == key.getPeerTags()[i].toString()
184191
}
185192
++elementCount
186193
assert unpacker.unpackString() == "Hits"

dd-trace-core/src/traceAgentTest/groovy/MetricsIntegrationTest.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import datadog.communication.ddagent.DDAgentFeaturesDiscovery
22
import datadog.communication.http.OkHttpUtils
33
import datadog.trace.api.Config
44
import datadog.trace.api.WellKnownTags
5+
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString
56
import datadog.trace.common.metrics.AggregateMetric
67
import datadog.trace.common.metrics.EventListener
78
import datadog.trace.common.metrics.MetricKey
@@ -34,11 +35,11 @@ class MetricsIntegrationTest extends AbstractTraceAgentTest {
3435
)
3536
writer.startBucket(2, System.nanoTime(), SECONDS.toNanos(10))
3637
writer.add(
37-
new MetricKey("resource1", "service1", "operation1", "sql", 0, false, true, "xyzzy", ["grault":"quux"]),
38+
new MetricKey("resource1", "service1", "operation1", "sql", 0, false, true, "xyzzy", [UTF8BytesString.create("grault:quux")]),
3839
new AggregateMetric().recordDurations(5, new AtomicLongArray(2, 1, 2, 250, 4, 5))
3940
)
4041
writer.add(
41-
new MetricKey("resource2", "service2", "operation2", "web", 200, false, true, "xyzzy", ["grault":"quux"]),
42+
new MetricKey("resource2", "service2", "operation2", "web", 200, false, true, "xyzzy", [UTF8BytesString.create("grault:quux")]),
4243
new AggregateMetric().recordDurations(10, new AtomicLongArray(1, 1, 200, 2, 3, 4, 5, 6, 7, 8, 9))
4344
)
4445
writer.finishBucket()

0 commit comments

Comments
 (0)