3333public 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