rkhachatryan commented on code in PR #25539: URL: https://github.com/apache/flink/pull/25539#discussion_r1814757339
########## 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())); + } Review Comment: Should this be closed? ########## 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: Can't we have a mapping from metric name to the expected value instead? ########## flink-metrics/flink-metrics-otel/src/test/java/org/apache/flink/metrics/otel/OpenTelemetryTestBase.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.api.common.time.Deadline; +import org.apache.flink.core.testutils.AllCallbackWrapper; +import org.apache.flink.core.testutils.TestContainerExtension; +import org.apache.flink.metrics.MetricConfig; +import org.apache.flink.util.TestLoggerExtension; +import org.apache.flink.util.function.ThrowingConsumer; +import org.apache.flink.util.function.ThrowingRunnable; + +import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode; +import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper; + +import com.esotericsoftware.minlog.Log; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.output.BaseConsumer; +import org.testcontainers.containers.output.OutputFrame; +import org.testcontainers.containers.output.Slf4jLogConsumer; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +/** Tests for {@link OpenTelemetryMetricReporter}. */ +@ExtendWith(TestLoggerExtension.class) +public class OpenTelemetryTestBase { + public static final Logger LOG = LoggerFactory.getLogger(OpenTelemetryTestBase.class); + + private static final Duration TIME_OUT = Duration.ofMinutes(2); + + @RegisterExtension + @Order(1) + private static final AllCallbackWrapper<TestContainerExtension<OtelTestContainer>> + OTEL_EXTENSION = + new AllCallbackWrapper<>( + new TestContainerExtension<>(() -> new OtelTestContainer())); + + @BeforeEach + public void setup() { + Slf4jLevelLogConsumer logConsumer = new Slf4jLevelLogConsumer(LOG); + OTEL_EXTENSION.getCustomExtension().getTestContainer().followOutput(logConsumer); + } + + public static OtelTestContainer getOtelContainer() { + return OTEL_EXTENSION.getCustomExtension().getTestContainer(); + } + + public static MetricConfig createMetricConfig() { + MetricConfig metricConfig = new MetricConfig(); + metricConfig.setProperty( + OpenTelemetryReporterOptions.EXPORTER_ENDPOINT.key(), + getOtelContainer().getGrpcEndpoint()); + return metricConfig; + } + + public static void eventuallyConsumeJson(ThrowingConsumer<JsonNode, Exception> jsonConsumer) + throws Exception { + eventually( + () -> { + // otel-collector dumps every report in a new line, so in order to re-use the + // same collector across multiple tests, let's read only the last line + getOtelContainer() + .copyFileFromContainer( + getOtelContainer().getOutputLogPath().toString(), + inputStream -> { + List<String> lines = new ArrayList<>(); + BufferedReader input = + new BufferedReader( + new InputStreamReader(inputStream)); + String last = ""; + String line; + + while ((line = input.readLine()) != null) { + lines.add(line); + last = line; + } + + ObjectMapper mapper = new ObjectMapper(); + JsonNode json = mapper.readValue(last, JsonNode.class); + try { + jsonConsumer.accept(json); Review Comment: What happens if there were no reports (and `last` == `""`)? Will `readValue` throw an exception? ########## flink-metrics/flink-metrics-otel/src/main/java/org/apache/flink/metrics/otel/VariableNameUtil.java: ########## @@ -0,0 +1,35 @@ +/* + * 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; + +/** + * Util for dealing with variables names in OTel reporting. Used to remove angel brackets that + * metric group codes adds to variables. + */ +public class VariableNameUtil { + private VariableNameUtil() {} + + /** Removes leading and trailing angle brackets. See ScopeFormat::SCOPE_VARIABLE_PREFIX. */ + public static String getVariableName(String str) { + if (str.startsWith("<") && str.endsWith(">")) { Review Comment: nit: can be replaced with direct character comparison ########## 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: Can `4` be replaced with `Arrays.stream(histogram.getStatistics().getValues()).sum()`? -- 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