Skip to content

Commit

Permalink
Modify proxy_execute_latency_millis metric to be collected only when …
Browse files Browse the repository at this point in the history
…QueryCommandExecutor (apache#25781)
  • Loading branch information
jiangML authored May 19, 2023
1 parent 7218116 commit 38a1620
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.HistogramMetricsCollector;
import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
import org.apache.shardingsphere.proxy.frontend.command.executor.QueryCommandExecutor;

import java.lang.reflect.Method;
import java.util.Collections;
Expand Down Expand Up @@ -51,11 +52,15 @@ private Map<String, Object> getBuckets() {

@Override
public void beforeMethod(final TargetAdviceObject target, final Method method, final Object[] args, final String pluginType) {
methodTimeRecorder.recordNow(method);
if (args[2] instanceof QueryCommandExecutor) {
methodTimeRecorder.recordNow(method);
}
}

@Override
public void afterMethod(final TargetAdviceObject target, final Method method, final Object[] args, final Object result, final String pluginType) {
MetricsCollectorRegistry.<HistogramMetricsCollector>get(config, pluginType).observe(methodTimeRecorder.getElapsedTimeAndClean(method));
if (args[2] instanceof QueryCommandExecutor) {
MetricsCollectorRegistry.<HistogramMetricsCollector>get(config, pluginType).observe(methodTimeRecorder.getElapsedTimeAndClean(method));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.collector.MetricsCollectorFixture;
import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.TargetAdviceObjectFixture;
import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.collector.MetricsCollectorFixture;
import org.apache.shardingsphere.proxy.frontend.mysql.command.admin.quit.MySQLComQuitExecutor;
import org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.query.MySQLComQueryPacketExecutor;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
Expand All @@ -31,6 +33,7 @@
import java.util.concurrent.TimeUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.mockito.Mockito.mock;

Expand All @@ -44,13 +47,26 @@ void reset() {
}

@Test
void assertExecuteLatencyHistogram() {
void assertExecuteLatencyHistogramWhenQueryCommandExecutor() {
ExecuteLatencyHistogramAdvice advice = new ExecuteLatencyHistogramAdvice();
TargetAdviceObjectFixture targetObject = new TargetAdviceObjectFixture();
Method method = mock(Method.class);
advice.beforeMethod(targetObject, method, new Object[]{}, "FIXTURE");
Object[] args = new Object[]{null, null, mock(MySQLComQueryPacketExecutor.class)};
advice.beforeMethod(targetObject, method, args, "FIXTURE");
Awaitility.await().pollDelay(500L, TimeUnit.MILLISECONDS).until(() -> true);
advice.afterMethod(targetObject, method, new Object[]{}, null, "FIXTURE");
advice.afterMethod(targetObject, method, args, null, "FIXTURE");
assertThat(Double.parseDouble(MetricsCollectorRegistry.get(config, "FIXTURE").toString()), greaterThanOrEqualTo(500d));
}

@Test
void assertExecuteLatencyHistogramWhenNotQueryCommandExecutor() {
ExecuteLatencyHistogramAdvice advice = new ExecuteLatencyHistogramAdvice();
TargetAdviceObjectFixture targetObject = new TargetAdviceObjectFixture();
Method method = mock(Method.class);
Object[] args = new Object[]{null, null, mock(MySQLComQuitExecutor.class)};
advice.beforeMethod(targetObject, method, args, "FIXTURE");
Awaitility.await().pollDelay(20L, TimeUnit.MILLISECONDS).until(() -> true);
advice.afterMethod(targetObject, method, args, null, "FIXTURE");
assertThat(Double.parseDouble(MetricsCollectorRegistry.get(config, "FIXTURE").toString()), equalTo(0d));
}
}

0 comments on commit 38a1620

Please sign in to comment.