|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.alibaba.fluss.metrics.opentelemetry; |
| 19 | + |
| 20 | +/* This file is based on source code of Apache Flink Project (https://flink.apache.org/), licensed by the Apache |
| 21 | + * Software Foundation (ASF) under the Apache License, Version 2.0. See the NOTICE file distributed with this work for |
| 22 | + * additional information regarding copyright ownership. */ |
| 23 | + |
| 24 | +import com.alibaba.fluss.metrics.Counter; |
| 25 | +import com.alibaba.fluss.metrics.MeterView; |
| 26 | +import com.alibaba.fluss.metrics.SimpleCounter; |
| 27 | +import com.alibaba.fluss.metrics.util.TestHistogram; |
| 28 | +import com.alibaba.fluss.shaded.guava32.com.google.common.collect.ImmutableMap; |
| 29 | + |
| 30 | +import io.opentelemetry.api.common.AttributeKey; |
| 31 | +import io.opentelemetry.api.common.Attributes; |
| 32 | +import io.opentelemetry.sdk.metrics.data.AggregationTemporality; |
| 33 | +import io.opentelemetry.sdk.metrics.data.DoublePointData; |
| 34 | +import io.opentelemetry.sdk.metrics.data.LongPointData; |
| 35 | +import io.opentelemetry.sdk.metrics.data.MetricData; |
| 36 | +import io.opentelemetry.sdk.metrics.data.MetricDataType; |
| 37 | +import io.opentelemetry.sdk.metrics.data.SummaryPointData; |
| 38 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableGaugeData; |
| 39 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableHistogramData; |
| 40 | +import io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData; |
| 41 | +import io.opentelemetry.sdk.resources.Resource; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | + |
| 44 | +import java.util.HashMap; |
| 45 | +import java.util.List; |
| 46 | +import java.util.Map; |
| 47 | +import java.util.Optional; |
| 48 | + |
| 49 | +import static org.assertj.core.api.Assertions.assertThat; |
| 50 | + |
| 51 | +/* This file is based on source code of Apache Flink Project (https://flink.apache.org/), licensed by the Apache |
| 52 | + * Software Foundation (ASF) under the Apache License, Version 2.0. See the NOTICE file distributed with this work for |
| 53 | + * additional information regarding copyright ownership. */ |
| 54 | + |
| 55 | +/** Tests for {@link OpenTelemetryAdapter}. */ |
| 56 | +public class OpenTelemetryAdapterTest { |
| 57 | + private static final OpenTelemetryAdapter.CollectionMetadata METADATA = |
| 58 | + new OpenTelemetryAdapter.CollectionMetadata( |
| 59 | + Resource.create(Attributes.empty()), 123L, 345L); |
| 60 | + |
| 61 | + private static final Map<String, String> VARIABLES = ImmutableMap.of("k1", "v1", "k2", "v2"); |
| 62 | + |
| 63 | + @Test |
| 64 | + void testCounter() { |
| 65 | + Optional<MetricData> metricData = |
| 66 | + OpenTelemetryAdapter.convertCounter( |
| 67 | + METADATA, 50L, 3L, new MetricMetadata("foo.bar.count", VARIABLES)); |
| 68 | + |
| 69 | + assertThat(metricData.isPresent()).isTrue(); |
| 70 | + assertThat(metricData.get().getName()).isEqualTo("foo.bar.count"); |
| 71 | + assertThat(metricData.get().getLongSumData().getAggregationTemporality()) |
| 72 | + .isEqualTo(AggregationTemporality.DELTA); |
| 73 | + assertThat(metricData.get().getLongSumData().isMonotonic()).isEqualTo(true); |
| 74 | + assertThat(metricData.get().getType()).isEqualTo(MetricDataType.LONG_SUM); |
| 75 | + assertThat(metricData.get().getLongSumData().getPoints().size()).isEqualTo(1); |
| 76 | + LongPointData data = metricData.get().getLongSumData().getPoints().iterator().next(); |
| 77 | + assertThat(data.getValue()).isEqualTo(47L); |
| 78 | + assertThat(asStringMap(data.getAttributes())).isEqualTo(VARIABLES); |
| 79 | + assertThat(metricData.get().getDoubleSumData()).isEqualTo(ImmutableSumData.empty()); |
| 80 | + assertThat(metricData.get().getLongGaugeData()).isEqualTo(ImmutableGaugeData.empty()); |
| 81 | + assertThat(metricData.get().getDoubleGaugeData()).isEqualTo(ImmutableGaugeData.empty()); |
| 82 | + assertThat(metricData.get().getHistogramData()).isEqualTo(ImmutableHistogramData.empty()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testGaugeDouble() { |
| 87 | + Optional<MetricData> metricData = |
| 88 | + OpenTelemetryAdapter.convertGauge( |
| 89 | + METADATA, () -> 123.456d, new MetricMetadata("foo.bar.value", VARIABLES)); |
| 90 | + |
| 91 | + assertThat(metricData.isPresent()).isTrue(); |
| 92 | + assertThat(metricData.get().getName()).isEqualTo("foo.bar.value"); |
| 93 | + assertThat(metricData.get().getType()).isEqualTo(MetricDataType.DOUBLE_GAUGE); |
| 94 | + assertThat(metricData.get().getDoubleGaugeData().getPoints().size()).isEqualTo(1); |
| 95 | + DoublePointData data = metricData.get().getDoubleGaugeData().getPoints().iterator().next(); |
| 96 | + assertThat(data.getValue()).isEqualTo(123.456d); |
| 97 | + assertThat(asStringMap(data.getAttributes())).isEqualTo(VARIABLES); |
| 98 | + assertThat(metricData.get().getLongSumData()).isEqualTo(ImmutableSumData.empty()); |
| 99 | + assertThat(metricData.get().getDoubleSumData()).isEqualTo(ImmutableSumData.empty()); |
| 100 | + assertThat(metricData.get().getLongGaugeData()).isEqualTo(ImmutableGaugeData.empty()); |
| 101 | + assertThat(metricData.get().getHistogramData()).isEqualTo(ImmutableHistogramData.empty()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testGaugeLong() { |
| 106 | + Optional<MetricData> metricData = |
| 107 | + OpenTelemetryAdapter.convertGauge( |
| 108 | + METADATA, () -> 125L, new MetricMetadata("foo.bar.value", VARIABLES)); |
| 109 | + |
| 110 | + assertThat(metricData.isPresent()).isTrue(); |
| 111 | + assertThat(metricData.get().getName()).isEqualTo("foo.bar.value"); |
| 112 | + assertThat(metricData.get().getType()).isEqualTo(MetricDataType.LONG_GAUGE); |
| 113 | + assertThat(metricData.get().getLongGaugeData().getPoints().size()).isEqualTo(1); |
| 114 | + LongPointData data = metricData.get().getLongGaugeData().getPoints().iterator().next(); |
| 115 | + assertThat(data.getValue()).isEqualTo(125L); |
| 116 | + assertThat(asStringMap(data.getAttributes())).isEqualTo(VARIABLES); |
| 117 | + assertThat(metricData.get().getLongSumData()).isEqualTo(ImmutableSumData.empty()); |
| 118 | + assertThat(metricData.get().getDoubleSumData()).isEqualTo(ImmutableSumData.empty()); |
| 119 | + assertThat(metricData.get().getDoubleGaugeData()).isEqualTo(ImmutableGaugeData.empty()); |
| 120 | + assertThat(metricData.get().getHistogramData()).isEqualTo(ImmutableHistogramData.empty()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testMeter() { |
| 125 | + Counter counter = new SimpleCounter(); |
| 126 | + MeterView meter = new MeterView(counter); |
| 127 | + counter.inc(345L); |
| 128 | + meter.update(); |
| 129 | + List<MetricData> metricData = |
| 130 | + OpenTelemetryAdapter.convertMeter( |
| 131 | + METADATA, |
| 132 | + meter, |
| 133 | + counter.getCount(), |
| 134 | + 20L, |
| 135 | + new MetricMetadata("foo.bar.value", VARIABLES)); |
| 136 | + |
| 137 | + assertThat(metricData.size()).isEqualTo(2); |
| 138 | + assertThat(metricData.get(0).getName()).isEqualTo("foo.bar.value.count"); |
| 139 | + assertThat(metricData.get(0).getType()).isEqualTo(MetricDataType.LONG_SUM); |
| 140 | + assertThat(metricData.get(0).getLongSumData().getAggregationTemporality()) |
| 141 | + .isEqualTo(AggregationTemporality.DELTA); |
| 142 | + assertThat(metricData.get(0).getLongSumData().getPoints().size()).isEqualTo(1); |
| 143 | + LongPointData data = metricData.get(0).getLongSumData().getPoints().iterator().next(); |
| 144 | + assertThat(data.getValue()).isEqualTo(325L); |
| 145 | + assertThat(asStringMap(data.getAttributes())).isEqualTo(VARIABLES); |
| 146 | + assertThat(metricData.get(0).getDoubleSumData()).isEqualTo(ImmutableSumData.empty()); |
| 147 | + assertThat(metricData.get(0).getLongGaugeData()).isEqualTo(ImmutableGaugeData.empty()); |
| 148 | + assertThat(metricData.get(0).getDoubleGaugeData()).isEqualTo(ImmutableGaugeData.empty()); |
| 149 | + assertThat(metricData.get(0).getHistogramData()).isEqualTo(ImmutableHistogramData.empty()); |
| 150 | + |
| 151 | + assertThat(metricData.get(1).getName()).isEqualTo("foo.bar.value.rate"); |
| 152 | + assertThat(metricData.get(1).getType()).isEqualTo(MetricDataType.DOUBLE_GAUGE); |
| 153 | + assertThat(metricData.get(1).getDoubleGaugeData().getPoints().size()).isEqualTo(1); |
| 154 | + DoublePointData data2 = |
| 155 | + metricData.get(1).getDoubleGaugeData().getPoints().iterator().next(); |
| 156 | + // 345L / 60 seconds |
| 157 | + assertThat(data2.getValue()).isEqualTo(5.75d); |
| 158 | + assertThat(asStringMap(data2.getAttributes())).isEqualTo(VARIABLES); |
| 159 | + assertThat(metricData.get(1).getLongSumData()).isEqualTo(ImmutableSumData.empty()); |
| 160 | + assertThat(metricData.get(1).getDoubleSumData()).isEqualTo(ImmutableSumData.empty()); |
| 161 | + assertThat(metricData.get(1).getLongGaugeData()).isEqualTo(ImmutableGaugeData.empty()); |
| 162 | + assertThat(metricData.get(1).getHistogramData()).isEqualTo(ImmutableHistogramData.empty()); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + void testHistogram() { |
| 167 | + TestHistogram histogram = new TestHistogram(); |
| 168 | + Optional<MetricData> metricData = |
| 169 | + OpenTelemetryAdapter.convertHistogram( |
| 170 | + METADATA, histogram, new MetricMetadata("foo.bar.histogram", VARIABLES)); |
| 171 | + |
| 172 | + assertThat(metricData.isPresent()).isTrue(); |
| 173 | + assertThat(metricData.get().getName()).isEqualTo("foo.bar.histogram"); |
| 174 | + assertThat(metricData.get().getType()).isEqualTo(MetricDataType.SUMMARY); |
| 175 | + assertThat(metricData.get().getSummaryData().getPoints().size()).isEqualTo(1); |
| 176 | + |
| 177 | + SummaryPointData summaryPointData = |
| 178 | + metricData.get().getSummaryData().getPoints().iterator().next(); |
| 179 | + |
| 180 | + assertThat(summaryPointData.getCount()).isEqualTo(1); |
| 181 | + assertThat(summaryPointData.getSum()).isEqualTo(4d); |
| 182 | + |
| 183 | + assertThat(summaryPointData.getValues().get(0).getQuantile()).isEqualTo(0); |
| 184 | + assertThat(summaryPointData.getValues().get(0).getValue()).isEqualTo(7); |
| 185 | + |
| 186 | + assertThat(summaryPointData.getValues().get(1).getQuantile()).isEqualTo(0.5); |
| 187 | + assertThat(summaryPointData.getValues().get(1).getValue()).isEqualTo(0.5); |
| 188 | + |
| 189 | + assertThat(summaryPointData.getValues().get(2).getQuantile()).isEqualTo(0.75); |
| 190 | + assertThat(summaryPointData.getValues().get(2).getValue()).isEqualTo(0.75); |
| 191 | + |
| 192 | + assertThat(summaryPointData.getValues().get(3).getQuantile()).isEqualTo(0.95); |
| 193 | + assertThat(summaryPointData.getValues().get(3).getValue()).isEqualTo(0.95); |
| 194 | + |
| 195 | + assertThat(summaryPointData.getValues().get(4).getQuantile()).isEqualTo(0.99); |
| 196 | + assertThat(summaryPointData.getValues().get(4).getValue()).isEqualTo(0.99); |
| 197 | + |
| 198 | + assertThat(summaryPointData.getValues().get(5).getQuantile()).isEqualTo(1); |
| 199 | + assertThat(summaryPointData.getValues().get(5).getValue()).isEqualTo(6); |
| 200 | + |
| 201 | + assertThat(asStringMap(summaryPointData.getAttributes())).isEqualTo(VARIABLES); |
| 202 | + } |
| 203 | + |
| 204 | + private Map<String, String> asStringMap(Attributes attributes) { |
| 205 | + Map<String, String> map = new HashMap<>(); |
| 206 | + for (Map.Entry<AttributeKey<?>, Object> entry : attributes.asMap().entrySet()) { |
| 207 | + map.put(entry.getKey().getKey(), (String) entry.getValue()); |
| 208 | + } |
| 209 | + return map; |
| 210 | + } |
| 211 | +} |
0 commit comments