Skip to content

Commit b75eaca

Browse files
committed
Add tests and move postprocessors later
1 parent 36d6362 commit b75eaca

File tree

6 files changed

+120
-48
lines changed

6 files changed

+120
-48
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public void add(MetricKey key, AggregateMetric aggregate) {
111111
final boolean hasHttpMethod = key.getHttpMethod() != null;
112112
final boolean hasHttpEndpoint = key.getHttpEndpoint() != null;
113113
final boolean hasServiceSource = key.getServiceSource() != null;
114-
final int mapSize = 15 + (hasServiceSource ? 1 : 0) + (hasHttpMethod ? 1 : 0) + (hasHttpEndpoint ? 1 : 0);
114+
final int mapSize =
115+
15 + (hasServiceSource ? 1 : 0) + (hasHttpMethod ? 1 : 0) + (hasHttpEndpoint ? 1 : 0);
115116

116117
writer.startMap(mapSize);
117118

dd-trace-core/src/main/java/datadog/trace/core/CoreSpan.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package datadog.trace.core;
22

3-
import static datadog.trace.api.DDTags.DD_SVC_SRC;
4-
53
import datadog.trace.api.DDTraceId;
64
import java.util.Map;
75

@@ -12,15 +10,7 @@ public interface CoreSpan<T extends CoreSpan<T>> {
1210
String getServiceName();
1311

1412
default CharSequence getServiceNameSource() {
15-
// overridden in DDSpan for a better implementation
16-
final Object obj = getTag(DD_SVC_SRC);
17-
if (obj == null) {
18-
return null;
19-
}
20-
if (obj instanceof CharSequence) {
21-
return (CharSequence) obj;
22-
}
23-
return obj.toString();
13+
return null;
2414
}
2515

2616
CharSequence getOperationName();

dd-trace-core/src/main/java/datadog/trace/core/tagprocessor/TagsPostProcessorFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private static class Lazy {
1313
private static TagsPostProcessor lazyProcessor = createLazyChain();
1414

1515
private static TagsPostProcessor createEagerChain() {
16-
final List<TagsPostProcessor> processors = new ArrayList<>(4);
16+
final List<TagsPostProcessor> processors = new ArrayList<>(3);
1717
processors.add(new PeerServiceCalculator());
1818
if (addBaseService) {
1919
processors.add(new BaseServiceAdder(Config.get().getServiceName()));
@@ -23,12 +23,11 @@ private static TagsPostProcessor createEagerChain() {
2323
if (Config.get().isTraceResourceRenamingEnabled()) {
2424
processors.add(new HttpEndpointPostProcessor());
2525
}
26-
processors.add(new ServiceNameSourceAdder()); // eager since needed for stats
2726
return new PostProcessorChain(processors.toArray(new TagsPostProcessor[0]));
2827
}
2928

3029
private static TagsPostProcessor createLazyChain() {
31-
final List<TagsPostProcessor> processors = new ArrayList<>(7);
30+
final List<TagsPostProcessor> processors = new ArrayList<>(8);
3231

3332
processors.add(new QueryObfuscator(Config.get().getObfuscationQueryRegexp()));
3433
if (addRemoteHostname) {
@@ -48,6 +47,7 @@ private static TagsPostProcessor createLazyChain() {
4847
processors.add(new SpanPointersProcessor());
4948
}
5049
processors.add(new IntegrationAdder());
50+
processors.add(new ServiceNameSourceAdder());
5151
return new PostProcessorChain(
5252
processors.toArray(processors.toArray(new TagsPostProcessor[0])));
5353
}

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

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
467467
"resource2",
468468
"service2",
469469
"operation2",
470-
null,
470+
null,
471471
"type",
472472
HTTP_OK,
473473
false,
@@ -789,6 +789,52 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
789789
aggregator.close()
790790
}
791791

792+
def "gather the service name source when the span is published"() {
793+
setup:
794+
MetricWriter writer = Mock(MetricWriter)
795+
Sink sink = Stub(Sink)
796+
DDAgentFeaturesDiscovery features = Mock(DDAgentFeaturesDiscovery)
797+
features.supportsMetrics() >> true
798+
features.peerTags() >> []
799+
ConflatingMetricsAggregator aggregator = new ConflatingMetricsAggregator(empty,
800+
features, HealthMetrics.NO_OP, sink, writer, 10, queueSize, reportingInterval, SECONDS, true)
801+
aggregator.start()
802+
803+
when: "publish span with a service name source"
804+
CountDownLatch latch = new CountDownLatch(1)
805+
long duration = 100
806+
aggregator.publish([
807+
new SimpleSpan("service", "operation", "resource", "type", true, true, false, 0, duration, 200, false, 0, "source")
808+
.setTag(SPAN_KIND, "server")
809+
])
810+
aggregator.report()
811+
def latchTriggered = latch.await(2, SECONDS)
812+
813+
then: "should create the same metric keys for spans with and without s"
814+
latchTriggered
815+
1 * writer.startBucket(1, _, _)
816+
1 * writer.add(new MetricKey(
817+
"resource",
818+
"service",
819+
"operation",
820+
"source",
821+
"type",
822+
200,
823+
false,
824+
false,
825+
"server",
826+
[],
827+
null,
828+
null
829+
), { AggregateMetric value ->
830+
value.getHitCount() == 1 && value.getDuration() == duration
831+
})
832+
1 * writer.finishBucket() >> { latch.countDown() }
833+
834+
cleanup:
835+
aggregator.close()
836+
}
837+
792838
def "test least recently written to aggregate flushed when size limit exceeded"() {
793839
setup:
794840
int maxAggregates = 10
@@ -821,7 +867,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
821867
"resource",
822868
"service" + i,
823869
"operation",
824-
null,
870+
null,
825871
"type",
826872
HTTP_OK,
827873
false,
@@ -838,7 +884,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
838884
"resource",
839885
"service0",
840886
"operation",
841-
null,
887+
null,
842888
"type",
843889
HTTP_OK,
844890
false,
@@ -886,7 +932,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
886932
"resource",
887933
"service" + i,
888934
"operation",
889-
null,
935+
null,
890936
"type",
891937
HTTP_OK,
892938
false,
@@ -920,7 +966,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
920966
"resource",
921967
"service" + i,
922968
"operation",
923-
null,
969+
null,
924970
"type",
925971
HTTP_OK,
926972
false,
@@ -937,7 +983,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
937983
"resource",
938984
"service0",
939985
"operation",
940-
null,
986+
null,
941987
"type",
942988
HTTP_OK,
943989
false,
@@ -985,7 +1031,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
9851031
"resource",
9861032
"service" + i,
9871033
"operation",
988-
null,
1034+
null,
9891035
"type",
9901036
HTTP_OK,
9911037
false,
@@ -1043,7 +1089,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
10431089
"resource",
10441090
"service" + i,
10451091
"operation",
1046-
null,
1092+
null,
10471093
"type",
10481094
HTTP_OK,
10491095
false,

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

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package datadog.trace.common.metrics
22

3+
import static datadog.trace.api.config.GeneralConfig.EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED
4+
import static java.util.concurrent.TimeUnit.MILLISECONDS
5+
import static java.util.concurrent.TimeUnit.SECONDS
6+
37
import datadog.metrics.api.Histograms
48
import datadog.metrics.impl.DDSketchHistograms
59
import datadog.trace.api.Config
10+
import datadog.trace.api.Pair
611
import datadog.trace.api.ProcessTags
712
import datadog.trace.api.WellKnownTags
8-
import datadog.trace.api.Pair
913
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString
1014
import datadog.trace.test.util.DDSpecification
11-
import org.msgpack.core.MessagePack
12-
import org.msgpack.core.MessageUnpacker
13-
1415
import java.nio.ByteBuffer
1516
import java.util.concurrent.atomic.AtomicLongArray
16-
17-
import static datadog.trace.api.config.GeneralConfig.EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED
18-
import static java.util.concurrent.TimeUnit.MILLISECONDS
19-
import static java.util.concurrent.TimeUnit.SECONDS
17+
import org.msgpack.core.MessagePack
18+
import org.msgpack.core.MessageUnpacker
2019

2120
class SerializingMetricWriterTest extends DDSpecification {
2221

@@ -55,7 +54,7 @@ class SerializingMetricWriterTest extends DDSpecification {
5554
"resource1",
5655
"service1",
5756
"operation1",
58-
null,
57+
null,
5958
"type",
6059
0,
6160
false,
@@ -76,7 +75,7 @@ class SerializingMetricWriterTest extends DDSpecification {
7675
"resource2",
7776
"service2",
7877
"operation2",
79-
null,
78+
null,
8079
"type2",
8180
200,
8281
true,
@@ -97,7 +96,7 @@ class SerializingMetricWriterTest extends DDSpecification {
9796
"GET /api/users/:id",
9897
"web-service",
9998
"http.request",
100-
null,
99+
null,
101100
"web",
102101
200,
103102
false,
@@ -116,7 +115,7 @@ class SerializingMetricWriterTest extends DDSpecification {
116115
"resource" + i,
117116
"service" + i,
118117
"operation" + i,
119-
null,
118+
null,
120119
"type",
121120
0,
122121
false,
@@ -280,6 +279,35 @@ class SerializingMetricWriterTest extends DDSpecification {
280279
}
281280
}
282281

282+
def "ServiceSource optional in the payload"() {
283+
setup:
284+
long startTime = MILLISECONDS.toNanos(System.currentTimeMillis())
285+
long duration = SECONDS.toNanos(10)
286+
WellKnownTags wellKnownTags = new WellKnownTags("runtimeid", "hostname", "env", "service", "version", "language")
287+
288+
// Create keys with different combinations of HTTP fields
289+
def keyWithNoSource = new MetricKey("resource", "service", "operation", null, "type", 200, false, false, "server", [], "GET", "/api/users")
290+
def keyWithSource = new MetricKey("resource", "service", "operation", "source", "type", 200, false, false, "server", [], "POST", null)
291+
292+
def content = [
293+
Pair.of(keyWithNoSource, new AggregateMetric().recordDurations(1, new AtomicLongArray(1L))),
294+
Pair.of(keyWithSource, new AggregateMetric().recordDurations(1, new AtomicLongArray(1L))),
295+
]
296+
297+
ValidatingSink sink = new ValidatingSink(wellKnownTags, startTime, duration, content)
298+
SerializingMetricWriter writer = new SerializingMetricWriter(wellKnownTags, sink, 128)
299+
300+
when:
301+
writer.startBucket(content.size(), startTime, duration)
302+
for (Pair<MetricKey, AggregateMetric> pair : content) {
303+
writer.add(pair.getLeft(), pair.getRight())
304+
}
305+
writer.finishBucket()
306+
307+
then:
308+
sink.validatedInput()
309+
}
310+
283311
def "HTTPMethod and HTTPEndpoint fields are optional in payload"() {
284312
setup:
285313
long startTime = MILLISECONDS.toNanos(System.currentTimeMillis())

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class SimpleSpan implements CoreSpan<SimpleSpan> {
1010
private final String serviceName
1111
private final String operationName
1212
private final CharSequence resourceName
13+
private final CharSequence serviceNameSource
1314
private final String type
1415
private final boolean measured
1516
private final boolean topLevel
@@ -24,22 +25,23 @@ class SimpleSpan implements CoreSpan<SimpleSpan> {
2425
private final Map<Object, Object> tags = [:]
2526

2627
SimpleSpan(
27-
String serviceName,
28-
String operationName,
29-
CharSequence resourceName,
30-
String type,
31-
boolean measured,
32-
boolean topLevel,
33-
boolean error,
34-
long startTime,
35-
long duration,
36-
int statusCode,
37-
boolean traceRoot = false,
38-
int longRunningVersion = 0
39-
) {
28+
String serviceName,
29+
String operationName,
30+
CharSequence resourceName,
31+
String type,
32+
boolean measured,
33+
boolean topLevel,
34+
boolean error,
35+
long startTime,
36+
long duration,
37+
int statusCode,
38+
boolean traceRoot = false,
39+
int longRunningVersion = 0,
40+
CharSequence serviceNameSource = null) {
4041
this.serviceName = serviceName
4142
this.operationName = operationName
4243
this.resourceName = resourceName
44+
this.serviceNameSource = serviceNameSource
4345
this.type = type
4446
this.measured = measured
4547
this.topLevel = topLevel
@@ -61,6 +63,11 @@ class SimpleSpan implements CoreSpan<SimpleSpan> {
6163
return serviceName
6264
}
6365

66+
@Override
67+
CharSequence getServiceNameSource() {
68+
return serviceNameSource
69+
}
70+
6471
@Override
6572
CharSequence getOperationName() {
6673
return operationName

0 commit comments

Comments
 (0)