forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor metrics data Composite (apache#12036)
* tmp * tmp * tmp * composite * add licence * remove * style * style * use anoymous composite * use anoymous composite --------- Co-authored-by: x-shadow-man <[email protected]> Co-authored-by: earthchen <[email protected]>
- Loading branch information
1 parent
1d28205
commit 3c518e9
Showing
53 changed files
with
692 additions
and
600 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...bbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ApplicationStatComposite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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.data; | ||
|
||
import org.apache.dubbo.common.utils.CollectionUtils; | ||
import org.apache.dubbo.metrics.model.MetricsCategory; | ||
import org.apache.dubbo.metrics.model.MetricsSupport; | ||
import org.apache.dubbo.metrics.model.key.MetricsKey; | ||
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; | ||
import org.apache.dubbo.metrics.report.MetricsExport; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
public class ApplicationStatComposite implements MetricsExport { | ||
|
||
private final Map<MetricsKey, Map<String, AtomicLong>> applicationNumStats = new ConcurrentHashMap<>(); | ||
|
||
public void init(List<MetricsKey> appKeys) { | ||
if (CollectionUtils.isEmpty(appKeys)) { | ||
return; | ||
} | ||
appKeys.forEach(appKey -> applicationNumStats.put(appKey, new ConcurrentHashMap<>())); | ||
} | ||
|
||
public void incrementSize(MetricsKey metricsKey, String applicationName, int size) { | ||
if (!applicationNumStats.containsKey(metricsKey)) { | ||
return; | ||
} | ||
applicationNumStats.get(metricsKey).computeIfAbsent(applicationName, k -> new AtomicLong(0L)).getAndAdd(size); | ||
} | ||
|
||
public void setApplicationKey(MetricsKey metricsKey, String applicationName, int num) { | ||
if (!applicationNumStats.containsKey(metricsKey)) { | ||
return; | ||
} | ||
applicationNumStats.get(metricsKey).computeIfAbsent(applicationName, k -> new AtomicLong(0L)).set(num); | ||
} | ||
|
||
|
||
@SuppressWarnings({"rawtypes"}) | ||
public List<GaugeMetricSample> export(MetricsCategory category) { | ||
List<GaugeMetricSample> list = new ArrayList<>(); | ||
for (MetricsKey type : applicationNumStats.keySet()) { | ||
Map<String, AtomicLong> stringAtomicLongMap = applicationNumStats.get(type); | ||
for (String applicationName : stringAtomicLongMap.keySet()) { | ||
list.add(convertToSample(applicationName, type, category, stringAtomicLongMap.get(applicationName))); | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
@SuppressWarnings({"rawtypes"}) | ||
private GaugeMetricSample convertToSample(String applicationName, MetricsKey type, MetricsCategory category, AtomicLong targetNumber) { | ||
return new GaugeMetricSample<>(type, MetricsSupport.applicationTags(applicationName), category, targetNumber, AtomicLong::get); | ||
} | ||
|
||
public Map<MetricsKey, Map<String, AtomicLong>> getApplicationNumStats() { | ||
return applicationNumStats; | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
...rics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/BaseStatComposite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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.data; | ||
|
||
import org.apache.dubbo.metrics.collector.MetricsCollector; | ||
import org.apache.dubbo.metrics.model.MetricsCategory; | ||
import org.apache.dubbo.metrics.model.key.MetricsKey; | ||
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; | ||
|
||
import org.apache.dubbo.metrics.report.MetricsExport; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
/** | ||
* As a data aggregator, use internal data containers calculates and classifies | ||
* the registry data collected by {@link MetricsCollector MetricsCollector}, and | ||
* provides an {@link MetricsExport MetricsExport} interface for exporting standard output formats. | ||
*/ | ||
public abstract class BaseStatComposite implements MetricsExport { | ||
|
||
private final ApplicationStatComposite applicationStatComposite = new ApplicationStatComposite(); | ||
private final ServiceStatComposite serviceStatComposite = new ServiceStatComposite(); | ||
private final RtStatComposite rtStatComposite = new RtStatComposite(); | ||
|
||
|
||
public BaseStatComposite() { | ||
init(applicationStatComposite, serviceStatComposite, rtStatComposite); | ||
} | ||
|
||
protected abstract void init(ApplicationStatComposite applicationStatComposite, ServiceStatComposite serviceStatComposite, RtStatComposite rtStatComposite); | ||
|
||
public void calcApplicationRt(String applicationName, String registryOpType, Long responseTime) { | ||
rtStatComposite.calcApplicationRt(applicationName, registryOpType, responseTime); | ||
} | ||
|
||
public void calcServiceKeyRt(String applicationName, String serviceKey, String registryOpType, Long responseTime) { | ||
rtStatComposite.calcServiceKeyRt(applicationName, serviceKey, registryOpType, responseTime); | ||
} | ||
|
||
public void setServiceKey(MetricsKey metricsKey, String applicationName, String serviceKey, int num) { | ||
serviceStatComposite.setServiceKey(metricsKey, applicationName, serviceKey, num); | ||
} | ||
|
||
public void setApplicationKey(MetricsKey metricsKey, String applicationName, int num) { | ||
applicationStatComposite.setApplicationKey(metricsKey, applicationName, num); | ||
} | ||
|
||
public void incrementApp(MetricsKey metricsKey, String applicationName, int size) { | ||
applicationStatComposite.incrementSize(metricsKey, applicationName, size); | ||
} | ||
|
||
public void incrementServiceKey(MetricsKey metricsKey, String applicationName, String attServiceKey, int size) { | ||
serviceStatComposite.incrementServiceKey(metricsKey, applicationName, attServiceKey, size); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings({"rawtypes"}) | ||
public List<GaugeMetricSample> export(MetricsCategory category) { | ||
List<GaugeMetricSample> list = new ArrayList<>(); | ||
list.addAll(applicationStatComposite.export(category)); | ||
list.addAll(rtStatComposite.export(category)); | ||
list.addAll(serviceStatComposite.export(category)); | ||
return list; | ||
} | ||
|
||
public ApplicationStatComposite getApplicationStatComposite() { | ||
return applicationStatComposite; | ||
} | ||
|
||
public RtStatComposite getRtStatComposite() { | ||
return rtStatComposite; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...etrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/RtStatComposite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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.data; | ||
|
||
import org.apache.dubbo.common.utils.ConcurrentHashMapUtils; | ||
import org.apache.dubbo.metrics.model.MetricsCategory; | ||
import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper; | ||
import org.apache.dubbo.metrics.model.container.AtomicLongContainer; | ||
import org.apache.dubbo.metrics.model.container.LongAccumulatorContainer; | ||
import org.apache.dubbo.metrics.model.container.LongContainer; | ||
import org.apache.dubbo.metrics.model.key.MetricsKey; | ||
import org.apache.dubbo.metrics.model.key.MetricsPlaceType; | ||
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; | ||
import org.apache.dubbo.metrics.report.MetricsExport; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.concurrent.atomic.LongAccumulator; | ||
import java.util.stream.Collectors; | ||
|
||
public class RtStatComposite implements MetricsExport { | ||
|
||
private final List<LongContainer<? extends Number>> rtStats = new ArrayList<>(); | ||
|
||
public void init(MetricsPlaceType... placeValues) { | ||
if (placeValues == null) { | ||
return; | ||
} | ||
Arrays.stream(placeValues).forEach(metricsPlaceType -> rtStats.addAll(initStats(metricsPlaceType))); | ||
} | ||
|
||
private List<LongContainer<? extends Number>> initStats(MetricsPlaceType placeValue) { | ||
List<LongContainer<? extends Number>> singleRtStats = new ArrayList<>(); | ||
singleRtStats.add(new AtomicLongContainer(new MetricsKeyWrapper(MetricsKey.METRIC_RT_LAST, placeValue))); | ||
singleRtStats.add(new LongAccumulatorContainer(new MetricsKeyWrapper(MetricsKey.METRIC_RT_MIN, placeValue), new LongAccumulator(Long::min, Long.MAX_VALUE))); | ||
singleRtStats.add(new LongAccumulatorContainer(new MetricsKeyWrapper(MetricsKey.METRIC_RT_MAX, placeValue), new LongAccumulator(Long::max, Long.MIN_VALUE))); | ||
singleRtStats.add(new AtomicLongContainer(new MetricsKeyWrapper(MetricsKey.METRIC_RT_SUM, placeValue), (responseTime, longAccumulator) -> longAccumulator.addAndGet(responseTime))); | ||
// AvgContainer is a special counter that stores the number of times but outputs function of sum/times | ||
AtomicLongContainer avgContainer = new AtomicLongContainer(new MetricsKeyWrapper(MetricsKey.METRIC_RT_AVG, placeValue), (k, v) -> v.incrementAndGet()); | ||
avgContainer.setValueSupplier(applicationName -> { | ||
LongContainer<? extends Number> totalContainer = rtStats.stream().filter(longContainer -> longContainer.isKeyWrapper(MetricsKey.METRIC_RT_SUM, placeValue.getType())).findFirst().get(); | ||
AtomicLong totalRtTimes = avgContainer.get(applicationName); | ||
AtomicLong totalRtSum = (AtomicLong) totalContainer.get(applicationName); | ||
return totalRtSum.get() / totalRtTimes.get(); | ||
}); | ||
singleRtStats.add(avgContainer); | ||
return singleRtStats; | ||
} | ||
|
||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
public void calcApplicationRt(String applicationName, String registryOpType, Long responseTime) { | ||
for (LongContainer container : rtStats.stream().filter(longContainer -> longContainer.specifyType(registryOpType)).collect(Collectors.toList())) { | ||
Number current = (Number) ConcurrentHashMapUtils.computeIfAbsent(container, applicationName, container.getInitFunc()); | ||
container.getConsumerFunc().accept(responseTime, current); | ||
} | ||
} | ||
|
||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
public void calcServiceKeyRt(String applicationName, String serviceKey, String registryOpType, Long responseTime) { | ||
for (LongContainer container : rtStats.stream().filter(longContainer -> longContainer.specifyType(registryOpType)).collect(Collectors.toList())) { | ||
Number current = (Number) ConcurrentHashMapUtils.computeIfAbsent(container, applicationName + "_" + serviceKey, container.getInitFunc()); | ||
container.getConsumerFunc().accept(responseTime, current); | ||
} | ||
} | ||
|
||
@SuppressWarnings({"rawtypes"}) | ||
public List<GaugeMetricSample> export(MetricsCategory category) { | ||
List<GaugeMetricSample> list = new ArrayList<>(); | ||
for (LongContainer<? extends Number> rtContainer : rtStats) { | ||
MetricsKeyWrapper metricsKeyWrapper = rtContainer.getMetricsKeyWrapper(); | ||
for (Map.Entry<String, ? extends Number> entry : rtContainer.entrySet()) { | ||
list.add(new GaugeMetricSample<>(metricsKeyWrapper.targetKey(), metricsKeyWrapper.targetDesc(), metricsKeyWrapper.tagName(entry.getKey()), category, entry.getKey().intern(), value -> rtContainer.getValueSupplier().apply(value.intern()))); | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
public List<LongContainer<? extends Number>> getRtStats() { | ||
return rtStats; | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...s/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ServiceStatComposite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.data; | ||
|
||
import org.apache.dubbo.common.utils.CollectionUtils; | ||
import org.apache.dubbo.metrics.model.MetricsCategory; | ||
import org.apache.dubbo.metrics.model.ServiceKeyMetric; | ||
import org.apache.dubbo.metrics.model.key.MetricsKey; | ||
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; | ||
import org.apache.dubbo.metrics.report.MetricsExport; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
public class ServiceStatComposite implements MetricsExport { | ||
|
||
private final Map<MetricsKey, Map<ServiceKeyMetric, AtomicLong>> serviceNumStats = new ConcurrentHashMap<>(); | ||
|
||
public void init(List<MetricsKey> serviceKeys) { | ||
if (CollectionUtils.isEmpty(serviceKeys)) { | ||
return; | ||
} | ||
serviceKeys.forEach(appKey -> serviceNumStats.put(appKey, new ConcurrentHashMap<>())); | ||
} | ||
|
||
public void incrementServiceKey(MetricsKey metricsKey, String applicationName, String serviceKey, int size) { | ||
if (!serviceNumStats.containsKey(metricsKey)) { | ||
return; | ||
} | ||
serviceNumStats.get(metricsKey).computeIfAbsent(new ServiceKeyMetric(applicationName, serviceKey), k -> new AtomicLong(0L)).getAndAdd(size); | ||
} | ||
|
||
public void setServiceKey(MetricsKey type, String applicationName, String serviceKey, int num) { | ||
if (!serviceNumStats.containsKey(type)) { | ||
return; | ||
} | ||
serviceNumStats.get(type).computeIfAbsent(new ServiceKeyMetric(applicationName, serviceKey), k -> new AtomicLong(0L)).set(num); | ||
} | ||
|
||
@SuppressWarnings({"rawtypes"}) | ||
public List<GaugeMetricSample> export(MetricsCategory category) { | ||
List<GaugeMetricSample> list = new ArrayList<>(); | ||
for (MetricsKey type : serviceNumStats.keySet()) { | ||
Map<ServiceKeyMetric, AtomicLong> stringAtomicLongMap = serviceNumStats.get(type); | ||
for (ServiceKeyMetric serviceKeyMetric : stringAtomicLongMap.keySet()) { | ||
list.add(new GaugeMetricSample<>(type, serviceKeyMetric.getTags(), category, stringAtomicLongMap, value -> value.get(serviceKeyMetric).get())); | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.