Skip to content

Commit

Permalink
Refactor MetricsPluginE2EIT (#32207)
Browse files Browse the repository at this point in the history
* Refactor MetricsPluginE2EIT

* Refactor MetricsPluginE2EIT

* Refactor MetricsPluginE2EIT
  • Loading branch information
terrymanu authored Jul 20, 2024
1 parent c6ab1c1 commit ac74d49
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,25 @@

package org.apache.shardingsphere.test.e2e.agent.metrics;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.util.json.JsonUtils;
import org.apache.shardingsphere.test.e2e.agent.common.env.AgentE2ETestEnvironment;
import org.apache.shardingsphere.test.e2e.agent.common.framework.AgentE2ETestActionExtension;
import org.apache.shardingsphere.test.e2e.agent.common.framework.AgentE2ETestCaseArgumentsProvider;
import org.apache.shardingsphere.test.e2e.agent.common.util.HttpUtils;
import org.apache.shardingsphere.test.e2e.agent.metrics.asserts.MetricMetadataAssert;
import org.apache.shardingsphere.test.e2e.agent.metrics.asserts.MetricQueryAssert;
import org.apache.shardingsphere.test.e2e.agent.metrics.asserts.MetricAssert;
import org.apache.shardingsphere.test.e2e.agent.metrics.cases.MetricE2ETestCases;
import org.apache.shardingsphere.test.e2e.agent.metrics.cases.MetricQueryAssertion;
import org.apache.shardingsphere.test.e2e.agent.metrics.cases.MetricTestCase;
import org.apache.shardingsphere.test.e2e.agent.metrics.result.MetricsMetaDataResult;
import org.apache.shardingsphere.test.e2e.agent.metrics.result.MetricsQueryResult;
import org.apache.shardingsphere.test.e2e.agent.metrics.cases.MetricE2ETestCase;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

import java.io.IOException;
import java.net.URLEncoder;

@ExtendWith(AgentE2ETestActionExtension.class)
@Slf4j
class MetricsPluginE2EIT {

@EnabledIf("isEnabled")
@ParameterizedTest
@ArgumentsSource(TestCaseArgumentsProvider.class)
void assertWithAgent(final MetricTestCase metricTestCase) {
String metaDataURL = AgentE2ETestEnvironment.getInstance().getPrometheusHttpUrl() + "/api/v1/metadata";
String queryURL = AgentE2ETestEnvironment.getInstance().getPrometheusHttpUrl() + "/api/v1/query";
assertMetadata(metaDataURL, metricTestCase);
assertQuery(queryURL, metricTestCase);
}

private void assertMetadata(final String metaDataURL, final MetricTestCase metricCase) {
String metricName = "counter".equalsIgnoreCase(metricCase.getMetricType()) && metricCase.getMetricName().endsWith("_total")
? metricCase.getMetricName().replace("_total", "")
: metricCase.getMetricName();
try {
String metaDataURLWithParam = String.join("", metaDataURL, "?metric=", URLEncoder.encode(metricName, "UTF-8"));
MetricMetadataAssert.assertIs(JsonUtils.fromJsonString(HttpUtils.getInstance().query(metaDataURLWithParam), MetricsMetaDataResult.class), metricCase);
} catch (final IOException ex) {
log.info("Access prometheus HTTP RESTFul API error: ", ex);
}
}

private void assertQuery(final String queryURL, final MetricTestCase metricCase) {
for (MetricQueryAssertion each : metricCase.getQueryAssertions()) {
try {
String queryURLWithParam = String.join("", queryURL, "?query=", URLEncoder.encode(each.getQuery(), "UTF-8"));
MetricQueryAssert.assertIs(JsonUtils.fromJsonString(HttpUtils.getInstance().query(queryURLWithParam), MetricsQueryResult.class), each);
} catch (final IOException ex) {
log.info("Access prometheus HTTP RESTFul API error: ", ex);
}
}
void assertWithAgent(final MetricE2ETestCase metricTestCase) {
MetricAssert.assertIs(AgentE2ETestEnvironment.getInstance().getPrometheusHttpUrl(), metricTestCase);
}

private static boolean isEnabled() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.shardingsphere.test.e2e.agent.metrics.asserts;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.util.json.JsonUtils;
import org.apache.shardingsphere.test.e2e.agent.common.util.HttpUtils;
import org.apache.shardingsphere.test.e2e.agent.metrics.asserts.response.MetricsMetaDataResponse;
import org.apache.shardingsphere.test.e2e.agent.metrics.asserts.response.MetricsMetaDataResponse.Metric;
import org.apache.shardingsphere.test.e2e.agent.metrics.asserts.response.MetricsQueryResponse;
import org.apache.shardingsphere.test.e2e.agent.metrics.asserts.response.MetricsQueryResponse.QueryDataResult;
import org.apache.shardingsphere.test.e2e.agent.metrics.cases.MetricE2ETestCase;
import org.apache.shardingsphere.test.e2e.agent.metrics.cases.MetricQueryAssertion;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* Metric assert.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MetricAssert {

/**
* Assert metric.
*
* @param prometheusURL prometheus URL
* @param expected expected test case
*/
public static void assertIs(final String prometheusURL, final MetricE2ETestCase expected) {
assertMetaData(prometheusURL + "/api/v1/metadata", expected);
expected.getQueryAssertions().forEach(each -> assertQueryData(prometheusURL + "/api/v1/query", each));
}

@SneakyThrows(IOException.class)
private static void assertMetaData(final String metaDataURL, final MetricE2ETestCase expected) {
String metricName = getMetricName(expected);
String metaDataQueryURL = String.join("", metaDataURL, "?metric=", encode(metricName));
MetricsMetaDataResponse actual = JsonUtils.fromJsonString(HttpUtils.getInstance().query(metaDataQueryURL), MetricsMetaDataResponse.class);
assertThat(String.format("Metric `%s` status is not success, error is `%s`", expected.getMetricName(), actual.getError()), actual.getStatus(), is("success"));
assertFalse(actual.getData().isEmpty(), String.format("Metric `%s` is empty.", expected.getMetricName()));
Collection<Metric> metrics = actual.getData().get(metricName);
assertFalse(metrics.isEmpty(), String.format("Metric `%s` is empty.", expected.getMetricName()));
for (Metric each : metrics) {
assertThat(String.format("Metric `%s` is not `%s` type.", expected.getMetricName(), expected.getMetricType()), each.getType(), is(expected.getMetricType()));
}
}

private static String getMetricName(final MetricE2ETestCase expected) {
return "counter".equalsIgnoreCase(expected.getMetricType()) && expected.getMetricName().endsWith("_total") ? expected.getMetricName().replace("_total", "") : expected.getMetricName();
}

@SneakyThrows(UnsupportedEncodingException.class)
private static String encode(final String value) {
return URLEncoder.encode(value, "UTF-8");
}

@SneakyThrows(IOException.class)
private static void assertQueryData(final String queryURL, final MetricQueryAssertion expected) {
String queryURLWithParam = String.join("", queryURL, "?query=", encode(expected.getQuery()));
MetricsQueryResponse actual = JsonUtils.fromJsonString(HttpUtils.getInstance().query(queryURLWithParam), MetricsQueryResponse.class);
assertThat(String.format("The query `%s` is not success, error message is `%s`", expected.getQuery(), actual.getError()), actual.getStatus(), is("success"));
assertFalse(actual.getData().getResult().isEmpty(), String.format("The query `%s` is empty.", expected.getQuery()));
actual.getData().getResult().forEach(each -> assertMetricData(each, expected));
}

private static void assertMetricData(final QueryDataResult actual, final MetricQueryAssertion expected) {
assertThat(actual.getMetric().get("__name__"), is(expected.getMetric()));
if (null != expected.getValue()) {
assertThat(actual.getValue().size(), is(2));
assertThat(String.format("The value of the `%s` is error", expected.getQuery()), Integer.valueOf(actual.getValue().get(1)), is(expected.getValue()));
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.test.e2e.agent.metrics.result;
package org.apache.shardingsphere.test.e2e.agent.metrics.asserts.response;

import lombok.Getter;
import lombok.Setter;
Expand All @@ -25,11 +25,11 @@
import java.util.Map;

/**
* Metrics meta data result.
* Metrics meta data response.
*/
@Getter
@Setter
public final class MetricsMetaDataResult implements JsonConfiguration {
public final class MetricsMetaDataResponse implements JsonConfiguration {

private String status;

Expand All @@ -44,7 +44,7 @@ public final class MetricsMetaDataResult implements JsonConfiguration {
*/
@Getter
@Setter
public static final class Metric {
public static final class Metric implements JsonConfiguration {

private String type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.test.e2e.agent.metrics.result;
package org.apache.shardingsphere.test.e2e.agent.metrics.asserts.response;

import lombok.Getter;
import lombok.Setter;
Expand All @@ -25,11 +25,11 @@
import java.util.Map;

/**
* Metrics query result.
* Metrics query response.
*/
@Getter
@Setter
public final class MetricsQueryResult implements JsonConfiguration {
public final class MetricsQueryResponse implements JsonConfiguration {

private String status;

Expand All @@ -44,7 +44,7 @@ public final class MetricsQueryResult implements JsonConfiguration {
*/
@Getter
@Setter
public static final class QueryData {
public static final class QueryData implements JsonConfiguration {

private String resultType;

Expand All @@ -56,7 +56,7 @@ public static final class QueryData {
*/
@Getter
@Setter
public static final class QueryDataResult {
public static final class QueryDataResult implements JsonConfiguration {

private Map<String, String> metric;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import java.util.LinkedList;

/**
* Metric test case.
* Metric E2E test case.
*/
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
public final class MetricTestCase implements AgentE2ETestCase {
public final class MetricE2ETestCase implements AgentE2ETestCase {

@XmlAttribute(name = "metric-name")
private String metricName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
*/
@XmlRootElement(name = "e2e-test-cases")
@Getter
public final class MetricE2ETestCases implements AgentE2ETestCases<MetricTestCase> {
public final class MetricE2ETestCases implements AgentE2ETestCases<MetricE2ETestCase> {

@XmlElement(name = "test-case")
private final Collection<MetricTestCase> testCases = new LinkedList<>();
private final Collection<MetricE2ETestCase> testCases = new LinkedList<>();
}
Loading

0 comments on commit ac74d49

Please sign in to comment.