Adds metrics initialization metrics for the service interface(#12850) (#12892)

* Fixed the observable collection thread pool indicator

* Adds metrics initialization metrics for the service interface(#12850)

---------

Co-authored-by: robin <pengrobin607@gmail.com>
Co-authored-by: songxiaosheng <songxiaosheng@elastic.link>
Co-authored-by: Albumen Kevin <jhq0812@gmail.com>
This commit is contained in:
robin977 2023-08-26 20:54:08 +08:00 committed by GitHub
parent 2a5b437d58
commit 711de7c2a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 345 additions and 4 deletions

View File

@ -38,11 +38,14 @@ import org.apache.dubbo.config.invoker.DelegateProviderMetaDataInvoker;
import org.apache.dubbo.config.support.Parameter;
import org.apache.dubbo.config.utils.ConfigValidationUtils;
import org.apache.dubbo.metadata.ServiceNameMapping;
import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.metrics.event.MetricsInitEvent;
import org.apache.dubbo.registry.client.metadata.MetadataUtils;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.ServerService;
import org.apache.dubbo.rpc.cluster.ConfiguratorFactory;
import org.apache.dubbo.rpc.model.ModuleModel;
@ -60,6 +63,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeSet;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@ -542,6 +546,16 @@ public class ServiceConfig<T> extends ServiceConfigBase<T> {
processServiceExecutor(url);
exportUrl(url, registryURLs, registerType);
initServiceMethodMetrics(url);
}
private void initServiceMethodMetrics(URL url) {
String [] methods = Optional.ofNullable(url.getParameter(METHODS_KEY)).map(i->i.split(",")).orElse( new String[]{});
Arrays.stream(methods).forEach( method-> {
RpcInvocation invocation = new RpcInvocation(url.getServiceKey(),url.getServiceModel(),method,interfaceName, url.getProtocolServiceKey(), null, null,null,null,null,null);
MetricsEventBus.publish(MetricsInitEvent.toMetricsInitEvent(application.getApplicationModel(),invocation));
});
}
private void processServiceExecutor(URL url) {

View File

@ -85,6 +85,12 @@ public abstract class CombMetricsCollector<E extends TimeCounterEvent> extends A
this.stats.incrementMethodKey(wrapper, methodMetric, size);
}
@Override
public void init(Invocation invocation, MetricsKeyWrapper wrapper) {
this.stats.initMethodKey(wrapper, invocation);
}
protected List<MetricSample> export(MetricsCategory category) {
return stats.export(category);
}

View File

@ -30,5 +30,7 @@ public interface MethodMetricsCollector<E extends TimeCounterEvent> extends Metr
void increment(MethodMetric methodMetric, MetricsKeyWrapper wrapper, int size);
void addMethodRt(Invocation invocation, String registryOpType, Long responseTime);
void init(Invocation invocation, MetricsKeyWrapper wrapper);
}

View File

@ -18,6 +18,7 @@
package org.apache.dubbo.metrics.collector;
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.metrics.event.MetricsEvent;
import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.listener.MetricsLifeListener;
import org.apache.dubbo.metrics.model.sample.MetricSample;
@ -42,4 +43,6 @@ public interface MetricsCollector<E extends TimeCounterEvent> extends MetricsLif
*/
List<MetricSample> collect();
default void initMetrics(MetricsEvent event) {};
}

View File

