🐛 fix prometheus init failed when start (#12349)

* 🐛 fix prometheus init failed when start

* 🐛 fix prometheus init failed when start

* 🐛 fix prometheus init failed when start
This commit is contained in:
songxiaosheng 2023-05-19 15:30:18 +08:00 committed by GitHub
parent 6bd2174f01
commit c22db81a8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 18 deletions

View File

@ -38,6 +38,7 @@ import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.threadpool.manager.ExecutorRepository;
import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
@ -373,19 +374,9 @@ public class DefaultApplicationDeployer extends AbstractDeployer<ApplicationMode
Optional<MetricsConfig> configOptional = configManager.getMetrics();
// TODO compatible with old usage of metrics, remove protocol check after new metrics is ready for use.
boolean importMetricsPrometheus; // Use package references instead of config checks
try {
Class.forName("io.micrometer.prometheus.PrometheusConfig");
importMetricsPrometheus = true;
} catch (ClassNotFoundException e) {
importMetricsPrometheus = false;
}
if (!importMetricsPrometheus) {
//use old metrics
if (!isSupportPrometheus()) {
return;
}
MetricsConfig metricsConfig = configOptional.orElse(new MetricsConfig(applicationModel));
if (StringUtils.isBlank(metricsConfig.getProtocol())) {
metricsConfig.setProtocol(PROTOCOL_PROMETHEUS);
@ -399,6 +390,18 @@ public class DefaultApplicationDeployer extends AbstractDeployer<ApplicationMode
applicationModel.getBeanFactory().registerBean(metricsReporter);
}
public static boolean isSupportPrometheus() {
return isClassPresent("io.micrometer.prometheus.PrometheusConfig")
&& isClassPresent("io.prometheus.client.exporter.BasicAuthHttpConnectionFactory")
&& isClassPresent("io.prometheus.client.exporter.HttpConnectionFactory")
&& isClassPresent("io.prometheus.client.exporter.PushGateway");
}
private static boolean isClassPresent(String className) {
return ClassUtils.isPresent(className, DefaultApplicationDeployer.class.getClassLoader());
}
private boolean isUsedRegistryAsConfigCenter(RegistryConfig registryConfig) {
return isUsedRegistryAsCenter(registryConfig, registryConfig::getUseAsConfigCenter, "config",

View File

@ -0,0 +1,33 @@
/*
* 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.dubbo.config.deploy;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class DefaultApplicationDeployerTest {
@Test
void isSupportPrometheus() {
boolean supportPrometheus = new DefaultApplicationDeployer(ApplicationModel.defaultModel()).isSupportPrometheus();
Assert.assertTrue(supportPrometheus,"DefaultApplicationDeployer.isSupportPrometheus() should return true");
}
}

View File

@ -71,7 +71,7 @@ public class DefaultMetricsCollector extends CombMetricsCollector<RequestEvent>
protected void init(RtStatComposite rtStatComposite) {
super.init(rtStatComposite);
rtStatComposite.init(MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD),
MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD));
MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD));
}
});
super.setEventMulticaster(new DefaultSubDispatcher(this));
@ -146,17 +146,17 @@ public class DefaultMetricsCollector extends CombMetricsCollector<RequestEvent>
public List<MetricSample> sample() {
List<MetricSample> samples = new ArrayList<>();
this.getCount(MetricsEvent.Type.APPLICATION_INFO).filter(e -> !e.isEmpty())
.ifPresent(map -> map.forEach((k, v) ->
samples.add(new CounterMetricSample<>(APPLICATION_METRIC_INFO.getName(),
APPLICATION_METRIC_INFO.getDescription(),
k.getTags(), APPLICATION, v)))
);
.ifPresent(map -> map.forEach((k, v) ->
samples.add(new CounterMetricSample<>(APPLICATION_METRIC_INFO.getName(),
APPLICATION_METRIC_INFO.getDescription(),
k.getTags(), APPLICATION, v)))
);
return samples;
}
@Override
protected void countConfigure(
MetricsCountSampleConfigurer<String, MetricsEvent.Type, ApplicationMetric> sampleConfigure) {
MetricsCountSampleConfigurer<String, MetricsEvent.Type, ApplicationMetric> sampleConfigure) {
sampleConfigure.configureMetrics(configure -> new ApplicationMetric(applicationModel));
}
};