Search in sources :

Example 1 with MetricsService

use of org.apache.dubbo.monitor.MetricsService in project dubbo by alibaba.

the class MetricsFilterTest method testInvokeMetricsMethodService.

public void testInvokeMetricsMethodService() {
    IMetricManager metricManager = MetricManager.getIMetricManager();
    metricManager.clear();
    MetricsFilter metricsFilter = new MetricsFilter();
    Invocation sayNameInvocation = new RpcInvocation("sayName", DemoService.class.getName(), "", new Class<?>[0], new Object[0]);
    Invocation echoInvocation = new RpcInvocation("echo", DemoService.class.getName(), "", new Class<?>[] { Integer.class }, new Integer[] { 1 });
    RpcContext.getContext().setRemoteAddress(host, url.getPort()).setLocalAddress(host, NetUtils.getAvailablePort());
    RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER_SIDE).addParameter(TIMEOUT_KEY, 300));
    AppResponse response = AppResponseBuilder.create().build();
    onInvokeReturns(response);
    for (int i = 0; i < 50; i++) {
        metricsFilter.invoke(serviceInvoker, sayNameInvocation);
        metricsFilter.invoke(serviceInvoker, echoInvocation);
        try {
            metricsFilter.invoke(timeoutInvoker, sayNameInvocation);
        } catch (RpcException e) {
        // ignore
        }
        try {
            metricsFilter.invoke(timeoutInvoker, echoInvocation);
        } catch (RpcException e) {
        // ignore
        }
    }
    Protocol protocol = DubboProtocol.getDubboProtocol();
    URL metricUrl = URL.valueOf("dubbo://" + url.getHost() + ":" + url.getPort() + "/" + MetricsService.class.getName() + "?" + METRICS_PORT + "=" + port);
    Invoker<MetricsService> invoker = protocol.refer(MetricsService.class, metricUrl);
    Invocation invocation = new RpcInvocation("getMetricsByGroup", DemoService.class.getName(), "", new Class<?>[] { String.class }, new Object[] { DUBBO_GROUP });
    try {
        Thread.sleep(15000);
    } catch (Exception e) {
    // ignore
    }
    String resStr = invoker.invoke(invocation).getValue().toString();
    List<MetricObject> metricObjectList = new Gson().fromJson(resStr, new TypeToken<List<MetricObject>>() {
    }.getType());
    Map<String, Map<String, Object>> methodMetricMap = new HashMap<>();
    for (int i = 0; i < metricObjectList.size(); i++) {
        MetricObject object = metricObjectList.get(i);
        String service = object.getTags().get("service");
        String method = service + "." + object.getTags().get("method");
        String metric = object.getMetric().substring(object.getMetric().lastIndexOf(".") + 1);
        Map map = methodMetricMap.get(method);
        if (map == null) {
            map = new HashMap();
            methodMetricMap.put(method, map);
        }
        map.put(metric, object.getValue());
    }
    Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("success_bucket_count"));
    Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("success_bucket_count"));
    Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("timeoutError_bucket_count"));
    Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("timeoutError_bucket_count"));
    Assertions.assertEquals(100.0 / 15, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("qps"));
    Assertions.assertEquals(100.0 / 15, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("qps"));
    Assertions.assertEquals(50.0 / 100.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("success_rate"));
    Assertions.assertEquals(50.0 / 100.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("success_rate"));
    invoker.destroy();
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MetricsService(org.apache.dubbo.monitor.MetricsService) HashMap(java.util.HashMap) DemoService(org.apache.dubbo.monitor.dubbo.service.DemoService) Gson(com.google.gson.Gson) IMetricManager(com.alibaba.metrics.IMetricManager) URL(org.apache.dubbo.common.URL) RpcException(org.apache.dubbo.rpc.RpcException) AppResponse(org.apache.dubbo.rpc.AppResponse) TypeToken(com.google.gson.reflect.TypeToken) RpcException(org.apache.dubbo.rpc.RpcException) Protocol(org.apache.dubbo.rpc.Protocol) DubboProtocol(org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol) MetricObject(com.alibaba.metrics.common.MetricObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with MetricsService

use of org.apache.dubbo.monitor.MetricsService in project dubbo by alibaba.

the class MetricsFilterTest method testInvokeMetricsService.

public void testInvokeMetricsService() {
    IMetricManager metricManager = MetricManager.getIMetricManager();
    metricManager.clear();
    MetricsFilter metricsFilter = new MetricsFilter();
    Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), "", new Class<?>[0], new Object[0]);
    RpcContext.getContext().setRemoteAddress(host, url.getPort()).setLocalAddress(host, NetUtils.getAvailablePort());
    RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER_SIDE).addParameter(TIMEOUT_KEY, 300));
    AppResponse response = AppResponseBuilder.create().build();
    onInvokeReturns(response);
    for (int i = 0; i < 50; i++) {
        try {
            metricsFilter.invoke(serviceInvoker, invocation);
            metricsFilter.invoke(timeoutInvoker, invocation);
        } catch (RpcException e) {
        // ignore
        }
    }
    Protocol protocol = DubboProtocol.getDubboProtocol();
    URL metricUrl = URL.valueOf("dubbo://" + url.getHost() + ":" + url.getPort() + "/" + MetricsService.class.getName() + "?" + METRICS_PORT + "=" + port);
    Invoker<MetricsService> invoker = protocol.refer(MetricsService.class, metricUrl);
    invocation = new RpcInvocation("getMetricsByGroup", DemoService.class.getName(), "", new Class<?>[] { String.class }, new Object[] { DUBBO_GROUP });
    try {
        Thread.sleep(5000);
    } catch (Exception e) {
    // ignore
    }
    String resStr = invoker.invoke(invocation).getValue().toString();
    List<MetricObject> metricObjectList = new Gson().fromJson(resStr, new TypeToken<List<MetricObject>>() {
    }.getType());
    Map<String, Object> metricMap = new HashMap<>();
    for (int i = 0; i < metricObjectList.size(); i++) {
        MetricObject object = metricObjectList.get(i);
        String metric = object.getMetric().substring(object.getMetric().lastIndexOf(".") + 1);
        if ((double) object.getValue() > 0.0 && object.getMetricLevel().equals(MetricLevel.MAJOR))
            metricMap.put(metric, object.getValue());
    }
    Assertions.assertEquals(50.0, metricMap.get("success_bucket_count"));
    Assertions.assertEquals(50.0, metricMap.get("timeoutError_bucket_count"));
    Assertions.assertEquals(100.0, metricMap.get("bucket_count"));
    Assertions.assertEquals(100.0 / 5, metricMap.get("qps"));
    Assertions.assertEquals(50.0 / 100.0, metricMap.get("success_rate"));
    invoker.destroy();
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MetricsService(org.apache.dubbo.monitor.MetricsService) HashMap(java.util.HashMap) DemoService(org.apache.dubbo.monitor.dubbo.service.DemoService) Gson(com.google.gson.Gson) IMetricManager(com.alibaba.metrics.IMetricManager) URL(org.apache.dubbo.common.URL) RpcException(org.apache.dubbo.rpc.RpcException) AppResponse(org.apache.dubbo.rpc.AppResponse) TypeToken(com.google.gson.reflect.TypeToken) RpcException(org.apache.dubbo.rpc.RpcException) MetricObject(com.alibaba.metrics.common.MetricObject) Protocol(org.apache.dubbo.rpc.Protocol) DubboProtocol(org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol) MetricObject(com.alibaba.metrics.common.MetricObject)