@ -112,6 +112,10 @@ public abstract class BaseStatComposite implements MetricsExport {
methodStatComposite.incrementMethodKey(metricsKeyWrapper, methodMetric, size);
}
public void initMethodKey(MetricsKeyWrapper metricsKeyWrapper, Invocation invocation) {
methodStatComposite.initMethodKey(metricsKeyWrapper, invocation);
}
@Override
public List<MetricSample> export(MetricsCategory category) {
List<MetricSample> list = new ArrayList<>();

View File

@ -26,6 +26,7 @@ import org.apache.dubbo.metrics.model.sample.CounterMetricSample;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.metrics.report.AbstractMetricsExport;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.ArrayList;
@ -54,6 +55,13 @@ public class MethodStatComposite extends AbstractMetricsExport {
metricsKeyWrappers.forEach(appKey -> methodNumStats.put(appKey, new ConcurrentHashMap<>()));
}
public void initMethodKey(MetricsKeyWrapper wrapper, Invocation invocation) {
if (!methodNumStats.containsKey(wrapper)) {
return;
}
methodNumStats.get(wrapper).computeIfAbsent(new MethodMetric(getApplicationModel(), invocation), k -> new AtomicLong(0L));
}
public void incrementMethodKey(MetricsKeyWrapper wrapper, MethodMetric methodMetric, int size) {
if (!methodNumStats.containsKey(wrapper)) {
return;

View File

@ -0,0 +1,48 @@
/*
* 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;
import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.model.MethodMetric;
import org.apache.dubbo.metrics.model.MetricsSupport;
import org.apache.dubbo.metrics.model.key.MetricsLevel;
import org.apache.dubbo.metrics.model.key.TypeWrapper;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SERVICE;
import static org.apache.dubbo.metrics.model.key.MetricsKey.METRIC_REQUESTS;
public class MetricsInitEvent extends TimeCounterEvent {
private static final TypeWrapper METRIC_EVENT = new TypeWrapper(MetricsLevel.SERVICE, METRIC_REQUESTS);
public MetricsInitEvent(ApplicationModel source,TypeWrapper typeWrapper) {
super(source,typeWrapper);
}
public static MetricsInitEvent toMetricsInitEvent(ApplicationModel applicationModel, Invocation invocation) {
MethodMetric methodMetric = new MethodMetric(applicationModel, invocation);
MetricsInitEvent initEvent = new MetricsInitEvent(applicationModel, METRIC_EVENT);
initEvent.putAttachment(MetricsConstants.INVOCATION, invocation);
initEvent.putAttachment(MetricsConstants.METHOD_METRICS, methodMetric);
initEvent.putAttachment(ATTACHMENT_KEY_SERVICE, MetricsSupport.getInterfaceName(invocation));
initEvent.putAttachment(MetricsConstants.INVOCATION_SIDE, MetricsSupport.getSide(invocation));
return initEvent;
}
}

View File

@ -43,6 +43,7 @@ public class SimpleMetricsEventMulticaster implements MetricsEventMulticaster {
if (event instanceof EmptyEvent) {
return;
}
if (validateIfApplicationConfigExist(event)) return;
for (MetricsListener listener : listeners) {
if (listener.isSupport(event)) {

View File

@ -35,5 +35,4 @@ public interface MetricsListener<E extends MetricsEvent> {
*/
void onEvent(E event);
}

View File

@ -222,6 +222,11 @@ public class MetricsSupport {
collector.increment(event.getAttachmentValue(METHOD_METRICS), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE);
}
public static void init(MetricsKey metricsKey, MetricsPlaceValue placeType, MethodMetricsCollector<TimeCounterEvent> collector, MetricsEvent event) {
collector.init(event.getAttachmentValue(INVOCATION), new MetricsKeyWrapper(metricsKey, placeType));
}
/**
* Dec method num
*/

View File

@ -18,6 +18,7 @@
package org.apache.dubbo.metrics;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.metrics.model.key.MetricsKey;
import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
import org.apache.dubbo.metrics.model.key.MetricsLevel;
import org.apache.dubbo.metrics.model.key.MetricsPlaceValue;
@ -69,4 +70,28 @@ public interface DefaultConstants {
new MetricsKeyWrapper(METRIC_REQUESTS_CODEC_FAILED, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
new MetricsKeyWrapper(METRIC_REQUESTS_CODEC_FAILED, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD))
);
List<MetricsKey> INIT_AGG_METHOD_KEYS = Arrays.asList(
MetricsKey.METRIC_REQUESTS_TOTAL_AGG,
MetricsKey.METRIC_REQUESTS_SUCCEED_AGG,
MetricsKey.METRIC_REQUESTS_FAILED_AGG,
MetricsKey.METRIC_REQUEST_BUSINESS_FAILED_AGG,
MetricsKey.METRIC_REQUESTS_TIMEOUT_AGG,
MetricsKey.METRIC_REQUESTS_LIMIT_AGG,
MetricsKey.METRIC_REQUESTS_TOTAL_FAILED_AGG,
MetricsKey.METRIC_REQUESTS_NETWORK_FAILED_AGG,
MetricsKey.METRIC_REQUESTS_CODEC_FAILED_AGG,
MetricsKey.METRIC_REQUESTS_TOTAL_SERVICE_UNAVAILABLE_FAILED_AGG
);
List<MetricsKey> INIT_DEFAULT_METHOD_KEYS = Arrays.asList(
MetricsKey.METRIC_REQUESTS,
MetricsKey.METRIC_REQUESTS_PROCESSING,
MetricsKey.METRIC_REQUESTS_FAILED_AGG,
MetricsKey.METRIC_REQUESTS_SUCCEED,
MetricsKey.METRIC_REQUESTS_TOTAL_FAILED,
MetricsKey.METRIC_REQUEST_BUSINESS_FAILED
);
}

View File

@ -46,6 +46,7 @@ import java.util.concurrent.ConcurrentMap;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
import static org.apache.dubbo.metrics.DefaultConstants.INIT_AGG_METHOD_KEYS;
import static org.apache.dubbo.metrics.DefaultConstants.METRIC_THROWABLE;
import static org.apache.dubbo.metrics.model.MetricsCategory.QPS;
import static org.apache.dubbo.metrics.model.MetricsCategory.REQUESTS;
@ -55,7 +56,7 @@ import static org.apache.dubbo.metrics.model.MetricsCategory.RT;
* Aggregation metrics collector implementation of {@link MetricsCollector}.
* This collector only enabled when metrics aggregation config is enabled.
*/
public class AggregateMetricsCollector implements MetricsCollector<RequestEvent> {
public class AggregateMetricsCollector implements MetricsCollector<RequestEvent>{
private int bucketNum = DEFAULT_BUCKET_NUM;
private int timeWindowSeconds = DEFAULT_TIME_WINDOW_SECONDS;
private int qpsTimeWindowMillSeconds = DEFAULT_QPS_TIME_WINDOW_MILL_SECONDS;
@ -262,4 +263,41 @@ public class AggregateMetricsCollector implements MetricsCollector<RequestEvent>
applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class).getEventMulticaster().addListener(this);
}
@Override
public void initMetrics(MetricsEvent event) {
MethodMetric metric = new MethodMetric(applicationModel, event.getAttachmentValue(MetricsConstants.INVOCATION));
initMethodMetric(event);
initQpsMetric(metric);
initRtMetric(metric);
initRtAgrMetric(metric);
}
public void initMethodMetric(MetricsEvent event){
INIT_AGG_METHOD_KEYS.stream().forEach(key->initWindowCounter(event,key));
}
public void initQpsMetric(MethodMetric metric){
ConcurrentHashMapUtils.computeIfAbsent(qps, metric, methodMetric -> new TimeWindowCounter(bucketNum, timeWindowSeconds));
}
public void initRtMetric(MethodMetric metric){
ConcurrentHashMapUtils.computeIfAbsent(rt, metric, k -> new TimeWindowQuantile(DEFAULT_COMPRESSION, bucketNum, timeWindowSeconds));
}
public void initRtAgrMetric(MethodMetric metric){
ConcurrentHashMapUtils.computeIfAbsent(rtAgr, metric, k -> new TimeWindowAggregator(bucketNum, timeWindowSeconds));
}
public void initWindowCounter(MetricsEvent event, MetricsKey targetKey){
MetricsKeyWrapper metricsKeyWrapper = new MetricsKeyWrapper(targetKey, MetricsPlaceValue.of(event.getAttachmentValue(MetricsConstants.INVOCATION_SIDE), MetricsLevel.SERVICE));
MethodMetric metric = new MethodMetric(applicationModel, event.getAttachmentValue(MetricsConstants.INVOCATION));
ConcurrentMap<MethodMetric, TimeWindowCounter> counter = methodTypeCounter.computeIfAbsent(metricsKeyWrapper, k -> new ConcurrentHashMap<>());
ConcurrentHashMapUtils.computeIfAbsent(counter, metric, methodMetric -> new TimeWindowCounter(bucketNum, timeWindowSeconds));
}
}

