Skip to content

Commit fe4d37b

Browse files
authored
add VtDriver version metric (#146)
1 parent a4363c5 commit fe4d37b

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed

src/main/java/com/jd/jdbc/monitor/HealthCheckCollector.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import java.util.List;
3030
import java.util.Map;
3131

32-
public final class HealthCheckCollector extends Collector {
32+
public final class HealthCheckCollector extends Collector implements Collector.Describable {
3333
private static final String COLLECT_NAME = "health_check";
3434

3535
private static final String COLLECT_HELP = "healthByAlias info in HealthCheck";
@@ -92,4 +92,10 @@ public static void buildGaugeMetric(GaugeMetricFamily labeledGauge, TabletHealth
9292
long uid = tablet.getAlias().getUid();
9393
labeledGauge.addMetric(labelValues, tabletHealthCheck.getServing().get() ? uid : -uid);
9494
}
95+
96+
@Override
97+
public List<MetricFamilySamples> describe() {
98+
GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, DefaultConfig.HEALTH_CHECK_LABEL_NAMES);
99+
return Collections.singletonList(labeledGauge);
100+
}
95101
}

src/main/java/com/jd/jdbc/monitor/HealthyCollector.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
import io.prometheus.client.Collector;
2424
import io.prometheus.client.GaugeMetricFamily;
2525
import java.util.ArrayList;
26+
import java.util.Collections;
2627
import java.util.Comparator;
2728
import java.util.List;
2829
import java.util.Map;
2930

30-
public final class HealthyCollector extends Collector {
31+
public final class HealthyCollector extends Collector implements Collector.Describable {
3132
private static final String COLLECT_NAME = "healthy";
3233

3334
private static final String COLLECT_HELP = "healthy info in HealthCheck";
@@ -108,4 +109,9 @@ public static long stateHealthyChecksum(Map<String, List<TabletHealthCheck>> hea
108109
return Crc32Utill.checksumByCrc32(sb.toString().getBytes());
109110
}
110111

112+
@Override
113+
public List<MetricFamilySamples> describe() {
114+
GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, DefaultConfig.HEALTH_CHECK_LABEL_NAMES);
115+
return Collections.singletonList(labeledGauge);
116+
}
111117
}

src/main/java/com/jd/jdbc/monitor/SqlErrorCollector.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import java.util.Map;
4141
import java.util.concurrent.TimeUnit;
4242

43-
public final class SqlErrorCollector extends Collector {
43+
public final class SqlErrorCollector extends Collector implements Collector.Describable {
4444
private static final Log LOG = LogFactory.getLog(SqlErrorCollector.class);
4545

4646
private static final Integer DEFAULT_CAPACITY = 300;
@@ -135,6 +135,12 @@ public void add(final String keyspace, final String userSQL, final Map<String, B
135135
}
136136
}
137137

138+
@Override
139+
public List<MetricFamilySamples> describe() {
140+
GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, LABEL_NAMES);
141+
return Collections.singletonList(labeledGauge);
142+
}
143+
138144
private static class Ignored {
139145
public static boolean match(final SQLException e) {
140146
return e instanceof SQLIntegrityConstraintViolationException

src/main/java/com/jd/jdbc/monitor/SrvKeyspaceCollector.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import java.util.List;
3030
import java.util.Map;
3131

32-
public final class SrvKeyspaceCollector extends Collector {
32+
public final class SrvKeyspaceCollector extends Collector implements Collector.Describable {
3333
private static final List<String> LABEL_NAMES = Lists.newArrayList("Keyspace");
3434

3535
private static final String COLLECT_NAME = "SrvKeyspaceCollector";
@@ -66,15 +66,15 @@ public final class SrvKeyspaceCollector extends Collector {
6666
.help("SrvKeyspaceTask update counter")
6767
.register(MonitorServer.getCollectorRegistry());
6868

69-
private static final SrvKeyspaceCollector srvKeyspaceCollector = new SrvKeyspaceCollector();
69+
private static final SrvKeyspaceCollector SRV_KEYSPACE_COLLECTOR = new SrvKeyspaceCollector();
7070

7171
private final List<ResilientServer> resilientServerList = new ArrayList<>();
7272

7373
private SrvKeyspaceCollector() {
7474
}
7575

7676
public static SrvKeyspaceCollector getInstance() {
77-
return srvKeyspaceCollector;
77+
return SRV_KEYSPACE_COLLECTOR;
7878
}
7979

8080
public static Counter getCounter() {
@@ -122,4 +122,10 @@ public List<MetricFamilySamples> collect() {
122122
public void add(final ResilientServer resilientServer) {
123123
resilientServerList.add(resilientServer);
124124
}
125+
126+
@Override
127+
public List<MetricFamilySamples> describe() {
128+
GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, LABEL_NAMES);
129+
return Collections.singletonList(labeledGauge);
130+
}
125131
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2021 JD Project Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.jd.jdbc.monitor;
18+
19+
import io.prometheus.client.Gauge;
20+
21+
public class VersionCollector {
22+
private static final Gauge VTDRIVER_VERSION_COUNTER = Gauge.build()
23+
.name("VtDriver_version")
24+
.help("VtDriver version info")
25+
.labelNames("version")
26+
.register(MonitorServer.getCollectorRegistry());
27+
28+
public static Gauge getVersionGauge() {
29+
return VTDRIVER_VERSION_COUNTER;
30+
}
31+
}

src/main/java/com/jd/jdbc/vitess/metadata/VitessDatabaseMetaData.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.jd.jdbc.common.Constant.DRIVER_MAJOR_VERSION;
2020
import static com.jd.jdbc.common.Constant.DRIVER_MINOR_VERSION;
2121
import static com.jd.jdbc.common.Constant.DRIVER_NAME;
22+
import com.jd.jdbc.monitor.VersionCollector;
2223
import com.jd.jdbc.pool.InnerConnection;
2324
import com.jd.jdbc.pool.StatefulConnectionPool;
2425
import com.jd.jdbc.queryservice.util.RoleUtils;
@@ -45,6 +46,7 @@ public class VitessDatabaseMetaData extends AbstractDatabaseMetaData {
4546
properties.load(VitessDatabaseMetaData.class.getClassLoader().getResourceAsStream("vtdriver-version.properties"));
4647
if (!properties.isEmpty()) {
4748
version = properties.getProperty("version");
49+
VersionCollector.getVersionGauge().labels(version).set(0);
4850
}
4951
} catch (IOException e) {
5052
}

0 commit comments

Comments
 (0)