Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W-15772694: Mule Runtime application error metric POC #2381

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -82,19 +77,6 @@ private String getShowStatsInfo() {
});
}

private void verifyMetricsExists(String metricName, String description, String resourceName, String instrumentationName,
long expectedValue, List<ExportedMeter> metrics) {
List<ExportedMeter> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,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;

Expand All @@ -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;
Expand Down Expand Up @@ -102,6 +106,19 @@ public void after() {
server.reset();
}

protected void verifyMetricsExists(String metricName, String description, String resourceName, String instrumentationName,
long expectedValue, List<ExportedMeter> metrics) {
List<ExportedMeter> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.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;
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";
}
}
22 changes: 22 additions & 0 deletions metrics/src/test/resources/metrics/error-counters-metric.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
~ 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.
-->

<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

<flow name="simple-flow">
<raise-error type="APP:UNEXPECTED" description="Expected error" />
<error-handler>
<on-error-continue/>
</error-handler>
</flow>

</mule>