Skip to content

Commit f0cb1f4

Browse files
committed
Handle latency with response
Signed-off-by: Jie WU <[email protected]>
1 parent d7d341a commit f0cb1f4

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

Diff for: pkg/ext-proc/handlers/response.go

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package handlers
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
47
configPb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
58
extProcPb "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3"
69
klog "k8s.io/klog/v2"
@@ -63,6 +66,18 @@ func (s *Server) HandleResponseHeaders(reqCtx *RequestContext, req *extProcPb.Pr
6366
}*/
6467
func (s *Server) HandleResponseBody(reqCtx *RequestContext, req *extProcPb.ProcessingRequest) (*extProcPb.ProcessingResponse, error) {
6568
klog.V(3).Info("Processing HandleResponseBody")
69+
body := req.Request.(*extProcPb.ProcessingRequest_ResponseBody)
70+
71+
res := Response{}
72+
if err := json.Unmarshal(body.ResponseBody.Body, &res); err != nil {
73+
return nil, fmt.Errorf("unmarshaling response body: %v", err)
74+
}
75+
reqCtx.Response = res
76+
// ResponseComplete is to indicate the response is complete. In non-streaming
77+
// case, it will be set to be true once the response is processed; in
78+
//streaming case, it will be set to be true once the last chunk is processed.
79+
reqCtx.ResponseComplete = true
80+
klog.V(3).Infof("Response: %+v", res)
6681

6782
resp := &extProcPb.ProcessingResponse{
6883
Response: &extProcPb.ProcessingResponse_ResponseBody{

Diff for: pkg/ext-proc/handlers/server.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (s *Server) Process(srv extProcPb.ExternalProcessor_ProcessServer) error {
9393
case *extProcPb.ProcessingRequest_ResponseBody:
9494
resp, err = s.HandleResponseBody(reqCtx, req)
9595
reqCtx.ResponseCompleteTimestamp = time.Now()
96-
if err == nil {
96+
if err == nil && reqCtx.ResponseComplete {
9797
metrics.RecordRequestLatencies(reqCtx.Model, reqCtx.ResolvedTargetModel, reqCtx.RequestReceivedTimestamp, reqCtx.ResponseCompleteTimestamp)
9898
}
9999
klog.V(3).Infof("Request context after HandleResponseBody: %+v", reqCtx)
@@ -138,4 +138,5 @@ type RequestContext struct {
138138
ResponseCompleteTimestamp time.Time
139139
RequestSize int
140140
Response Response
141+
ResponseComplete bool
141142
}

Diff for: pkg/manifests/ext_proc.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ spec:
108108
request:
109109
body: Buffered
110110
response:
111-
body: Streamed
112111
# The timeouts are likely not needed here. We can experiment with removing/tuning them slowly.
113112
# The connection limits are more important and will cause the opaque: ext_proc_gRPC_error_14 error in Envoy GW if not configured correctly.
114113
messageTimeout: 1000s

0 commit comments

Comments
 (0)