View File

@ -20,6 +20,7 @@ package org.apache.dubbo.metrics.collector;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.metrics.DefaultConstants;
import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.collector.sample.MetricsCountSampleConfigurer;
import org.apache.dubbo.metrics.collector.sample.MetricsSampler;
import org.apache.dubbo.metrics.collector.sample.SimpleMetricsCountSampler;
@ -29,9 +30,12 @@ import org.apache.dubbo.metrics.data.MethodStatComposite;
import org.apache.dubbo.metrics.data.RtStatComposite;
import org.apache.dubbo.metrics.event.DefaultSubDispatcher;
import org.apache.dubbo.metrics.event.MetricsEvent;
import org.apache.dubbo.metrics.event.MetricsInitEvent;
import org.apache.dubbo.metrics.event.RequestEvent;
import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.model.ApplicationMetric;
import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.MetricsSupport;
import org.apache.dubbo.metrics.model.key.MetricsLevel;
import org.apache.dubbo.metrics.model.key.MetricsPlaceValue;
import org.apache.dubbo.metrics.model.sample.CounterMetricSample;
@ -40,9 +44,12 @@ import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.apache.dubbo.metrics.DefaultConstants.INIT_DEFAULT_METHOD_KEYS;
import static org.apache.dubbo.metrics.model.MetricsCategory.APPLICATION;
import static org.apache.dubbo.metrics.model.key.MetricsKey.APPLICATION_METRIC_INFO;
import static org.apache.dubbo.metrics.model.key.MetricsKey.METRIC_REQUESTS_SERVICE_UNAVAILABLE_FAILED;
/**
* Default implementation of {@link MetricsCollector}
@ -58,6 +65,11 @@ public class DefaultMetricsCollector extends CombMetricsCollector<RequestEvent>
private final ApplicationModel applicationModel;
private final List<MetricsSampler> samplers = new ArrayList<>();
private final List<MetricsCollector> collectors = new ArrayList<>();
private final AtomicBoolean initialized = new AtomicBoolean();
public DefaultMetricsCollector(ApplicationModel applicationModel) {
super(new BaseStatComposite(applicationModel) {
@Override
@ -137,7 +149,26 @@ public class DefaultMetricsCollector extends CombMetricsCollector<RequestEvent>
@Override
public boolean isSupport(MetricsEvent event) {
return event instanceof RequestEvent;
return event instanceof RequestEvent || event instanceof MetricsInitEvent;
}
@Override
public void onEvent(TimeCounterEvent event) {
if(event instanceof MetricsInitEvent){
if(initialized.compareAndSet(false,true)) {
collectors.addAll(applicationModel.getBeanFactory().getBeansOfType(MetricsCollector.class));
}
collectors.stream().forEach(collector->collector.initMetrics(event));
return;
}
super.onEvent(event);
}
@Override
public void initMetrics(MetricsEvent event) {
MetricsPlaceValue dynamicPlaceType = MetricsPlaceValue.of(event.getAttachmentValue(MetricsConstants.INVOCATION_SIDE), MetricsLevel.METHOD);
INIT_DEFAULT_METHOD_KEYS.stream().forEach(key->MetricsSupport.init(key, dynamicPlaceType, (MethodMetricsCollector) this, event));
MetricsSupport.init(METRIC_REQUESTS_SERVICE_UNAVAILABLE_FAILED, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD), (MethodMetricsCollector) this, event);
}
public SimpleMetricsCountSampler<String, MetricsEvent.Type, ApplicationMetric> applicationSampler = new SimpleMetricsCountSampler<String, MetricsEvent.Type, ApplicationMetric>() {

View File

@ -34,10 +34,10 @@ public class MetricThreadPoolExhaustedListener implements ThreadPoolExhaustedLis
public MetricThreadPoolExhaustedListener(String threadPoolExecutorName,ThreadRejectMetricsCountSampler sampler) {
this.threadPoolExecutorName=threadPoolExecutorName;
this.threadRejectMetricsCountSampler=sampler;
this.threadRejectMetricsCountSampler.addMetricName(threadPoolExecutorName);
}
@Override
public void onEvent(ThreadPoolExhaustedEvent event) {
threadRejectMetricsCountSampler.addMetricName(threadPoolExecutorName);
threadRejectMetricsCountSampler.inc(threadPoolExecutorName,threadPoolExecutorName);
}
}

View File

@ -50,6 +50,10 @@ public abstract class SimpleMetricsCountSampler<S, K, M extends Metric>
metricCounter.get(metricName));
}
protected void initMetricsCounter(S source, K metricsName){
getAtomicCounter(source,metricsName);
}
protected abstract void countConfigure(MetricsCountSampleConfigurer<S, K, M> sampleConfigure);
private AtomicLong getAtomicCounter(S source, K metricsName) {

View File

@ -43,6 +43,7 @@ public class ThreadRejectMetricsCountSampler extends SimpleMetricsCountSampler<S
public void addMetricName(String name){
this.metricNames.add(name);
this.initMetricsCounter(name,name);
}
@Override

View File

@ -0,0 +1,152 @@
/*
* 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.collector;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.MetricsConfig;
import org.apache.dubbo.config.nested.AggregationConfig;
import org.apache.dubbo.metrics.aggregate.TimeWindowCounter;
import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.metrics.event.MetricsInitEvent;
import org.apache.dubbo.metrics.model.ServiceKeyMetric;
import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
import org.apache.dubbo.metrics.model.key.MetricsLevel;
import org.apache.dubbo.metrics.model.key.MetricsPlaceValue;
import org.apache.dubbo.metrics.model.sample.CounterMetricSample;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import static org.apache.dubbo.metrics.DefaultConstants.INIT_AGG_METHOD_KEYS;
import static org.apache.dubbo.metrics.DefaultConstants.INIT_DEFAULT_METHOD_KEYS;
import static org.apache.dubbo.metrics.model.key.MetricsKey.METRIC_REQUESTS;
import static org.apache.dubbo.metrics.model.key.MetricsKey.METRIC_REQUESTS_PROCESSING;
class InitServiceMetricsTest {
private ApplicationModel applicationModel;
private String interfaceName;
private String methodName;
private String group;
private String version;
private String side;
private DefaultMetricsCollector defaultCollector;
private AggregateMetricsCollector aggregateMetricsCollector;
@BeforeEach
public void setup() {
FrameworkModel frameworkModel = FrameworkModel.defaultModel();
applicationModel = frameworkModel.newApplication();
ApplicationConfig config = new ApplicationConfig();
config.setName("MockMetrics");
MetricsConfig metricsConfig = new MetricsConfig();
AggregationConfig aggregationConfig = new AggregationConfig();
aggregationConfig.setEnabled(true);
aggregationConfig.setBucketNum(12);
aggregationConfig.setTimeWindowSeconds(120);
metricsConfig.setAggregation(aggregationConfig);
applicationModel.getApplicationConfigManager().setMetrics(metricsConfig);
applicationModel.getApplicationConfigManager().setApplication(config);
defaultCollector = applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class);
defaultCollector.setCollectEnabled(true);
aggregateMetricsCollector = applicationModel.getBeanFactory().getOrRegisterBean(AggregateMetricsCollector.class);
aggregateMetricsCollector.setCollectEnabled(true);
interfaceName = "org.apache.dubbo.MockInterface";
methodName = "mockMethod";
group = "mockGroup";
version = "1.0.0";
side = CommonConstants.PROVIDER_SIDE;
String serviceKey=group+"/"+interfaceName+":"+version;
String protocolServiceKey=serviceKey+":dubbo";
RpcInvocation invocation = new RpcInvocation(serviceKey,null,methodName,interfaceName, protocolServiceKey, null, null,null,null,null,null);
MetricsEventBus.publish(MetricsInitEvent.toMetricsInitEvent(applicationModel,invocation));
}
@AfterEach
public void teardown() {
applicationModel.destroy();
}
@Test
void testMetricsInitEvent() {
List<MetricSample> metricSamples = defaultCollector.collect();
//INIT_DEFAULT_METHOD_KEYS.size() = 6
Assertions.assertEquals(INIT_DEFAULT_METHOD_KEYS.size(), metricSamples.size());
List<String> metricsNames = metricSamples.stream().map(MetricSample::getName).collect(Collectors.toList());
String REQUESTS = new MetricsKeyWrapper(METRIC_REQUESTS, MetricsPlaceValue.of(side, MetricsLevel.SERVICE)).targetKey();
String PROCESSING = new MetricsKeyWrapper(METRIC_REQUESTS_PROCESSING, MetricsPlaceValue.of(side, MetricsLevel.SERVICE)).targetKey();
Assertions.assertTrue(metricsNames.contains(REQUESTS));
Assertions.assertTrue(metricsNames.contains(PROCESSING));
for (MetricSample metricSample : metricSamples) {
if (metricSample instanceof GaugeMetricSample) {
GaugeMetricSample<?> gaugeMetricSample = (GaugeMetricSample<?>) metricSample;
Object objVal = gaugeMetricSample.getValue();
if (objVal instanceof Map) {
Map<ServiceKeyMetric, AtomicLong> value = (Map<ServiceKeyMetric, AtomicLong>) objVal;
Assertions.assertTrue(value.values().stream().allMatch(atomicLong -> atomicLong.intValue() == 0));
}
} else {
AtomicLong value = (AtomicLong) ((CounterMetricSample<?>) metricSample).getValue();
Assertions.assertEquals(0, value.intValue());
}
}
List<MetricSample> samples = aggregateMetricsCollector.collect();
//INIT_AGG_METHOD_KEYS.size(10) + qps(1) + rt(4) +rtAgr(3)= 18
Assertions.assertEquals(INIT_AGG_METHOD_KEYS.size()+ 1+ 4+ 3, samples.size());
for (MetricSample metricSample : samples) {
if (metricSample instanceof GaugeMetricSample) {
GaugeMetricSample<?> gaugeMetricSample = (GaugeMetricSample<?>) metricSample;
Object objVal = gaugeMetricSample.getValue();
if (objVal instanceof TimeWindowCounter) {
Assertions.assertEquals(0.0,((TimeWindowCounter) objVal).get());
}
}
}
}
}