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

🐛 fix prometheus init failed when start #12349

Merged
merged 5 commits into from
May 19, 2023
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 @@ -38,6 +38,7 @@
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;
Expand Down Expand Up @@ -373,19 +374,9 @@ private void initMetricsReporter() {
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);
Expand All @@ -399,6 +390,18 @@ private void initMetricsReporter() {
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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected void init(MethodStatComposite methodStatComposite) {
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));
Expand Down Expand Up @@ -146,17 +146,17 @@ public boolean isSupport(MetricsEvent event) {
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));
}
};
Expand Down