From 9c36e81393ad4723a40b605e4f4e9ef0eac5255c Mon Sep 17 00:00:00 2001 From: Ivan Fritzler Date: Tue, 28 May 2024 11:39:07 -0300 Subject: [PATCH 1/2] WIP --- ...elemetryFlowStatisticsSummaryTestCase.java | 18 ------ .../AbstractOpenTelemetryMetricsTestCase.java | 18 ++++++ .../OpenTelemetryErrorCountersTestCase.java | 57 +++++++++++++++++++ .../metrics/error-counters-metric.xml | 22 +++++++ 4 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 metrics/src/test/java/org/mule/test/components/metrics/OpenTelemetryErrorCountersTestCase.java create mode 100644 metrics/src/test/resources/metrics/error-counters-metric.xml diff --git a/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryFlowStatisticsSummaryTestCase.java b/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryFlowStatisticsSummaryTestCase.java index 2ed2e3e306..299bef3314 100644 --- a/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryFlowStatisticsSummaryTestCase.java +++ b/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryFlowStatisticsSummaryTestCase.java @@ -20,16 +20,11 @@ import static org.mule.runtime.core.internal.management.stats.DefaultFlowsSummaryStatistics.DECLARED_TRIGGER_FLOWS_APP_NAME; import static org.mule.runtime.core.internal.management.stats.DefaultFlowsSummaryStatistics.FLOWS_SUMMARY_APP_STATISTICS_NAME; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertThat; import org.mule.tck.probe.JUnitProbe; import org.mule.tck.probe.PollingProber; -import org.mule.test.components.metrics.export.ExportedMeter; - -import java.util.List; -import java.util.stream.Collectors; import org.junit.Test; @@ -82,19 +77,6 @@ private String getShowStatsInfo() { }); } - private void verifyMetricsExists(String metricName, String description, String resourceName, String instrumentationName, - long expectedValue, List metrics) { - List exportedMetersForMetric = - metrics.stream().filter(metric -> metric.getName().equals(metricName)).collect(Collectors.toList()); - assertThat(exportedMetersForMetric, hasSize(1)); - ExportedMeter exportedMeter = exportedMetersForMetric.get(0); - assertThat(exportedMeter.getName(), equalTo(metricName)); - assertThat(exportedMeter.getDescription(), equalTo(description)); - assertThat(exportedMeter.getResourceName(), equalTo(resourceName)); - assertThat(exportedMeter.getInstrumentName(), equalTo(instrumentationName)); - assertThat(exportedMeter.getValue(), equalTo(expectedValue)); - } - abstract String getResourceName(); abstract long getExpectedDeclaredPrivateFlows(); diff --git a/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryMetricsTestCase.java b/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryMetricsTestCase.java index f78367cad0..b584d5413a 100644 --- a/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryMetricsTestCase.java +++ b/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryMetricsTestCase.java @@ -6,6 +6,9 @@ */ package org.mule.test.components.metrics; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertThat; import static org.mule.runtime.api.util.MuleSystemProperties.MULE_ENABLE_STATISTICS; import static org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader; import static org.mule.runtime.core.api.util.IOUtils.getResourceAsUrl; @@ -30,6 +33,7 @@ import java.io.UncheckedIOException; import java.util.List; import java.util.concurrent.CompletionStage; +import java.util.stream.Collectors; import com.google.protobuf.InvalidProtocolBufferException; import com.linecorp.armeria.server.ServerBuilder; @@ -102,6 +106,19 @@ public void after() { server.reset(); } + protected void verifyMetricsExists(String metricName, String description, String resourceName, String instrumentationName, + long expectedValue, List metrics) { + List exportedMetersForMetric = + metrics.stream().filter(metric -> metric.getName().equals(metricName)).collect(Collectors.toList()); + assertThat(exportedMetersForMetric, hasSize(1)); + ExportedMeter exportedMeter = exportedMetersForMetric.get(0); + assertThat(exportedMeter.getName(), equalTo(metricName)); + assertThat(exportedMeter.getDescription(), equalTo(description)); + assertThat(exportedMeter.getResourceName(), equalTo(resourceName)); + assertThat(exportedMeter.getInstrumentName(), equalTo(instrumentationName)); + assertThat(exportedMeter.getValue(), equalTo(expectedValue)); + } + /** * A Test Grpc Server Rule that captures the metrics. Till reset, it will obtain only the first metrics exported. */ @@ -120,6 +137,7 @@ protected void configure(ServerBuilder sb) { @NotNull ServiceRequestContext ctx, byte @NotNull [] message) { try { + System.out.println(OpenTelemetryMetricsTestUtils.getMetrics(parseFrom(message))); if (metrics == null) { metrics = OpenTelemetryMetricsTestUtils.getMetrics(parseFrom(message)); } diff --git a/metrics/src/test/java/org/mule/test/components/metrics/OpenTelemetryErrorCountersTestCase.java b/metrics/src/test/java/org/mule/test/components/metrics/OpenTelemetryErrorCountersTestCase.java new file mode 100644 index 0000000000..ac78aecca4 --- /dev/null +++ b/metrics/src/test/java/org/mule/test/components/metrics/OpenTelemetryErrorCountersTestCase.java @@ -0,0 +1,57 @@ +/* + * Copyright 2023 Salesforce, Inc. All rights reserved. + * The software in this package is published under the terms of the CPAL v1.0 + * license, a copy of which has been included with this distribution in the + * LICENSE.txt file. + */ +package org.mule.test.components.metrics; + +import org.junit.Test; +import org.mule.tck.probe.JUnitProbe; +import org.mule.tck.probe.PollingProber; + +public class OpenTelemetryErrorCountersTestCase extends AbstractOpenTelemetryMetricsTestCase { + + private static final int TIMEOUT_MILLIS = 30000; + private static final int POLL_DELAY_MILLIS = 100; + public static final String EXPECTED_METRIC_NAME = "error-count-97abc3d9"; + public static final String EXPECTED_METRIC_DESCRIPTION = "Mule runtime error count"; + public static final String EXPECTED_RESOURCE_NAME = "OpenTelemetryErrorCountersTestCase#test"; + public static final String EXPECTED_INSTRUMENTATION_NAME = "flow.construct.statistics"; + + @Test + public void test() throws Exception { + flowRunner("simple-flow").withPayload(TEST_PAYLOAD).run().getMessage(); + PollingProber probe = new PollingProber(TIMEOUT_MILLIS, POLL_DELAY_MILLIS); + probe.check(new JUnitProbe() { + + @Override + protected boolean test() throws Exception { + try { + verifyMetricsExists(EXPECTED_METRIC_NAME, EXPECTED_METRIC_DESCRIPTION, EXPECTED_RESOURCE_NAME, + EXPECTED_INSTRUMENTATION_NAME, 1, server.getMetrics()); + } catch (Throwable e) { + return false; + } + return true; + } + + @Override + public String describeFailure() { + return "Error on verifying error counters metric: " + getShowStatsInfo(); + } + + private String getShowStatsInfo() { + StringBuffer statsInfo = new StringBuffer(); + server.getMetrics().forEach(metric -> statsInfo.append(metric.getName()).append(": ").append(metric.getValue()) + .append(System.lineSeparator())); + return statsInfo.toString(); + } + }); + } + + @Override + protected String getConfigFile() { + return "metrics/error-counters-metric.xml"; + } +} diff --git a/metrics/src/test/resources/metrics/error-counters-metric.xml b/metrics/src/test/resources/metrics/error-counters-metric.xml new file mode 100644 index 0000000000..67ab531d0b --- /dev/null +++ b/metrics/src/test/resources/metrics/error-counters-metric.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + From de0e6e7d8518e7ebc8db93f88058177222fe3efa Mon Sep 17 00:00:00 2001 From: Ivan Fritzler Date: Tue, 28 May 2024 12:15:48 -0300 Subject: [PATCH 2/2] Self review --- .../metrics/AbstractOpenTelemetryMetricsTestCase.java | 7 +++---- .../metrics/OpenTelemetryErrorCountersTestCase.java | 3 ++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryMetricsTestCase.java b/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryMetricsTestCase.java index b584d5413a..5ebf5a8808 100644 --- a/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryMetricsTestCase.java +++ b/metrics/src/test/java/org/mule/test/components/metrics/AbstractOpenTelemetryMetricsTestCase.java @@ -6,9 +6,6 @@ */ package org.mule.test.components.metrics; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasSize; -import static org.junit.Assert.assertThat; import static org.mule.runtime.api.util.MuleSystemProperties.MULE_ENABLE_STATISTICS; import static org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader; import static org.mule.runtime.core.api.util.IOUtils.getResourceAsUrl; @@ -23,6 +20,9 @@ import static java.util.concurrent.CompletableFuture.completedFuture; import static io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest.parseFrom; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertThat; import static org.testcontainers.Testcontainers.exposeHostPorts; import static org.testcontainers.containers.BindMode.READ_ONLY; @@ -137,7 +137,6 @@ protected void configure(ServerBuilder sb) { @NotNull ServiceRequestContext ctx, byte @NotNull [] message) { try { - System.out.println(OpenTelemetryMetricsTestUtils.getMetrics(parseFrom(message))); if (metrics == null) { metrics = OpenTelemetryMetricsTestUtils.getMetrics(parseFrom(message)); } diff --git a/metrics/src/test/java/org/mule/test/components/metrics/OpenTelemetryErrorCountersTestCase.java b/metrics/src/test/java/org/mule/test/components/metrics/OpenTelemetryErrorCountersTestCase.java index ac78aecca4..45c24b7520 100644 --- a/metrics/src/test/java/org/mule/test/components/metrics/OpenTelemetryErrorCountersTestCase.java +++ b/metrics/src/test/java/org/mule/test/components/metrics/OpenTelemetryErrorCountersTestCase.java @@ -6,10 +6,11 @@ */ package org.mule.test.components.metrics; -import org.junit.Test; import org.mule.tck.probe.JUnitProbe; import org.mule.tck.probe.PollingProber; +import org.junit.Test; + public class OpenTelemetryErrorCountersTestCase extends AbstractOpenTelemetryMetricsTestCase { private static final int TIMEOUT_MILLIS = 30000;