songxiaosheng commented on code in PR #12273: URL: https://github.com/apache/dubbo/pull/12273#discussion_r1199958294
########## dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/DefaultMetricsReporter.java: ########## @@ -0,0 +1,92 @@ +/* + * 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.metrics.report; + +import io.micrometer.core.instrument.Counter; +import io.micrometer.core.instrument.Gauge; +import io.micrometer.core.instrument.Timer; +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.rpc.model.ApplicationModel; + +import java.util.HashMap; +import java.util.Map; + +import java.util.concurrent.TimeUnit; + +public class DefaultMetricsReporter extends AbstractMetricsReporter { + + SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry(); + + protected DefaultMetricsReporter(URL url, ApplicationModel applicationModel) { + super(url, applicationModel); + } + + @Override + public String getResponse() { + return null; + } + + @Override + public String getResponseWithName(String metricsName) { + //name->tags->value + Map<String, Map<String, Object>> result = new HashMap<>(); + StringBuilder sb = new StringBuilder(); + meterRegistry.getMeters().stream().filter(meter -> { + if (metricsName != null) { + return meter.getId().getName().contains(metricsName); Review Comment: meter and getId and getName need add Null check ########## dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java: ########## @@ -369,27 +371,47 @@ private void initMetricsService() { } private void initMetricsReporter() { + if (!isSupportMetrics()) { + return; + } DefaultMetricsCollector collector = applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class); Optional<MetricsConfig> configOptional = configManager.getMetrics(); - - // TODO compatible with old usage of metrics, remove protocol check after new metrics is ready for use. - if (!isSupportPrometheus()) { - return; + boolean importMetricsPrometheus; + try { + Class.forName("io.micrometer.prometheus.PrometheusConfig"); + importMetricsPrometheus = true; + } catch (ClassNotFoundException e) { + importMetricsPrometheus = false; } + //If no specific metrics type is configured and there is no Prometheus dependency in the dependencies. MetricsConfig metricsConfig = configOptional.orElse(new MetricsConfig(applicationModel)); if (StringUtils.isBlank(metricsConfig.getProtocol())) { - metricsConfig.setProtocol(PROTOCOL_PROMETHEUS); + metricsConfig.setProtocol(importMetricsPrometheus ? PROTOCOL_PROMETHEUS : PROTOCOL_DEFAULT); } collector.setCollectEnabled(true); collector.collectApplication(); collector.setThreadpoolCollectEnabled(Optional.ofNullable(metricsConfig.getEnableThreadpool()).orElse(true)); MetricsReporterFactory metricsReporterFactory = getExtensionLoader(MetricsReporterFactory.class).getAdaptiveExtension(); - MetricsReporter metricsReporter = metricsReporterFactory.createMetricsReporter(metricsConfig.toUrl()); + MetricsReporter metricsReporter = null; + try { + metricsReporter = metricsReporterFactory.createMetricsReporter(metricsConfig.toUrl()); + } catch (IllegalStateException e) { + if (e.getMessage().startsWith("No such extension org.apache.dubbo.metrics.report.MetricsReporterFactory")) { + logger.warn(COMMON_METRICS_COLLECTOR_EXCEPTION, "", "", e.getMessage()); + return; + } else { + throw e; + } + } metricsReporter.init(); applicationModel.getBeanFactory().registerBean(metricsReporter); } + public boolean isSupportMetrics() { + return isClassPresent("io.micrometer.core.instrument.MeterRegistry"); Review Comment: this method is replaced by newer method isSupportPrometheus() -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
