Repository: apache/dubbo
File: dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java (Java)

Code before:
    private boolean enableRtPxx;
    private boolean enableRt;
    private boolean enableRequest;

    private final ConcurrentMap<MethodMetric, TimeWindowAggregator> rtAgr = new ConcurrentHashMap<>();


    public AggregateMetricsCollector(ApplicationModel applicationModel) {
        this.applicationModel = applicationModel;
        ConfigManager configManager = applicationModel.getApplicationConfigManager();
        if (isCollectEnabled()) {
            // only registered when aggregation is enabled.
            Optional<MetricsConfig> optional = configManager.getMetrics();
            if (optional.isPresent()) {
                registerListener();
                AggregationConfig aggregation = optional.get().getAggregation();
                this.bucketNum = aggregation.getBucketNum() == null ?
                    DEFAULT_BUCKET_NUM : aggregation.getBucketNum();
                this.timeWindowSeconds = aggregation.getTimeWindowSeconds() == null ?
                    DEFAULT_TIME_WINDOW_SECONDS : aggregation.getTimeWindowSeconds();
                this.qpsTimeWindowMillSeconds = aggregation.getQpsTimeWindowMillSeconds() == null ?
                    DEFAULT_QPS_TIME_WINDOW_MILL_SECONDS : aggregation.getQpsTimeWindowMillSeconds();
                this.enableQps = aggregation.getEnableQps() == null || aggregation.getEnableQps();
                this.enableRtPxx = aggregation.getEnableRtPxx() == null || aggregation.getEnableRtPxx();
                this.enableRt = aggregation.getEnableRt() == null || aggregation.getEnableRt();
                this.enableRequest = aggregation.getEnableRequest() == null || aggregation.getEnableRequest();
            }
        }
    }

    public void setCollectEnabled(Boolean collectEnabled) {
        if (collectEnabled != null) {
            this.collectEnabled = collectEnabled;
        }
    }


    @Override
    public boolean isCollectEnabled() {
        if (collectEnabled == null) {
            ConfigManager configManager = applicationModel.getApplicationConfigManager();
            configManager.getMetrics().ifPresent(metricsConfig -> setCollectEnabled(metricsConfig.getAggregation().getEnabled()));
        }
        return Optional.ofNullable(collectEnabled).orElse(true);
    }

    @Override
    public boolean isSupport(MetricsEvent event) {
        return event instanceof RequestEvent;
    }


Review comment (suggestion):
Maybe it could be written more cohesive
public boolean getEnableQps() {
        return Optional.ofNullable(enableQps).orElse(true);
    }

Code after:
    private boolean enableRtPxx;
    private boolean enableRt;
    private boolean enableRequest;

    private final ConcurrentMap<MethodMetric, TimeWindowAggregator> rtAgr = new ConcurrentHashMap<>();


    public AggregateMetricsCollector(ApplicationModel applicationModel) {
        this.applicationModel = applicationModel;
        ConfigManager configManager = applicationModel.getApplicationConfigManager();
        if (isCollectEnabled()) {
            // only registered when aggregation is enabled.
            Optional<MetricsConfig> optional = configManager.getMetrics();
            if (optional.isPresent()) {
                registerListener();
                AggregationConfig aggregation = optional.get().getAggregation();
                this.bucketNum = Optional.ofNullable(aggregation.getBucketNum()).orElse(DEFAULT_BUCKET_NUM);
                this.timeWindowSeconds = Optional.ofNullable(aggregation.getTimeWindowSeconds())
                    .orElse(DEFAULT_TIME_WINDOW_SECONDS);
                this.qpsTimeWindowMillSeconds = Optional.ofNullable(aggregation.getQpsTimeWindowMillSeconds())
                    .orElse(DEFAULT_QPS_TIME_WINDOW_MILL_SECONDS);
                this.enableQps = Optional.ofNullable(aggregation.getEnableQps()).orElse(true);
                this.enableRtPxx = Optional.ofNullable(aggregation.getEnableRtPxx()).orElse(true);
                this.enableRt = Optional.ofNullable(aggregation.getEnableRt()).orElse(true);
                this.enableRequest = Optional.ofNullable(aggregation.getEnableRequest()).orElse(true);
            }
        }
    }

    public void setCollectEnabled(Boolean collectEnabled) {
        if (collectEnabled != null) {
            this.collectEnabled = collectEnabled;
        }
    }


    @Override
    public boolean isCollectEnabled() {
        if (collectEnabled == null) {
            ConfigManager configManager = applicationModel.getApplicationConfigManager();
            configManager.getMetrics().ifPresent(metricsConfig -> setCollectEnabled(metricsConfig.getAggregation().getEnabled()));
        }
        return Optional.ofNullable(collectEnabled).orElse(true);
    }

    @Override
    public boolean isSupport(MetricsEvent event) {
        return event instanceof RequestEvent;
    }

    @Override