Example 3 with MetricsService

use of org.apache.dubbo.monitor.MetricsService in project dubbo by alibaba.

the class MetricsFilter method initMetricsInvoker.

private Invoker<MetricsService> initMetricsInvoker() {
    return new Invoker<MetricsService>() {

        @Override
        public Class<MetricsService> getInterface() {
            return MetricsService.class;
        }

        @Override
        public Result invoke(Invocation invocation) throws RpcException {
            String group = invocation.getArguments()[0].toString();
            MetricRegistry registry = MetricManager.getIMetricManager().getMetricRegistryByGroup(group);
            SortedMap<MetricName, FastCompass> fastCompasses = registry.getFastCompasses();
            long timestamp = System.currentTimeMillis();
            double rateFactor = TimeUnit.SECONDS.toSeconds(1);
            double durationFactor = 1.0 / TimeUnit.MILLISECONDS.toNanos(1);
            MetricsCollector collector = MetricsCollectorFactory.createNew(CollectLevel.NORMAL, Collections.EMPTY_MAP, rateFactor, durationFactor, null);
            for (Map.Entry<MetricName, FastCompass> entry : fastCompasses.entrySet()) {
                collector.collect(entry.getKey(), entry.getValue(), timestamp);
            }
            List<MetricObject> res = collector.build();
            res.addAll(getThreadPoolMessage());
            return AsyncRpcResult.newDefaultAsyncResult(JSON.toJSONString(res), invocation);
        }

        @Override
        public URL getUrl() {
            return URL.valueOf(protocolName + "://" + NetUtils.getIpByConfig() + ":" + port + "/" + MetricsService.class.getName());
        }

        @Override
        public boolean isAvailable() {
            return false;
        }

        @Override
        public void destroy() {
        }
    };
}
Also used : MetricsCollector(com.alibaba.metrics.common.MetricsCollector) Invocation(org.apache.dubbo.rpc.Invocation) MetricsService(org.apache.dubbo.monitor.MetricsService) MetricRegistry(com.alibaba.metrics.MetricRegistry) MetricName(com.alibaba.metrics.MetricName) Invoker(org.apache.dubbo.rpc.Invoker) FastCompass(com.alibaba.metrics.FastCompass) MetricObject(com.alibaba.metrics.common.MetricObject) HashMap(java.util.HashMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 4 with MetricsService

use of org.apache.dubbo.monitor.MetricsService in project dubbo by alibaba.

the class MetricsFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (exported.compareAndSet(false, true)) {
        this.protocolName = invoker.getUrl().getParameter(METRICS_PROTOCOL) == null ? DEFAULT_PROTOCOL : invoker.getUrl().getParameter(METRICS_PROTOCOL);
        Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(protocolName);
        this.port = invoker.getUrl().getParameter(METRICS_PORT) == null ? protocol.getDefaultPort() : Integer.parseInt(invoker.getUrl().getParameter(METRICS_PORT));
        Invoker<MetricsService> metricsInvoker = initMetricsInvoker();
        try {
            protocol.export(metricsInvoker);
        } catch (RuntimeException e) {
            logger.error("Metrics Service need to be configured" + " when multiple processes are running on a host" + e.getMessage());
        }
    }
    RpcContext context = RpcContext.getContext();
    boolean isProvider = context.isProviderSide();
    long start = System.currentTimeMillis();
    try {
        // proceed invocation chain
        Result result = invoker.invoke(invocation);
        long duration = System.currentTimeMillis() - start;
        reportMetrics(invoker, invocation, duration, "success", isProvider);
        return result;
    } catch (RpcException e) {
        long duration = System.currentTimeMillis() - start;
        String result = "error";
        if (e.isTimeout()) {
            result = "timeoutError";
        }
        if (e.isBiz()) {
            result = "bisError";
        }
        if (e.isNetwork()) {
            result = "networkError";
        }
        if (e.isSerialization()) {
            result = "serializationError";
        }
        reportMetrics(invoker, invocation, duration, result, isProvider);
        throw e;
    }
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) MetricsService(org.apache.dubbo.monitor.MetricsService) RpcException(org.apache.dubbo.rpc.RpcException) Protocol(org.apache.dubbo.rpc.Protocol) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result)

Aggregations

MetricsService (org.apache.dubbo.monitor.MetricsService)4 MetricObject (com.alibaba.metrics.common.MetricObject)3 HashMap (java.util.HashMap)3 Invocation (org.apache.dubbo.rpc.Invocation)3 Protocol (org.apache.dubbo.rpc.Protocol)3 RpcException (org.apache.dubbo.rpc.RpcException)3 IMetricManager (com.alibaba.metrics.IMetricManager)2 Gson (com.google.gson.Gson)2 TypeToken (com.google.gson.reflect.TypeToken)2 Map (java.util.Map)2 URL (org.apache.dubbo.common.URL)2 DemoService (org.apache.dubbo.monitor.dubbo.service.DemoService)2 AppResponse (org.apache.dubbo.rpc.AppResponse)2 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)2 DubboProtocol (org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol)2 FastCompass (com.alibaba.metrics.FastCompass)1 MetricName (com.alibaba.metrics.MetricName)1 MetricRegistry (com.alibaba.metrics.MetricRegistry)1 MetricsCollector (com.alibaba.metrics.common.MetricsCollector)1 SortedMap (java.util.SortedMap)1