7
7
8
8
package org .elasticsearch .xpack .oteldata .otlp .datapoint ;
9
9
10
+ import io .opentelemetry .proto .metrics .v1 .AggregationTemporality ;
11
+ import io .opentelemetry .proto .metrics .v1 .Histogram ;
10
12
import io .opentelemetry .proto .metrics .v1 .HistogramDataPoint ;
13
+ import io .opentelemetry .proto .metrics .v1 .Metric ;
11
14
12
15
import com .carrotsearch .randomizedtesting .annotations .ParametersFactory ;
13
16
14
17
import org .elasticsearch .test .ESTestCase ;
15
18
16
19
import java .util .ArrayList ;
20
+ import java .util .HashSet ;
17
21
import java .util .List ;
18
22
23
+ import static org .hamcrest .Matchers .equalTo ;
24
+
19
25
public class HistogramConverterTests extends ESTestCase {
20
26
21
27
@ SuppressWarnings ("unused" )
22
28
private final String name ;
23
29
private final HistogramDataPoint dataPoint ;
24
30
private final List <Long > expectedCounts ;
25
31
private final List <Double > expectedValues ;
32
+ private final boolean valid ;
26
33
27
- public HistogramConverterTests (String name , HistogramDataPoint dataPoint , List <Long > expectedCounts , List <Double > expectedValues ) {
34
+ public HistogramConverterTests (
35
+ String name ,
36
+ HistogramDataPoint dataPoint ,
37
+ List <Long > expectedCounts ,
38
+ List <Double > expectedValues ,
39
+ boolean valid
40
+ ) {
28
41
this .name = name ;
29
42
this .dataPoint = dataPoint ;
30
43
this .expectedCounts = expectedCounts ;
31
44
this .expectedValues = expectedValues ;
45
+ this .valid = valid ;
32
46
}
33
47
34
48
public void testHistograms () throws Exception {
49
+ DataPoint .Histogram histogram = new DataPoint .Histogram (
50
+ dataPoint ,
51
+ Metric .newBuilder ()
52
+ .setHistogram (
53
+ Histogram .newBuilder ()
54
+ .setAggregationTemporality (AggregationTemporality .AGGREGATION_TEMPORALITY_DELTA )
55
+ .addDataPoints (dataPoint )
56
+ )
57
+ .build ()
58
+ );
59
+ assertThat (histogram .isValid (new HashSet <>()), equalTo (valid ));
60
+ if (valid == false ) {
61
+ return ;
62
+ }
35
63
List <Long > actualCounts = new ArrayList <>();
36
64
HistogramConverter .counts (dataPoint , actualCounts ::add );
37
65
List <Double > actualValues = new ArrayList <>();
@@ -47,48 +75,56 @@ public void testHistograms() throws Exception {
47
75
@ ParametersFactory (argumentFormatting = "%1$s" )
48
76
public static List <Object []> testCases () {
49
77
return List .of (
50
- new Object [] { "empty" , HistogramDataPoint .newBuilder ().build (), List .of (), List .of () },
78
+ new Object [] { "empty" , HistogramDataPoint .newBuilder ().build (), List .of (), List .of (), true },
51
79
new Object [] {
52
80
"single bucket" ,
53
81
HistogramDataPoint .newBuilder ().addBucketCounts (10L ).addExplicitBounds (5.0 ).build (),
54
82
List .of (10L ),
55
- List .of (2.5 ) },
83
+ List .of (2.5 ),
84
+ true },
85
+ new Object [] { "single count" , HistogramDataPoint .newBuilder ().addBucketCounts (10L ).build (), List .of (10L ), List .of (), false },
56
86
new Object [] {
57
87
"two buckets" ,
58
88
HistogramDataPoint .newBuilder ().addAllBucketCounts (List .of (5L , 10L )).addExplicitBounds (5.0 ).build (),
59
89
List .of (5L , 10L ),
60
- List .of (2.5 , 5.0 ) },
90
+ List .of (2.5 , 5.0 ),
91
+ true },
61
92
new Object [] {
62
93
"three buckets" ,
63
94
HistogramDataPoint .newBuilder ().addAllBucketCounts (List .of (5L , 10L , 15L )).addAllExplicitBounds (List .of (5.0 , 10.0 )).build (),
64
95
List .of (5L , 10L , 15L ),
65
- List .of (2.5 , 7.5 , 10.0 ) },
96
+ List .of (2.5 , 7.5 , 10.0 ),
97
+ true },
66
98
new Object [] {
67
99
"zero count buckets" ,
68
100
HistogramDataPoint .newBuilder ().addAllBucketCounts (List .of (5L , 0L , 15L )).addAllExplicitBounds (List .of (5.0 , 10.0 )).build (),
69
101
List .of (5L , 15L ),
70
- List .of (2.5 , 10.0 ) },
102
+ List .of (2.5 , 10.0 ),
103
+ true },
71
104
new Object [] {
72
105
"negative bounds" ,
73
106
HistogramDataPoint .newBuilder ()
74
107
.addAllBucketCounts (List .of (5L , 10L , 15L ))
75
108
.addAllExplicitBounds (List .of (-10.0 , 10.0 ))
76
109
.build (),
77
110
List .of (5L , 10L , 15L ),
78
- List .of (-10.0 , 0.0 , 10.0 ) },
111
+ List .of (-10.0 , 0.0 , 10.0 ),
112
+ true },
79
113
new Object [] {
80
114
"all negative bounds" ,
81
115
HistogramDataPoint .newBuilder ().addAllBucketCounts (List .of (5L , 10L )).addExplicitBounds (-5.0 ).build (),
82
116
List .of (5L , 10L ),
83
- List .of (-5.0 , -5.0 ) },
117
+ List .of (-5.0 , -5.0 ),
118
+ true },
84
119
new Object [] {
85
120
"multiple buckets with varying distances" ,
86
121
HistogramDataPoint .newBuilder ()
87
122
.addAllBucketCounts (List .of (5L , 10L , 15L , 20L ))
88
123
.addAllExplicitBounds (List .of (1.0 , 5.0 , 20.0 ))
89
124
.build (),
90
125
List .of (5L , 10L , 15L , 20L ),
91
- List .of (0.5 , 3.0 , 12.5 , 20.0 ) }
126
+ List .of (0.5 , 3.0 , 12.5 , 20.0 ),
127
+ true }
92
128
);
93
129
}
94
130
}
0 commit comments