wxbty commented on code in PR #13275:
URL: https://github.com/apache/dubbo/pull/13275#discussion_r1382551765
##########
dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/ThreadRejectMetricsCountSampler.java:
##########
@@ -47,28 +48,23 @@ public
ThreadRejectMetricsCountSampler(DefaultMetricsCollector collector) {
this.collector.addSampler(this);
}
- public void addMetricName(String name){
+ public void addMetricName(String name) {
this.metricNames.add(name);
- this.initMetricsCounter(name,name);
+ this.initMetricsCounter(name, name);
samplesChanged.set(true);
}
@Override
public List<MetricSample> sample() {
- List<MetricSample> metricSamples = new ArrayList<>();
- metricNames.stream().forEach(name->collect(metricSamples,name));
- return metricSamples;
- }
-
-
- private void collect(List<MetricSample> list, String metricName) {
- count(list, metricName, MetricsKey.THREAD_POOL_THREAD_REJECT_COUNT);
- }
-
- private <T extends Metric> void count(List<MetricSample> list, String
metricName, MetricsKey metricsKey) {
- getCount(metricName).filter(e -> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) ->
- list.add(getGaugeMetricSample(metricsKey, k, THREAD_POOL, v,
AtomicLong::get))));
+ return metricNames
+ .stream()
+ .map(metricName -> getCount(metricName)
+ .entrySet()
+ .stream()
+ .map(ele ->
getGaugeMetricSample(MetricsKey.THREAD_POOL_THREAD_REJECT_COUNT, ele.getKey(),
THREAD_POOL, ele.getValue(), AtomicLong::get))
+ .collect(Collectors.toList()))
+ .filter(CollectionUtils::isNotEmpty)
+ .flatMap(Collection::stream).collect(Collectors.toList());
Review Comment:
This nesting is too complicated. Please define the code in the map as a
method and give it a meaningful name.
##########
dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSampler.java:
##########
@@ -72,13 +73,12 @@ public void addExecutors(String name, ExecutorService
executorService) {
@Override
public List<MetricSample> sample() {
- List<MetricSample> metricSamples = new ArrayList<>();
-
- sampleThreadPoolExecutor.forEach((name, executor) -> {
- metricSamples.addAll(createMetricsSample(name, executor));
- });
+ return sampleThreadPoolExecutor.entrySet()
+ .stream()
+ .map(ele -> createMetricsSample(ele.getKey(), ele.getValue()))
+ .flatMap(Collection::stream)
+ .collect(Collectors.toList());
Review Comment:
A map can be omitted. sampleThreadPoolExecutor.entrySet().stream()
.flatMap(entry -> createMetricsSample(entry.getKey(),
entry.getValue()).stream())
.collect(Collectors.toList());
##########
dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/MetricsGlobalRegistry.java:
##########
@@ -38,12 +37,10 @@ public class MetricsGlobalRegistry {
* 3. Dubbo's own CompositeMeterRegistry is used by default
*/
public static CompositeMeterRegistry getCompositeRegistry(ApplicationModel
applicationModel) {
- Optional<MetricsConfig> configOptional =
applicationModel.getApplicationConfigManager().getMetrics();
- if (configOptional.isPresent() &&
configOptional.get().getUseGlobalRegistry() != null &&
configOptional.get().getUseGlobalRegistry()) {
- return Metrics.globalRegistry;
- } else {
- return compositeRegistry;
- }
+ return applicationModel.getApplicationConfigManager().getMetrics()
+ .filter(MetricsConfig::getUseGlobalRegistry)
Review Comment:
useGlobalRegistry may be null, this filter will trigger NPE
--
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]