Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 9b82dc5

Browse files
authored
Stats: Only include the simple class name in the error message for ViewData.check methods. (#1267)
* Stats: Only include the simple class name in the error message for ViewData.check methods. * Copy checkArgument and lazily create error message. * Rename help methods to avoid InconsistentOverloads. * Improve error messages in the unit tests.
1 parent d68f786 commit 9b82dc5

File tree

2 files changed

+57
-39
lines changed

2 files changed

+57
-39
lines changed

api/src/main/java/io/opencensus/stats/ViewData.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import io.opencensus.common.Function;
2222
import io.opencensus.common.Functions;
2323
import io.opencensus.common.Timestamp;
24-
import io.opencensus.internal.Utils;
2524
import io.opencensus.stats.Aggregation.Count;
2625
import io.opencensus.stats.Aggregation.Distribution;
2726
import io.opencensus.stats.Aggregation.LastValue;
@@ -210,31 +209,36 @@ private static void checkWindow(
210209
new Function<View.AggregationWindow.Cumulative, Void>() {
211210
@Override
212211
public Void apply(View.AggregationWindow.Cumulative arg) {
213-
Utils.checkArgument(
214-
windowData instanceof AggregationWindowData.CumulativeData,
215-
createErrorMessageForWindow(arg, windowData));
212+
throwIfWindowMismatch(
213+
windowData instanceof AggregationWindowData.CumulativeData, arg, windowData);
216214
return null;
217215
}
218216
},
219217
new Function<View.AggregationWindow.Interval, Void>() {
220218
@Override
221219
public Void apply(View.AggregationWindow.Interval arg) {
222-
Utils.checkArgument(
223-
windowData instanceof AggregationWindowData.IntervalData,
224-
createErrorMessageForWindow(arg, windowData));
220+
throwIfWindowMismatch(
221+
windowData instanceof AggregationWindowData.IntervalData, arg, windowData);
225222
return null;
226223
}
227224
},
228225
Functions.</*@Nullable*/ Void>throwAssertionError());
229226
}
230227

228+
private static void throwIfWindowMismatch(
229+
boolean isValid, View.AggregationWindow window, AggregationWindowData windowData) {
230+
if (!isValid) {
231+
throw new IllegalArgumentException(createErrorMessageForWindow(window, windowData));
232+
}
233+
}
234+
231235
private static String createErrorMessageForWindow(
232236
View.AggregationWindow window, AggregationWindowData windowData) {
233237
return "AggregationWindow and AggregationWindowData types mismatch. "
234238
+ "AggregationWindow: "
235-
+ window
239+
+ window.getClass().getSimpleName()
236240
+ " AggregationWindowData: "
237-
+ windowData;
241+
+ windowData.getClass().getSimpleName();
238242
}
239243

