Skip to content

Commit

Permalink
add VtDriver version metric (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweicugw authored Nov 30, 2023
1 parent a4363c5 commit fe4d37b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/main/java/com/jd/jdbc/monitor/HealthCheckCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<MetricFamilySamples> describe() {
GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, DefaultConfig.HEALTH_CHECK_LABEL_NAMES);
return Collections.singletonList(labeledGauge);
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/jd/jdbc/monitor/HealthyCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -108,4 +109,9 @@ public static long stateHealthyChecksum(Map<String, List<TabletHealthCheck>> hea
return Crc32Utill.checksumByCrc32(sb.toString().getBytes());
}

@Override
public List<MetricFamilySamples> describe() {
GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, DefaultConfig.HEALTH_CHECK_LABEL_NAMES);
return Collections.singletonList(labeledGauge);
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/jd/jdbc/monitor/SqlErrorCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,6 +135,12 @@ public void add(final String keyspace, final String userSQL, final Map<String, B
}
}

@Override
public List<MetricFamilySamples> 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
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/jd/jdbc/monitor/SrvKeyspaceCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> LABEL_NAMES = Lists.newArrayList("Keyspace");

private static final String COLLECT_NAME = "SrvKeyspaceCollector";
Expand Down Expand Up @@ -66,15 +66,15 @@ 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<ResilientServer> resilientServerList = new ArrayList<>();

private SrvKeyspaceCollector() {
}

public static SrvKeyspaceCollector getInstance() {
return srvKeyspaceCollector;
return SRV_KEYSPACE_COLLECTOR;
}

public static Counter getCounter() {
Expand Down Expand Up @@ -122,4 +122,10 @@ public List<MetricFamilySamples> collect() {
public void add(final ResilientServer resilientServer) {
resilientServerList.add(resilientServer);
}

@Override
public List<MetricFamilySamples> describe() {
GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, LABEL_NAMES);
return Collections.singletonList(labeledGauge);
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/jd/jdbc/monitor/VersionCollector.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
}
Expand Down

0 comments on commit fe4d37b

Please sign in to comment.