55import datadog .trace .bootstrap .instrumentation .api .UTF8BytesString ;
66import java .util .Collections ;
77import java .util .List ;
8+ import java .util .Objects ;
89
910/** The aggregation key for tracked metrics. */
1011public final class MetricKey {
1112 private final UTF8BytesString resource ;
1213 private final UTF8BytesString service ;
14+ private final UTF8BytesString serviceSource ;
1315 private final UTF8BytesString operationName ;
1416 private final UTF8BytesString type ;
1517 private final int httpStatusCode ;
@@ -21,35 +23,11 @@ public final class MetricKey {
2123 private final UTF8BytesString httpMethod ;
2224 private final UTF8BytesString httpEndpoint ;
2325
24- // Constructor without httpMethod and httpEndpoint for backward compatibility
25- public MetricKey (
26- CharSequence resource ,
27- CharSequence service ,
28- CharSequence operationName ,
29- CharSequence type ,
30- int httpStatusCode ,
31- boolean synthetics ,
32- boolean isTraceRoot ,
33- CharSequence spanKind ,
34- List <UTF8BytesString > peerTags ) {
35- this (
36- resource ,
37- service ,
38- operationName ,
39- type ,
40- httpStatusCode ,
41- synthetics ,
42- isTraceRoot ,
43- spanKind ,
44- peerTags ,
45- null ,
46- null );
47- }
48-
4926 public MetricKey (
5027 CharSequence resource ,
5128 CharSequence service ,
5229 CharSequence operationName ,
30+ CharSequence serviceSource ,
5331 CharSequence type ,
5432 int httpStatusCode ,
5533 boolean synthetics ,
@@ -60,6 +38,7 @@ public MetricKey(
6038 CharSequence httpEndpoint ) {
6139 this .resource = null == resource ? EMPTY : UTF8BytesString .create (resource );
6240 this .service = null == service ? EMPTY : UTF8BytesString .create (service );
41+ this .serviceSource = null == serviceSource ? null : UTF8BytesString .create (serviceSource );
6342 this .operationName = null == operationName ? EMPTY : UTF8BytesString .create (operationName );
6443 this .type = null == type ? EMPTY : UTF8BytesString .create (type );
6544 this .httpStatusCode = httpStatusCode ;
@@ -79,18 +58,34 @@ public MetricKey(
7958
8059 // Only include httpMethod and httpEndpoint in hash if they are not null
8160 // This ensures backward compatibility when the feature is disabled
82- this .hash =
83- -196_513_505 * Boolean .hashCode (this .isTraceRoot )
84- + -1_807_454_463 * this .spanKind .hashCode ()
85- + 887_503_681 * this .peerTags .hashCode ()
86- + (this .httpMethod != null ? 28_629_151 * this .httpMethod .hashCode () : 0 )
87- + (this .httpEndpoint != null ? 923_521 * this .httpEndpoint .hashCode () : 0 )
88- + 29_791 * this .resource .hashCode ()
89- + 961 * this .service .hashCode ()
90- + 31 * this .operationName .hashCode ()
91- + this .type .hashCode ()
92- + 31 * httpStatusCode
93- + (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 ;
9489 }
9590
9691 public UTF8BytesString getResource () {
@@ -101,6 +96,10 @@ public UTF8BytesString getService() {
10196 return service ;
10297 }
10398
99+ public UTF8BytesString getServiceSource () {
100+ return serviceSource ;
101+ }
102+
104103 public UTF8BytesString getOperationName () {
105104 return operationName ;
106105 }
@@ -144,29 +143,19 @@ public boolean equals(Object o) {
144143 }
145144 if ((o instanceof MetricKey )) {
146145 MetricKey metricKey = (MetricKey ) o ;
147- boolean basicEquals =
148- hash == metricKey .hash
149- && synthetics == metricKey .synthetics
150- && httpStatusCode == metricKey .httpStatusCode
151- && resource .equals (metricKey .resource )
152- && service .equals (metricKey .service )
153- && operationName .equals (metricKey .operationName )
154- && type .equals (metricKey .type )
155- && isTraceRoot == metricKey .isTraceRoot
156- && spanKind .equals (metricKey .spanKind )
157- && peerTags .equals (metricKey .peerTags );
158-
159- // Only compare httpMethod and httpEndpoint if at least one of them is not null
160- // This ensures backward compatibility when the feature is disabled
161- boolean thisHasEndpoint = httpMethod != null || httpEndpoint != null ;
162- boolean otherHasEndpoint = metricKey .httpMethod != null || metricKey .httpEndpoint != null ;
163-
164- if (thisHasEndpoint || otherHasEndpoint ) {
165- return basicEquals
166- && java .util .Objects .equals (httpMethod , metricKey .httpMethod )
167- && java .util .Objects .equals (httpEndpoint , metricKey .httpEndpoint );
168- }
169- 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 );
170159 }
171160 return false ;
172161 }
0 commit comments