support metric for grpc server request (#11427)
* support metric for grpc server request * change way to judge field * optimize record
This commit is contained in:
parent
d4e2ccda82
commit
6b0636dd5d
@ -16,6 +16,7 @@
|
||||
|
||||
package com.alibaba.nacos.core.monitor;
|
||||
|
||||
import com.alibaba.nacos.common.utils.StringUtils;
|
||||
import io.micrometer.core.instrument.DistributionSummary;
|
||||
import io.micrometer.core.instrument.ImmutableTag;
|
||||
import io.micrometer.core.instrument.Tag;
|
||||
@ -26,6 +27,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@ -282,4 +284,31 @@ public final class MetricsMonitor {
|
||||
public static Map<String, AtomicInteger> getModuleConnectionCnt() {
|
||||
return moduleConnectionCnt;
|
||||
}
|
||||
|
||||
/**
|
||||
* record request event.
|
||||
*
|
||||
* @param requestClass requestClass
|
||||
* @param success success
|
||||
* @param errorCode errorCode
|
||||
* @param throwableClass throwableClass
|
||||
* @param module module
|
||||
* @param costTime cost
|
||||
*/
|
||||
public static void recordGrpcRequestEvent(String requestClass,
|
||||
boolean success,
|
||||
int errorCode,
|
||||
String throwableClass,
|
||||
String module,
|
||||
long costTime) {
|
||||
NacosMeterRegistryCenter.timer(METER_REGISTRY, "grpc_server_requests",
|
||||
Arrays.asList(
|
||||
Tag.of("requestClass", requestClass),
|
||||
Tag.of("success", String.valueOf(success)),
|
||||
Tag.of("errorCode", String.valueOf(errorCode)),
|
||||
Tag.of("throwableClass", StringUtils.isBlank(throwableClass) ? "None" : throwableClass),
|
||||
Tag.of("module", StringUtils.isBlank(module) ? "unknown" : module)
|
||||
)
|
||||
).record(costTime, TimeUnit.NANOSECONDS);
|
||||
}
|
||||
}
|
||||
|
@ -25,8 +25,10 @@ import com.alibaba.nacos.api.remote.request.RequestMeta;
|
||||
import com.alibaba.nacos.api.remote.request.ServerCheckRequest;
|
||||
import com.alibaba.nacos.api.remote.response.ErrorResponse;
|
||||
import com.alibaba.nacos.api.remote.response.Response;
|
||||
import com.alibaba.nacos.api.remote.response.ResponseCode;
|
||||
import com.alibaba.nacos.api.remote.response.ServerCheckResponse;
|
||||
import com.alibaba.nacos.common.remote.client.grpc.GrpcUtils;
|
||||
import com.alibaba.nacos.core.monitor.MetricsMonitor;
|
||||
import com.alibaba.nacos.core.remote.Connection;
|
||||
import com.alibaba.nacos.core.remote.ConnectionManager;
|
||||
import com.alibaba.nacos.core.remote.RequestHandler;
|
||||
@ -75,6 +77,7 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase {
|
||||
|
||||
traceIfNecessary(grpcRequest, true);
|
||||
String type = grpcRequest.getMetadata().getType();
|
||||
long startTime = System.nanoTime();
|
||||
|
||||
//server is on starting.
|
||||
if (!ApplicationUtils.isStarted()) {
|
||||
@ -84,6 +87,8 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase {
|
||||
responseObserver.onNext(payloadResponse);
|
||||
|
||||
responseObserver.onCompleted();
|
||||
MetricsMonitor.recordGrpcRequestEvent(type, false,
|
||||
NacosException.INVALID_SERVER_STATUS, null, null, System.nanoTime() - startTime);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -93,6 +98,8 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase {
|
||||
traceIfNecessary(serverCheckResponseP, false);
|
||||
responseObserver.onNext(serverCheckResponseP);
|
||||
responseObserver.onCompleted();
|
||||
MetricsMonitor.recordGrpcRequestEvent(type, true,
|
||||
0, null, null, System.nanoTime() - startTime);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -105,6 +112,8 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase {
|
||||
traceIfNecessary(payloadResponse, false);
|
||||
responseObserver.onNext(payloadResponse);
|
||||
responseObserver.onCompleted();
|
||||
MetricsMonitor.recordGrpcRequestEvent(type, false,
|
||||
NacosException.NO_HANDLER, null, null, System.nanoTime() - startTime);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -119,6 +128,8 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase {
|
||||
traceIfNecessary(payloadResponse, false);
|
||||
responseObserver.onNext(payloadResponse);
|
||||
responseObserver.onCompleted();
|
||||
MetricsMonitor.recordGrpcRequestEvent(type, false,
|
||||
NacosException.UN_REGISTER, null, null, System.nanoTime() - startTime);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -132,6 +143,8 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase {
|
||||
traceIfNecessary(payloadResponse, false);
|
||||
responseObserver.onNext(payloadResponse);
|
||||
responseObserver.onCompleted();
|
||||
MetricsMonitor.recordGrpcRequestEvent(type, false,
|
||||
NacosException.BAD_GATEWAY, e.getClass().getSimpleName(), null, System.nanoTime() - startTime);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -142,6 +155,9 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase {
|
||||
traceIfNecessary(payloadResponse, false);
|
||||
responseObserver.onNext(payloadResponse);
|
||||
responseObserver.onCompleted();
|
||||
|
||||
MetricsMonitor.recordGrpcRequestEvent(type, false,
|
||||
NacosException.BAD_GATEWAY, null, null, System.nanoTime() - startTime);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -154,6 +170,9 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase {
|
||||
traceIfNecessary(payloadResponse, false);
|
||||
responseObserver.onNext(payloadResponse);
|
||||
responseObserver.onCompleted();
|
||||
|
||||
MetricsMonitor.recordGrpcRequestEvent(type, false,
|
||||
NacosException.BAD_GATEWAY, null, null, System.nanoTime() - startTime);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -181,6 +200,8 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase {
|
||||
responseObserver.onNext(payloadResponse);
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
MetricsMonitor.recordGrpcRequestEvent(type, response.isSuccess(),
|
||||
response.getErrorCode(), null, request.getModule(), System.nanoTime() - startTime);
|
||||
} catch (Throwable e) {
|
||||
Loggers.REMOTE_DIGEST
|
||||
.error("[{}] Fail to handle request from connection [{}] ,error message :{}", "grpc", connectionId,
|
||||
@ -189,6 +210,8 @@ public class GrpcRequestAcceptor extends RequestGrpc.RequestImplBase {
|
||||
traceIfNecessary(payloadResponse, false);
|
||||
responseObserver.onNext(payloadResponse);
|
||||
responseObserver.onCompleted();
|
||||
MetricsMonitor.recordGrpcRequestEvent(type, false,
|
||||
ResponseCode.FAIL.getCode(), e.getClass().getSimpleName(), request.getModule(), System.nanoTime() - startTime);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user