Skip to content

Commit 2a4f690

Browse files
authored
Calculate client stats also if the span kind is eligible (#9157)
1 parent 4c1b86a commit 2a4f690

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

communication/src/main/java/datadog/communication/ddagent/DDAgentFeaturesDiscovery.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package datadog.communication.ddagent;
22

33
import static datadog.communication.serialization.msgpack.MsgPackWriter.FIXARRAY;
4-
import static java.util.Collections.emptyList;
4+
import static java.util.Collections.emptySet;
55
import static java.util.Collections.singletonList;
6-
import static java.util.Collections.unmodifiableList;
6+
import static java.util.Collections.unmodifiableSet;
77

88
import com.squareup.moshi.JsonAdapter;
99
import com.squareup.moshi.Moshi;
@@ -88,8 +88,8 @@ public class DDAgentFeaturesDiscovery implements DroppingPolicy {
8888
private volatile String evpProxyEndpoint;
8989
private volatile String version;
9090
private volatile String telemetryProxyEndpoint;
91-
private volatile List<String> peerTags = emptyList();
92-
private volatile List<String> spanKindsToComputedStats = emptyList();
91+
private volatile Set<String> peerTags = emptySet();
92+
private volatile Set<String> spanKindsToComputedStats = emptySet();
9393

9494
private long lastTimeDiscovered;
9595

@@ -123,8 +123,8 @@ private void reset() {
123123
version = null;
124124
lastTimeDiscovered = 0;
125125
telemetryProxyEndpoint = null;
126-
peerTags = emptyList();
127-
spanKindsToComputedStats = emptyList();
126+
peerTags = emptySet();
127+
spanKindsToComputedStats = emptySet();
128128
}
129129

130130
/** Run feature discovery, unconditionally. */
@@ -295,11 +295,16 @@ private boolean processInfoResponse(String response) {
295295
|| Boolean.TRUE.equals(canDrop));
296296

297297
Object peer_tags = map.get("peer_tags");
298-
peerTags = peer_tags == null ? emptyList() : unmodifiableList((List<String>) peer_tags);
298+
peerTags =
299+
peer_tags instanceof List
300+
? unmodifiableSet(new HashSet<>((List<String>) peer_tags))
301+
: emptySet();
299302

300303
Object span_kinds = map.get("span_kinds_stats_computed");
301304
spanKindsToComputedStats =
302-
span_kinds == null ? emptyList() : unmodifiableList((List<String>) span_kinds);
305+
span_kinds instanceof List
306+
? unmodifiableSet(new HashSet<>((List<String>) span_kinds))
307+
: emptySet();
303308
}
304309
try {
305310
state = Strings.sha256(response);
@@ -357,11 +362,11 @@ public boolean supportsLongRunning() {
357362
return supportsLongRunning;
358363
}
359364

360-
public List<String> peerTags() {
365+
public Set<String> peerTags() {
361366
return peerTags;
362367
}
363368

364-
public List<String> spanKindsToComputedStats() {
369+
public Set<String> spanKindsToComputedStats() {
365370
return spanKindsToComputedStats;
366371
}
367372

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static datadog.communication.ddagent.DDAgentFeaturesDiscovery.V6_METRICS_ENDPOINT;
44
import static datadog.trace.api.Functions.UTF8_ENCODE;
5+
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND;
56
import static datadog.trace.common.metrics.AggregateMetric.ERROR_TAG;
67
import static datadog.trace.common.metrics.AggregateMetric.TOP_LEVEL_TAG;
78
import static datadog.trace.common.metrics.SignalItem.ReportSignal.REPORT;
@@ -230,7 +231,14 @@ public boolean publish(List<? extends CoreSpan<?>> trace) {
230231
}
231232

232233
private boolean shouldComputeMetric(CoreSpan<?> span) {
233-
return (span.isMeasured() || span.isTopLevel()) && span.getDurationNano() > 0;
234+
return (span.isMeasured() || span.isTopLevel() || spanKindEligible(span))
235+
&& span.getDurationNano() > 0;
236+
}
237+
238+
private boolean spanKindEligible(CoreSpan<?> span) {
239+
final Object spanKind = span.getTag(SPAN_KIND);
240+
// use toString since it could be a CharSequence...
241+
return spanKind != null && features.spanKindsToComputedStats().contains(spanKind.toString());
234242
}
235243

236244
private boolean publish(CoreSpan<?> span, boolean isTopLevel) {

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import java.util.concurrent.TimeUnit
1313
import java.util.concurrent.TimeoutException
1414
import java.util.function.Supplier
1515

16+
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND
1617
import static java.util.concurrent.TimeUnit.MILLISECONDS
1718
import static java.util.concurrent.TimeUnit.SECONDS
1819

@@ -32,7 +33,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
3233
Sink sink = Mock(Sink)
3334
DDAgentFeaturesDiscovery features = Mock(DDAgentFeaturesDiscovery)
3435
features.supportsMetrics() >> true
35-
WellKnownTags wellKnownTags = new WellKnownTags("runtimeid", "hostname", "env", "service", "version","language")
36+
WellKnownTags wellKnownTags = new WellKnownTags("runtimeid", "hostname", "env", "service", "version", "language")
3637
ConflatingMetricsAggregator aggregator = new ConflatingMetricsAggregator(
3738
wellKnownTags,
3839
empty,
@@ -61,7 +62,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
6162
Sink sink = Mock(Sink)
6263
DDAgentFeaturesDiscovery features = Mock(DDAgentFeaturesDiscovery)
6364
features.supportsMetrics() >> true
64-
WellKnownTags wellKnownTags = new WellKnownTags("runtimeid", "hostname", "env", "service", "version","language")
65+
WellKnownTags wellKnownTags = new WellKnownTags("runtimeid", "hostname", "env", "service", "version", "language")
6566
ConflatingMetricsAggregator aggregator = new ConflatingMetricsAggregator(
6667
wellKnownTags,
6768
[ignoredResourceName].toSet(),
@@ -120,6 +121,44 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
120121
aggregator.close()
121122
}
122123

124+
def "should compute stats for span kind #kind"() {
125+
setup:
126+
MetricWriter writer = Mock(MetricWriter)
127+
Sink sink = Stub(Sink)
128+
DDAgentFeaturesDiscovery features = Mock(DDAgentFeaturesDiscovery)
129+
features.supportsMetrics() >> true
130+
features.spanKindsToComputedStats() >> ["client", "server", "producer", "consumer"]
131+
ConflatingMetricsAggregator aggregator = new ConflatingMetricsAggregator(empty,
132+
features, sink, writer, 10, queueSize, reportingInterval, SECONDS)
133+
aggregator.start()
134+
135+
when:
136+
CountDownLatch latch = new CountDownLatch(1)
137+
def span = Spy(new SimpleSpan("service", "operation", "resource", "type", false, false, false, 0, 100, HTTP_OK))
138+
span.getTag(SPAN_KIND) >> kind
139+
aggregator.publish([span])
140+
aggregator.report()
141+
def latchTriggered = latch.await(2, SECONDS)
142+
143+
then:
144+
latchTriggered == statsComputed
145+
(statsComputed ? 1 : 0) * writer.startBucket(1, _, _)
146+
(statsComputed ? 1 : 0) * writer.add(new MetricKey("resource", "service", "operation", "type", HTTP_OK, false), _) >> { MetricKey key, AggregateMetric value ->
147+
value.getHitCount() == 1 && value.getTopLevelCount() == 0 && value.getDuration() == 100
148+
}
149+
(statsComputed ? 1 : 0) * writer.finishBucket() >> { latch.countDown() }
150+
151+
cleanup:
152+
aggregator.close()
153+
154+
where:
155+
kind | statsComputed
156+
"client" | true
157+
UTF8BytesString.create("server") | true
158+
"internal" | false
159+
null | false
160+
}
161+
123162
def "measured spans do not contribute to top level count"() {
124163
setup:
125164
MetricWriter writer = Mock(MetricWriter)
@@ -472,7 +511,7 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
472511
ConflatingMetricsAggregator aggregator = new ConflatingMetricsAggregator(empty,
473512
features, sink, writer, 10, queueSize, 200, MILLISECONDS)
474513
final spans = [
475-
new SimpleSpan("service" , "operation", "resource", "type", false, true, false, 0, 10, HTTP_OK)
514+
new SimpleSpan("service", "operation", "resource", "type", false, true, false, 0, 10, HTTP_OK)
476515
]
477516
aggregator.start()
478517

0 commit comments

Comments
 (0)