28
28
import org .apache .commons .lang3 .StringUtils ;
29
29
30
30
import java .util .*;
31
+ import java .util .stream .IntStream ;
31
32
32
33
@ Data
33
34
@ Slf4j
@@ -40,6 +41,7 @@ public class MetricDefinition implements Cloneable {
40
41
private Boolean isMonotonic ;
41
42
private Boolean isDouble ;
42
43
private List <Double > quantiles ;
44
+ private List <Double > bounds ;
43
45
private String valueFunction ;
44
46
private Integer payloadFrequencySeconds ;
45
47
private Integer payloadCount ;
@@ -93,14 +95,15 @@ private void validateOTelType() {
93
95
throw new GeneratorException ("Invalid OTeltype '" + otelType + "' found for metric " + name + " ." +
94
96
"Valid types are " + StringUtils .join (Constants .validMetricTypes ));
95
97
}
96
- validateAggregationTemporalityForSum ();
98
+ validateAggregationTemporality ();
97
99
}
98
100
99
- private void validateAggregationTemporalityForSum () {
100
- if (otelType .equalsIgnoreCase (Constants .SUM )) {
101
+ private void validateAggregationTemporality () {
102
+ if (otelType .equals (Constants .SUM ) || otelType .equals (Constants .HISTOGRAM ) ||
103
+ otelType .equals (Constants .EXP_HISTOGRAM )) {
101
104
if (aggregationTemporality ==null || aggregationTemporality .isBlank ()) {
102
- throw new GeneratorException ("OTel type for metric " + name + " is of 'sum ' type but Aggregation " +
103
- "temporality not provided" );
105
+ throw new GeneratorException ("OTel type for metric " + name + " is of '" + otelType + " ' type but " +
106
+ "Aggregation temporality not provided" );
104
107
}
105
108
//Check aggregation temporality is valid
106
109
if (!(aggregationTemporality .equalsIgnoreCase (Constants .CUMULATIVE ) ||
@@ -109,11 +112,48 @@ private void validateAggregationTemporalityForSum() {
109
112
" specified for metric " + name + ". Valid types are (Cumulative, Delta)" );
110
113
}
111
114
}
115
+ validateQuantiles ();
116
+ }
117
+
118
+ private void validateQuantiles () {
119
+ if (otelType .equals (Constants .SUMMARY )) {
120
+ if (quantiles == null || quantiles .isEmpty ()) {
121
+ throw new GeneratorException ("OTel metric " + name + " of type summary does not have any quantiles " +
122
+ "specified." );
123
+ }
124
+ }
125
+ validateBounds ();
126
+ }
127
+
128
+ private void validateBounds () {
129
+ if (otelType .equals (Constants .HISTOGRAM )) {
130
+ if (bounds == null || bounds .isEmpty ()) {
131
+ throw new GeneratorException ("OTel metric " + name + " of type histogram does not have any bounds " +
132
+ "specified." );
133
+ }
134
+ if (bounds .size () > 1 && IntStream .range (0 , bounds .size ()-1 )
135
+ .anyMatch (idx -> bounds .get (idx ) >= bounds .get (idx + 1 ))) {
136
+ throw new GeneratorException ("Invalid bounds provided for metric " + name + ". Bound values must be " +
137
+ "in strictly ascending order" );
138
+ }
139
+ }
112
140
validateAttributes ();
113
141
}
114
142
115
143
private void validateAttributes () {
116
144
attributes = GeneratorUtils .validateAttributes (attributes );
145
+ checkValueFunction ();
146
+ }
147
+
148
+ private void checkValueFunction () {
149
+ if ((otelType .equals (Constants .SUMMARY ) || otelType .equals (Constants .HISTOGRAM ) ||
150
+ otelType .equals (Constants .EXP_HISTOGRAM )) && (!valueFunction .contains ("Summary" ))) {
151
+ log .warn ("Metric " + name + " should be using a summary variant of the value expression" );
152
+ }
153
+ if ((otelType .equals (Constants .SUM ) || otelType .equals (Constants .GAUGE )) &&
154
+ (valueFunction .contains ("Summary" ))) {
155
+ log .warn ("Metric " + name + " should not be using the summary variant of the value expression" );
156
+ }
117
157
}
118
158
119
159
private void validateResourceTypes (Set <String > allResourceTypes ) {
0 commit comments