Skip to content

Commit c039416

Browse files
authored
Report grpc methods latency by project (#35)
Signed-off-by: Oleksii Moskalenko <[email protected]>
1 parent 3530711 commit c039416

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

serving/src/main/java/feast/serving/controller/ServingServiceGRpcController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import feast.proto.serving.ServingServiceGrpc.ServingServiceImplBase;
2626
import feast.serving.config.FeastProperties;
2727
import feast.serving.exception.SpecRetrievalException;
28+
import feast.serving.interceptors.GrpcMonitoringContext;
2829
import feast.serving.interceptors.GrpcMonitoringInterceptor;
2930
import feast.serving.service.ServingServiceV2;
3031
import feast.serving.util.RequestHelper;
@@ -86,6 +87,9 @@ public void getOnlineFeaturesV2(
8687
// project set at root level overrides the project set at feature table level
8788
this.authorizationService.authorizeRequest(
8889
SecurityContextHolder.getContext(), request.getProject());
90+
91+
// update monitoring context
92+
GrpcMonitoringContext.getInstance().setProject(request.getProject());
8993
}
9094
RequestHelper.validateOnlineRequest(request);
9195
Span span = tracer.buildSpan("getOnlineFeaturesV2").start();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright 2018-2021 The Feast Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package feast.serving.interceptors;
18+
19+
import java.util.Optional;
20+
21+
public class GrpcMonitoringContext {
22+
private static GrpcMonitoringContext INSTANCE;
23+
24+
final ThreadLocal<String> project = new ThreadLocal();
25+
26+
private GrpcMonitoringContext() {}
27+
28+
public static GrpcMonitoringContext getInstance() {
29+
if (INSTANCE == null) {
30+
INSTANCE = new GrpcMonitoringContext();
31+
}
32+
33+
return INSTANCE;
34+
}
35+
36+
public void setProject(String name) {
37+
this.project.set(name);
38+
}
39+
40+
public Optional<String> getProject() {
41+
return Optional.ofNullable(this.project.get());
42+
}
43+
44+
public void clearProject() {
45+
this.project.set(null);
46+
}
47+
}

serving/src/main/java/feast/serving/interceptors/GrpcMonitoringInterceptor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.grpc.ServerCallHandler;
2525
import io.grpc.ServerInterceptor;
2626
import io.grpc.Status;
27+
import java.util.Optional;
2728

2829
/**
2930
* GrpcMonitoringInterceptor intercepts GRPC calls to provide request latency histogram metrics in
@@ -39,12 +40,16 @@ public <ReqT, RespT> Listener<ReqT> interceptCall(
3940
String fullMethodName = call.getMethodDescriptor().getFullMethodName();
4041
String methodName = fullMethodName.substring(fullMethodName.indexOf("/") + 1);
4142

43+
GrpcMonitoringContext.getInstance().clearProject();
44+
4245
return next.startCall(
4346
new SimpleForwardingServerCall<ReqT, RespT>(call) {
4447
@Override
4548
public void close(Status status, Metadata trailers) {
49+
Optional<String> projectName = GrpcMonitoringContext.getInstance().getProject();
50+
4651
Metrics.requestLatency
47-
.labels(methodName)
52+
.labels(methodName, projectName.orElse(""))
4853
.observe((System.currentTimeMillis() - startCallMillis) / 1000f);
4954
Metrics.grpcRequestCount.labels(methodName, status.getCode().name()).inc();
5055
super.close(status, trailers);

serving/src/main/java/feast/serving/util/Metrics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class Metrics {
2626
.name("request_latency_seconds")
2727
.subsystem("feast_serving")
2828
.help("Request latency in seconds")
29-
.labelNames("method")
29+
.labelNames("method", "project")
3030
.register();
3131

3232
public static final Histogram requestEntityCountDistribution =

0 commit comments

Comments
 (0)