|
5 | 5 | import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString; |
6 | 6 | import java.util.Collections; |
7 | 7 | import java.util.List; |
| 8 | +import java.util.Objects; |
8 | 9 |
|
9 | 10 | /** The aggregation key for tracked metrics. */ |
10 | 11 | public final class MetricKey { |
11 | 12 | private final UTF8BytesString resource; |
12 | 13 | private final UTF8BytesString service; |
13 | | - // note: it won't participate to equals/hashcode since not part of aggregation |
14 | 14 | private final UTF8BytesString serviceSource; |
15 | 15 | private final UTF8BytesString operationName; |
16 | 16 | private final UTF8BytesString type; |
@@ -58,18 +58,34 @@ public MetricKey( |
58 | 58 |
|
59 | 59 | // Only include httpMethod and httpEndpoint in hash if they are not null |
60 | 60 | // This ensures backward compatibility when the feature is disabled |
61 | | - this.hash = |
62 | | - -196_513_505 * Boolean.hashCode(this.isTraceRoot) |
63 | | - + -1_807_454_463 * this.spanKind.hashCode() |
64 | | - + 887_503_681 * this.peerTags.hashCode() |
65 | | - + (this.httpMethod != null ? 28_629_151 * this.httpMethod.hashCode() : 0) |
66 | | - + (this.httpEndpoint != null ? 923_521 * this.httpEndpoint.hashCode() : 0) |
67 | | - + 29_791 * this.resource.hashCode() |
68 | | - + 961 * this.service.hashCode() |
69 | | - + 31 * this.operationName.hashCode() |
70 | | - + this.type.hashCode() |
71 | | - + 31 * httpStatusCode |
72 | | - + (this.synthetics ? 1 : 0); |
| 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 | + } |
| 88 | + this.hash = tmpHash; |
73 | 89 | } |
74 | 90 |
|
75 | 91 | public UTF8BytesString getResource() { |
@@ -127,29 +143,19 @@ public boolean equals(Object o) { |
127 | 143 | } |
128 | 144 | if ((o instanceof MetricKey)) { |
129 | 145 | MetricKey metricKey = (MetricKey) o; |
130 | | - boolean basicEquals = |
131 | | - hash == metricKey.hash |
132 | | - && synthetics == metricKey.synthetics |
133 | | - && httpStatusCode == metricKey.httpStatusCode |
134 | | - && resource.equals(metricKey.resource) |
135 | | - && service.equals(metricKey.service) |
136 | | - && operationName.equals(metricKey.operationName) |
137 | | - && type.equals(metricKey.type) |
138 | | - && isTraceRoot == metricKey.isTraceRoot |
139 | | - && spanKind.equals(metricKey.spanKind) |
140 | | - && peerTags.equals(metricKey.peerTags); |
141 | | - |
142 | | - // Only compare httpMethod and httpEndpoint if at least one of them is not null |
143 | | - // This ensures backward compatibility when the feature is disabled |
144 | | - boolean thisHasEndpoint = httpMethod != null || httpEndpoint != null; |
145 | | - boolean otherHasEndpoint = metricKey.httpMethod != null || metricKey.httpEndpoint != null; |
146 | | - |
147 | | - if (thisHasEndpoint || otherHasEndpoint) { |
148 | | - return basicEquals |
149 | | - && java.util.Objects.equals(httpMethod, metricKey.httpMethod) |
150 | | - && java.util.Objects.equals(httpEndpoint, metricKey.httpEndpoint); |
151 | | - } |
152 | | - return basicEquals; |
| 146 | + return hash == metricKey.hash |
| 147 | + && synthetics == metricKey.synthetics |
| 148 | + && httpStatusCode == metricKey.httpStatusCode |
| 149 | + && resource.equals(metricKey.resource) |
| 150 | + && service.equals(metricKey.service) |
| 151 | + && operationName.equals(metricKey.operationName) |
| 152 | + && type.equals(metricKey.type) |
| 153 | + && isTraceRoot == metricKey.isTraceRoot |
| 154 | + && spanKind.equals(metricKey.spanKind) |
| 155 | + && peerTags.equals(metricKey.peerTags) |
| 156 | + && Objects.equals(serviceSource, metricKey.serviceSource) |
| 157 | + && Objects.equals(httpMethod, metricKey.httpMethod) |
| 158 | + && Objects.equals(httpEndpoint, metricKey.httpEndpoint); |
153 | 159 | } |
154 | 160 | return false; |
155 | 161 | } |
|
0 commit comments