From fe4d37b64808266bc427da5f7958ad79c150ba40 Mon Sep 17 00:00:00 2001 From: wangweicugw <38103831+wangweicugw@users.noreply.github.com> Date: Thu, 30 Nov 2023 09:08:13 +0800 Subject: [PATCH] add VtDriver version metric (#146) --- .../jd/jdbc/monitor/HealthCheckCollector.java | 8 ++++- .../com/jd/jdbc/monitor/HealthyCollector.java | 8 ++++- .../jd/jdbc/monitor/SqlErrorCollector.java | 8 ++++- .../jd/jdbc/monitor/SrvKeyspaceCollector.java | 12 +++++-- .../com/jd/jdbc/monitor/VersionCollector.java | 31 +++++++++++++++++++ .../metadata/VitessDatabaseMetaData.java | 2 ++ 6 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/jd/jdbc/monitor/VersionCollector.java diff --git a/src/main/java/com/jd/jdbc/monitor/HealthCheckCollector.java b/src/main/java/com/jd/jdbc/monitor/HealthCheckCollector.java index cdeccb9..3896254 100644 --- a/src/main/java/com/jd/jdbc/monitor/HealthCheckCollector.java +++ b/src/main/java/com/jd/jdbc/monitor/HealthCheckCollector.java @@ -29,7 +29,7 @@ import java.util.List; import java.util.Map; -public final class HealthCheckCollector extends Collector { +public final class HealthCheckCollector extends Collector implements Collector.Describable { private static final String COLLECT_NAME = "health_check"; private static final String COLLECT_HELP = "healthByAlias info in HealthCheck"; @@ -92,4 +92,10 @@ public static void buildGaugeMetric(GaugeMetricFamily labeledGauge, TabletHealth long uid = tablet.getAlias().getUid(); labeledGauge.addMetric(labelValues, tabletHealthCheck.getServing().get() ? uid : -uid); } + + @Override + public List describe() { + GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, DefaultConfig.HEALTH_CHECK_LABEL_NAMES); + return Collections.singletonList(labeledGauge); + } } diff --git a/src/main/java/com/jd/jdbc/monitor/HealthyCollector.java b/src/main/java/com/jd/jdbc/monitor/HealthyCollector.java index a00d9e7..d031995 100644 --- a/src/main/java/com/jd/jdbc/monitor/HealthyCollector.java +++ b/src/main/java/com/jd/jdbc/monitor/HealthyCollector.java @@ -23,11 +23,12 @@ import io.prometheus.client.Collector; import io.prometheus.client.GaugeMetricFamily; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; -public final class HealthyCollector extends Collector { +public final class HealthyCollector extends Collector implements Collector.Describable { private static final String COLLECT_NAME = "healthy"; private static final String COLLECT_HELP = "healthy info in HealthCheck"; @@ -108,4 +109,9 @@ public static long stateHealthyChecksum(Map> hea return Crc32Utill.checksumByCrc32(sb.toString().getBytes()); } + @Override + public List describe() { + GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, DefaultConfig.HEALTH_CHECK_LABEL_NAMES); + return Collections.singletonList(labeledGauge); + } } diff --git a/src/main/java/com/jd/jdbc/monitor/SqlErrorCollector.java b/src/main/java/com/jd/jdbc/monitor/SqlErrorCollector.java index eb41847..34b6e65 100644 --- a/src/main/java/com/jd/jdbc/monitor/SqlErrorCollector.java +++ b/src/main/java/com/jd/jdbc/monitor/SqlErrorCollector.java @@ -40,7 +40,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; -public final class SqlErrorCollector extends Collector { +public final class SqlErrorCollector extends Collector implements Collector.Describable { private static final Log LOG = LogFactory.getLog(SqlErrorCollector.class); private static final Integer DEFAULT_CAPACITY = 300; @@ -135,6 +135,12 @@ public void add(final String keyspace, final String userSQL, final Map describe() { + GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, LABEL_NAMES); + return Collections.singletonList(labeledGauge); + } + private static class Ignored { public static boolean match(final SQLException e) { return e instanceof SQLIntegrityConstraintViolationException diff --git a/src/main/java/com/jd/jdbc/monitor/SrvKeyspaceCollector.java b/src/main/java/com/jd/jdbc/monitor/SrvKeyspaceCollector.java index 7a10da8..1436a71 100644 --- a/src/main/java/com/jd/jdbc/monitor/SrvKeyspaceCollector.java +++ b/src/main/java/com/jd/jdbc/monitor/SrvKeyspaceCollector.java @@ -29,7 +29,7 @@ import java.util.List; import java.util.Map; -public final class SrvKeyspaceCollector extends Collector { +public final class SrvKeyspaceCollector extends Collector implements Collector.Describable { private static final List LABEL_NAMES = Lists.newArrayList("Keyspace"); private static final String COLLECT_NAME = "SrvKeyspaceCollector"; @@ -66,7 +66,7 @@ public final class SrvKeyspaceCollector extends Collector { .help("SrvKeyspaceTask update counter") .register(MonitorServer.getCollectorRegistry()); - private static final SrvKeyspaceCollector srvKeyspaceCollector = new SrvKeyspaceCollector(); + private static final SrvKeyspaceCollector SRV_KEYSPACE_COLLECTOR = new SrvKeyspaceCollector(); private final List resilientServerList = new ArrayList<>(); @@ -74,7 +74,7 @@ private SrvKeyspaceCollector() { } public static SrvKeyspaceCollector getInstance() { - return srvKeyspaceCollector; + return SRV_KEYSPACE_COLLECTOR; } public static Counter getCounter() { @@ -122,4 +122,10 @@ public List collect() { public void add(final ResilientServer resilientServer) { resilientServerList.add(resilientServer); } + + @Override + public List describe() { + GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, LABEL_NAMES); + return Collections.singletonList(labeledGauge); + } } diff --git a/src/main/java/com/jd/jdbc/monitor/VersionCollector.java b/src/main/java/com/jd/jdbc/monitor/VersionCollector.java new file mode 100644 index 0000000..a13f5a7 --- /dev/null +++ b/src/main/java/com/jd/jdbc/monitor/VersionCollector.java @@ -0,0 +1,31 @@ +/* +Copyright 2021 JD Project Authors. + +Licensed 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 com.jd.jdbc.monitor; + +import io.prometheus.client.Gauge; + +public class VersionCollector { + private static final Gauge VTDRIVER_VERSION_COUNTER = Gauge.build() + .name("VtDriver_version") + .help("VtDriver version info") + .labelNames("version") + .register(MonitorServer.getCollectorRegistry()); + + public static Gauge getVersionGauge() { + return VTDRIVER_VERSION_COUNTER; + } +} diff --git a/src/main/java/com/jd/jdbc/vitess/metadata/VitessDatabaseMetaData.java b/src/main/java/com/jd/jdbc/vitess/metadata/VitessDatabaseMetaData.java index c519516..ce4a1a6 100644 --- a/src/main/java/com/jd/jdbc/vitess/metadata/VitessDatabaseMetaData.java +++ b/src/main/java/com/jd/jdbc/vitess/metadata/VitessDatabaseMetaData.java @@ -19,6 +19,7 @@ import static com.jd.jdbc.common.Constant.DRIVER_MAJOR_VERSION; import static com.jd.jdbc.common.Constant.DRIVER_MINOR_VERSION; import static com.jd.jdbc.common.Constant.DRIVER_NAME; +import com.jd.jdbc.monitor.VersionCollector; import com.jd.jdbc.pool.InnerConnection; import com.jd.jdbc.pool.StatefulConnectionPool; import com.jd.jdbc.queryservice.util.RoleUtils; @@ -45,6 +46,7 @@ public class VitessDatabaseMetaData extends AbstractDatabaseMetaData { properties.load(VitessDatabaseMetaData.class.getClassLoader().getResourceAsStream("vtdriver-version.properties")); if (!properties.isEmpty()) { version = properties.getProperty("version"); + VersionCollector.getVersionGauge().labels(version).set(0); } } catch (IOException e) { }