240244
private static void checkAggregation(
@@ -247,18 +251,16 @@ public Void apply(Sum arg) {
247251
new Function<MeasureDouble, Void>() {
248252
@Override
249253
public Void apply(MeasureDouble arg) {
250-
Utils.checkArgument(
251-
aggregationData instanceof SumDataDouble,
252-
createErrorMessageForAggregation(aggregation, aggregationData));
254+
throwIfAggregationMismatch(
255+
aggregationData instanceof SumDataDouble, aggregation, aggregationData);
253256
return null;
254257
}
255258
},
256259
new Function<MeasureLong, Void>() {
257260
@Override
258261
public Void apply(MeasureLong arg) {
259-
Utils.checkArgument(
260-
aggregationData instanceof SumDataLong,
261-
createErrorMessageForAggregation(aggregation, aggregationData));
262+
throwIfAggregationMismatch(
263+
aggregationData instanceof SumDataLong, aggregation, aggregationData);
262264
return null;
263265
}
264266
},
@@ -269,18 +271,16 @@ public Void apply(MeasureLong arg) {
269271
new Function<Count, Void>() {
270272
@Override
271273
public Void apply(Count arg) {
272-
Utils.checkArgument(
273-
aggregationData instanceof CountData,
274-
createErrorMessageForAggregation(aggregation, aggregationData));
274+
throwIfAggregationMismatch(
275+
aggregationData instanceof CountData, aggregation, aggregationData);
275276
return null;
276277
}
277278
},
278279
new Function<Distribution, Void>() {
279280
@Override
280281
public Void apply(Distribution arg) {
281-
Utils.checkArgument(
282-
aggregationData instanceof DistributionData,
283-
createErrorMessageForAggregation(aggregation, aggregationData));
282+
throwIfAggregationMismatch(
283+
aggregationData instanceof DistributionData, aggregation, aggregationData);
284284
return null;
285285
}
286286
},
@@ -291,18 +291,18 @@ public Void apply(LastValue arg) {
291291
new Function<MeasureDouble, Void>() {
292292
@Override
293293
public Void apply(MeasureDouble arg) {
294-
Utils.checkArgument(
294+
throwIfAggregationMismatch(
295295
aggregationData instanceof LastValueDataDouble,
296-
createErrorMessageForAggregation(aggregation, aggregationData));
296+
aggregation,
297+
aggregationData);
297298
return null;
298299
}
299300
},
300301
new Function<MeasureLong, Void>() {
301302
@Override
302303
public Void apply(MeasureLong arg) {
303-
Utils.checkArgument(
304-
aggregationData instanceof LastValueDataLong,
305-
createErrorMessageForAggregation(aggregation, aggregationData));
304+
throwIfAggregationMismatch(
305+
aggregationData instanceof LastValueDataLong, aggregation, aggregationData);
306306
return null;
307307
}
308308
},
@@ -317,23 +317,32 @@ public Void apply(Aggregation arg) {
317317
// we need to continue supporting Mean, since it could still be used by users and some
318318
// deprecated RPC views.
319319
if (arg instanceof Aggregation.Mean) {
320-
Utils.checkArgument(
320+
throwIfAggregationMismatch(
321321
aggregationData instanceof AggregationData.MeanData,
322-
createErrorMessageForAggregation(aggregation, aggregationData));
322+
aggregation,
323+
aggregationData);
323324
return null;
324325
}
325326
throw new AssertionError();
326327
}
327328
});
328329
}
329330

331+
private static void throwIfAggregationMismatch(
332+
boolean isValid, Aggregation aggregation, AggregationData aggregationData) {
333+
if (!isValid) {
334+
throw new IllegalArgumentException(
335+
createErrorMessageForAggregation(aggregation, aggregationData));
336+
}
337+
}
338+
330339
private static String createErrorMessageForAggregation(
331340
Aggregation aggregation, AggregationData aggregationData) {
332341
return "Aggregation and AggregationData types mismatch. "
333342
+ "Aggregation: "
334-
+ aggregation
343+
+ aggregation.getClass().getSimpleName()
335344
+ " AggregationData: "
336-
+ aggregationData;
345+
+ aggregationData.getClass().getSimpleName();
337346
}
338347

339348
/**

api/src/test/java/io/opencensus/stats/ViewDataTest.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,19 @@ public Void apply(IntervalData windowData) {
160160

161161
@Test
162162
public void preventWindowAndAggregationWindowDataMismatch() {
163+
CumulativeData cumulativeData =
164+
CumulativeData.create(Timestamp.fromMillis(1000), Timestamp.fromMillis(2000));
163165
thrown.expect(IllegalArgumentException.class);
164-
thrown.expectMessage("AggregationWindow and AggregationWindowData types mismatch. ");
166+
thrown.expectMessage(
167+
"AggregationWindow and AggregationWindowData types mismatch. "
168+
+ "AggregationWindow: "
169+
+ INTERVAL_HOUR.getClass().getSimpleName()
170+
+ " AggregationWindowData: "
171+
+ cumulativeData.getClass().getSimpleName());
165172
ViewData.create(
166173
View.create(NAME, DESCRIPTION, MEASURE_DOUBLE, DISTRIBUTION, TAG_KEYS, INTERVAL_HOUR),
167174
ENTRIES,
168-
CumulativeData.create(Timestamp.fromMillis(1000), Timestamp.fromMillis(2000)));
175+
cumulativeData);
169176
}
170177

171178
@Test
@@ -213,12 +220,7 @@ public void preventAggregationAndAggregationDataMismatch_Mean_Distribution() {
213220
@Test
214221
public void preventAggregationAndAggregationDataMismatch_Distribution_Count() {
215222
aggregationAndAggregationDataMismatch(
216-
createView(DISTRIBUTION),
217-
ImmutableMap.of(
218-
Arrays.asList(V1, V2),
219-
DistributionData.create(1, 1, 1, 1, 0, Arrays.asList(0L, 1L, 0L)),
220-
Arrays.asList(V10, V20),
221-
CountData.create(100)));
223+
createView(DISTRIBUTION), ImmutableMap.of(Arrays.asList(V10, V20), CountData.create(100)));
222224
}
223225

224226
@Test
@@ -249,8 +251,15 @@ private void aggregationAndAggregationDataMismatch(
249251
View view, Map<List<TagValue>, ? extends AggregationData> entries) {
250252
CumulativeData cumulativeData =
251253
CumulativeData.create(Timestamp.fromMillis(1000), Timestamp.fromMillis(2000));
254+
Aggregation aggregation = view.getAggregation();
255+
AggregationData aggregationData = entries.values().iterator().next();
252256
thrown.expect(IllegalArgumentException.class);
253-
thrown.expectMessage("Aggregation and AggregationData types mismatch. ");
257+
thrown.expectMessage(
258+
"Aggregation and AggregationData types mismatch. "
259+
+ "Aggregation: "
260+
+ aggregation.getClass().getSimpleName()
261+
+ " AggregationData: "
262+
+ aggregationData.getClass().getSimpleName());
254263
ViewData.create(view, entries, cumulativeData);
255264
}
256265

0 commit comments

Comments
 (0)