提交 40e57c04 编写于 作者: X xiaoyu

build base framework for metrics

上级 d0d7648b
......@@ -18,6 +18,12 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>sharding-metrics-spi</module>
<module>sharding-metrics-facade</module>
<module>sharding-metrics-prometheus</module>
</modules>
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere</artifactId>
......@@ -25,12 +31,4 @@
</parent>
<artifactId>sharding-metrics</artifactId>
<name>${project.artifactId}</name>
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-executor</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sharding-metrics</artifactId>
<groupId>org.apache.shardingsphere</groupId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sharding-metrics-facade</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-metrics-spi</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* 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.facade;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.metrics.api.CounterMetricsTracker;
import org.apache.shardingsphere.metrics.api.GaugeMetricsTracker;
import org.apache.shardingsphere.metrics.api.HistogramMetricsTracker;
import org.apache.shardingsphere.metrics.api.HistogramMetricsTrackerDelegate;
import org.apache.shardingsphere.metrics.api.MetricsTracker;
import org.apache.shardingsphere.metrics.api.NoneHistogramMetricsTrackerDelegate;
import org.apache.shardingsphere.metrics.api.NoneSummaryMetricsTrackerDelegate;
import org.apache.shardingsphere.metrics.api.SummaryMetricsTracker;
import org.apache.shardingsphere.metrics.api.SummaryMetricsTrackerDelegate;
import org.apache.shardingsphere.metrics.enums.MetricsTypeEnum;
import org.apache.shardingsphere.metrics.spi.MetricsTrackerManager;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Metrics tracker facade.
*/
@Slf4j
public final class MetricsTrackerFacade {
private static final Map<String, MetricsTrackerManager> METRICS_MAP = new HashMap<>();
private static final MetricsTrackerFacade INSTANCE = new MetricsTrackerFacade();
private volatile AtomicBoolean isInit = new AtomicBoolean(false);
private Optional<MetricsTrackerManager> metricsTrackerManager = Optional.empty();
private MetricsTrackerFacade() {
loadMetricsManager();
}
/**
* Get metrics tracker facade.
*
* @return metrics tracker facade
*/
public static MetricsTrackerFacade getInstance() {
return INSTANCE;
}
/**
* Find metrics tracker manager.
*
* @param metricsName metrics name
* @return metrics tracker manager
*/
public Optional<MetricsTrackerManager> findMetricsTrackerManager(final String metricsName) {
return Optional.ofNullable(METRICS_MAP.get(metricsName));
}
/**
* Init for metrics tracker manager.
*
* @param metricsName metrics name
* @param port port
*/
public void init(final String metricsName, final int port) {
if (!isInit.compareAndSet(false, true)) {
return;
}
metricsTrackerManager = findMetricsTrackerManager(metricsName);
metricsTrackerManager.ifPresent(manager -> manager.init(port));
}
/**
* Inc of counter metrics tracker.
*
* @param metricsLabel metrics label
* @param labelValues label values
*/
public void counterInc(final String metricsLabel, final String... labelValues) {
metricsTrackerManager.flatMap(manager -> manager.getMetricsTrackerFactory().create(MetricsTypeEnum.COUNTER.name(), metricsLabel))
.ifPresent(metricsTracker -> ((CounterMetricsTracker) metricsTracker).inc(1.0, labelValues));
}
/**
* Inc of gauge metrics tracker.
*
* @param metricsLabel metrics label
* @param labelValues label values
*/
public void gaugeInc(final String metricsLabel, final String... labelValues) {
metricsTrackerManager.flatMap(manager -> manager.getMetricsTrackerFactory().create(MetricsTypeEnum.GAUGE.name(), metricsLabel))
.ifPresent(metricsTracker -> ((GaugeMetricsTracker) metricsTracker).inc(1.0, labelValues));
}
/**
* Dec of gauge metrics tracker.
*
* @param metricsLabel metrics label
* @param labelValues label values
*/
public void gaugeDec(final String metricsLabel, final String... labelValues) {
metricsTrackerManager.flatMap(manager -> manager.getMetricsTrackerFactory().create(MetricsTypeEnum.GAUGE.name(), metricsLabel))
.ifPresent(metricsTracker -> ((GaugeMetricsTracker) metricsTracker).dec(1.0, labelValues));
}
/**
* Start timer of histogram metrics tracker.
*
* @param metricsLabel metrics label
* @param labelValues label values
* @return histogram metrics tracker delegate
*/
public HistogramMetricsTrackerDelegate histogramStartTimer(final String metricsLabel, final String... labelValues) {
Optional<MetricsTracker> metricsTracker = metricsTrackerManager.flatMap(manager -> manager.getMetricsTrackerFactory().create(MetricsTypeEnum.HISTOGRAM.name(), metricsLabel));
if (metricsTracker.isPresent()) {
return ((HistogramMetricsTracker) metricsTracker.get()).startTimer(labelValues);
} else {
return new NoneHistogramMetricsTrackerDelegate();
}
}
/**
* Observe amount of time since start time with histogram metrics tracker.
*
* @param delegate histogram metrics tracker delegate
*/
public void histogramObserveDuration(final HistogramMetricsTrackerDelegate delegate) {
delegate.observeDuration();
}
/**
* Start timer of summary metrics tracker.
*
* @param metricsLabel metrics label
* @param labelValues label values
* @return summary metrics tracker delegate
*/
public SummaryMetricsTrackerDelegate summaryStartTimer(final String metricsLabel, final String... labelValues) {
Optional<MetricsTracker> metricsTracker = metricsTrackerManager.flatMap(manager -> manager.getMetricsTrackerFactory().create(MetricsTypeEnum.SUMMARY.name(), metricsLabel));
if (metricsTracker.isPresent()) {
return ((SummaryMetricsTracker) metricsTracker.get()).startTimer(labelValues);
} else {
return new NoneSummaryMetricsTrackerDelegate();
}
}
/**
* Observe amount of time since start time with summary metrics tracker.
*
* @param delegate summary metrics tracker delegate
*/
public void summaryObserveDuration(final SummaryMetricsTrackerDelegate delegate) {
delegate.observeDuration();
}
private void loadMetricsManager() {
for (MetricsTrackerManager each : ServiceLoader.load(MetricsTrackerManager.class)) {
if (METRICS_MAP.containsKey(each.getType())) {
log.warn("Find more than one {} metricsTracker manager implementation class, use `{}` now",
each.getType(), METRICS_MAP.get(each.getType()).getClass().getName());
continue;
}
METRICS_MAP.put(each.getType(), each);
}
}
}
/*
* 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.facade;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class MetricsTrackerFacadeTest {
private MetricsTrackerFacade metricsTrackerFacade = MetricsTrackerFacade.getInstance();
@Test
public void assertFindMetricsTrackerManager() {
assertThat(metricsTrackerFacade.findMetricsTrackerManager("fixture1").isPresent(), is(false));
assertThat(metricsTrackerFacade.findMetricsTrackerManager("fixture").isPresent(), is(true));
}
}
/*
* 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.facade.fixture;
import org.apache.shardingsphere.metrics.api.MetricsTrackerFactory;
import org.apache.shardingsphere.metrics.spi.MetricsTrackerManager;
import java.util.Properties;
public class MetricsTrackerManagerFixture implements MetricsTrackerManager {
@Override
public void init(final int port) {
}
@Override
public MetricsTrackerFactory getMetricsTrackerFactory() {
return null;
}
@Override
public String getType() {
return "fixture";
}
@Override
public Properties getProperties() {
return null;
}
@Override
public void setProperties(final Properties properties) {
}
}
#
# 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.
#
org.apache.shardingsphere.metrics.facade.fixture.MetricsTrackerManagerFixture
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sharding-metrics</artifactId>
<groupId>org.apache.shardingsphere</groupId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sharding-metrics-prometheus</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-metrics-spi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
<version>0.6.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* 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;
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.gauge.ChannelCountGaugeMetricsTracker;
import org.apache.shardingsphere.metrics.prometheus.impl.histogram.RequestLatencyHistogramMetricsTracker;
import org.apache.shardingsphere.metrics.prometheus.impl.summary.RequestLatencySummaryMetricsTracker;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
/**
* Prometheus metrics tracker factory.
*/
public class PrometheusMetricsTrackerFactory implements MetricsTrackerFactory {
private static final Collection<MetricsTracker> REGISTER = new ArrayList<>();
static {
REGISTER.add(new RequestTotalCounterMetricsTracker());
REGISTER.add(new SQLStatementCounterMetricsTracker());
REGISTER.add(new ChannelCountGaugeMetricsTracker());
REGISTER.add(new RequestLatencyHistogramMetricsTracker());
REGISTER.add(new RequestLatencySummaryMetricsTracker());
}
@Override
public Optional<MetricsTracker> create(final String metricsType, final String metricsLabel) {
return REGISTER.stream().filter(each -> each.metricsLabel().equals(metricsLabel) && each.metricsType().equals(metricsType)).findFirst();
}
}
/*
* 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;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.HTTPServer;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.metrics.api.MetricsTrackerFactory;
import org.apache.shardingsphere.metrics.spi.MetricsTrackerManager;
import java.net.InetSocketAddress;
import java.util.Properties;
/**
* Prometheus metrics tracker manager.
*/
@Getter
@Setter
public class PrometheusMetricsTrackerManager implements MetricsTrackerManager {
private Properties properties = new Properties();
private MetricsTrackerFactory metricsTrackerFactory = new PrometheusMetricsTrackerFactory();
@Override
public String getType() {
return "prometheus";
}
@SneakyThrows
@Override
public void init(final int port) {
new HTTPServer(new InetSocketAddress(port), CollectorRegistry.defaultRegistry, true);
}
}
/*
* 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;
/**
* Request total counter metrics tracker.
*/
public class RequestTotalCounterMetricsTracker implements CounterMetricsTracker {
private static final Counter REQUEST_TOTAL = Counter.build()
.name("request_total")
.help("proxy request total count")
.register();
@Override
public void inc(final double amount, final String... labelValues) {
REQUEST_TOTAL.inc(amount);
}
@Override
public String metricsLabel() {
return MetricsLabelEnum.REQUEST_TOTAL.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;
/**
* SQL statement counter metrics tracker.
*/
public class SQLStatementCounterMetricsTracker implements CounterMetricsTracker {
private static final Counter SQL_STATEMENT_COUNT = Counter.build()
.name("sql_statement_count")
.labelNames("sql_type")
.help("proxy sql statement count")
.register();
@Override
public void inc(final double amount, final String... labelValues) {
SQL_STATEMENT_COUNT.labels(labelValues).inc(amount);
}
@Override
public String metricsLabel() {
return MetricsLabelEnum.SQL_STATEMENT_COUNT.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.gauge;
import io.prometheus.client.Gauge;
import org.apache.shardingsphere.metrics.api.GaugeMetricsTracker;
import org.apache.shardingsphere.metrics.enums.MetricsLabelEnum;
/**
* Channel count gauge metrics tracker.
*/
public class ChannelCountGaugeMetricsTracker implements GaugeMetricsTracker {
private static final Gauge CHANNEL_COUNT = Gauge.build().name("channel_count").help("proxy channel count").register();
@Override
public void inc(final double amount, final String... labelValues) {
CHANNEL_COUNT.inc(amount);
}
@Override
public void dec(final double amount, final String... labelValues) {
CHANNEL_COUNT.dec(amount);
}
@Override
public String metricsLabel() {
return MetricsLabelEnum.CHANNEL_COUNT.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.histogram;
import io.prometheus.client.Histogram;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.metrics.api.HistogramMetricsTrackerDelegate;
/**
* Prometheus histogram metrics tracker delegate.
*/
@RequiredArgsConstructor
public class PrometheusHistogramMetricsTrackerDelegate implements HistogramMetricsTrackerDelegate {
private final Histogram.Timer timer;
@Override
public void observeDuration() {
timer.observeDuration();
}
}
/*
* 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.histogram;
import io.prometheus.client.Histogram;
import org.apache.shardingsphere.metrics.api.HistogramMetricsTrackerDelegate;
import org.apache.shardingsphere.metrics.api.HistogramMetricsTracker;
import org.apache.shardingsphere.metrics.enums.MetricsLabelEnum;
/**
* Request latency histogram metrics tracker.
*/
public class RequestLatencyHistogramMetricsTracker implements HistogramMetricsTracker {
private static final Histogram REQUEST_LATENCY = Histogram.build()
.name("requests_latency_Histogram_millis").help("Requests Latency Histogram Millis (ms)")
.exponentialBuckets(1.0, 2, 10)
.register();
@Override
public HistogramMetricsTrackerDelegate startTimer(final String... labelValues) {
Histogram.Timer timer = REQUEST_LATENCY.labels(labelValues).startTimer();
return new PrometheusHistogramMetricsTrackerDelegate(timer);
}
@Override
public String metricsLabel() {
return MetricsLabelEnum.REQUEST_LATENCY.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.summary;
import io.prometheus.client.Summary;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.metrics.api.SummaryMetricsTrackerDelegate;
/**
* Prometheus summary metrics tracker delegate.
*/
@RequiredArgsConstructor
public class PrometheusSummaryMetricsTrackerDelegate implements SummaryMetricsTrackerDelegate {
private final Summary.Timer timer;
@Override
public void observeDuration() {
timer.observeDuration();
}
}
/*
* 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.summary;
import io.prometheus.client.Summary;
import org.apache.shardingsphere.metrics.api.SummaryMetricsTracker;
import org.apache.shardingsphere.metrics.api.SummaryMetricsTrackerDelegate;
import org.apache.shardingsphere.metrics.enums.MetricsLabelEnum;
import java.util.concurrent.TimeUnit;
/**
* Request latency summary metrics tracker.
*/
public class RequestLatencySummaryMetricsTracker implements SummaryMetricsTracker {
private static final Summary REQUEST_LATENCY = Summary.build()
.name("requests_latency_summary_millis").help("Requests Latency Summary Millis (ms)")
.quantile(0.5, 0.05)
.quantile(0.95, 0.01)
.quantile(0.99, 0.001)
.maxAgeSeconds(TimeUnit.MINUTES.toSeconds(5))
.ageBuckets(5)
.register();
@Override
public SummaryMetricsTrackerDelegate startTimer(final String... labelValues) {
Summary.Timer timer = REQUEST_LATENCY.labels(labelValues).startTimer();
return new PrometheusSummaryMetricsTrackerDelegate(timer);
}
@Override
public String metricsLabel() {
return MetricsLabelEnum.REQUEST_LATENCY.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.
#
org.apache.shardingsphere.metrics.prometheus.PrometheusMetricsTrackerManager
/*
* 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;
import org.apache.shardingsphere.metrics.enums.MetricsLabelEnum;
import org.apache.shardingsphere.metrics.enums.MetricsTypeEnum;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(MockitoJUnitRunner.class)
public final class PrometheusMetricsTrackerFactoryTest {
@Test
public void assertCreate() {
PrometheusMetricsTrackerFactory factory = new PrometheusMetricsTrackerFactory();
assertThat(factory.create(MetricsTypeEnum.COUNTER.name(), MetricsLabelEnum.REQUEST_LATENCY.getName()).isPresent(), is(false));
assertThat(factory.create(MetricsTypeEnum.COUNTER.name(), MetricsLabelEnum.REQUEST_TOTAL.getName()).isPresent(), is(true));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sharding-metrics</artifactId>
<groupId>org.apache.shardingsphere</groupId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sharding-metrics-spi</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-spi</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* 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.api;
import org.apache.shardingsphere.metrics.enums.MetricsTypeEnum;
/**
* Counter metrics tracker interface.
*/
public interface CounterMetricsTracker extends MetricsTracker {
/**
* Increment the counter with label values by the given amount.
*
* @param amount amount
* @param labelValues label values
*/
void inc(double amount, String... labelValues);
/**
* Metrics type.
*
* @return metrics type
*/
default String metricsType() {
return MetricsTypeEnum.COUNTER.name();
}
}
/*
* 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.api;
import org.apache.shardingsphere.metrics.enums.MetricsTypeEnum;
/**
* Gauge metrics tracker interface.
*/
public interface GaugeMetricsTracker extends MetricsTracker {
/**
* Increment the Gauge with label values by the given amount.
*
* @param amount amount
* @param labelValues label values
*/
void inc(double amount, String... labelValues);
/**
* Decrement the Gauge with label values by the given amount.
*
* @param amount amount
* @param labelValues label values
*/
void dec(double amount, String... labelValues);
/**
* Metrics type.
*
* @return metrics type
*/
default String metricsType() {
return MetricsTypeEnum.GAUGE.name();
}
}
/*
* 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.api;
import org.apache.shardingsphere.metrics.enums.MetricsTypeEnum;
/**
* Histogram metrics tracker.
*/
public interface HistogramMetricsTracker extends MetricsTracker {
/**
* Start timer with histogram.
*
* @param labelValues label values
* @return histogram metrics tracker delegate
*/
default HistogramMetricsTrackerDelegate startTimer(String... labelValues) {
return new NoneHistogramMetricsTrackerDelegate();
}
/**
* Observe the given amount.
*
* @param amount amount
*/
default void observer(long amount) {
}
/**
* Metrics type.
*
* @return metrics type
*/
default String metricsType() {
return MetricsTypeEnum.HISTOGRAM.name();
}
}
/*
* 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.api;
/**
* Histogram metrics tracker delegate.
*/
public interface HistogramMetricsTrackerDelegate {
/**
* Observe amount of time since start time.
*/
default void observeDuration() {
}
}
/*
* 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.api;
/**
* Metrics tracker.
*/
public interface MetricsTracker {
/**
* Metrics label.
*
* @return metrics label
*/
String metricsLabel();
/**
* Metrics type.
*
* @return metrics type
*/
String metricsType();
}
/*
* 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.api;
import java.util.Optional;
/**
* Metrics tracker factory.
*/
public interface MetricsTrackerFactory {
/**
* Create of metrics tracker.
*
* @param metricsType metrics type
* @param metricsLabel metrics label
* @return metrics tracker
*/
Optional<MetricsTracker> create(String metricsType, String metricsLabel);
}
/*
* 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.api;
public class NoneHistogramMetricsTrackerDelegate implements HistogramMetricsTrackerDelegate {
}
/*
* 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.api;
/**
* None summary metrics tracker delegate.
*/
public class NoneSummaryMetricsTrackerDelegate implements SummaryMetricsTrackerDelegate {
}
/*
* 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.api;
import org.apache.shardingsphere.metrics.enums.MetricsTypeEnum;
/**
* Summary metrics tracker.
*/
public interface SummaryMetricsTracker extends MetricsTracker {
/**
* Start timer with summary.
*
* @param labelValues label values
* @return Summary metrics tracker delegate
*/
default SummaryMetricsTrackerDelegate startTimer(String... labelValues) {
return new NoneSummaryMetricsTrackerDelegate();
}
/**
* Observe the given amount.
*
* @param amount amount
*/
default void observer(long amount) {
}
/**
* Metrics type.
*
* @return metrics type
*/
default String metricsType() {
return MetricsTypeEnum.SUMMARY.name();
}
}
/*
* 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.api;
/**
* Summary metrics tracker delegate.
*/
public interface SummaryMetricsTrackerDelegate {
/**
* Observe amount of time in seconds since start time.
*/
default void observeDuration() {
}
}
/*
* 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.enums;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* Metrics label enum.
*/
@RequiredArgsConstructor
@Getter
public enum MetricsLabelEnum {
/**
* Request total metrics label.
*/
REQUEST_TOTAL("request_total"),
/**
* SQL statement count metrics label.
*/
SQL_STATEMENT_COUNT("sql_statement_count"),
/**
* Channel count metrics label.
*/
CHANNEL_COUNT("channel_count"),
/**
* Request latency metrics label.
*/
REQUEST_LATENCY("request_latency");
private final String name;
}
/*
* 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.enums;
/**
* Metrics type enum.
*/
public enum MetricsTypeEnum {
/**
* Counter metrics type.
*/
COUNTER,
/**
* Gauge metrics type.
*/
GAUGE,
/**
* Histogram metrics type.
*/
HISTOGRAM,
/**
* Summary metrics type.
*/
SUMMARY
}
/*
* 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.spi;
import org.apache.shardingsphere.metrics.api.MetricsTrackerFactory;
import org.apache.shardingsphere.spi.type.TypedSPI;
/**
* Metrics tracker manager.
*/
public interface MetricsTrackerManager extends TypedSPI {
/**
* Init metrics tracker.
*
* @param port port
*/
void init(int port);
/**
* Gets metrics tracker factory.
*
* @return metrics tracker factory
*/
MetricsTrackerFactory getMetricsTrackerFactory();
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册