This is an automated email from the ASF dual-hosted git repository.
zhaojinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 4987dff3695 Add InterceptorsYamlSwapper (#22799)
4987dff3695 is described below
commit 4987dff36959a26cdf4a46d3cc45d97f006eb705
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Dec 11 16:20:27 2022 +0800
Add InterceptorsYamlSwapper (#22799)
* Add InterceptorsYamlSwapper
* Add InterceptorsYamlSwapper
* Add InterceptorsYamlSwapper
* Add InterceptorsYamlSwapper
* Add InterceptorsYamlSwapper
* Add InterceptorsYamlSwapper
---
.../agent/core/{ => yaml}/entity/Interceptor.java | 5 +-
.../agent/core/{ => yaml}/entity/Interceptors.java | 5 +-
.../agent/core/{ => yaml}/entity/TargetPoint.java | 6 +-
.../yaml/swapper/InterceptorsYamlSwapper.java} | 24 +++---
.../yaml/swapper/InterceptorsYamlSwapperTest.java | 88 ++++++++++++++++++++++
agent/core/src/test/resources/interceptors.yaml | 36 ++++-----
.../PrometheusPluginDefinitionService.java | 16 +---
7 files changed, 134 insertions(+), 46 deletions(-)
diff --git
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/entity/Interceptor.java
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/entity/Interceptor.java
similarity index 87%
rename from
agent/core/src/main/java/org/apache/shardingsphere/agent/core/entity/Interceptor.java
rename to
agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/entity/Interceptor.java
index aa0adccb32f..bdbd105cded 100644
---
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/entity/Interceptor.java
+++
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/entity/Interceptor.java
@@ -15,11 +15,12 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.agent.core.entity;
+package org.apache.shardingsphere.agent.core.yaml.entity;
import lombok.Getter;
import lombok.Setter;
+import java.util.Collection;
import java.util.LinkedList;
/**
@@ -37,5 +38,5 @@ public final class Interceptor {
private String constructAdvice;
- private LinkedList<TargetPoint> points;
+ private Collection<TargetPoint> points = new LinkedList<>();
}
diff --git
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/entity/Interceptors.java
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/entity/Interceptors.java
similarity index 85%
rename from
agent/core/src/main/java/org/apache/shardingsphere/agent/core/entity/Interceptors.java
rename to
agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/entity/Interceptors.java
index c93baa2b97a..c063a03563e 100644
---
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/entity/Interceptors.java
+++
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/entity/Interceptors.java
@@ -15,12 +15,13 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.agent.core.entity;
+package org.apache.shardingsphere.agent.core.yaml.entity;
import lombok.Getter;
import lombok.Setter;
import java.util.Collection;
+import java.util.LinkedList;
/**
* Interceptors.
@@ -29,5 +30,5 @@ import java.util.Collection;
@Getter
public final class Interceptors {
- private Collection<Interceptor> interceptors;
+ private Collection<Interceptor> interceptors = new LinkedList<>();
}
diff --git
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/entity/TargetPoint.java
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/entity/TargetPoint.java
similarity index 94%
rename from
agent/core/src/main/java/org/apache/shardingsphere/agent/core/entity/TargetPoint.java
rename to
agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/entity/TargetPoint.java
index 4b66834a5c6..8d8a3b610ac 100644
---
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/entity/TargetPoint.java
+++
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/entity/TargetPoint.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.agent.core.entity;
+package org.apache.shardingsphere.agent.core.yaml.entity;
import lombok.Getter;
import lombok.Setter;
@@ -27,7 +27,7 @@ import lombok.Setter;
@Setter
public final class TargetPoint {
- private String type;
-
private String name;
+
+ private String type;
}
diff --git
a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/entity/InterceptorConvertTest.java
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/swapper/InterceptorsYamlSwapper.java
similarity index 62%
rename from
agent/core/src/test/java/org/apache/shardingsphere/agent/core/entity/InterceptorConvertTest.java
rename to
agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/swapper/InterceptorsYamlSwapper.java
index 1447df58306..f184f3000a0 100644
---
a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/entity/InterceptorConvertTest.java
+++
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/yaml/swapper/InterceptorsYamlSwapper.java
@@ -15,19 +15,25 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.agent.core.entity;
+package org.apache.shardingsphere.agent.core.yaml.swapper;
-import org.junit.Test;
+import org.apache.shardingsphere.agent.core.yaml.entity.Interceptors;
import org.yaml.snakeyaml.Yaml;
-import static org.junit.Assert.assertFalse;
+import java.io.InputStream;
-public final class InterceptorConvertTest {
+/**
+ * Interceptors YAML swapper.
+ */
+public final class InterceptorsYamlSwapper {
- @Test
- public void assertConvert() {
- Yaml yaml = new Yaml();
- Interceptors interceptors =
yaml.loadAs(this.getClass().getResourceAsStream("/interceptors.yaml"),
Interceptors.class);
- assertFalse(interceptors.getInterceptors().isEmpty());
+ /**
+ * unmarshal interceptors.
+ *
+ * @param inputStream input stream
+ * @return unmarshalled interceptors
+ */
+ public Interceptors unmarshal(final InputStream inputStream) {
+ return new Yaml().loadAs(inputStream, Interceptors.class);
}
}
diff --git
a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/yaml/swapper/InterceptorsYamlSwapperTest.java
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/yaml/swapper/InterceptorsYamlSwapperTest.java
new file mode 100644
index 00000000000..b8713027c7b
--- /dev/null
+++
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/yaml/swapper/InterceptorsYamlSwapperTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.agent.core.yaml.swapper;
+
+import org.apache.shardingsphere.agent.core.yaml.entity.Interceptor;
+import org.apache.shardingsphere.agent.core.yaml.entity.Interceptors;
+import org.apache.shardingsphere.agent.core.yaml.entity.TargetPoint;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public final class InterceptorsYamlSwapperTest {
+
+ @Test
+ public void assertUnmarshal() {
+ Interceptors actual = new
InterceptorsYamlSwapper().unmarshal(getClass().getResourceAsStream("/interceptors.yaml"));
+ assertThat(actual.getInterceptors().size(), is(5));
+ List<Interceptor> actualInterceptors = new
ArrayList<>(actual.getInterceptors());
+ assertInterceptor(actualInterceptors.get(0),
createExpectedInterceptor("org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask",
+
"org.apache.shardingsphere.agent.metrics.api.advice.CommandExecutorTaskAdvice",
+ null, null, Arrays.asList(createExpectedTargetPoint("run",
"instance"), createExpectedTargetPoint("processException", "instance"))));
+ assertInterceptor(actualInterceptors.get(1),
createExpectedInterceptor("org.apache.shardingsphere.proxy.frontend.netty.FrontendChannelInboundHandler",
+
"org.apache.shardingsphere.agent.metrics.api.advice.ChannelHandlerAdvice",
+ null, null, Arrays.asList(
+ createExpectedTargetPoint("channelActive",
"instance"), createExpectedTargetPoint("channelRead", "instance"),
createExpectedTargetPoint("channelInactive", "instance"))));
+ assertInterceptor(actualInterceptors.get(2),
createExpectedInterceptor("org.apache.shardingsphere.infra.route.engine.SQLRouteEngine",
+
"org.apache.shardingsphere.agent.metrics.api.advice.SQLRouteEngineAdvice",
null, null, Collections.singleton(createExpectedTargetPoint("route",
"instance"))));
+ assertInterceptor(actualInterceptors.get(3),
createExpectedInterceptor("org.apache.shardingsphere.proxy.backend.communication.jdbc.transaction.BackendTransactionManager",
+
"org.apache.shardingsphere.agent.metrics.api.advice.TransactionAdvice",
+ null, null, Arrays.asList(createExpectedTargetPoint("commit",
"instance"), createExpectedTargetPoint("rollback", "instance"))));
+ assertInterceptor(actualInterceptors.get(4),
createExpectedInterceptor("org.apache.shardingsphere.infra.config.datasource.JDBCParameterDecoratorHelper",
+ null,
"org.apache.shardingsphere.agent.metrics.api.advice.DataSourceAdvice", null,
Collections.singleton(createExpectedTargetPoint("decorate", "static"))));
+ }
+
+ private void assertInterceptor(final Interceptor actual, final Interceptor
expected) {
+ assertThat(actual.getTarget(), is(expected.getTarget()));
+ assertThat(actual.getInstanceAdvice(),
is(expected.getInstanceAdvice()));
+ assertThat(actual.getStaticAdvice(), is(expected.getStaticAdvice()));
+ assertThat(actual.getConstructAdvice(),
is(expected.getConstructAdvice()));
+ assertThat(actual.getPoints().isEmpty(),
is(expected.getPoints().isEmpty()));
+ Iterator<TargetPoint> expectedTargetPoints =
expected.getPoints().iterator();
+ for (TargetPoint each : actual.getPoints()) {
+ TargetPoint expectedTargetPoint = expectedTargetPoints.next();
+ assertThat(each.getName(), is(expectedTargetPoint.getName()));
+ assertThat(each.getType(), is(expectedTargetPoint.getType()));
+ }
+ }
+
+ private Interceptor createExpectedInterceptor(final String target, final
String instanceAdvice, final String staticAdvice, final String constructAdvice,
final Collection<TargetPoint> points) {
+ Interceptor result = new Interceptor();
+ result.setTarget(target);
+ result.setInstanceAdvice(instanceAdvice);
+ result.setStaticAdvice(staticAdvice);
+ result.setConstructAdvice(constructAdvice);
+ result.setPoints(points);
+ return result;
+ }
+
+ private TargetPoint createExpectedTargetPoint(final String name, final
String type) {
+ TargetPoint result = new TargetPoint();
+ result.setName(name);
+ result.setType(type);
+ return result;
+ }
+}
diff --git a/agent/core/src/test/resources/interceptors.yaml
b/agent/core/src/test/resources/interceptors.yaml
index 210880f7988..98d2a654221 100644
--- a/agent/core/src/test/resources/interceptors.yaml
+++ b/agent/core/src/test/resources/interceptors.yaml
@@ -19,33 +19,33 @@ interceptors:
- target:
org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask
instanceAdvice:
org.apache.shardingsphere.agent.metrics.api.advice.CommandExecutorTaskAdvice
points:
- - type: instance
- name: run
- - type: instance
- name: processException
+ - name: run
+ type: instance
+ - name: processException
+ type: instance
- target:
org.apache.shardingsphere.proxy.frontend.netty.FrontendChannelInboundHandler
instanceAdvice:
org.apache.shardingsphere.agent.metrics.api.advice.ChannelHandlerAdvice
points:
- - type: instance
- name: channelActive
- - type: instance
- name: channelRead
- - type: instance
- name: channelInactive
+ - name: channelActive
+ type: instance
+ - name: channelRead
+ type: instance
+ - name: channelInactive
+ type: instance
- target: org.apache.shardingsphere.infra.route.engine.SQLRouteEngine
instanceAdvice:
org.apache.shardingsphere.agent.metrics.api.advice.SQLRouteEngineAdvice
points:
- - type: instance
- name: route
+ - name: route
+ type: instance
- target:
org.apache.shardingsphere.proxy.backend.communication.jdbc.transaction.BackendTransactionManager
instanceAdvice:
org.apache.shardingsphere.agent.metrics.api.advice.TransactionAdvice
points:
- - type: instance
- name: commit
- - type: instance
- name: rollback
+ - name: commit
+ type: instance
+ - name: rollback
+ type: instance
- target:
org.apache.shardingsphere.infra.config.datasource.JDBCParameterDecoratorHelper
staticAdvice:
org.apache.shardingsphere.agent.metrics.api.advice.DataSourceAdvice
points:
- - type: static
- name: decorate
+ - name: decorate
+ type: static
diff --git
a/agent/plugins/metrics/prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/definition/PrometheusPluginDefinitionService.java
b/agent/plugins/metrics/prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/definition/PrometheusPluginDefinitionService.java
index e291dc19383..3e1954790ee 100644
---
a/agent/plugins/metrics/prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/definition/PrometheusPluginDefinitionService.java
+++
b/agent/plugins/metrics/prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/definition/PrometheusPluginDefinitionService.java
@@ -19,13 +19,10 @@ package
org.apache.shardingsphere.agent.metrics.prometheus.definition;
import net.bytebuddy.matcher.ElementMatchers;
import
org.apache.shardingsphere.agent.api.point.PluginInterceptorPoint.Builder;
-import org.apache.shardingsphere.agent.core.entity.Interceptor;
-import org.apache.shardingsphere.agent.core.entity.Interceptors;
-import org.apache.shardingsphere.agent.core.entity.TargetPoint;
import
org.apache.shardingsphere.agent.core.definition.AbstractPluginDefinitionService;
-import org.yaml.snakeyaml.Yaml;
-
-import java.io.InputStream;
+import org.apache.shardingsphere.agent.core.yaml.entity.Interceptor;
+import org.apache.shardingsphere.agent.core.yaml.entity.TargetPoint;
+import
org.apache.shardingsphere.agent.core.yaml.swapper.InterceptorsYamlSwapper;
/**
* Metrics plugin definition service.
@@ -34,9 +31,7 @@ public final class PrometheusPluginDefinitionService extends
AbstractPluginDefin
@Override
protected void defineProxyInterceptors() {
- InputStream inputStream =
getClass().getResourceAsStream("/prometheus/interceptors.yaml");
- Interceptors interceptors = new Yaml().loadAs(inputStream,
Interceptors.class);
- for (Interceptor each : interceptors.getInterceptors()) {
+ for (Interceptor each : new
InterceptorsYamlSwapper().unmarshal(getClass().getResourceAsStream("/prometheus/interceptors.yaml")).getInterceptors())
{
if (null == each.getTarget()) {
continue;
}
@@ -44,9 +39,6 @@ public final class PrometheusPluginDefinitionService extends
AbstractPluginDefin
if (null != each.getConstructAdvice() &&
!("".equals(each.getConstructAdvice()))) {
builder.onConstructor(ElementMatchers.isConstructor()).implement(each.getConstructAdvice()).build();
}
- if (null == each.getPoints()) {
- continue;
- }
String[] instancePoints = each.getPoints().stream().filter(i ->
"instance".equals(i.getType())).map(TargetPoint::getName).toArray(String[]::new);
String[] staticPoints = each.getPoints().stream().filter(i ->
"static".equals(i.getType())).map(TargetPoint::getName).toArray(String[]::new);
if (instancePoints.length > 0) {