Skip to content

Commit

Permalink
support metric for grpc server request (alibaba#11427)
Browse files Browse the repository at this point in the history
* support metric for grpc server request

* change way to judge field

* optimize record
  • Loading branch information
Daydreamer-ia authored Dec 29, 2023
1 parent d4e2ccd commit 6b0636d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,7 @@
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;

Expand Down Expand Up @@ -282,4 +284,31 @@ public static void refreshModuleConnectionCount(Map<String, Integer> connectionC
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
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;
Expand Down Expand Up @@ -75,6 +77,7 @@ public void request(Payload grpcRequest, StreamObserver<Payload> responseObserve

traceIfNecessary(grpcRequest, true);
String type = grpcRequest.getMetadata().getType();
long startTime = System.nanoTime();

//server is on starting.
if (!ApplicationUtils.isStarted()) {
Expand All @@ -84,6 +87,8 @@ public void request(Payload grpcRequest, StreamObserver<Payload> responseObserve
responseObserver.onNext(payloadResponse);

responseObserver.onCompleted();
MetricsMonitor.recordGrpcRequestEvent(type, false,
NacosException.INVALID_SERVER_STATUS, null, null, System.nanoTime() - startTime);
return;
}

Expand All @@ -93,6 +98,8 @@ public void request(Payload grpcRequest, StreamObserver<Payload> responseObserve
traceIfNecessary(serverCheckResponseP, false);
responseObserver.onNext(serverCheckResponseP);
responseObserver.onCompleted();
MetricsMonitor.recordGrpcRequestEvent(type, true,
0, null, null, System.nanoTime() - startTime);
return;
}

Expand All @@ -105,6 +112,8 @@ public void request(Payload grpcRequest, StreamObserver<Payload> responseObserve
traceIfNecessary(payloadResponse, false);
responseObserver.onNext(payloadResponse);
responseObserver.onCompleted();
MetricsMonitor.recordGrpcRequestEvent(type, false,
NacosException.NO_HANDLER, null, null, System.nanoTime() - startTime);
return;
}

Expand All @@ -119,6 +128,8 @@ public void request(Payload grpcRequest, StreamObserver<Payload> responseObserve
traceIfNecessary(payloadResponse, false);
responseObserver.onNext(payloadResponse);
responseObserver.onCompleted();
MetricsMonitor.recordGrpcRequestEvent(type, false,
NacosException.UN_REGISTER, null, null, System.nanoTime() - startTime);
return;
}

Expand All @@ -132,6 +143,8 @@ public void request(Payload grpcRequest, StreamObserver<Payload> responseObserve
traceIfNecessary(payloadResponse, false);
responseObserver.onNext(payloadResponse);
responseObserver.onCompleted();
MetricsMonitor.recordGrpcRequestEvent(type, false,
NacosException.BAD_GATEWAY, e.getClass().getSimpleName(), null, System.nanoTime() - startTime);
return;
}

Expand All @@ -142,6 +155,9 @@ public void request(Payload grpcRequest, StreamObserver<Payload> responseObserve
traceIfNecessary(payloadResponse, false);
responseObserver.onNext(payloadResponse);
responseObserver.onCompleted();

MetricsMonitor.recordGrpcRequestEvent(type, false,
NacosException.BAD_GATEWAY, null, null, System.nanoTime() - startTime);
return;
}

Expand All @@ -154,6 +170,9 @@ public void request(Payload grpcRequest, StreamObserver<Payload> responseObserve
traceIfNecessary(payloadResponse, false);
responseObserver.onNext(payloadResponse);
responseObserver.onCompleted();

MetricsMonitor.recordGrpcRequestEvent(type, false,
NacosException.BAD_GATEWAY, null, null, System.nanoTime() - startTime);
return;
}

Expand Down Expand Up @@ -181,6 +200,8 @@ public void request(Payload grpcRequest, StreamObserver<Payload> responseObserve
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,
Expand All @@ -189,6 +210,8 @@ public void request(Payload grpcRequest, StreamObserver<Payload> responseObserve
traceIfNecessary(payloadResponse, false);
responseObserver.onNext(payloadResponse);
responseObserver.onCompleted();
MetricsMonitor.recordGrpcRequestEvent(type, false,
ResponseCode.FAIL.getCode(), e.getClass().getSimpleName(), request.getModule(), System.nanoTime() - startTime);
}

}
Expand Down

0 comments on commit 6b0636d

Please sign in to comment.