Skip to content

Commit d1aa27e

Browse files
TEOTEO520teoteo
authored andcommitted
[apache#6468] feature(core): add metrics for mysql backend connect pool (apache#6470)
### What changes were proposed in this pull request? Support metrics for JDBC backend connect pool : 1. activeConnectionCount 2. idleConnectionCount 3. maxConnectionCount ### Why are the changes needed? Support metrics for JDBC connection pool Fix: apache#6468 ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? No need to test --------- Co-authored-by: teo <litianhang@bilibili.com> Co-authored-by: teo <litianhang@bilibili.co>
1 parent 38f7bb5 commit d1aa27e

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

core/src/main/java/org/apache/gravitino/metrics/MetricNames.java

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
public class MetricNames {
2323
public static final String HTTP_PROCESS_DURATION = "http-request-duration-seconds";
2424
public static final String SERVER_IDLE_THREAD_NUM = "http-server.idle-thread.num";
25+
public static final String ENTITY_STORE_RELATION_DATASOURCE_ACTIVE_CONNECTIONS =
26+
"entity-store.relation-datasource.active-connections";
27+
public static final String ENTITY_STORE_RELATION_DATASOURCE_IDLE_CONNECTIONS =
28+
"entity-store.relation-datasource.idle-connections";
29+
public static final String ENTITY_STORE_RELATION_DATASOURCE_MAX_CONNECTIONS =
30+
"entity-store.relation-datasource.max-connections";
2531

2632
private MetricNames() {}
2733
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.gravitino.metrics.source;
21+
22+
import com.codahale.metrics.Gauge;
23+
import org.apache.commons.dbcp2.BasicDataSource;
24+
import org.apache.gravitino.metrics.MetricNames;
25+
26+
public class RelationDatasourceMetricsSource extends MetricsSource {
27+
28+
public RelationDatasourceMetricsSource(BasicDataSource dataSource) {
29+
super(MetricsSource.GRAVITINO_SERVER_METRIC_NAME);
30+
registerGauge(
31+
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_ACTIVE_CONNECTIONS,
32+
(Gauge<Integer>) dataSource::getNumActive);
33+
registerGauge(
34+
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_IDLE_CONNECTIONS,
35+
(Gauge<Integer>) dataSource::getNumIdle);
36+
registerGauge(
37+
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_MAX_CONNECTIONS,
38+
(Gauge<Integer>) dataSource::getMaxTotal);
39+
}
40+
}

core/src/main/java/org/apache/gravitino/storage/relational/session/SqlSessionFactoryHelper.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import org.apache.commons.pool2.impl.BaseObjectPoolConfig;
2727
import org.apache.gravitino.Config;
2828
import org.apache.gravitino.Configs;
29+
import org.apache.gravitino.GravitinoEnv;
30+
import org.apache.gravitino.metrics.MetricsSystem;
31+
import org.apache.gravitino.metrics.source.RelationDatasourceMetricsSource;
2932
import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType;
3033
import org.apache.gravitino.storage.relational.mapper.CatalogMetaMapper;
3134
import org.apache.gravitino.storage.relational.mapper.FilesetMetaMapper;
@@ -102,7 +105,12 @@ public void init(Config config) {
102105
dataSource.setSoftMinEvictableIdleTimeMillis(
103106
BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME.toMillis());
104107
dataSource.setLifo(BaseObjectPoolConfig.DEFAULT_LIFO);
105-
108+
MetricsSystem metricsSystem = GravitinoEnv.getInstance().metricsSystem();
109+
// Add null check to avoid NPE when metrics system is not initialized in test environments
110+
if (metricsSystem != null) {
111+
// Register connection pool metrics when metrics system is available
112+
metricsSystem.register(new RelationDatasourceMetricsSource(dataSource));
113+
}
106114
// Create the transaction factory and env
107115
TransactionFactory transactionFactory = new JdbcTransactionFactory();
108116
Environment environment = new Environment("development", transactionFactory, dataSource);

core/src/test/java/org/apache/gravitino/metrics/TestExtractMetricNameAndLabel.java

+30
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,35 @@ void testMapperConfig() {
7979
+ "_"
8080
+ Collector.sanitizeMetricName(MetricNames.HTTP_PROCESS_DURATION),
8181
ImmutableMap.of("operation", "update-table"));
82+
83+
checkResult(
84+
MetricsSource.GRAVITINO_SERVER_METRIC_NAME
85+
+ "."
86+
+ MetricNames.ENTITY_STORE_RELATION_DATASOURCE_ACTIVE_CONNECTIONS,
87+
Collector.sanitizeMetricName(MetricsSource.GRAVITINO_SERVER_METRIC_NAME)
88+
+ "_"
89+
+ Collector.sanitizeMetricName(
90+
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_ACTIVE_CONNECTIONS),
91+
ImmutableMap.of());
92+
93+
checkResult(
94+
MetricsSource.GRAVITINO_SERVER_METRIC_NAME
95+
+ "."
96+
+ MetricNames.ENTITY_STORE_RELATION_DATASOURCE_IDLE_CONNECTIONS,
97+
Collector.sanitizeMetricName(MetricsSource.GRAVITINO_SERVER_METRIC_NAME)
98+
+ "_"
99+
+ Collector.sanitizeMetricName(
100+
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_IDLE_CONNECTIONS),
101+
ImmutableMap.of());
102+
103+
checkResult(
104+
MetricsSource.GRAVITINO_SERVER_METRIC_NAME
105+
+ "."
106+
+ MetricNames.ENTITY_STORE_RELATION_DATASOURCE_MAX_CONNECTIONS,
107+
Collector.sanitizeMetricName(MetricsSource.GRAVITINO_SERVER_METRIC_NAME)
108+
+ "_"
109+
+ Collector.sanitizeMetricName(
110+
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_MAX_CONNECTIONS),
111+
ImmutableMap.of());
82112
}
83113
}

0 commit comments

Comments
 (0)