-
Notifications
You must be signed in to change notification settings - Fork 313
Add peer tags, span kind and trace root flag to MetricKey bucket #9178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8d53b58
chore(css): Add peer tags, span kind and trace root flag to MetricKey…
bric3 a4614de
chore(css): Use a map to create the metric key
bric3 908548d
Improves CSS peer tag aggregation (#9336)
amarziali f565188
Hardcode eligible span kind since agent backpropagated are deprecated
amarziali ce2279b
revisit peer tags aggregation rules according to the rfc
amarziali 81d8a79
IsTraceRoot is a tristate
amarziali 34da211
Don't confuse trace root with top levels
amarziali bcf4d04
Fix build after rebase
amarziali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...-core/src/jmh/java/datadog/trace/common/metrics/ConflatingMetricsAggregatorBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package datadog.trace.common.metrics; | ||
|
||
import static java.util.concurrent.TimeUnit.MICROSECONDS; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
||
import datadog.communication.ddagent.DDAgentFeaturesDiscovery; | ||
import datadog.communication.monitor.Monitoring; | ||
import datadog.trace.api.WellKnownTags; | ||
import datadog.trace.core.CoreSpan; | ||
import datadog.trace.core.monitor.HealthMetrics; | ||
import datadog.trace.util.Strings; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
@State(Scope.Benchmark) | ||
@Warmup(iterations = 1, time = 30, timeUnit = SECONDS) | ||
@Measurement(iterations = 3, time = 30, timeUnit = SECONDS) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(MICROSECONDS) | ||
@Fork(value = 1) | ||
public class ConflatingMetricsAggregatorBenchmark { | ||
private final DDAgentFeaturesDiscovery featuresDiscovery = | ||
new FixedAgentFeaturesDiscovery( | ||
Collections.singleton("peer.hostname"), Collections.emptySet()); | ||
private final ConflatingMetricsAggregator aggregator = | ||
new ConflatingMetricsAggregator( | ||
new WellKnownTags("", "", "", "", "", ""), | ||
Collections.emptySet(), | ||
featuresDiscovery, | ||
HealthMetrics.NO_OP, | ||
new NullSink(), | ||
2048, | ||
2048); | ||
private final List<CoreSpan<?>> spans = generateTrace(64); | ||
|
||
static List<CoreSpan<?>> generateTrace(int len) { | ||
final List<CoreSpan<?>> trace = new ArrayList<>(); | ||
for (int i = 0; i < len; i++) { | ||
SimpleSpan span = new SimpleSpan("", "", "", "", true, true, false, 0, 10, -1); | ||
span.setTag("peer.hostname", Strings.random(10)); | ||
trace.add(span); | ||
} | ||
return trace; | ||
} | ||
|
||
static class NullSink implements Sink { | ||
|
||
@Override | ||
public void register(EventListener listener) {} | ||
|
||
@Override | ||
public void accept(int messageCount, ByteBuffer buffer) {} | ||
} | ||
|
||
static class FixedAgentFeaturesDiscovery extends DDAgentFeaturesDiscovery { | ||
private final Set<String> peerTags; | ||
private final Set<String> spanKinds; | ||
|
||
public FixedAgentFeaturesDiscovery(Set<String> peerTags, Set<String> spanKinds) { | ||
// create a fixed discovery with metrics enabled | ||
super(null, Monitoring.DISABLED, null, false, true); | ||
this.peerTags = peerTags; | ||
this.spanKinds = spanKinds; | ||
} | ||
|
||
@Override | ||
public void discover() { | ||
// do nothing | ||
} | ||
|
||
@Override | ||
public boolean supportsMetrics() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Set<String> peerTags() { | ||
return peerTags; | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void benchmark(Blackhole blackhole) { | ||
blackhole.consume(aggregator.publish(spans)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
package datadog.trace.common.metrics; | ||
|
||
import static datadog.communication.ddagent.DDAgentFeaturesDiscovery.V6_METRICS_ENDPOINT; | ||
import static datadog.trace.api.DDTags.BASE_SERVICE; | ||
import static datadog.trace.api.Functions.UTF8_ENCODE; | ||
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND; | ||
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_CLIENT; | ||
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_CONSUMER; | ||
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_INTERNAL; | ||
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_PRODUCER; | ||
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_SERVER; | ||
import static datadog.trace.common.metrics.AggregateMetric.ERROR_TAG; | ||
import static datadog.trace.common.metrics.AggregateMetric.TOP_LEVEL_TAG; | ||
import static datadog.trace.common.metrics.SignalItem.ReportSignal.REPORT; | ||
import static datadog.trace.common.metrics.SignalItem.StopSignal.STOP; | ||
import static datadog.trace.util.AgentThreadFactory.AgentThread.METRICS_AGGREGATOR; | ||
import static datadog.trace.util.AgentThreadFactory.THREAD_JOIN_TIMOUT_MS; | ||
import static datadog.trace.util.AgentThreadFactory.newAgentThread; | ||
import static java.util.Collections.unmodifiableSet; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
||
import datadog.communication.ddagent.DDAgentFeaturesDiscovery; | ||
import datadog.communication.ddagent.SharedCommunicationObjects; | ||
import datadog.trace.api.Config; | ||
import datadog.trace.api.Pair; | ||
import datadog.trace.api.WellKnownTags; | ||
import datadog.trace.api.cache.DDCache; | ||
import datadog.trace.api.cache.DDCaches; | ||
|
@@ -25,14 +33,18 @@ | |
import datadog.trace.core.DDTraceCoreInfo; | ||
import datadog.trace.core.monitor.HealthMetrics; | ||
import datadog.trace.util.AgentTaskScheduler; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Queue; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Function; | ||
import org.jctools.maps.NonBlockingHashMap; | ||
import org.jctools.queues.MpscCompoundQueue; | ||
import org.jctools.queues.SpmcArrayQueue; | ||
|
@@ -49,8 +61,32 @@ public final class ConflatingMetricsAggregator implements MetricsAggregator, Eve | |
private static final DDCache<String, UTF8BytesString> SERVICE_NAMES = | ||
DDCaches.newFixedSizeCache(32); | ||
|
||
private static final DDCache<CharSequence, UTF8BytesString> SPAN_KINDS = | ||
DDCaches.newFixedSizeCache(16); | ||
private static final DDCache< | ||
String, Pair<DDCache<String, UTF8BytesString>, Function<String, UTF8BytesString>>> | ||
PEER_TAGS_CACHE = | ||
DDCaches.newFixedSizeCache( | ||
64); // it can be unbounded since those values are returned by the agent and should be | ||
// under control. 64 entries is enough in this case to contain all the peer tags. | ||
private static final Function< | ||
String, Pair<DDCache<String, UTF8BytesString>, Function<String, UTF8BytesString>>> | ||
PEER_TAGS_CACHE_ADDER = | ||
key -> | ||
Pair.of( | ||
DDCaches.newFixedSizeCache(512), | ||
value -> UTF8BytesString.create(key + ":" + value)); | ||
private static final CharSequence SYNTHETICS_ORIGIN = "synthetics"; | ||
|
||
private static final Set<String> ELIGIBLE_SPAN_KINDS_FOR_METRICS = | ||
unmodifiableSet( | ||
new HashSet<>( | ||
Arrays.asList( | ||
SPAN_KIND_SERVER, SPAN_KIND_CLIENT, SPAN_KIND_CONSUMER, SPAN_KIND_PRODUCER))); | ||
|
||
private static final Set<String> ELIGIBLE_SPAN_KINDS_FOR_PEER_AGGREGATION = | ||
unmodifiableSet(new HashSet<>(Arrays.asList(SPAN_KIND_CLIENT, SPAN_KIND_PRODUCER))); | ||
|
||
private final Set<String> ignoredResources; | ||
private final Queue<Batch> batchPool; | ||
private final NonBlockingHashMap<MetricKey, Batch> pending; | ||
|
@@ -262,18 +298,23 @@ private boolean shouldComputeMetric(CoreSpan<?> span) { | |
private boolean spanKindEligible(CoreSpan<?> span) { | ||
final Object spanKind = span.getTag(SPAN_KIND); | ||
// use toString since it could be a CharSequence... | ||
return spanKind != null && features.spanKindsToComputedStats().contains(spanKind.toString()); | ||
return spanKind != null && ELIGIBLE_SPAN_KINDS_FOR_METRICS.contains(spanKind.toString()); | ||
} | ||
|
||
private boolean publish(CoreSpan<?> span, boolean isTopLevel) { | ||
final CharSequence spanKind = span.getTag(SPAN_KIND, ""); | ||
MetricKey newKey = | ||
new MetricKey( | ||
span.getResourceName(), | ||
SERVICE_NAMES.computeIfAbsent(span.getServiceName(), UTF8_ENCODE), | ||
span.getOperationName(), | ||
span.getType(), | ||
span.getHttpStatusCode(), | ||
isSynthetic(span)); | ||
isSynthetic(span), | ||
span.getParentId() == 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Shouldn't we use |
||
SPAN_KINDS.computeIfAbsent( | ||
spanKind, UTF8BytesString::create), // save repeated utf8 conversions | ||
getPeerTags(span, spanKind.toString())); | ||
boolean isNewKey = false; | ||
MetricKey key = keys.putIfAbsent(newKey, newKey); | ||
if (null == key) { | ||
|
@@ -288,7 +329,7 @@ private boolean publish(CoreSpan<?> span, boolean isTopLevel) { | |
// returning false means that either the batch can't take any | ||
// more data, or it has already been consumed | ||
if (batch.add(tag, durationNanos)) { | ||
// added to a pending batch prior to consumption | ||
// added to a pending batch prior to consumption, | ||
// so skip publishing to the queue (we also know | ||
// the key isn't rare enough to override the sampler) | ||
return false; | ||
|
@@ -308,6 +349,34 @@ private boolean publish(CoreSpan<?> span, boolean isTopLevel) { | |
return isNewKey || span.getError() > 0; | ||
} | ||
|
||
private List<UTF8BytesString> getPeerTags(CoreSpan<?> span, String spanKind) { | ||
if (ELIGIBLE_SPAN_KINDS_FOR_PEER_AGGREGATION.contains(spanKind)) { | ||
List<UTF8BytesString> peerTags = new ArrayList<>(); | ||
for (String peerTag : features.peerTags()) { | ||
Object value = span.getTag(peerTag); | ||
if (value != null) { | ||
final Pair<DDCache<String, UTF8BytesString>, Function<String, UTF8BytesString>> | ||
cacheAndCreator = PEER_TAGS_CACHE.computeIfAbsent(peerTag, PEER_TAGS_CACHE_ADDER); | ||
peerTags.add( | ||
cacheAndCreator | ||
.getLeft() | ||
.computeIfAbsent(value.toString(), cacheAndCreator.getRight())); | ||
} | ||
} | ||
return peerTags; | ||
} else if (SPAN_KIND_INTERNAL.equals(spanKind)) { | ||
// in this case only the base service should be aggregated if present | ||
final String baseService = span.getTag(BASE_SERVICE); | ||
if (baseService != null) { | ||
final Pair<DDCache<String, UTF8BytesString>, Function<String, UTF8BytesString>> | ||
cacheAndCreator = PEER_TAGS_CACHE.computeIfAbsent(BASE_SERVICE, PEER_TAGS_CACHE_ADDER); | ||
return Collections.singletonList( | ||
cacheAndCreator.getLeft().computeIfAbsent(baseService, cacheAndCreator.getRight())); | ||
} | ||
} | ||
return Collections.emptyList(); | ||
} | ||
|
||
private static boolean isSynthetic(CoreSpan<?> span) { | ||
return span.getOrigin() != null && SYNTHETICS_ORIGIN.equals(span.getOrigin().toString()); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 suggestion: Can't we make a dedicated type for the cache and creator rather than using a
Pair<DDCache<String, UTF8BytesString>, Function<String, UTF8BytesString>>
? It feels hard to read.Because it always end up calling
cache.computeIfAbsent(key, creator)
, so the wholePair<...>
thing can be simplified as a functional interface like:Function<String, UTF8BytesString>
.