-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Add a way to get the last measurement #825
Comments
I'm not sure I follow the exact problem your having. Do you have a sample unit test showing the issue? There are some docs on testing: https://netflix.github.io/spectator/en/latest/intro/testing/ |
I do, though it's internal atm. The gist of it is: Map<Id, Double> expectedMetrics = Map.of(
registry.createId("some.id.1", "bucket", "bucket", "key", expectedKey1, "success", "true"), 1D,
registry.createId("some.id.2", "bucket", "bucket", "key", expectedKey2, "success", "true"), 1D,
registry.createId("some.id.3", "bucket", "bucket", "key", expectedKey3, "success", "true"), 1D,
registry.createId("some.id.4", "bucket", "bucket", "count", "3", "success", "true"), 1D,
registry.createId("some.id.5"), 2D,
registry.createId("some.id.6", "key", expectedKey4, "status", "OK"), 1D);
runTheTest(registry);
Truth.assertThat(buildMap(registry)).containsExactlyEntriesIn(expectedMetrics); Build Map looks like: private static Map<Id, Double> buildMap(Registry registry) {
Map<Id, Double> actualMeasurements = new LinkedHashMap<>();
for (Meter m : registry) {
Measurement last = null;
for (Measurement ms : m.measure()) {
if (last == null || ms.timestamp() > last.timestamp()) {
last = ms;
}
}
actualMeasurements.put(m.id(), Objects.requireNonNull(last).value());
}
return Collections.unmodifiableMap(actualMeasurements);
} The problem with this approach is that's verbose. Calling Providing a |
When unit testing that counters, gauges, and other meters are correctly updated, it's verbose to get the latest numbers. I think it would be useful to provide a way to get the last measurement value on a meter. Currently, I call
Meter.measure
, sort the entries by timestamp, and pick the last one.One way to fix this would be a
lastMeasurement
default method on the interface, that does effectively the same thing. Individual implementations that keep an ordered list of measurements can override it and provide an optimized variant. This would allow me to build an "expected" and "actual" measurement map, fromId
toDouble
. I could compare the two maps to ensure they match in my tests.The text was updated successfully, but these errors were encountered: