Skip to content

Commit 7059a72

Browse files
authored
Don't recompute DSM pathway hash for known tags (#7307)
1 parent facbcfa commit 7059a72

6 files changed

Lines changed: 133 additions & 107 deletions

File tree

dd-trace-core/src/main/java/datadog/trace/core/datastreams/DataStreamContextExtractor.java

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

33
import datadog.trace.api.TraceConfig;
4-
import datadog.trace.api.WellKnownTags;
54
import datadog.trace.api.time.TimeSource;
65
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
76
import datadog.trace.bootstrap.instrumentation.api.TagContext;
@@ -12,17 +11,17 @@ public class DataStreamContextExtractor implements HttpCodec.Extractor {
1211
private final HttpCodec.Extractor delegate;
1312
private final TimeSource timeSource;
1413
private final Supplier<TraceConfig> traceConfigSupplier;
15-
private final WellKnownTags wellKnownTags;
14+
private final long hashOfKnownTags;
1615

1716
public DataStreamContextExtractor(
1817
HttpCodec.Extractor delegate,
1918
TimeSource timeSource,
2019
Supplier<TraceConfig> traceConfigSupplier,
21-
WellKnownTags wellKnownTags) {
20+
long hashOfKnownTags) {
2221
this.delegate = delegate;
2322
this.timeSource = timeSource;
2423
this.traceConfigSupplier = traceConfigSupplier;
25-
this.wellKnownTags = wellKnownTags;
24+
this.hashOfKnownTags = hashOfKnownTags;
2625
}
2726

2827
@Override
@@ -38,15 +37,15 @@ public <C> TagContext extract(C carrier, AgentPropagation.ContextVisitor<C> gett
3837

3938
if (shouldExtractPathwayContext) {
4039
DefaultPathwayContext pathwayContext =
41-
DefaultPathwayContext.extract(carrier, getter, this.timeSource, this.wellKnownTags);
40+
DefaultPathwayContext.extract(carrier, getter, this.timeSource, this.hashOfKnownTags);
4241

4342
extracted.withPathwayContext(pathwayContext);
4443
}
4544

4645
return extracted;
4746
} else if (traceConfigSupplier.get().isDataStreamsEnabled()) {
4847
DefaultPathwayContext pathwayContext =
49-
DefaultPathwayContext.extract(carrier, getter, this.timeSource, this.wellKnownTags);
48+
DefaultPathwayContext.extract(carrier, getter, this.timeSource, this.hashOfKnownTags);
5049

5150
if (pathwayContext != null) {
5251
extracted = new TagContext();

dd-trace-core/src/main/java/datadog/trace/core/datastreams/DefaultDataStreamsMonitoring.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class DefaultDataStreamsMonitoring implements DataStreamsMonitoring, Even
6565
private final DatastreamsPayloadWriter payloadWriter;
6666
private final DDAgentFeaturesDiscovery features;
6767
private final TimeSource timeSource;
68-
private final WellKnownTags wellKnownTags;
68+
private final long hashOfKnownTags;
6969
private final Supplier<TraceConfig> traceConfigSupplier;
7070
private final long bucketDurationNanos;
7171
private final DataStreamContextInjector injector;
@@ -124,7 +124,7 @@ public DefaultDataStreamsMonitoring(
124124
this.features = features;
125125
this.timeSource = timeSource;
126126
this.traceConfigSupplier = traceConfigSupplier;
127-
this.wellKnownTags = wellKnownTags;
127+
this.hashOfKnownTags = DefaultPathwayContext.getBaseHash(wellKnownTags);
128128
this.payloadWriter = payloadWriter;
129129
this.bucketDurationNanos = bucketDurationNanos;
130130
this.injector = new DataStreamContextInjector(this);
@@ -189,15 +189,16 @@ public void setProduceCheckpoint(String type, String target) {
189189
@Override
190190
public PathwayContext newPathwayContext() {
191191
if (configSupportsDataStreams) {
192-
return new DefaultPathwayContext(timeSource, wellKnownTags);
192+
return new DefaultPathwayContext(timeSource, hashOfKnownTags);
193193
} else {
194194
return AgentTracer.NoopPathwayContext.INSTANCE;
195195
}
196196
}
197197

198198
@Override
199199
public HttpCodec.Extractor extractor(HttpCodec.Extractor delegate) {
200-
return new DataStreamContextExtractor(delegate, timeSource, traceConfigSupplier, wellKnownTags);
200+
return new DataStreamContextExtractor(
201+
delegate, timeSource, traceConfigSupplier, hashOfKnownTags);
201202
}
202203

203204
@Override
@@ -213,7 +214,7 @@ public void mergePathwayContextIntoSpan(AgentSpan span, DataStreamsContextCarrie
213214
carrier,
214215
DataStreamsContextCarrierAdapter.INSTANCE,
215216
this.timeSource,
216-
this.wellKnownTags);
217+
this.hashOfKnownTags);
217218
((DDSpan) span).context().mergePathwayContext(pathwayContext);
218219
}
219220
}

dd-trace-core/src/main/java/datadog/trace/core/datastreams/DefaultPathwayContext.java

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
public class DefaultPathwayContext implements PathwayContext {
3434
private static final Logger log = LoggerFactory.getLogger(DefaultPathwayContext.class);
3535
private final Lock lock = new ReentrantLock();
36-
private final WellKnownTags wellKnownTags;
36+
private final long hashOfKnownTags;
3737
private final TimeSource timeSource;
3838
private final GrowingByteArrayOutput outputBuffer =
3939
GrowingByteArrayOutput.withInitialCapacity(20);
@@ -68,20 +68,19 @@ public class DefaultPathwayContext implements PathwayContext {
6868
TagsProcessor.DATASET_NAMESPACE_TAG,
6969
TagsProcessor.MANUAL_TAG));
7070

71-
public DefaultPathwayContext(TimeSource timeSource, WellKnownTags wellKnownTags) {
71+
public DefaultPathwayContext(TimeSource timeSource, long hashOfKnownTags) {
7272
this.timeSource = timeSource;
73-
this.wellKnownTags = wellKnownTags;
73+
this.hashOfKnownTags = hashOfKnownTags;
7474
}
7575

7676
private DefaultPathwayContext(
7777
TimeSource timeSource,
78-
WellKnownTags wellKnownTags,
78+
long hashOfKnownTags,
7979
long pathwayStartNanos,
8080
long pathwayStartNanoTicks,
8181
long edgeStartNanoTicks,
8282
long hash) {
83-
this.timeSource = timeSource;
84-
this.wellKnownTags = wellKnownTags;
83+
this(timeSource, hashOfKnownTags);
8584
this.pathwayStartNanos = pathwayStartNanos;
8685
this.pathwayStartNanoTicks = pathwayStartNanoTicks;
8786
this.edgeStartNanoTicks = edgeStartNanoTicks;
@@ -127,7 +126,7 @@ public void setCheckpoint(
127126
// So far, each tag key has only one tag value, so we're initializing the capacity to match
128127
// the number of tag keys for now. We should revisit this later if it's no longer the case.
129128
List<String> allTags = new ArrayList<>(sortedTags.size());
130-
PathwayHashBuilder pathwayHashBuilder = new PathwayHashBuilder(wellKnownTags);
129+
PathwayHashBuilder pathwayHashBuilder = new PathwayHashBuilder(hashOfKnownTags);
131130
DataSetHashBuilder aggregationHashBuilder = new DataSetHashBuilder();
132131

133132
if (!started) {
@@ -272,19 +271,19 @@ public String toString() {
272271

273272
private static class PathwayContextExtractor implements AgentPropagation.KeyClassifier {
274273
private final TimeSource timeSource;
275-
private final WellKnownTags wellKnownTags;
274+
private final long hashOfKnownTags;
276275
private DefaultPathwayContext extractedContext;
277276

278-
PathwayContextExtractor(TimeSource timeSource, WellKnownTags wellKnownTags) {
277+
PathwayContextExtractor(TimeSource timeSource, long hashOfKnownTags) {
279278
this.timeSource = timeSource;
280-
this.wellKnownTags = wellKnownTags;
279+
this.hashOfKnownTags = hashOfKnownTags;
281280
}
282281

283282
@Override
284283
public boolean accept(String key, String value) {
285284
if (PathwayContext.PROPAGATION_KEY_BASE64.equalsIgnoreCase(key)) {
286285
try {
287-
extractedContext = strDecode(timeSource, wellKnownTags, value);
286+
extractedContext = strDecode(timeSource, hashOfKnownTags, value);
288287
} catch (IOException e) {
289288
return false;
290289
}
@@ -296,28 +295,28 @@ public boolean accept(String key, String value) {
296295
private static class BinaryPathwayContextExtractor
297296
implements AgentPropagation.BinaryKeyClassifier {
298297
private final TimeSource timeSource;
299-
private final WellKnownTags wellKnownTags;
298+
private final long hashOfKnownTags;
300299
private DefaultPathwayContext extractedContext;
301300

302-
BinaryPathwayContextExtractor(TimeSource timeSource, WellKnownTags wellKnownTags) {
301+
BinaryPathwayContextExtractor(TimeSource timeSource, long hashOfKnownTags) {
303302
this.timeSource = timeSource;
304-
this.wellKnownTags = wellKnownTags;
303+
this.hashOfKnownTags = hashOfKnownTags;
305304
}
306305

307306
@Override
308307
public boolean accept(String key, byte[] value) {
309308
// older versions support, should be removed in the future
310309
if (PathwayContext.PROPAGATION_KEY.equalsIgnoreCase(key)) {
311310
try {
312-
extractedContext = decode(timeSource, wellKnownTags, value);
311+
extractedContext = decode(timeSource, hashOfKnownTags, value);
313312
} catch (IOException e) {
314313
return false;
315314
}
316315
}
317316

318317
if (PathwayContext.PROPAGATION_KEY_BASE64.equalsIgnoreCase(key)) {
319318
try {
320-
extractedContext = base64Decode(timeSource, wellKnownTags, value);
319+
extractedContext = base64Decode(timeSource, hashOfKnownTags, value);
321320
} catch (IOException e) {
322321
return false;
323322
}
@@ -330,13 +329,13 @@ static <C> DefaultPathwayContext extract(
330329
C carrier,
331330
AgentPropagation.ContextVisitor<C> getter,
332331
TimeSource timeSource,
333-
WellKnownTags wellKnownTags) {
332+
long hashOfKnownTags) {
334333
if (getter instanceof AgentPropagation.BinaryContextVisitor) {
335334
return extractBinary(
336-
carrier, (AgentPropagation.BinaryContextVisitor) getter, timeSource, wellKnownTags);
335+
carrier, (AgentPropagation.BinaryContextVisitor) getter, timeSource, hashOfKnownTags);
337336
}
338337
PathwayContextExtractor pathwayContextExtractor =
339-
new PathwayContextExtractor(timeSource, wellKnownTags);
338+
new PathwayContextExtractor(timeSource, hashOfKnownTags);
340339
getter.forEachKey(carrier, pathwayContextExtractor);
341340
if (pathwayContextExtractor.extractedContext == null) {
342341
log.debug("No context extracted");
@@ -350,9 +349,9 @@ static <C> DefaultPathwayContext extractBinary(
350349
C carrier,
351350
AgentPropagation.BinaryContextVisitor<C> getter,
352351
TimeSource timeSource,
353-
WellKnownTags wellKnownTags) {
352+
long hashOfKnownTags) {
354353
BinaryPathwayContextExtractor pathwayContextExtractor =
355-
new BinaryPathwayContextExtractor(timeSource, wellKnownTags);
354+
new BinaryPathwayContextExtractor(timeSource, hashOfKnownTags);
356355
getter.forEachKey(carrier, pathwayContextExtractor);
357356
if (pathwayContextExtractor.extractedContext == null) {
358357
log.debug("No context extracted");
@@ -363,18 +362,18 @@ static <C> DefaultPathwayContext extractBinary(
363362
}
364363

365364
private static DefaultPathwayContext strDecode(
366-
TimeSource timeSource, WellKnownTags wellKnownTags, String data) throws IOException {
365+
TimeSource timeSource, long hashOfKnownTags, String data) throws IOException {
367366
byte[] base64Bytes = data.getBytes(UTF_8);
368-
return base64Decode(timeSource, wellKnownTags, base64Bytes);
367+
return base64Decode(timeSource, hashOfKnownTags, base64Bytes);
369368
}
370369

371370
private static DefaultPathwayContext base64Decode(
372-
TimeSource timeSource, WellKnownTags wellKnownTags, byte[] data) throws IOException {
373-
return decode(timeSource, wellKnownTags, Base64.getDecoder().decode(data));
371+
TimeSource timeSource, long hashOfKnownTags, byte[] data) throws IOException {
372+
return decode(timeSource, hashOfKnownTags, Base64.getDecoder().decode(data));
374373
}
375374

376375
private static DefaultPathwayContext decode(
377-
TimeSource timeSource, WellKnownTags wellKnownTags, byte[] data) throws IOException {
376+
TimeSource timeSource, long hashOfKnownTags, byte[] data) throws IOException {
378377
ByteArrayInput input = ByteArrayInput.wrap(data);
379378

380379
long hash = input.readLongLE();
@@ -394,7 +393,7 @@ private static DefaultPathwayContext decode(
394393

395394
return new DefaultPathwayContext(
396395
timeSource,
397-
wellKnownTags,
396+
hashOfKnownTags,
398397
pathwayStartNanos,
399398
pathwayStartNanoTicks,
400399
edgeStartNanoTicks,
@@ -411,35 +410,35 @@ public long addValue(String val) {
411410
}
412411

413412
private static class PathwayHashBuilder {
414-
private final StringBuilder builder;
415-
416-
public PathwayHashBuilder(WellKnownTags wellKnownTags) {
417-
builder = new StringBuilder();
418-
builder.append(wellKnownTags.getService());
419-
builder.append(wellKnownTags.getEnv());
413+
private long hash;
420414

421-
String primaryTag = Config.get().getPrimaryTag();
422-
if (primaryTag != null) {
423-
builder.append(primaryTag);
424-
}
415+
public PathwayHashBuilder(long baseHash) {
416+
hash = baseHash;
425417
}
426418

427419
public void addTag(String tag) {
428-
builder.append(tag);
420+
hash = FNV64Hash.continueHash(hash, tag, FNV64Hash.Version.v1);
429421
}
430422

431-
public long generateHash() {
432-
return FNV64Hash.generateHash(builder.toString(), FNV64Hash.Version.v1);
423+
public long getHash() {
424+
return hash;
433425
}
426+
}
434427

435-
@Override
436-
public String toString() {
437-
return builder.toString();
428+
public static long getBaseHash(WellKnownTags wellKnownTags) {
429+
StringBuilder builder = new StringBuilder();
430+
builder.append(wellKnownTags.getService());
431+
builder.append(wellKnownTags.getEnv());
432+
433+
String primaryTag = Config.get().getPrimaryTag();
434+
if (primaryTag != null) {
435+
builder.append(primaryTag);
438436
}
437+
return FNV64Hash.generateHash(builder.toString(), FNV64Hash.Version.v1);
439438
}
440439

441440
private long generateNodeHash(PathwayHashBuilder pathwayHashBuilder) {
442-
return pathwayHashBuilder.generateHash();
441+
return pathwayHashBuilder.getHash();
443442
}
444443

445444
private long generatePathwayHash(long nodeHash, long parentHash) {

0 commit comments

Comments
 (0)