diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java index 04bb400ee31..55e92df0884 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java @@ -34,6 +34,8 @@ public interface MetricsConstants { String TAG_VERSION_KEY = "version"; + String TAG_APPLICATION_VERSION_KEY = "application.version"; + String ENABLE_JVM_METRICS_KEY = "enable.jvm.metrics"; String AGGREGATION_COLLECTOR_KEY = "aggregation"; diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java index 6736730641b..25115c169db 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java @@ -17,6 +17,7 @@ package org.apache.dubbo.config.deploy; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.Version; import org.apache.dubbo.common.config.ConfigurationUtils; import org.apache.dubbo.common.config.Environment; import org.apache.dubbo.common.config.ReferenceCache; @@ -364,6 +365,7 @@ private void initMetricsReporter() { // TODO compatible with old usage of metrics, remove protocol check after new metrics is ready for use. if (metricsConfig != null && PROTOCOL_PROMETHEUS.equals(metricsConfig.getProtocol())) { collector.setCollectEnabled(true); + collector.addApplicationInfo(applicationModel.getApplicationName(), Version.getVersion()); String protocol = metricsConfig.getProtocol(); if (StringUtils.isNotEmpty(protocol)) { MetricsReporterFactory metricsReporterFactory = getExtensionLoader(MetricsReporterFactory.class).getAdaptiveExtension(); diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatHandler.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatHandler.java index d2c800ed5a0..d9b2e0d25a0 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatHandler.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatHandler.java @@ -30,4 +30,7 @@ public interface MetricsStatHandler { MetricsEvent increase(String applicationName, Invocation invocation); MetricsEvent decrease(String applicationName, Invocation invocation); + + MetricsEvent addApplication(String applicationName, String version); + } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/ApplicationEvent.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/ApplicationEvent.java new file mode 100644 index 00000000000..07e762bf940 --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/ApplicationEvent.java @@ -0,0 +1,36 @@ +/* + * 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.event; + +public class ApplicationEvent extends MetricsEvent{ + private ApplicationEvent.Type type; + + public ApplicationEvent(Object source, ApplicationEvent.Type type) { + super(source); + this.type = type; + } + + public ApplicationEvent.Type getType() { + return type; + } + + public void setType(ApplicationEvent.Type type) { + this.type = type; + } + +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java index cd2579815fe..dee72086cbc 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java @@ -44,4 +44,16 @@ public Object getSource() { public String toString() { return getClass().getName() + "[source=" + source + "]"; } + + public enum Type { + TOTAL, + SUCCEED, + BUSINESS_FAILED, + REQUEST_TIMEOUT, + REQUEST_LIMIT, + PROCESSING, + UNKNOWN_FAILED, + TOTAL_FAILED, + APPLICATION_INFO + } } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/RequestEvent.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/RequestEvent.java index 7808d8fc189..cfa639cae2d 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/RequestEvent.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/RequestEvent.java @@ -36,14 +36,5 @@ public void setType(Type type) { this.type = type; } - public enum Type { - TOTAL, - SUCCEED, - BUSINESS_FAILED, - REQUEST_TIMEOUT, - REQUEST_LIMIT, - PROCESSING, - UNKNOWN_FAILED, - TOTAL_FAILED - } + } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ApplicationMetric.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ApplicationMetric.java new file mode 100644 index 00000000000..773dfea5ac4 --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ApplicationMetric.java @@ -0,0 +1,65 @@ +/* + * 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.model; + +import java.util.HashMap; +import java.util.Map; + +import static org.apache.dubbo.common.constants.MetricsConstants.TAG_HOSTNAME; +import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_VERSION_KEY; +import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME; +import static org.apache.dubbo.common.constants.MetricsConstants.TAG_IP; +import static org.apache.dubbo.common.utils.NetUtils.getLocalHost; +import static org.apache.dubbo.common.utils.NetUtils.getLocalHostName; + +public class ApplicationMetric implements Metric { + private String applicationName; + private String version; + + public ApplicationMetric(String applicationName, String version) { + this.applicationName = applicationName; + this.version = version; + } + + public String getApplicationName() { + return applicationName; + } + + public void setApplicationName(String applicationName) { + this.applicationName = applicationName; + } + + public String getData() { + return version; + } + + public void setData(String version) { + this.version = version; + } + + @Override + public Map getTags() { + Map tags = new HashMap<>(); + tags.put(TAG_IP, getLocalHost()); + tags.put(TAG_HOSTNAME, getLocalHostName()); + tags.put(TAG_APPLICATION_NAME, applicationName); + + tags.put(TAG_APPLICATION_VERSION_KEY, version); + return tags; + } +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java index 79f49a56e65..2cece7f77d5 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java @@ -38,7 +38,7 @@ /** * Metric class for method. */ -public class MethodMetric { +public class MethodMetric implements Metric { private String applicationName; private String interfaceName; private String methodName; diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/Metric.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/Metric.java new file mode 100644 index 00000000000..c9baa9c0da0 --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/Metric.java @@ -0,0 +1,24 @@ +/* + * 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.model; + +import java.util.Map; + +public interface Metric { + Map getTags(); +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsCategory.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsCategory.java index 4f059b059b0..dd0df8da47b 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsCategory.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsCategory.java @@ -24,4 +24,5 @@ public enum MetricsCategory { RT, QPS, REQUESTS, + APPLICATION } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java index e50ec855ae7..147e08a2de5 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java @@ -18,6 +18,7 @@ package org.apache.dubbo.metrics.model; public enum MetricsKey { + APPLICATION_METRIC_INFO("dubbo.application.info.total", "Total Application Info"), // provider metrics key PROVIDER_METRIC_REQUESTS("dubbo.provider.requests.total", "Total Requests"), diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java index 535d66e6001..c44c9b0b579 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java @@ -101,7 +101,7 @@ private void onRTEvent(RTEvent event) { private void onRequestEvent(RequestEvent event) { MethodMetric metric = (MethodMetric) event.getSource(); - RequestEvent.Type type = event.getType(); + MetricsEvent.Type type = event.getType(); TimeWindowCounter counter = null; switch (type) { case TOTAL: diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java index a14d42d5198..5835b920519 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java @@ -21,7 +21,6 @@ import org.apache.dubbo.metrics.collector.stat.MetricsStatHandler; import org.apache.dubbo.metrics.event.EmptyEvent; import org.apache.dubbo.metrics.event.MetricsEvent; -import org.apache.dubbo.metrics.event.RequestEvent; import org.apache.dubbo.metrics.event.SimpleMetricsEventMulticaster; import org.apache.dubbo.metrics.listener.MetricsListener; import org.apache.dubbo.metrics.model.MetricsKey; @@ -36,6 +35,7 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.function.Function; +import static org.apache.dubbo.metrics.model.MetricsCategory.APPLICATION; import static org.apache.dubbo.metrics.model.MetricsCategory.REQUESTS; import static org.apache.dubbo.metrics.model.MetricsCategory.RT; @@ -63,85 +63,96 @@ public Boolean isCollectEnabled() { } public void increaseTotalRequests(String applicationName, Invocation invocation) { - increaseAndPublishEvent(applicationName, RequestEvent.Type.TOTAL, invocation); + increaseAndPublishEvent(applicationName, MetricsEvent.Type.TOTAL, invocation); } public void increaseSucceedRequests(String applicationName, Invocation invocation) { - increaseAndPublishEvent(applicationName, RequestEvent.Type.SUCCEED, invocation); + increaseAndPublishEvent(applicationName, MetricsEvent.Type.SUCCEED, invocation); } public void increaseUnknownFailedRequests(String applicationName, Invocation invocation) { - increaseAndPublishEvent(applicationName, RequestEvent.Type.UNKNOWN_FAILED, invocation); + increaseAndPublishEvent(applicationName, MetricsEvent.Type.UNKNOWN_FAILED, invocation); } public void businessFailedRequests(String applicationName, Invocation invocation) { - increaseAndPublishEvent(applicationName, RequestEvent.Type.BUSINESS_FAILED, invocation); + increaseAndPublishEvent(applicationName, MetricsEvent.Type.BUSINESS_FAILED, invocation); } public void timeoutRequests(String applicationName, Invocation invocation) { - increaseAndPublishEvent(applicationName,RequestEvent.Type.REQUEST_TIMEOUT, invocation); + increaseAndPublishEvent(applicationName,MetricsEvent.Type.REQUEST_TIMEOUT, invocation); } public void limitRequests(String applicationName, Invocation invocation) { - increaseAndPublishEvent(applicationName,RequestEvent.Type.REQUEST_LIMIT, invocation); + increaseAndPublishEvent(applicationName,MetricsEvent.Type.REQUEST_LIMIT, invocation); } public void increaseProcessingRequests(String applicationName, Invocation invocation) { - increaseAndPublishEvent(applicationName,RequestEvent.Type.PROCESSING, invocation); + increaseAndPublishEvent(applicationName,MetricsEvent.Type.PROCESSING, invocation); } public void decreaseProcessingRequests(String applicationName, Invocation invocation) { - decreaseAndPublishEvent(applicationName,RequestEvent.Type.PROCESSING, invocation); + decreaseAndPublishEvent(applicationName,MetricsEvent.Type.PROCESSING, invocation); } public void totalFailedRequests(String applicationName, Invocation invocation) { - increaseAndPublishEvent(applicationName,RequestEvent.Type.TOTAL_FAILED, invocation); + increaseAndPublishEvent(applicationName,MetricsEvent.Type.TOTAL_FAILED, invocation); } - private void increaseAndPublishEvent(String applicationName, RequestEvent.Type total, Invocation invocation) { + private void increaseAndPublishEvent(String applicationName, MetricsEvent.Type total, Invocation invocation) { this.eventMulticaster.publishEvent(doExecute(total, statHandler -> statHandler.increase(applicationName,invocation))); } - private void decreaseAndPublishEvent(String applicationName, RequestEvent.Type total, Invocation invocation) { - this.eventMulticaster.publishEvent(doExecute(total, statHandler -> statHandler.decrease(applicationName,invocation))); + private void decreaseAndPublishEvent(String applicationName, MetricsEvent.Type type, Invocation invocation) { + this.eventMulticaster.publishEvent(doExecute(type, statHandler -> statHandler.decrease(applicationName,invocation))); } public void addRT(String applicationName,Invocation invocation, Long responseTime) { this.eventMulticaster.publishEvent(stats.addRtAndRetrieveEvent(applicationName,invocation, responseTime)); } - + public void addApplicationInfo(String applicationName, String version) { + doExecute(MetricsEvent.Type.APPLICATION_INFO, statHandler -> statHandler.addApplication(applicationName,version)); + } @Override public List collect() { List list = new ArrayList<>(); + collectApplication(list); collectRequests(list); collectRT(list); return list; } + private void collectApplication(List list) { + doCollect(MetricsEvent.Type.APPLICATION_INFO, MetricsStatHandler::get).filter(e -> !e.isEmpty()) + .ifPresent(map -> map.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.APPLICATION_METRIC_INFO, k.getTags(), + APPLICATION, v::get)))); + + + } + private void collectRequests(List list) { - doCollect(RequestEvent.Type.TOTAL, MetricsStatHandler::get).filter(e -> !e.isEmpty()) + doCollect(MetricsEvent.Type.TOTAL, MetricsStatHandler::get).filter(e -> !e.isEmpty()) .ifPresent(map -> map.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS, k.getTags(), REQUESTS, v::get)))); - doCollect(RequestEvent.Type.SUCCEED, MetricsStatHandler::get).filter(e -> !e.isEmpty()) + doCollect(MetricsEvent.Type.SUCCEED, MetricsStatHandler::get).filter(e -> !e.isEmpty()) .ifPresent(map -> map.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED, k.getTags(), REQUESTS, v::get)))); - doCollect(RequestEvent.Type.UNKNOWN_FAILED, MetricsStatHandler::get).filter(e -> !e.isEmpty()) + doCollect(MetricsEvent.Type.UNKNOWN_FAILED, MetricsStatHandler::get).filter(e -> !e.isEmpty()) .ifPresent(map -> map.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_FAILED, k.getTags(), REQUESTS, v::get)))); - doCollect(RequestEvent.Type.PROCESSING, MetricsStatHandler::get).filter(e -> !e.isEmpty()) + doCollect(MetricsEvent.Type.PROCESSING, MetricsStatHandler::get).filter(e -> !e.isEmpty()) .ifPresent(map -> map.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_PROCESSING, k.getTags(), REQUESTS, v::get)))); - doCollect(RequestEvent.Type.BUSINESS_FAILED, MetricsStatHandler::get).filter(e -> !e.isEmpty()) + doCollect(MetricsEvent.Type.BUSINESS_FAILED, MetricsStatHandler::get).filter(e -> !e.isEmpty()) .ifPresent(map -> map.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUEST_BUSINESS_FAILED, k.getTags(), REQUESTS, v::get)))); - doCollect(RequestEvent.Type.REQUEST_TIMEOUT, MetricsStatHandler::get).filter(e -> !e.isEmpty()) + doCollect(MetricsEvent.Type.REQUEST_TIMEOUT, MetricsStatHandler::get).filter(e -> !e.isEmpty()) .ifPresent(map -> map.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_TIMEOUT, k.getTags(), REQUESTS, v::get)))); - doCollect(RequestEvent.Type.REQUEST_LIMIT, MetricsStatHandler::get).filter(e -> !e.isEmpty()) + doCollect(MetricsEvent.Type.REQUEST_LIMIT, MetricsStatHandler::get).filter(e -> !e.isEmpty()) .ifPresent(map -> map.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_LIMIT, k.getTags(), REQUESTS, v::get)))); - doCollect(RequestEvent.Type.TOTAL_FAILED, MetricsStatHandler::get).filter(e -> !e.isEmpty()) + doCollect(MetricsEvent.Type.TOTAL_FAILED, MetricsStatHandler::get).filter(e -> !e.isEmpty()) .ifPresent(map -> map.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_TOTAL_FAILED, k.getTags(), REQUESTS, v::get)))); } @@ -162,19 +173,19 @@ private void collectRT(List list) { } private < - T> Optional doCollect(RequestEvent.Type requestType, Function statExecutor) { + T> Optional doCollect(MetricsEvent.Type metricsEventType, Function statExecutor) { if (isCollectEnabled()) { - MetricsStatHandler handler = stats.getHandler(requestType); + MetricsStatHandler handler = stats.getHandler(metricsEventType); T result = statExecutor.apply(handler); return Optional.ofNullable(result); } return Optional.empty(); } - private MetricsEvent doExecute(RequestEvent.Type - requestType, Function statExecutor) { + private MetricsEvent doExecute(MetricsEvent.Type metricsEventType, + Function statExecutor) { if (isCollectEnabled()) { - MetricsStatHandler handler = stats.getHandler(requestType); + MetricsStatHandler handler = stats.getHandler(metricsEventType); return statExecutor.apply(handler); } return EmptyEvent.instance(); diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/DefaultMetricsStatHandler.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/DefaultMetricsStatHandler.java index 44ad17fadee..077fd41f644 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/DefaultMetricsStatHandler.java +++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/DefaultMetricsStatHandler.java @@ -23,6 +23,7 @@ import org.apache.dubbo.metrics.event.EmptyEvent; import org.apache.dubbo.metrics.event.MetricsEvent; +import org.apache.dubbo.metrics.model.ApplicationMetric; import org.apache.dubbo.metrics.model.MethodMetric; import org.apache.dubbo.rpc.Invocation; @@ -31,6 +32,8 @@ public class DefaultMetricsStatHandler implements MetricsStatHandler { private final Map counts = new ConcurrentHashMap<>(); + private final Map applicationMetrics = new ConcurrentHashMap<>(); + public DefaultMetricsStatHandler() { } @@ -42,7 +45,13 @@ public MetricsEvent increase(String applicationName, Invocation invocation) { public MetricsEvent decrease(String applicationName, Invocation invocation) { return this.doDecrExecute(applicationName,invocation); } - + @Override + public MetricsEvent addApplication(String applicationName, String version) { + ApplicationMetric applicationMetric = new ApplicationMetric(applicationName, version); + AtomicLong count = applicationMetrics.computeIfAbsent(applicationMetric, k -> new AtomicLong(0L)); + count.incrementAndGet(); + return EmptyEvent.instance(); + } protected MetricsEvent doExecute(String applicationName, Invocation invocation, BiConsumer> execute) { MethodMetric metric = new MethodMetric(applicationName, invocation); execute.accept(metric, counts); @@ -73,4 +82,10 @@ public Map get() { public MetricsEvent retrieveMetricsEvent(MethodMetric metric) { return EmptyEvent.instance(); } + + public Map getApplicationMetric() { + return applicationMetrics; + } + + } diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatComposite.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatComposite.java index cff37bb878d..f1f4157af9e 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatComposite.java +++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatComposite.java @@ -34,7 +34,7 @@ public class MetricsStatComposite { - public Map stats = new ConcurrentHashMap<>(); + public Map stats = new ConcurrentHashMap<>(); private final ConcurrentMap lastRT = new ConcurrentHashMap<>(); private final ConcurrentMap minRT = new ConcurrentHashMap<>(); private final ConcurrentMap maxRT = new ConcurrentHashMap<>(); @@ -48,7 +48,7 @@ public MetricsStatComposite(DefaultMetricsCollector collector) { this.init(); } - public MetricsStatHandler getHandler(RequestEvent.Type statType) { + public MetricsStatHandler getHandler(MetricsEvent.Type statType) { return stats.get(statType); } @@ -101,19 +101,24 @@ public MetricsEvent addRtAndRetrieveEvent(String applicationName, Invocation inv return new RTEvent(metric, responseTime); } + public void addApplicationInfo(String applicationName, String version) { + MetricsStatHandler metricsStatHandler = stats.get(MetricsEvent.Type.APPLICATION_INFO); + metricsStatHandler.addApplication(applicationName, version); + } private void init() { - stats.put(RequestEvent.Type.TOTAL, buildMetricsStatHandler(RequestEvent.Type.TOTAL)); - stats.put(RequestEvent.Type.SUCCEED, buildMetricsStatHandler(RequestEvent.Type.SUCCEED)); - stats.put(RequestEvent.Type.UNKNOWN_FAILED, buildMetricsStatHandler(RequestEvent.Type.UNKNOWN_FAILED)); - stats.put(RequestEvent.Type.BUSINESS_FAILED, buildMetricsStatHandler(RequestEvent.Type.BUSINESS_FAILED)); - stats.put(RequestEvent.Type.PROCESSING, new DefaultMetricsStatHandler()); - stats.put(RequestEvent.Type.REQUEST_LIMIT, buildMetricsStatHandler(RequestEvent.Type.REQUEST_LIMIT)); - stats.put(RequestEvent.Type.REQUEST_TIMEOUT, buildMetricsStatHandler(RequestEvent.Type.REQUEST_TIMEOUT)); - stats.put(RequestEvent.Type.TOTAL_FAILED, buildMetricsStatHandler(RequestEvent.Type.TOTAL_FAILED)); + stats.put(MetricsEvent.Type.TOTAL, buildMetricsStatHandler(MetricsEvent.Type.TOTAL)); + stats.put(MetricsEvent.Type.SUCCEED, buildMetricsStatHandler(MetricsEvent.Type.SUCCEED)); + stats.put(MetricsEvent.Type.UNKNOWN_FAILED, buildMetricsStatHandler(MetricsEvent.Type.UNKNOWN_FAILED)); + stats.put(MetricsEvent.Type.BUSINESS_FAILED, buildMetricsStatHandler(MetricsEvent.Type.BUSINESS_FAILED)); + stats.put(MetricsEvent.Type.PROCESSING, new DefaultMetricsStatHandler()); + stats.put(MetricsEvent.Type.REQUEST_LIMIT, buildMetricsStatHandler(MetricsEvent.Type.REQUEST_LIMIT)); + stats.put(MetricsEvent.Type.REQUEST_TIMEOUT, buildMetricsStatHandler(MetricsEvent.Type.REQUEST_TIMEOUT)); + stats.put(MetricsEvent.Type.TOTAL_FAILED, buildMetricsStatHandler(MetricsEvent.Type.TOTAL_FAILED)); + stats.put(MetricsEvent.Type.APPLICATION_INFO, buildMetricsStatHandler(MetricsEvent.Type.APPLICATION_INFO)); } - private DefaultMetricsStatHandler buildMetricsStatHandler(RequestEvent.Type type) { + private DefaultMetricsStatHandler buildMetricsStatHandler(MetricsEvent.Type type) { return new DefaultMetricsStatHandler() { @Override public MetricsEvent retrieveMetricsEvent(MethodMetric metric) { @@ -121,4 +126,6 @@ public MetricsEvent retrieveMetricsEvent(MethodMetric metric) { } }; } + + } diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java index b9874cb5bca..38ac41f162c 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java +++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java @@ -33,7 +33,6 @@ import org.apache.dubbo.common.lang.ShutdownHookCallbacks; import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.metrics.DubboMetrics; import org.apache.dubbo.metrics.collector.AggregateMetricsCollector; import org.apache.dubbo.metrics.collector.DefaultMetricsCollector; @@ -45,9 +44,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import static org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_METRICS_COLLECTOR_EXCEPTION; @@ -68,10 +64,7 @@ public abstract class AbstractMetricsReporter implements MetricsReporter { protected final CompositeMeterRegistry compositeRegistry = new CompositeMeterRegistry(); private final ApplicationModel applicationModel; - private ScheduledExecutorService collectorSyncJobExecutor = null; - private static final int DEFAULT_SCHEDULE_INITIAL_DELAY = 5; - private static final int DEFAULT_SCHEDULE_PERIOD = 30; protected AbstractMetricsReporter(URL url, ApplicationModel applicationModel) { this.url = url; @@ -83,7 +76,6 @@ public void init() { if (initialized.compareAndSet(false, true)) { addJvmMetrics(); initCollectors(); - scheduleMetricsCollectorSyncJob(); doInit(); @@ -134,45 +126,41 @@ private void initCollectors() { collectors.add(applicationModel.getBeanFactory().getBean(AggregateMetricsCollector.class)); } - private void scheduleMetricsCollectorSyncJob() { - NamedThreadFactory threadFactory = new NamedThreadFactory("metrics-collector-sync-job", true); - collectorSyncJobExecutor = Executors.newScheduledThreadPool(1, threadFactory); - collectorSyncJobExecutor.scheduleWithFixedDelay(() -> { - addDubboMeterRegistry(); - collectors.forEach(collector -> { - List samples = collector.collect(); - for (MetricSample sample : samples) { - try { - switch (sample.getType()) { - case GAUGE: - GaugeMetricSample gaugeSample = (GaugeMetricSample) sample; - List tags = new ArrayList<>(); - gaugeSample.getTags().forEach((k, v) -> { - if (v == null) { - v = ""; - } - - tags.add(Tag.of(k, v)); - }); - - Gauge.builder(gaugeSample.getName(), gaugeSample.getSupplier()) - .description(gaugeSample.getDescription()).tags(tags).register(compositeRegistry); - break; - case COUNTER: - case TIMER: - case LONG_TASK_TIMER: - case DISTRIBUTION_SUMMARY: - // TODO - break; - default: - break; - } - } catch (Exception e) { - logger.error(COMMON_METRICS_COLLECTOR_EXCEPTION, "", "", "error occurred when synchronize metrics collector.", e); + public void refreshData() { + addDubboMeterRegistry(); + collectors.forEach(collector -> { + List samples = collector.collect(); + for (MetricSample sample : samples) { + try { + switch (sample.getType()) { + case GAUGE: + GaugeMetricSample gaugeSample = (GaugeMetricSample) sample; + List tags = new ArrayList<>(); + gaugeSample.getTags().forEach((k, v) -> { + if (v == null) { + v = ""; + } + + tags.add(Tag.of(k, v)); + }); + + Gauge.builder(gaugeSample.getName(), gaugeSample.getSupplier()) + .description(gaugeSample.getDescription()).tags(tags).register(compositeRegistry); + break; + case COUNTER: + case TIMER: + case LONG_TASK_TIMER: + case DISTRIBUTION_SUMMARY: + // TODO + break; + default: + break; } + } catch (Exception e) { + logger.error(COMMON_METRICS_COLLECTOR_EXCEPTION, "", "", "error occurred when synchronize metrics collector.", e); } - }); - }, DEFAULT_SCHEDULE_INITIAL_DELAY, DEFAULT_SCHEDULE_PERIOD, TimeUnit.SECONDS); + } + }); } private void registerDubboShutdownHook() { @@ -180,10 +168,6 @@ private void registerDubboShutdownHook() { } public void destroy() { - if (collectorSyncJobExecutor != null) { - collectorSyncJobExecutor.shutdownNow(); - } - doDestroy(); } diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java index bc9346e33d9..543791aa542 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java +++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java @@ -157,7 +157,7 @@ void testListener() { collector.increaseTotalRequests(applicationName, invocation); Assertions.assertNotNull(mockListener.getCurEvent()); Assertions.assertTrue(mockListener.getCurEvent() instanceof RequestEvent); - Assertions.assertEquals(((RequestEvent) mockListener.getCurEvent()).getType(), RequestEvent.Type.TOTAL); + Assertions.assertEquals(((RequestEvent) mockListener.getCurEvent()).getType(), MetricsEvent.Type.TOTAL); collector.addRT(applicationName, invocation, 5L); Assertions.assertTrue(mockListener.getCurEvent() instanceof RTEvent); diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/event/RequestEventTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/event/RequestEventTest.java index 742c5b7be90..d4031e90d50 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/event/RequestEventTest.java +++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/event/RequestEventTest.java @@ -17,6 +17,7 @@ package org.apache.dubbo.metrics.metrics.event; +import org.apache.dubbo.metrics.event.MetricsEvent; import org.apache.dubbo.metrics.event.RequestEvent; import org.apache.dubbo.metrics.model.MethodMetric; import org.junit.jupiter.api.Assertions; @@ -27,7 +28,7 @@ class RequestEventTest { @Test void testNewEvent() { MethodMetric metric = new MethodMetric(); - RequestEvent.Type type = RequestEvent.Type.TOTAL; + MetricsEvent.Type type = MetricsEvent.Type.TOTAL; RequestEvent event = new RequestEvent(metric, type); Assertions.assertEquals(event.getSource(), metric); diff --git a/dubbo-metrics/dubbo-metrics-prometheus/src/main/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsReporter.java b/dubbo-metrics/dubbo-metrics-prometheus/src/main/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsReporter.java index 970bab51429..d110751fb2a 100644 --- a/dubbo-metrics/dubbo-metrics-prometheus/src/main/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsReporter.java +++ b/dubbo-metrics/dubbo-metrics-prometheus/src/main/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsReporter.java @@ -87,6 +87,7 @@ private void exportHttpServer() { try { prometheusExporterHttpServer = HttpServer.create(new InetSocketAddress(port), 0); prometheusExporterHttpServer.createContext(path, httpExchange -> { + refreshData(); String response = prometheusRegistry.scrape(); httpExchange.sendResponseHeaders(200, response.getBytes().length); try (OutputStream os = httpExchange.getResponseBody()) { @@ -124,6 +125,7 @@ private void schedulePushJob() { protected void push(PushGateway pushGateway, String job) { try { + refreshData(); pushGateway.pushAdd(prometheusRegistry.getPrometheusRegistry(), job); } catch (IOException e) { logger.error(COMMON_METRICS_COLLECTOR_EXCEPTION, "", "", "Error occurred when pushing metrics to prometheus: ", e); diff --git a/dubbo-metrics/dubbo-metrics-prometheus/src/test/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsReporterTest.java b/dubbo-metrics/dubbo-metrics-prometheus/src/test/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsReporterTest.java index 333286f67e5..4f0dab159ba 100644 --- a/dubbo-metrics/dubbo-metrics-prometheus/src/test/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsReporterTest.java +++ b/dubbo-metrics/dubbo-metrics-prometheus/src/test/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsReporterTest.java @@ -21,9 +21,11 @@ import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.MetricsConfig; import org.apache.dubbo.config.nested.PrometheusConfig; +import org.apache.dubbo.metrics.collector.DefaultMetricsCollector; import org.apache.dubbo.rpc.model.ApplicationModel; import io.micrometer.prometheus.PrometheusMeterRegistry; +import org.apache.dubbo.rpc.model.FrameworkModel; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; @@ -46,12 +48,15 @@ class PrometheusMetricsReporterTest { private MetricsConfig metricsConfig; private ApplicationModel applicationModel; + private FrameworkModel frameworkModel; @BeforeEach public void setup() { metricsConfig = new MetricsConfig(); applicationModel = ApplicationModel.defaultModel(); metricsConfig.setProtocol(PROTOCOL_PROMETHEUS); + frameworkModel = FrameworkModel.defaultModel(); + frameworkModel.getBeanFactory().getOrRegisterBean(DefaultMetricsCollector.class); } @AfterEach @@ -71,15 +76,15 @@ void testJvmMetrics() { PrometheusMeterRegistry prometheusRegistry = reporter.getPrometheusRegistry(); Double d1 = prometheusRegistry.getPrometheusRegistry().getSampleValue("none_exist_metric"); Double d2 = prometheusRegistry.getPrometheusRegistry().getSampleValue("jvm_gc_memory_promoted_bytes_total", - new String[]{"application_name"},new String[]{name}); + new String[]{"application_name"}, new String[]{name}); Assertions.assertNull(d1); Assertions.assertNotNull(d2); } @Test void testExporter() { - int port = NetUtils.getAvailablePort(); - + int port = 31539; +// NetUtils.getAvailablePort(); PrometheusConfig prometheusConfig = new PrometheusConfig(); PrometheusConfig.Exporter exporter = new PrometheusConfig.Exporter(); exporter.setMetricsPort(port); @@ -88,10 +93,15 @@ void testExporter() { prometheusConfig.setExporter(exporter); metricsConfig.setPrometheus(prometheusConfig); metricsConfig.setEnableJvmMetrics(true); + ApplicationModel.defaultModel().getApplicationConfigManager().setApplication(new ApplicationConfig("metrics-test")); PrometheusMetricsReporter reporter = new PrometheusMetricsReporter(metricsConfig.toUrl(), applicationModel); reporter.init(); - + try { + Thread.sleep(60000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } try (CloseableHttpClient client = HttpClients.createDefault()) { HttpGet request = new HttpGet("http://localhost:" + port + "/metrics"); CloseableHttpResponse response = client.execute(request);