Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#6468] feature(core): add metrics for mysql backend connect pool #6470

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
public class MetricNames {
public static final String HTTP_PROCESS_DURATION = "http-request-duration-seconds";
public static final String SERVER_IDLE_THREAD_NUM = "http-server.idle-thread.num";
public static final String ENTITY_STORE_RELATION_DATASOURCE_ACTIVE_CONNECTIONS =
"entity-store.relation-datasource.active-connections";
public static final String ENTITY_STORE_RELATION_DATASOURCE_IDLE_CONNECTIONS =
"entity-store.relation-datasource.idle-connections";
public static final String ENTITY_STORE_RELATION_DATASOURCE_MAX_CONNECTIONS =
"entity-store.relation-datasource.max-connections";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change underscores to dashes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other metrics in MetricNames also use dashes:
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are these from?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear, I came from the Prometheus world. https://prometheus.io/docs/practices/naming/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The community practices for database metrics: https://opentelemetry.io/docs/specs/semconv/database/database-metrics/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the metrics names will be converted to Prometheus format when exporting to prometheus, like a.b.c-d will be converted to a_b_c_d

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointer. It is good to have such a converter, but can we simply avoid the - in the first place? It is dangerous and annoying if people want to write some expressions. - can be interpreted as minus.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's usual to use - as the seq for names, and mostly the name doesn't contain expressions.


private MetricNames() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.gravitino.metrics.source;

import com.codahale.metrics.Gauge;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.gravitino.metrics.MetricNames;

public class RelationDatasourceMetricsSource extends MetricsSource {

public RelationDatasourceMetricsSource(BasicDataSource dataSource) {
super(MetricsSource.GRAVITINO_SERVER_METRIC_NAME);
registerGauge(
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_ACTIVE_CONNECTIONS,
(Gauge<Integer>) dataSource::getNumActive);
registerGauge(
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_IDLE_CONNECTIONS,
(Gauge<Integer>) dataSource::getNumIdle);
registerGauge(
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_MAX_CONNECTIONS,
(Gauge<Integer>) dataSource::getMaxTotal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import org.apache.commons.pool2.impl.BaseObjectPoolConfig;
import org.apache.gravitino.Config;
import org.apache.gravitino.Configs;
import org.apache.gravitino.GravitinoEnv;
import org.apache.gravitino.metrics.MetricsSystem;
import org.apache.gravitino.metrics.source.RelationDatasourceMetricsSource;
import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType;
import org.apache.gravitino.storage.relational.mapper.CatalogMetaMapper;
import org.apache.gravitino.storage.relational.mapper.FilesetMetaMapper;
Expand Down Expand Up @@ -102,7 +105,12 @@ public void init(Config config) {
dataSource.setSoftMinEvictableIdleTimeMillis(
BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME.toMillis());
dataSource.setLifo(BaseObjectPoolConfig.DEFAULT_LIFO);

MetricsSystem metricsSystem = GravitinoEnv.getInstance().metricsSystem();
// Add null check to avoid NPE when metrics system is not initialized in test environments
if (metricsSystem != null) {
// Register connection pool metrics when metrics system is available
metricsSystem.register(new RelationDatasourceMetricsSource(dataSource));
}
// Create the transaction factory and env
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,35 @@ void testMapperConfig() {
+ "_"
+ Collector.sanitizeMetricName(MetricNames.HTTP_PROCESS_DURATION),
ImmutableMap.of("operation", "update-table"));

checkResult(
MetricsSource.GRAVITINO_SERVER_METRIC_NAME
+ "."
+ MetricNames.ENTITY_STORE_RELATION_DATASOURCE_ACTIVE_CONNECTIONS,
Collector.sanitizeMetricName(MetricsSource.GRAVITINO_SERVER_METRIC_NAME)
+ "_"
+ Collector.sanitizeMetricName(
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_ACTIVE_CONNECTIONS),
ImmutableMap.of());

checkResult(
MetricsSource.GRAVITINO_SERVER_METRIC_NAME
+ "."
+ MetricNames.ENTITY_STORE_RELATION_DATASOURCE_IDLE_CONNECTIONS,
Collector.sanitizeMetricName(MetricsSource.GRAVITINO_SERVER_METRIC_NAME)
+ "_"
+ Collector.sanitizeMetricName(
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_IDLE_CONNECTIONS),
ImmutableMap.of());

checkResult(
MetricsSource.GRAVITINO_SERVER_METRIC_NAME
+ "."
+ MetricNames.ENTITY_STORE_RELATION_DATASOURCE_MAX_CONNECTIONS,
Collector.sanitizeMetricName(MetricsSource.GRAVITINO_SERVER_METRIC_NAME)
+ "_"
+ Collector.sanitizeMetricName(
MetricNames.ENTITY_STORE_RELATION_DATASOURCE_MAX_CONNECTIONS),
ImmutableMap.of());
}
}