[ISSUE #10117]Add registration metrics. (#11152)

* [ISSUE #10117]Add registration metrics recording for batch instance registration.

* [ISSUE #10117]Add registration metrics recording for batch instance registration.

* [ISSUE #10117]Adjustment Plan.

* [ISSUE #10117]Remove code.

* Fix checkstyle.

* fix ci.

* Optimize code.

* Optimize code.

* Fix unit test.

* Fix unit test.
This commit is contained in:
阿魁 2023-09-25 15:34:27 +08:00 committed by GitHub
parent 51b6a443d3
commit 719f8aed04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 7 deletions

View File

@ -38,4 +38,6 @@ public class RemoteConstants {
public static final String LABEL_MODULE_CONFIG = "config";
public static final String LABEL_MODULE_NAMING = "naming";
public static final String MONITOR_LABEL_NONE = "none";
}

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.client.monitor;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;
@ -33,6 +34,10 @@ public class MetricsMonitor {
.labelNames("module", "method", "url", "code").name("nacos_client_request").help("nacos_client_request")
.register();
private static final Counter NACOS_CLIENT_NAMING_REQUEST_FAILED_TOTAL = Counter.build()
.name("nacos_client_naming_request_failed_total").help("nacos_client_naming_request_failed_total")
.labelNames("module", "req_class", "res_status", "res_code", "err_class").register();
public static Gauge.Child getServiceInfoMapSizeMonitor() {
return NACOS_MONITOR.labels("naming", "serviceInfoMapSize");
}
@ -52,5 +57,10 @@ public class MetricsMonitor {
public static Histogram.Child getNamingRequestMonitor(String method, String url, String code) {
return NACOS_CLIENT_REQUEST_HISTOGRAM.labels("naming", method, url, code);
}
public static Counter.Child getNamingRequestFailedMonitor(String reqClass, String resStatus, String resCode,
String errClass) {
return NACOS_CLIENT_NAMING_REQUEST_FAILED_TOTAL.labels("naming", reqClass, resStatus, resCode, errClass);
}
}

View File

@ -41,6 +41,7 @@ import com.alibaba.nacos.api.remote.response.ResponseCode;
import com.alibaba.nacos.api.selector.AbstractSelector;
import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder;
import com.alibaba.nacos.client.naming.event.ServerListChangedEvent;
import com.alibaba.nacos.client.naming.remote.AbstractNamingClientProxy;
@ -63,10 +64,12 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.alibaba.nacos.api.remote.RemoteConstants.MONITOR_LABEL_NONE;
import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
/**
@ -365,11 +368,11 @@ public class NamingGrpcClientProxy extends AbstractNamingClientProxy {
private <T extends Response> T requestToServer(AbstractNamingRequest request, Class<T> responseClass)
throws NacosException {
Response response = null;
try {
request.putAllHeader(
getSecurityHeaders(request.getNamespace(), request.getGroupName(), request.getServiceName()));
Response response =
requestTimeout < 0 ? rpcClient.request(request) : rpcClient.request(request, requestTimeout);
response = requestTimeout < 0 ? rpcClient.request(request) : rpcClient.request(request, requestTimeout);
if (ResponseCode.SUCCESS.getCode() != response.getResultCode()) {
throw new NacosException(response.getErrorCode(), response.getMessage());
}
@ -378,12 +381,33 @@ public class NamingGrpcClientProxy extends AbstractNamingClientProxy {
}
NAMING_LOGGER.error("Server return unexpected response '{}', expected response should be '{}'",
response.getClass().getName(), responseClass.getName());
throw new NacosException(NacosException.SERVER_ERROR, "Server return invalid response");
} catch (NacosException e) {
recordRequestFailedMetrics(request, e, response);
throw e;
} catch (Exception e) {
recordRequestFailedMetrics(request, e, response);
throw new NacosException(NacosException.SERVER_ERROR, "Request nacos server failed: ", e);
}
throw new NacosException(NacosException.SERVER_ERROR, "Server return invalid response");
}
/**
* Records registration metrics for a service instance.
*
* @param request The registration request object.
* @param exception The Exception encountered during the registration process, or null if registration was
* successful.
* @param response The response object containing registration result information, or null if registration failed.
*/
private void recordRequestFailedMetrics(AbstractNamingRequest request, Exception exception, Response response) {
if (Objects.isNull(response)) {
MetricsMonitor.getNamingRequestFailedMonitor(request.getClass().getSimpleName(), MONITOR_LABEL_NONE,
MONITOR_LABEL_NONE, exception.getClass().getSimpleName()).inc();
} else {
MetricsMonitor.getNamingRequestFailedMonitor(request.getClass().getSimpleName(),
String.valueOf(response.getResultCode()), String.valueOf(response.getErrorCode()),
MONITOR_LABEL_NONE).inc();
}
}
@Override

View File

@ -144,7 +144,7 @@ public class NamingGrpcClientProxyTest {
uuidField.setAccessible(true);
uuid = (String) uuidField.get(client);
Assert.assertTrue(RpcClientFactory.getClient(uuid) != null);
Assert.assertNotNull(RpcClientFactory.getClient(uuid));
Field rpcClientField = NamingGrpcClientProxy.class.getDeclaredField("rpcClient");
rpcClientField.setAccessible(true);
((RpcClient) rpcClientField.get(client)).shutdown();
@ -187,7 +187,7 @@ public class NamingGrpcClientProxyTest {
try {
client.registerService(SERVICE_NAME, GROUP_NAME, instance);
} catch (NacosException ex) {
Assert.assertEquals(null, ex.getCause());
Assert.assertNull(ex.getCause());
throw ex;
}
@ -435,7 +435,7 @@ public class NamingGrpcClientProxyTest {
@Test
public void testShutdown() throws Exception {
client.shutdown();
Assert.assertTrue(RpcClientFactory.getClient(uuid) == null);
Assert.assertNull(RpcClientFactory.getClient(uuid));
//verify(this.rpcClient, times(1)).shutdown();
}