Skip to content

Commit adc5c80

Browse files
amarzialidevflow.devflow-routing-intake
andauthored
Use hashingutils for MetricsKey's hash (#10677)
Use hashingutils for MetricsKey's hash Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>
1 parent 76ba4c7 commit adc5c80

1 file changed

Lines changed: 14 additions & 36 deletions

File tree

  • dd-trace-core/src/main/java/datadog/trace/common/metrics

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

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static datadog.trace.bootstrap.instrumentation.api.UTF8BytesString.EMPTY;
44

55
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
6+
import datadog.trace.util.HashingUtils;
67
import java.util.Collections;
78
import java.util.List;
89
import java.util.Objects;
@@ -49,42 +50,19 @@ public MetricKey(
4950
this.httpMethod = httpMethod == null ? null : UTF8BytesString.create(httpMethod);
5051
this.httpEndpoint = httpEndpoint == null ? null : UTF8BytesString.create(httpEndpoint);
5152

52-
// Unrolled polynomial hashcode to avoid varargs allocation
53-
// and eliminate data dependency between iterations as in Arrays.hashCode.
54-
// Coefficient constants are powers of 31, with integer overflow (hence negative numbers).
55-
// See
56-
// https://richardstartin.github.io/posts/collecting-rocks-and-benchmarks
57-
// https://richardstartin.github.io/posts/still-true-in-java-9-handwritten-hash-codes-are-faster
58-
59-
// Only include httpMethod and httpEndpoint in hash if they are not null
60-
// This ensures backward compatibility when the feature is disabled
61-
62-
// Note: all the multiplication got constant folded at compile time (see javap -verbose ...)
63-
int tmpHash =
64-
(int) (31L * 31 * 31 * 31 * 31 * 31 * 31 * 31) * Boolean.hashCode(this.isTraceRoot) // 8
65-
+ (int) (31L * 31 * 31 * 31 * 31 * 31 * 31) * this.spanKind.hashCode() // 7
66-
+ 31 * 31 * 31 * 31 * 31 * 31 * this.peerTags.hashCode() // 6
67-
+ 31 * 31 * 31 * 31 * 31 * this.resource.hashCode() // 5
68-
+ 31 * 31 * 31 * 31 * this.service.hashCode() // 4
69-
+ 31 * 31 * 31 * this.operationName.hashCode() // 3
70-
+ 31 * 31 * this.type.hashCode() // 2
71-
+ 31 * this.httpStatusCode // 1
72-
+ (this.synthetics ? 1 : 0); // 0
73-
// optional fields
74-
if (this.serviceSource != null) {
75-
tmpHash +=
76-
(int) (31L * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31) * this.serviceSource.hashCode(); // 9
77-
}
78-
if (this.httpEndpoint != null) {
79-
tmpHash +=
80-
(int) (31L * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31)
81-
* this.httpEndpoint.hashCode(); // 10
82-
}
83-
if (this.httpMethod != null) {
84-
tmpHash +=
85-
(int) (31L * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31)
86-
* this.httpMethod.hashCode(); // 11
87-
}
53+
int tmpHash = 0;
54+
tmpHash = HashingUtils.addToHash(tmpHash, this.isTraceRoot);
55+
tmpHash = HashingUtils.addToHash(tmpHash, this.spanKind);
56+
tmpHash = HashingUtils.addToHash(tmpHash, this.peerTags);
57+
tmpHash = HashingUtils.addToHash(tmpHash, this.resource);
58+
tmpHash = HashingUtils.addToHash(tmpHash, this.service);
59+
tmpHash = HashingUtils.addToHash(tmpHash, this.operationName);
60+
tmpHash = HashingUtils.addToHash(tmpHash, this.type);
61+
tmpHash = HashingUtils.addToHash(tmpHash, this.httpStatusCode);
62+
tmpHash = HashingUtils.addToHash(tmpHash, this.synthetics);
63+
tmpHash = HashingUtils.addToHash(tmpHash, this.serviceSource);
64+
tmpHash = HashingUtils.addToHash(tmpHash, this.httpEndpoint);
65+
tmpHash = HashingUtils.addToHash(tmpHash, this.httpMethod);
8866
this.hash = tmpHash;
8967
}
9068

0 commit comments

Comments
 (0)