|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 4 |
|
| 5 | +import com.google.gson.Gson; |
| 6 | +import com.google.gson.GsonBuilder; |
| 7 | +import io.getunleash.util.HistogramBucketSerializer; |
5 | 8 | import java.util.Collections; |
6 | 9 | import java.util.List; |
7 | 10 | import java.util.Map; |
@@ -300,4 +303,60 @@ private NumericMetricSample sample(long value) { |
300 | 303 | private NumericMetricSample sample(Map<String, String> labels, long value) { |
301 | 304 | return new NumericMetricSample(labels, value); |
302 | 305 | } |
| 306 | + |
| 307 | + @Test |
| 308 | + public void should_serialize_histogram_with_positive_infinity_as_plus_inf_string() { |
| 309 | + InMemoryMetricRegistry registry = new InMemoryMetricRegistry(); |
| 310 | + Histogram histogram = |
| 311 | + registry.histogram( |
| 312 | + new BucketMetricOptions( |
| 313 | + "serialization_test", "test serialization", List.of(1.0, 5.0))); |
| 314 | + |
| 315 | + histogram.observe(10.0, Map.of("env", "prod")); |
| 316 | + |
| 317 | + List<CollectedMetric> metrics = registry.collect(); |
| 318 | + assertThat(metrics).hasSize(1); |
| 319 | + |
| 320 | + Gson gson = |
| 321 | + new GsonBuilder() |
| 322 | + .registerTypeAdapter(HistogramBucket.class, new HistogramBucketSerializer()) |
| 323 | + .create(); |
| 324 | + |
| 325 | + String json = gson.toJson(metrics.get(0)); |
| 326 | + |
| 327 | + assertThat(json).contains("\"le\":\"+Inf\""); |
| 328 | + assertThat(json).contains("\"le\":1.0"); |
| 329 | + assertThat(json).contains("\"le\":5.0"); |
| 330 | + } |
| 331 | + |
| 332 | + @Test |
| 333 | + public void should_serialize_histogram_buckets_correctly() { |
| 334 | + InMemoryMetricRegistry registry = new InMemoryMetricRegistry(); |
| 335 | + Histogram histogram = |
| 336 | + registry.histogram( |
| 337 | + new BucketMetricOptions( |
| 338 | + "bucket_serialization", "test buckets", List.of(0.5, 2.0))); |
| 339 | + |
| 340 | + histogram.observe(0.3); |
| 341 | + histogram.observe(1.5); |
| 342 | + histogram.observe(10.0); |
| 343 | + |
| 344 | + List<CollectedMetric> metrics = registry.collect(); |
| 345 | + assertThat(metrics).hasSize(1); |
| 346 | + |
| 347 | + BucketMetricSample sample = (BucketMetricSample) metrics.get(0).getSamples().get(0); |
| 348 | + assertThat(sample.getBuckets()).hasSize(3); |
| 349 | + |
| 350 | + Gson gson = |
| 351 | + new GsonBuilder() |
| 352 | + .registerTypeAdapter(HistogramBucket.class, new HistogramBucketSerializer()) |
| 353 | + .create(); |
| 354 | + |
| 355 | + String json = gson.toJson(sample); |
| 356 | + |
| 357 | + assertThat(json).contains("\"le\":0.5"); |
| 358 | + assertThat(json).contains("\"le\":2.0"); |
| 359 | + assertThat(json).contains("\"le\":\"+Inf\""); |
| 360 | + assertThat(json).contains("\"count\":"); |
| 361 | + } |
303 | 362 | } |
0 commit comments