Skip to content

Commit 551c203

Browse files
authored
[ISSUE#13183] Add client metrics switch. (#13191)
1 parent 83e145b commit 551c203

File tree

7 files changed

+376
-16
lines changed

7 files changed

+376
-16
lines changed

api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,9 @@ public static class SystemEnv {
130130
public static final String ALIBABA_ALIWARE_ENDPOINT_URL = "ALIBABA_ALIWARE_ENDPOINT_URL";
131131
}
132132

133-
}
133+
/**
134+
* Client Metric Switch.
135+
*/
136+
public static final String ENABLE_CLIENT_METRICS = "enableClientMetrics";
137+
138+
}

client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ public class ClientWorker implements Closeable {
151151

152152
private static final int THREAD_MULTIPLE = 1;
153153

154+
private boolean enableClientMetrics = true;
155+
154156
/**
155157
* index(taskId)-> total cache count for this taskId.
156158
*/
@@ -298,7 +300,13 @@ void removeCache(String dataId, String group, String tenant) {
298300
}
299301
LOGGER.info("[{}] [unsubscribe] {}", agent.getName(), groupKey);
300302

301-
MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size());
303+
if (enableClientMetrics) {
304+
try {
305+
MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size());
306+
} catch (Throwable t) {
307+
LOGGER.error("Failed to update metrics for listen config count", t);
308+
}
309+
}
302310
}
303311

