-
Notifications
You must be signed in to change notification settings - Fork 337
Memory-efficiency pass on ClientStatsAggregator + adversarial benchmark #11389
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
Draft
dougqh
wants to merge
37
commits into
dougqh/control-tag-cardinality
Choose a base branch
from
dougqh/metrics-memory-efficiency
base: dougqh/control-tag-cardinality
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
d5af760
Fold AggregateMetric into AggregateEntry
dougqh fea1476
Store peer tags as UTF8BytesString[] instead of List on AggregateEntry
dougqh 4d29128
Add miss-path JMH benchmark for ClientStatsAggregator
dougqh 8cf1bf1
Back cardinality handlers with flat open-addressed tables
dougqh ae91c66
Lazy-allocate the error latency histogram on AggregateEntry
dougqh a8cae22
Raise per-field cardinality limits to match real workloads
dougqh 890075b
Add end-to-end JMH benchmark for the trace pipeline
dougqh cf24265
Add adversarial JMH benchmark targeting the metrics subsystem
dougqh 823a5d4
Document adversarial-case behavior in client metrics design doc
dougqh dd3eeef
Merge dougqh/control-tag-cardinality into dougqh/metrics-memory-effic…
dougqh b1358f8
Add high-level Overview section to client-metrics design doc
dougqh 17a442f
Notify on peer-tag cardinality blocks
dougqh dc2b7f2
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh 3a71d09
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh bce086d
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh 04e0e18
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh 4d05fae
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh aa1cfa4
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh d42c706
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh f11072b
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh d35e91f
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh badd36b
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh e8d543a
Adapt reconcileSwapsSchemaWhenTagSetChanges to UTF8BytesString[] peer…
dougqh 45d9689
Merge remote-tracking branch 'origin/dougqh/control-tag-cardinality' …
dougqh 3cd7a9a
Merge remote-tracking branch 'origin/dougqh/control-tag-cardinality' …
dougqh 441a9ce
Merge remote-tracking branch 'origin/dougqh/control-tag-cardinality' …
dougqh 588f274
Merge remote-tracking branch 'origin/dougqh/control-tag-cardinality' …
dougqh 96276d9
Merge remote-tracking branch 'origin/dougqh/control-tag-cardinality' …
dougqh 4d683dc
Merge remote-tracking branch 'origin/dougqh/control-tag-cardinality' …
dougqh dc5c0da
Merge remote-tracking branch 'origin/dougqh/control-tag-cardinality' …
dougqh b965401
Merge remote-tracking branch 'origin/dougqh/control-tag-cardinality' …
dougqh 48a8970
Merge remote-tracking branch 'origin/dougqh/control-tag-cardinality' …
dougqh 556ad9d
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh c9d5e4b
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh eb04f80
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh 9b071c3
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh d3095aa
Merge branch 'dougqh/control-tag-cardinality' into dougqh/metrics-mem…
dougqh 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
83 changes: 83 additions & 0 deletions
83
...ore/src/jmh/java/datadog/trace/common/metrics/ClientStatsAggregatorMissPathBenchmark.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,83 @@ | ||
| package datadog.trace.common.metrics; | ||
|
|
||
| import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND; | ||
| import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_CLIENT; | ||
| import static java.util.concurrent.TimeUnit.MICROSECONDS; | ||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
|
||
| import datadog.communication.ddagent.DDAgentFeaturesDiscovery; | ||
| import datadog.trace.api.WellKnownTags; | ||
| import datadog.trace.core.CoreSpan; | ||
| import datadog.trace.core.monitor.HealthMetrics; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| 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; | ||
|
|
||
| /** | ||
| * Miss-path variant of {@link ClientStatsAggregatorBenchmark}. Each op publishes a single-span | ||
| * trace from a pre-built pool where every span has a unique (service, operation, resource) tuple. | ||
| * After cardinality budgets fill, fields canonicalize to the {@code blocked_by_tracer} sentinel, | ||
| * but the producer still allocates a {@link SpanSnapshot} per op and enqueues it for the aggregator | ||
| * -- so the steady state exercises the per-op publish allocations + the consumer's | ||
| * canonicalize/match work, not the hit-path-only pattern of the other benchmarks. | ||
| * | ||
| * <p>Run with {@code -prof gc} to compare allocation rates against master's {@code | ||
| * ConflatingMetricsAggregator}. | ||
| */ | ||
| @State(Scope.Benchmark) | ||
| @Warmup(iterations = 1, time = 15, timeUnit = SECONDS) | ||
| @Measurement(iterations = 3, time = 15, timeUnit = SECONDS) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(MICROSECONDS) | ||
| @Fork(value = 1) | ||
| public class ClientStatsAggregatorMissPathBenchmark { | ||
|
|
||
| private static final int POOL_SIZE = 4096; | ||
|
|
||
| private final DDAgentFeaturesDiscovery featuresDiscovery = | ||
| new ClientStatsAggregatorBenchmark.FixedAgentFeaturesDiscovery( | ||
| Collections.singleton("peer.hostname"), Collections.emptySet()); | ||
| private final ClientStatsAggregator aggregator = | ||
| new ClientStatsAggregator( | ||
| new WellKnownTags("", "", "", "", "", ""), | ||
| Collections.emptySet(), | ||
| featuresDiscovery, | ||
| HealthMetrics.NO_OP, | ||
| new ClientStatsAggregatorBenchmark.NullSink(), | ||
| 2048, | ||
| 2048, | ||
| false); | ||
|
|
||
| private final List<List<CoreSpan<?>>> pool = generatePool(POOL_SIZE); | ||
| private int cursor; | ||
|
|
||
| static List<List<CoreSpan<?>>> generatePool(int n) { | ||
| List<List<CoreSpan<?>>> out = new ArrayList<>(n); | ||
| for (int i = 0; i < n; i++) { | ||
| SimpleSpan span = | ||
| new SimpleSpan( | ||
| "svc-" + i, "op-" + i, "res-" + i, "type-" + (i & 7), true, true, false, 0, 10, -1); | ||
| span.setTag(SPAN_KIND, SPAN_KIND_CLIENT); | ||
| span.setTag("peer.hostname", "host-" + i); | ||
| out.add(Collections.singletonList(span)); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void benchmark(Blackhole blackhole) { | ||
| int idx = cursor; | ||
| cursor = (idx + 1) % POOL_SIZE; | ||
| blackhole.consume(aggregator.publish(pool.get(idx))); | ||
| } | ||
| } |
176 changes: 176 additions & 0 deletions
176
dd-trace-core/src/jmh/java/datadog/trace/common/metrics/TracePipelineBenchmark.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,176 @@ | ||
| package datadog.trace.common.metrics; | ||
|
|
||
| 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_SERVER; | ||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
|
||
| import datadog.trace.api.WellKnownTags; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.common.writer.Writer; | ||
| import datadog.trace.core.CoreTracer; | ||
| import datadog.trace.core.DDSpan; | ||
| import datadog.trace.core.monitor.HealthMetrics; | ||
| import java.lang.reflect.Field; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| 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.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.TearDown; | ||
| import org.openjdk.jmh.annotations.Threads; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| /** | ||
| * End-to-end JMH benchmark of a 3-span HTTP-style trace through {@link CoreTracer}: one {@code | ||
| * span.kind=server} root + two {@code span.kind=client} children, as if a service handled an | ||
| * incoming request that made two outbound HTTP calls. Children inherit the server span as parent | ||
| * via implicit scope-based parentage; the root finishes last so {@code PendingTrace.write} -> | ||
| * {@code tracer.write(trace)} -> metricsAggregator.publish + writer.write (no-op) runs | ||
| * synchronously on the producing thread. | ||
| * | ||
| * <p>Runs multi-threaded ({@link Threads} = 8 by default; override with {@code -t N}) so the | ||
| * allocation rate {@code -prof gc} reports reflects multiple producers hitting the shared metrics | ||
| * aggregator + writer pipeline, and so we can compare total throughput between revisions. | ||
| * | ||
| * <p>Reflection is used to swap the tracer's default no-op {@code metricsAggregator} for a real | ||
| * {@link ClientStatsAggregator} so the metrics pipeline actually runs. | ||
| * | ||
| * <p>Two modes via {@code @Param}: | ||
| * | ||
| * <ul> | ||
| * <li>{@code stable} -- every op uses the same labels (cache-hit path on the consumer). | ||
| * <li>{@code varied} -- every op uses unique service / operation / resource per span (miss path | ||
| * until cardinality budgets fill, then sentinel collapse). | ||
| * </ul> | ||
| */ | ||
| @State(Scope.Benchmark) | ||
| @Warmup(iterations = 2, time = 15, timeUnit = SECONDS) | ||
| @Measurement(iterations = 5, time = 15, timeUnit = SECONDS) | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @OutputTimeUnit(SECONDS) | ||
| @Threads(8) | ||
| @Fork(value = 2) | ||
| public class TracePipelineBenchmark { | ||
|
|
||
| @Param({"stable", "varied"}) | ||
| String mode; | ||
|
|
||
| private CoreTracer tracer; | ||
| private ClientStatsAggregator aggregator; | ||
| private boolean stable; | ||
|
|
||
| @State(Scope.Thread) | ||
| public static class ThreadState { | ||
| int cursor; | ||
| } | ||
|
|
||
| @Setup | ||
| public void setup() throws Exception { | ||
| this.stable = "stable".equals(mode); | ||
| this.tracer = CoreTracer.builder().writer(new NoopWriter()).strictTraceWrites(false).build(); | ||
| this.aggregator = | ||
| new ClientStatsAggregator( | ||
| new WellKnownTags("", "", "", "", "", ""), | ||
| Collections.emptySet(), | ||
| new ClientStatsAggregatorBenchmark.FixedAgentFeaturesDiscovery( | ||
| Collections.singleton("peer.hostname"), Collections.emptySet()), | ||
| HealthMetrics.NO_OP, | ||
| new ClientStatsAggregatorBenchmark.NullSink(), | ||
| 2048, | ||
| 2048, | ||
| false); | ||
| this.aggregator.start(); | ||
| // Replace the no-op aggregator the tracer was constructed with. The field is package-private | ||
| // in datadog.trace.core; reflect since this benchmark lives in the metrics package. | ||
| Field f = CoreTracer.class.getDeclaredField("metricsAggregator"); | ||
| f.setAccessible(true); | ||
| f.set(this.tracer, this.aggregator); | ||
| } | ||
|
|
||
| @TearDown | ||
| public void tearDown() { | ||
| aggregator.close(); | ||
| tracer.close(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void threeSpanTrace(ThreadState ts, Blackhole blackhole) { | ||
| int idx = ts.cursor++; | ||
| String service = stable ? "svc" : "svc-" + idx; | ||
| String serverOp = stable ? "servlet.request" : "servlet.request-" + idx; | ||
| String serverResource = stable ? "GET /widgets/{id}" : "GET /widgets/" + idx; | ||
| String clientOp = stable ? "http.request" : "http.request-" + idx; | ||
| String clientResource1 = stable ? "GET /downstream-a" : "GET /downstream-a/" + idx; | ||
| String clientResource2 = stable ? "GET /downstream-b" : "GET /downstream-b/" + idx; | ||
| String hostA = stable ? "host-a" : "host-a-" + idx; | ||
| String hostB = stable ? "host-b" : "host-b-" + idx; | ||
|
|
||
| AgentSpan server = tracer.startSpan("servlet", serverOp); | ||
| server.setResourceName(serverResource); | ||
| server.setServiceName(service); | ||
| server.setTag(SPAN_KIND, SPAN_KIND_SERVER); | ||
| AgentScope serverScope = tracer.activateSpan(server); | ||
| try { | ||
| AgentSpan client1 = tracer.startSpan("okhttp", clientOp); | ||
| client1.setResourceName(clientResource1); | ||
| client1.setServiceName(service); | ||
| client1.setTag(SPAN_KIND, SPAN_KIND_CLIENT); | ||
| client1.setTag("peer.hostname", hostA); | ||
| AgentScope client1Scope = tracer.activateSpan(client1); | ||
| try { | ||
| // simulated unit of in-call work would go here | ||
| } finally { | ||
| client1Scope.close(); | ||
| } | ||
| client1.finish(); | ||
|
|
||
| AgentSpan client2 = tracer.startSpan("okhttp", clientOp); | ||
| client2.setResourceName(clientResource2); | ||
| client2.setServiceName(service); | ||
| client2.setTag(SPAN_KIND, SPAN_KIND_CLIENT); | ||
| client2.setTag("peer.hostname", hostB); | ||
| AgentScope client2Scope = tracer.activateSpan(client2); | ||
| try { | ||
| // simulated unit of in-call work would go here | ||
| } finally { | ||
| client2Scope.close(); | ||
| } | ||
| client2.finish(); | ||
| } finally { | ||
| serverScope.close(); | ||
| } | ||
| // Finishing the root last triggers PendingTrace.write -> tracer.write -> metrics + writer on | ||
| // this thread, since all child refs have already decremented to zero. | ||
| server.finish(); | ||
| blackhole.consume(server); | ||
| } | ||
|
|
||
| private static final class NoopWriter implements Writer { | ||
| @Override | ||
| public void write(List<DDSpan> trace) {} | ||
|
|
||
| @Override | ||
| public void start() {} | ||
|
|
||
| @Override | ||
| public boolean flush() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() {} | ||
|
|
||
| @Override | ||
| public void incrementDropCounts(int spanCount) {} | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
I'd prefer not to use reflection. Can we expose a way to set the metrics aggregator through the CoreTracer builder instead?