未验证 提交 2225e0e1 编写于 作者: kimmking's avatar kimmking 提交者: GitHub

Merge pull request #5555 from yu199195/metrics-point-1

Add sharding and transaction counter metrics.
......@@ -137,7 +137,9 @@ public final class MetricsTrackerFacade {
* @param delegate histogram metrics tracker delegate
*/
public void histogramObserveDuration(final HistogramMetricsTrackerDelegate delegate) {
MetricsTrackerHandler.getInstance().histogramObserveDuration(delegate);
if (enabled) {
MetricsTrackerHandler.getInstance().histogramObserveDuration(delegate);
}
}
/**
......@@ -160,7 +162,9 @@ public final class MetricsTrackerFacade {
* @param delegate summary metrics tracker delegate
*/
public void summaryObserveDuration(final SummaryMetricsTrackerDelegate delegate) {
MetricsTrackerHandler.getInstance().summaryObserveDuration(delegate);
if (enabled) {
MetricsTrackerHandler.getInstance().summaryObserveDuration(delegate);
}
}
private void loadMetricsManager() {
......
......@@ -150,7 +150,11 @@ public final class MetricsTrackerHandler {
* @param delegate histogram metrics tracker delegate
*/
public void histogramObserveDuration(final HistogramMetricsTrackerDelegate delegate) {
delegate.observeDuration();
if (async) {
executorService.execute(delegate::observeDuration);
} else {
delegate.observeDuration();
}
}
/**
......@@ -178,7 +182,11 @@ public final class MetricsTrackerHandler {
* @param delegate summary metrics tracker delegate
*/
public void summaryObserveDuration(final SummaryMetricsTrackerDelegate delegate) {
delegate.observeDuration();
if (async) {
executorService.execute(delegate::observeDuration);
} else {
delegate.observeDuration();
}
}
/**
......
......@@ -71,11 +71,15 @@ public final class MetricsTrackerHandlerTest {
histogramDelegate.ifPresent(delegate -> {
handler.histogramObserveDuration(delegate);
assertThat(delegate.getClass().getName(), is(NoneHistogramMetricsTrackerDelegate.class.getName()));
handler.histogramObserveDuration(delegate);
});
FieldUtil.setField(handler, "async", false);
Optional<HistogramMetricsTrackerDelegate> syncHistogram = handler.histogramStartTimer(METRICS_LABEL);
assertThat(syncHistogram.isPresent(), is(true));
syncHistogram.ifPresent(delegate -> assertThat(delegate.getClass().getName(), is(NoneHistogramMetricsTrackerDelegate.class.getName())));
syncHistogram.ifPresent(delegate -> {
assertThat(delegate.getClass().getName(), is(NoneHistogramMetricsTrackerDelegate.class.getName()));
handler.histogramObserveDuration(delegate);
});
}
@Test
......@@ -85,12 +89,16 @@ public final class MetricsTrackerHandlerTest {
summaryDelegate.ifPresent(delegate -> {
handler.summaryObserveDuration(delegate);
assertThat(delegate.getClass().getName(), is(NoneSummaryMetricsTrackerDelegate.class.getName()));
handler.summaryObserveDuration(delegate);
});
FieldUtil.setField(handler, "async", false);
Optional<SummaryMetricsTrackerDelegate> syncSummary = handler.summaryStartTimer(METRICS_LABEL);
assertThat(syncSummary.isPresent(), is(true));
syncSummary.ifPresent(delegate -> assertThat(delegate.getClass().getName(), is(NoneSummaryMetricsTrackerDelegate.class.getName())));
syncSummary.ifPresent(delegate -> {
assertThat(delegate.getClass().getName(), is(NoneSummaryMetricsTrackerDelegate.class.getName()));
handler.summaryObserveDuration(delegate);
});
}
@After
......
......@@ -21,6 +21,8 @@ import org.apache.shardingsphere.metrics.api.MetricsTracker;
import org.apache.shardingsphere.metrics.api.MetricsTrackerFactory;
import org.apache.shardingsphere.metrics.prometheus.impl.counter.RequestTotalCounterMetricsTracker;
import org.apache.shardingsphere.metrics.prometheus.impl.counter.SQLStatementCounterMetricsTracker;
import org.apache.shardingsphere.metrics.prometheus.impl.counter.ShardingCounterMetricsTracker;
import org.apache.shardingsphere.metrics.prometheus.impl.counter.TransactionCounterMetricsTracker;
import org.apache.shardingsphere.metrics.prometheus.impl.gauge.ChannelCountGaugeMetricsTracker;
import org.apache.shardingsphere.metrics.prometheus.impl.histogram.RequestLatencyHistogramMetricsTracker;
import org.apache.shardingsphere.metrics.prometheus.impl.summary.RequestLatencySummaryMetricsTracker;
......@@ -42,6 +44,8 @@ public final class PrometheusMetricsTrackerFactory implements MetricsTrackerFact
REGISTER.add(new ChannelCountGaugeMetricsTracker());
REGISTER.add(new RequestLatencyHistogramMetricsTracker());
REGISTER.add(new RequestLatencySummaryMetricsTracker());
REGISTER.add(new ShardingCounterMetricsTracker());
REGISTER.add(new TransactionCounterMetricsTracker());
}
@Override
......
/*
* 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.metrics.prometheus.impl.counter;
import io.prometheus.client.Counter;
import org.apache.shardingsphere.metrics.api.CounterMetricsTracker;
import org.apache.shardingsphere.metrics.enums.MetricsLabelEnum;
/**
* Sharding counter metrics tracker.
*/
public final class ShardingCounterMetricsTracker implements CounterMetricsTracker {
private static final Counter SHARDING = Counter.build()
.name("sharding")
.labelNames("datasource", "table")
.help("collect sharding datasource and table count")
.register();
@Override
public void inc(final double amount, final String... labelValues) {
SHARDING.labels(labelValues).inc(amount);
}
@Override
public String metricsLabel() {
return MetricsLabelEnum.SHARDING.getName();
}
}
/*
* 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.metrics.prometheus.impl.counter;
import io.prometheus.client.Counter;
import org.apache.shardingsphere.metrics.api.CounterMetricsTracker;
import org.apache.shardingsphere.metrics.enums.MetricsLabelEnum;
/**
* Transaction counter metrics tracker.
*/
public final class TransactionCounterMetricsTracker implements CounterMetricsTracker {
private static final Counter TRANSACTION = Counter.build()
.name("transaction")
.labelNames("status")
.help("collect sharding datasource and table count")
.register();
@Override
public void inc(final double amount, final String... labelValues) {
TRANSACTION.labels(labelValues).inc(amount);
}
@Override
public String metricsLabel() {
return MetricsLabelEnum.TRANSACTION.getName();
}
}
/*
* 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.metrics.prometheus.impl.counter;
import org.apache.shardingsphere.metrics.enums.MetricsLabelEnum;
import org.apache.shardingsphere.metrics.enums.MetricsTypeEnum;
import org.apache.shardingsphere.metrics.prometheus.impl.AbstractPrometheusCollectorRegistry;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class ShardingCounterMetricsTrackerTest extends AbstractPrometheusCollectorRegistry {
@Test
public void assertShardingCounter() {
ShardingCounterMetricsTracker tracker = new ShardingCounterMetricsTracker();
assertThat(tracker.metricsLabel(), is(MetricsLabelEnum.SHARDING.getName()));
assertThat(tracker.metricsType(), is(MetricsTypeEnum.COUNTER.name()));
String name = "sharding";
String[] labelNames = {"datasource", "table"};
String[] labelValues0 = {"ds0", "t_order_0"};
tracker.inc(1.0, labelValues0);
Double ds0 = getCollectorRegistry().getSampleValue(name, labelNames, labelValues0);
assertThat(ds0, is(1.0));
String[] labelValues1 = {"ds1", "t_order_1"};
tracker.inc(2.0, labelValues1);
Double ds1 = getCollectorRegistry().getSampleValue(name, labelNames, labelValues1);
assertThat(ds1, is(2.0));
}
@Test(expected = IllegalArgumentException.class)
public void assertNoLabels() {
ShardingCounterMetricsTracker tracker = new ShardingCounterMetricsTracker();
tracker.inc(1.0);
}
@Test(expected = IllegalArgumentException.class)
public void assertMoreLabels() {
ShardingCounterMetricsTracker tracker = new ShardingCounterMetricsTracker();
tracker.inc(1.0, "ds0", "t_order_0", " t_order_1");
}
}
/*
* 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.metrics.prometheus.impl.counter;
import org.apache.shardingsphere.metrics.enums.MetricsLabelEnum;
import org.apache.shardingsphere.metrics.enums.MetricsTypeEnum;
import org.apache.shardingsphere.metrics.prometheus.impl.AbstractPrometheusCollectorRegistry;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class TransactionCounterMetricsTrackerTest extends AbstractPrometheusCollectorRegistry {
@Test
public void assertShardingCounter() {
TransactionCounterMetricsTracker tracker = new TransactionCounterMetricsTracker();
assertThat(tracker.metricsLabel(), is(MetricsLabelEnum.TRANSACTION.getName()));
assertThat(tracker.metricsType(), is(MetricsTypeEnum.COUNTER.name()));
String name = "transaction";
String[] labelNames = {"status"};
String[] beginValues = {"begin"};
tracker.inc(1.0, beginValues);
Double beginValue = getValue(name, labelNames, beginValues);
assertThat(beginValue, is(1.0));
String[] commitValues = {"commit"};
tracker.inc(2.0, commitValues);
Double commitValue = getValue(name, labelNames, commitValues);
assertThat(commitValue, is(2.0));
String[] rollbackValues = {"rollback"};
tracker.inc(3.0, rollbackValues);
Double rollbackValue = getValue(name, labelNames, rollbackValues);
assertThat(rollbackValue, is(3.0));
}
@Test(expected = IllegalArgumentException.class)
public void assertNoLabels() {
TransactionCounterMetricsTracker tracker = new TransactionCounterMetricsTracker();
tracker.inc(1.0);
}
@Test(expected = IllegalArgumentException.class)
public void assertModeLabels() {
TransactionCounterMetricsTracker tracker = new TransactionCounterMetricsTracker();
String[] labelValues = {"begin", "rollback"};
tracker.inc(1.0, labelValues);
}
private Double getValue(final String name, final String[] labelNames, final String[] labelValue) {
return getCollectorRegistry().getSampleValue(name, labelNames, labelValue);
}
}
......@@ -45,7 +45,17 @@ public enum MetricsLabelEnum {
/**
* Request latency metrics label.
*/
REQUEST_LATENCY("request_latency");
REQUEST_LATENCY("request_latency"),
/**
* Sharding metrics label.
*/
SHARDING("sharding"),
/**
* Transaction metrics label.
*/
TRANSACTION("transaction");
private final String name;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册