3
3
import static datadog .trace .bootstrap .instrumentation .api .UTF8BytesString .EMPTY ;
4
4
5
5
import datadog .trace .bootstrap .instrumentation .api .UTF8BytesString ;
6
+ import java .util .Arrays ;
7
+ import java .util .List ;
6
8
7
9
/** The aggregation key for tracked metrics. */
8
10
public final class MetricKey {
@@ -13,25 +15,45 @@ public final class MetricKey {
13
15
private final int httpStatusCode ;
14
16
private final boolean synthetics ;
15
17
private final int hash ;
18
+ private final boolean isTraceRoot ;
19
+ private final UTF8BytesString spanKind ;
20
+ private final UTF8BytesString [] peerTags ;
16
21
17
22
public MetricKey (
18
23
CharSequence resource ,
19
24
CharSequence service ,
20
25
CharSequence operationName ,
21
26
CharSequence type ,
22
27
int httpStatusCode ,
23
- boolean synthetics ) {
28
+ boolean synthetics ,
29
+ boolean isTraceRoot ,
30
+ CharSequence spanKind ,
31
+ List <CharSequence > peerTags ) {
24
32
this .resource = null == resource ? EMPTY : UTF8BytesString .create (resource );
25
33
this .service = null == service ? EMPTY : UTF8BytesString .create (service );
26
34
this .operationName = null == operationName ? EMPTY : UTF8BytesString .create (operationName );
27
35
this .type = null == type ? EMPTY : UTF8BytesString .create (type );
28
36
this .httpStatusCode = httpStatusCode ;
29
37
this .synthetics = synthetics ;
30
- // unrolled polynomial hashcode which avoids allocating varargs
31
- // the constants are 31^5, 31^4, 31^3, 31^2, 31^1, 31^0
38
+ this .isTraceRoot = isTraceRoot ;
39
+ this .spanKind = null == spanKind ? EMPTY : UTF8BytesString .create (spanKind );
40
+ this .peerTags = new UTF8BytesString [peerTags .size ()];
41
+ for (int i = 0 ; i < peerTags .size (); i ++) {
42
+ this .peerTags [i ] = UTF8BytesString .create (peerTags .get (i ));
43
+ }
44
+
45
+ // Unrolled polynomial hashcode to avoid varargs allocation
46
+ // and eliminate data dependency between iterations as in Arrays.hashCode.
47
+ // Coefficient constants are powers of 31, with integer overflow (hence negative numbers).
48
+ // See
49
+ // * https://richardstartin.github.io/posts/collecting-rocks-and-benchmarks
50
+ // * https://richardstartin.github.io/posts/still-true-in-java-9-handwritten-hash-codes-are-faster
32
51
this .hash =
33
- 28629151 * this .resource .hashCode ()
34
- + 923521 * this .service .hashCode ()
52
+ -196513505 * Boolean .hashCode (this .isTraceRoot )
53
+ + -1807454463 * this .spanKind .hashCode ()
54
+ + 887_503_681 * Arrays .hashCode (this .peerTags )
55
+ + 28_629_151 * this .resource .hashCode ()
56
+ + 923_521 * this .service .hashCode ()
35
57
+ 29791 * this .operationName .hashCode ()
36
58
+ 961 * this .type .hashCode ()
37
59
+ 31 * httpStatusCode
@@ -62,6 +84,18 @@ public boolean isSynthetics() {
62
84
return synthetics ;
63
85
}
64
86
87
+ public boolean isTraceRoot () {
88
+ return isTraceRoot ;
89
+ }
90
+
91
+ public UTF8BytesString getSpanKind () {
92
+ return spanKind ;
93
+ }
94
+
95
+ public UTF8BytesString [] getPeerTags () {
96
+ return peerTags ;
97
+ }
98
+
65
99
@ Override
66
100
public boolean equals (Object o ) {
67
101
if (this == o ) {
@@ -75,7 +109,10 @@ public boolean equals(Object o) {
75
109
&& resource .equals (metricKey .resource )
76
110
&& service .equals (metricKey .service )
77
111
&& operationName .equals (metricKey .operationName )
78
- && type .equals (metricKey .type );
112
+ && type .equals (metricKey .type )
113
+ && isTraceRoot == metricKey .isTraceRoot
114
+ && spanKind .equals (metricKey .spanKind )
115
+ && Arrays .equals (peerTags , metricKey .peerTags );
79
116
}
80
117
return false ;
81
118
}
0 commit comments