|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.jmxscraper.target_systems; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.assertj.core.api.Assertions.entry; |
| 10 | + |
| 11 | +import io.opentelemetry.proto.common.v1.KeyValue; |
| 12 | +import io.opentelemetry.proto.metrics.v1.Metric; |
| 13 | +import io.opentelemetry.proto.metrics.v1.NumberDataPoint; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.function.Consumer; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import org.assertj.core.api.MapAssert; |
| 20 | + |
| 21 | +/** Metrics assertions */ |
| 22 | +class MetricAssertions { |
| 23 | + |
| 24 | + private MetricAssertions() {} |
| 25 | + |
| 26 | + static void assertGauge(Metric metric, String name, String description, String unit) { |
| 27 | + assertThat(metric.getName()).isEqualTo(name); |
| 28 | + assertThat(metric.getDescription()).isEqualTo(description); |
| 29 | + assertThat(metric.getUnit()).isEqualTo(unit); |
| 30 | + assertThat(metric.hasGauge()).isTrue(); |
| 31 | + assertThat(metric.getGauge().getDataPointsList()) |
| 32 | + .satisfiesExactly(point -> assertThat(point.getAttributesList()).isEmpty()); |
| 33 | + } |
| 34 | + |
| 35 | + static void assertSum(Metric metric, String name, String description, String unit) { |
| 36 | + assertSum(metric, name, description, unit, /* isMonotonic= */ true); |
| 37 | + } |
| 38 | + |
| 39 | + static void assertSum( |
| 40 | + Metric metric, String name, String description, String unit, boolean isMonotonic) { |
| 41 | + assertThat(metric.getName()).isEqualTo(name); |
| 42 | + assertThat(metric.getDescription()).isEqualTo(description); |
| 43 | + assertThat(metric.getUnit()).isEqualTo(unit); |
| 44 | + assertThat(metric.hasSum()).isTrue(); |
| 45 | + assertThat(metric.getSum().getDataPointsList()) |
| 46 | + .satisfiesExactly(point -> assertThat(point.getAttributesList()).isEmpty()); |
| 47 | + assertThat(metric.getSum().getIsMonotonic()).isEqualTo(isMonotonic); |
| 48 | + } |
| 49 | + |
| 50 | + static void assertTypedGauge( |
| 51 | + Metric metric, String name, String description, String unit, List<String> types) { |
| 52 | + assertThat(metric.getName()).isEqualTo(name); |
| 53 | + assertThat(metric.getDescription()).isEqualTo(description); |
| 54 | + assertThat(metric.getUnit()).isEqualTo(unit); |
| 55 | + assertThat(metric.hasGauge()).isTrue(); |
| 56 | + assertTypedPoints(metric.getGauge().getDataPointsList(), types); |
| 57 | + } |
| 58 | + |
| 59 | + static void assertTypedSum( |
| 60 | + Metric metric, String name, String description, String unit, List<String> types) { |
| 61 | + assertThat(metric.getName()).isEqualTo(name); |
| 62 | + assertThat(metric.getDescription()).isEqualTo(description); |
| 63 | + assertThat(metric.getUnit()).isEqualTo(unit); |
| 64 | + assertThat(metric.hasSum()).isTrue(); |
| 65 | + assertTypedPoints(metric.getSum().getDataPointsList(), types); |
| 66 | + } |
| 67 | + |
| 68 | + @SafeVarargs |
| 69 | + static void assertSumWithAttributes( |
| 70 | + Metric metric, |
| 71 | + String name, |
| 72 | + String description, |
| 73 | + String unit, |
| 74 | + Consumer<MapAssert<String, String>>... attributeGroupAssertions) { |
| 75 | + assertThat(metric.getName()).isEqualTo(name); |
| 76 | + assertThat(metric.getDescription()).isEqualTo(description); |
| 77 | + assertThat(metric.getUnit()).isEqualTo(unit); |
| 78 | + assertThat(metric.hasSum()).isTrue(); |
| 79 | + assertAttributedPoints(metric.getSum().getDataPointsList(), attributeGroupAssertions); |
| 80 | + } |
| 81 | + |
| 82 | + @SafeVarargs |
| 83 | + static void assertGaugeWithAttributes( |
| 84 | + Metric metric, |
| 85 | + String name, |
| 86 | + String description, |
| 87 | + String unit, |
| 88 | + Consumer<MapAssert<String, String>>... attributeGroupAssertions) { |
| 89 | + assertThat(metric.getName()).isEqualTo(name); |
| 90 | + assertThat(metric.getDescription()).isEqualTo(description); |
| 91 | + assertThat(metric.getUnit()).isEqualTo(unit); |
| 92 | + assertThat(metric.hasGauge()).isTrue(); |
| 93 | + assertAttributedPoints(metric.getGauge().getDataPointsList(), attributeGroupAssertions); |
| 94 | + } |
| 95 | + |
| 96 | + @SuppressWarnings("unchecked") |
| 97 | + private static void assertTypedPoints(List<NumberDataPoint> points, List<String> types) { |
| 98 | + Consumer<MapAssert<String, String>>[] assertions = |
| 99 | + types.stream() |
| 100 | + .map( |
| 101 | + type -> |
| 102 | + (Consumer<MapAssert<String, String>>) |
| 103 | + attrs -> attrs.containsOnly(entry("name", type))) |
| 104 | + .toArray(Consumer[]::new); |
| 105 | + |
| 106 | + assertAttributedPoints(points, assertions); |
| 107 | + } |
| 108 | + |
| 109 | + @SuppressWarnings("unchecked") |
| 110 | + private static void assertAttributedPoints( |
| 111 | + List<NumberDataPoint> points, |
| 112 | + Consumer<MapAssert<String, String>>... attributeGroupAssertions) { |
| 113 | + Consumer<Map<String, String>>[] assertions = |
| 114 | + Arrays.stream(attributeGroupAssertions) |
| 115 | + .map(assertion -> (Consumer<Map<String, String>>) m -> assertion.accept(assertThat(m))) |
| 116 | + .toArray(Consumer[]::new); |
| 117 | + assertThat(points) |
| 118 | + .extracting( |
| 119 | + numberDataPoint -> |
| 120 | + numberDataPoint.getAttributesList().stream() |
| 121 | + .collect( |
| 122 | + Collectors.toMap( |
| 123 | + KeyValue::getKey, keyValue -> keyValue.getValue().getStringValue()))) |
| 124 | + .satisfiesExactlyInAnyOrder(assertions); |
| 125 | + } |
| 126 | +} |
0 commit comments