304312
/**
@@ -373,7 +381,13 @@ public CacheData addCacheDataIfAbsent(String dataId, String group) {
373381

374382
LOGGER.info("[{}] [subscribe] {}", this.agent.getName(), key);
375383

376-
MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size());
384+
if (enableClientMetrics) {
385+
try {
386+
MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size());
387+
} catch (Throwable t) {
388+
LOGGER.error("Failed to update metrics for listen config count", t);
389+
}
390+
}
377391

378392
return cache;
379393
}
@@ -420,7 +434,13 @@ public CacheData addCacheDataIfAbsent(String dataId, String group, String tenant
420434
}
421435
LOGGER.info("[{}] [subscribe] {}", agent.getName(), key);
422436

423-
MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size());
437+
if (enableClientMetrics) {
438+
try {
439+
MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size());
440+
} catch (Throwable t) {
441+
LOGGER.error("Failed to update metrics for listen config count", t);
442+
}
443+
}
424444

425445
return cache;
426446
}
@@ -523,6 +543,8 @@ private void init(NacosClientProperties properties) {
523543

524544
this.enableRemoteSyncConfig = Boolean.parseBoolean(
525545
properties.getProperty(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG));
546+
this.enableClientMetrics = Boolean.parseBoolean(
547+
properties.getProperty(PropertyKeyConst.ENABLE_CLIENT_METRICS, "true"));
526548
initAppLabels(properties.getProperties(SourceType.PROPERTIES));
527549
}
528550

client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public class ServiceInfoHolder implements Closeable {
5757

5858
private String notifierEventScope;
5959

60+
private boolean enableClientMetrics = true;
61+
6062
public ServiceInfoHolder(String namespace, String notifierEventScope, NacosClientProperties properties) {
6163
cacheDir = CacheDirUtil.initCacheDir(namespace, properties);
6264
instancesDiffer = new InstancesDiffer();
@@ -68,6 +70,8 @@ public ServiceInfoHolder(String namespace, String notifierEventScope, NacosClien
6870
this.failoverReactor = new FailoverReactor(this, notifierEventScope);
6971
this.pushEmptyProtection = isPushEmptyProtect(properties);
7072
this.notifierEventScope = notifierEventScope;
73+
this.enableClientMetrics = Boolean.parseBoolean(
74+
properties.getProperty(PropertyKeyConst.ENABLE_CLIENT_METRICS, "true"));
7175
}
7276

7377
private boolean isLoadCacheAtStart(NacosClientProperties properties) {
@@ -136,7 +140,15 @@ public ServiceInfo processServiceInfo(ServiceInfo serviceInfo) {
136140
if (StringUtils.isBlank(serviceInfo.getJsonFromServer())) {
137141
serviceInfo.setJsonFromServer(JacksonUtils.toJson(serviceInfo));
138142
}
139-
MetricsMonitor.getServiceInfoMapSizeMonitor().set(serviceInfoMap.size());
143+
144+
if (enableClientMetrics) {
145+
try {
146+
MetricsMonitor.getServiceInfoMapSizeMonitor().set(serviceInfoMap.size());
147+
} catch (Throwable t) {
148+
NAMING_LOGGER.error("Failed to update metrics for service info map size", t);
149+
}
150+
}
151+
140152
if (diff.hasDifferent()) {
141153
NAMING_LOGGER.info("current ips:({}) service: {} -> {}", serviceInfo.ipCount(), serviceKey,
142154
JacksonUtils.toJson(serviceInfo.getHosts()));

client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.alibaba.nacos.client.naming.remote.gprc;
1818

19+
import com.alibaba.nacos.api.PropertyKeyConst;
1920
import com.alibaba.nacos.api.ability.constant.AbilityKey;
2021
import com.alibaba.nacos.api.ability.constant.AbilityStatus;
2122
import com.alibaba.nacos.api.common.Constants;
@@ -94,6 +95,8 @@ public class NamingGrpcClientProxy extends AbstractNamingClientProxy {
9495

9596
private final NamingGrpcRedoService redoService;
9697

98+
private boolean enableClientMetrics = true;
99+
97100
public NamingGrpcClientProxy(String namespaceId, SecurityProxy securityProxy, ServerListFactory serverListFactory,
98101
NacosClientProperties properties, ServiceInfoHolder serviceInfoHolder) throws NacosException {
99102
super(securityProxy);
@@ -108,6 +111,8 @@ public NamingGrpcClientProxy(String namespaceId, SecurityProxy securityProxy, Se
108111
.createGrpcClientConfig(properties.asProperties(), labels);
109112
this.rpcClient = RpcClientFactory.createClient(uuid, ConnectionType.GRPC, grpcClientConfig);
110113
this.redoService = new NamingGrpcRedoService(this, properties);
114+
this.enableClientMetrics = Boolean.parseBoolean(
115+
properties.getProperty(PropertyKeyConst.ENABLE_CLIENT_METRICS, "true"));
111116
NAMING_LOGGER.info("Create naming rpc client for uuid->{}", uuid);
112117
start(serverListFactory, serviceInfoHolder);
113118
}
@@ -478,13 +483,21 @@ private <T extends Response> T requestToServer(AbstractNamingRequest request, Cl
478483
* @param response The response object containing registration result information, or null if registration failed.
479484
*/
480485
private void recordRequestFailedMetrics(AbstractNamingRequest request, Exception exception, Response response) {
481-
if (Objects.isNull(response)) {
482-
MetricsMonitor.getNamingRequestFailedMonitor(request.getClass().getSimpleName(), MONITOR_LABEL_NONE,
483-
MONITOR_LABEL_NONE, exception.getClass().getSimpleName()).inc();
484-
} else {
485-
MetricsMonitor.getNamingRequestFailedMonitor(request.getClass().getSimpleName(),
486-
String.valueOf(response.getResultCode()), String.valueOf(response.getErrorCode()),
487-
MONITOR_LABEL_NONE).inc();
486+
if (!enableClientMetrics) {
487+
return;
488+
}
489+
490+
try {
491+
if (Objects.isNull(response)) {
492+
MetricsMonitor.getNamingRequestFailedMonitor(request.getClass().getSimpleName(), MONITOR_LABEL_NONE,
493+
MONITOR_LABEL_NONE, exception.getClass().getSimpleName()).inc();
494+
} else {
495+
MetricsMonitor.getNamingRequestFailedMonitor(request.getClass().getSimpleName(),
496+
String.valueOf(response.getResultCode()), String.valueOf(response.getErrorCode()),
497+
MONITOR_LABEL_NONE).inc();
498+
}
499+
} catch (Throwable t) {
500+
NAMING_LOGGER.warn("Fail to record metrics for request {}", request.getClass().getSimpleName(), t);
488501
}
489502
}
490503

client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxy.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public class NamingHttpClientProxy extends AbstractNamingClientProxy {
108108

109109
private int serverPort = DEFAULT_SERVER_PORT;
110110

111+
private boolean enableClientMetrics = true;
112+
111113
public NamingHttpClientProxy(String namespaceId, SecurityProxy securityProxy, NamingServerListManager serverListManager,
112114
NacosClientProperties properties) {
113115
super(securityProxy);
@@ -116,6 +118,8 @@ public NamingHttpClientProxy(String namespaceId, SecurityProxy securityProxy, Na
116118
this.namespaceId = namespaceId;
117119
this.maxRetry = ConvertUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_REQUEST_DOMAIN_RETRY_COUNT,
118120
String.valueOf(UtilAndComs.REQUEST_DOMAIN_RETRY_COUNT)));
121+
this.enableClientMetrics = Boolean.parseBoolean(
122+
properties.getProperty(PropertyKeyConst.ENABLE_CLIENT_METRICS, "true"));
119123
}
120124

121125
@Override
@@ -431,8 +435,15 @@ public String callServer(String api, Map<String, String> params, Map<String, Str
431435
Query.newInstance().initParams(params), body, method, String.class);
432436
end = System.currentTimeMillis();
433437

434-
MetricsMonitor.getNamingRequestMonitor(method, url, String.valueOf(restResult.getCode()))
435-
.observe(end - start);
438+
if (enableClientMetrics) {
439+
try {
440+
MetricsMonitor.getNamingRequestMonitor(method, url, String.valueOf(restResult.getCode()))
441+
.observe(end - start);
442+
} catch (Throwable t) {
443+
NAMING_LOGGER.error("Failed to record metrics. Method: {}, URL: {}, HTTP Status Code: {}",
444+
method, url, restResult.getCode(), t);
445+
}
446+
}
436447

437448
if (restResult.ok()) {
438449
return restResult.getData();

0 commit comments

Comments
 (0)