1
1
package io .prometheus .client .exemplars .api ;
2
2
3
+ import java .util .Arrays ;
4
+
3
5
/**
4
6
* Immutable data class holding an Exemplar.
5
7
*/
6
8
public class Exemplar {
7
9
8
- private final String traceId ;
9
- private final String spanId ;
10
+ public static final String TRACE_ID = "trace_id" ;
11
+ public static final String SPAN_ID = "span_id" ;
12
+
13
+ private final String [] labels ;
10
14
private final double value ;
11
15
private final long timestampMs ;
12
16
13
- public Exemplar (String traceId , String spanId , double value , long timestampMs ) {
14
- if (traceId == null ) {
15
- throw new NullPointerException ("traceId" );
16
- }
17
- if (spanId == null ) {
18
- throw new NullPointerException ("spanId" );
19
- }
20
- this .traceId = traceId ;
21
- this .spanId = spanId ;
17
+ /**
18
+ * Create an Exemplar without a timestamp
19
+ *
20
+ * @param value the observed value
21
+ * @param labels name/value pairs. Expecting an even number of strings. The combined length of the label names and
22
+ * values must not exceed 128 UTF-8 characters. Neither a label name nor a label value may be null.
23
+ */
24
+ public Exemplar (double value , String ... labels ) {
25
+ this (value , 0 , labels );
26
+ }
27
+
28
+ /**
29
+ * Create an Exemplar
30
+ *
31
+ * @param value the observed value
32
+ * @param timestampMs as in {@link System#currentTimeMillis()}
33
+ * @param labels name/value pairs. Expecting an even number of strings. The combined length of the
34
+ * label names and values must not exceed 128 UTF-8 characters. Neither a label name
35
+ * nor a label value may be null.
36
+ */
37
+ public Exemplar (double value , long timestampMs , String ... labels ) {
38
+ validateLabels (labels );
39
+ this .labels = Arrays .copyOf (labels , labels .length );
22
40
this .value = value ;
23
41
this .timestampMs = timestampMs ;
24
42
}
25
43
26
- public Exemplar ( String traceId , String spanId , double value ) {
27
- this ( traceId , spanId , value , System . currentTimeMillis ()) ;
44
+ public int getNumberOfLabels ( ) {
45
+ return labels . length / 2 ;
28
46
}
29
47
30
- public String getTraceId ( ) {
31
- return traceId ;
48
+ public String getLabelName ( int i ) {
49
+ return labels [ 2 * i ] ;
32
50
}
33
51
34
- public String getSpanId ( ) {
35
- return spanId ;
52
+ public String getLabelValue ( int i ) {
53
+ return labels [ 2 * i + 1 ] ;
36
54
}
37
55
38
56
public double getValue () {
39
57
return value ;
40
58
}
41
59
60
+ /**
61
+ * @return 0 means no timestamp.
62
+ */
42
63
public long getTimestampMs () {
43
64
return timestampMs ;
44
65
}
45
66
67
+ private void validateLabels (String ... labels ) {
68
+ if (labels .length % 2 != 0 ) {
69
+ throw new IllegalArgumentException ("labels are name/value pairs, expecting an even number" );
70
+ }
71
+ int charsTotal = 0 ;
72
+ for (int i = 0 ; i < labels .length ; i ++) {
73
+ if (labels [i ] == null ) {
74
+ throw new IllegalArgumentException ("labels[" + i + "] is null" );
75
+ }
76
+ charsTotal += labels [i ].length ();
77
+ }
78
+ if (charsTotal > 128 ) {
79
+ throw new IllegalArgumentException (
80
+ "the combined length of the label names and values must not exceed 128 UTF-8 characters" );
81
+ }
82
+ }
83
+
46
84
@ Override
47
85
public boolean equals (Object obj ) {
48
86
if (this == obj ) {
@@ -52,16 +90,14 @@ public boolean equals(Object obj) {
52
90
return false ;
53
91
}
54
92
Exemplar other = (Exemplar ) obj ;
55
- return traceId .equals (other .traceId ) &&
56
- spanId .equals (other .spanId ) &&
93
+ return Arrays .equals (this .labels , other .labels ) &&
57
94
Double .compare (other .value , value ) == 0 &&
58
95
timestampMs == other .timestampMs ;
59
96
}
60
97
61
98
@ Override
62
99
public int hashCode () {
63
- int hash = traceId .hashCode ();
64
- hash = 37 * hash + spanId .hashCode ();
100
+ int hash = Arrays .hashCode (labels );
65
101
long d = Double .doubleToLongBits (value );
66
102
hash = 37 * hash + (int ) (d ^ (d >>> 32 ));
67
103
hash = 37 * hash + (int ) timestampMs ;
0 commit comments