pnowojski commented on code in PR #25539: URL: https://github.com/apache/flink/pull/25539#discussion_r1814845364
########## flink-metrics/flink-metrics-otel/src/test/java/org/apache/flink/metrics/otel/OpenTelemetryMetricReporterITCase.java: ########## @@ -0,0 +1,276 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.metrics.otel; + +import org.apache.flink.metrics.CharacterFilter; +import org.apache.flink.metrics.Gauge; +import org.apache.flink.metrics.Histogram; +import org.apache.flink.metrics.LogicalScopeProvider; +import org.apache.flink.metrics.MeterView; +import org.apache.flink.metrics.MetricConfig; +import org.apache.flink.metrics.MetricGroup; +import org.apache.flink.metrics.SimpleCounter; +import org.apache.flink.metrics.groups.UnregisteredMetricsGroup; +import org.apache.flink.metrics.util.TestHistogram; +import org.apache.flink.util.TestLoggerExtension; + +import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode; + +import org.assertj.core.data.Percentage; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import java.time.Clock; +import java.time.Instant; +import java.util.List; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link OpenTelemetryMetricReporter}. */ +@ExtendWith(TestLoggerExtension.class) +public class OpenTelemetryMetricReporterITCase extends OpenTelemetryTestBase { + + private static final long TIME_MS = 1234; + + private OpenTelemetryMetricReporter reporter; + private final Histogram histogram = new TestHistogram(); + + @BeforeEach + public void setUpEach() { + reporter = + new OpenTelemetryMetricReporter( + Clock.fixed(Instant.ofEpochMilli(TIME_MS), Clock.systemUTC().getZone())); + } + + @Test + public void testReport() throws Exception { + MetricConfig metricConfig = createMetricConfig(); + MetricGroup group = new TestMetricGroup(); + + reporter.open(metricConfig); + + SimpleCounter counter = new SimpleCounter(); + reporter.notifyOfAddedMetric(counter, "foo.counter", group); + + Gauge<Double> gauge = () -> 123.456d; + reporter.notifyOfAddedMetric(gauge, "foo.gauge", group); + + reporter.report(); + + MeterView meter = new MeterView(counter); + reporter.notifyOfAddedMetric(meter, "foo.meter", group); + + reporter.notifyOfAddedMetric(histogram, "foo.histogram", group); + + reporter.report(); + reporter.close(); + + eventuallyConsumeJson( + (json) -> { + JsonNode scopeMetrics = + json.findPath("resourceMetrics").findPath("scopeMetrics"); + assertThat(scopeMetrics.findPath("scope").findPath("name").asText()) + .isEqualTo("io.confluent.flink.common.metrics"); + JsonNode metrics = scopeMetrics.findPath("metrics"); + + List<String> metricNames = extractMetricNames(json); + assertThat(metricNames) + .contains( + "flink.logical.scope.foo.counter", + "flink.logical.scope.foo.gauge", + "flink.logical.scope.foo.meter.count", + "flink.logical.scope.foo.meter.rate", + "flink.logical.scope.foo.histogram"); + + metrics.forEach(OpenTelemetryMetricReporterITCase::assertMetrics); + }); + } + + private static void assertMetrics(JsonNode metric) { + String name = metric.findPath("name").asText(); + if (name.equals("flink.logical.scope.foo.counter")) { + assertThat(metric.at("/sum/dataPoints").findPath("asInt").asInt()).isEqualTo(0); + } else if (name.equals("flink.logical.scope.foo.gauge")) { + assertThat(metric.at("/gauge/dataPoints").findPath("asDouble").asDouble()) + .isCloseTo(123.456, Percentage.withPercentage(1)); + } else if (name.equals("flink.logical.scope.foo.meter.count")) { + assertThat(metric.at("/sum/dataPoints").findPath("asInt").asInt()).isEqualTo(0); + } else if (name.equals("flink.logical.scope.foo.meter.rate")) { + assertThat(metric.at("/gauge/dataPoints").findPath("asDouble").asDouble()) + .isEqualTo(0.0); + } else if (name.equals("flink.logical.scope.foo.histogram")) { + assertThat(metric.at("/summary/dataPoints").findPath("sum").asInt()).isEqualTo(4); + } Review Comment: I'm slightly against that, as the assertions are a bit different (`asInt`, `asDouble`, `sum`). It could be handled with lambda functions doing the whole thing like `findPath("asInt").asInt()).isEqualTo(0);`. This would be slightly nicer, but debugging and stack traces would be mangled. So if I had to add a couple more assertions, I would do it but with the current size I would keep it as is. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org