Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue-Proxy adds header to response after ServeHTTP() is called #15642

Open
bonavadeur opened this issue Dec 2, 2024 · 1 comment
Open

Queue-Proxy adds header to response after ServeHTTP() is called #15642

bonavadeur opened this issue Dec 2, 2024 · 1 comment
Labels
kind/question Further information is requested

Comments

@bonavadeur
Copy link

Hi everyone,

In Knative Serving, specifically in the file pkg/queue/request_metric.go, within the function func (h *requestMetricsHandler) ServeHTTP(), how can I add a header to the response packet after h.next.ServeHTTP() is called? I attempted to add a header using the w parameter inside a deferred func(), but it didn't work.

func (h *requestMetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    rr := pkghttp.NewResponseRecorder(w, http.StatusOK)
    startTime := time.Now()
    defer func() {
        // Filter probe requests for revision metrics.
        if netheader.IsProbe(r) {
            return
        }
        // If ServeHTTP panics, recover, record the failure and panic again.
        err := recover()
        latency := time.Since(startTime)
        routeTag := GetRouteTagNameFromRequest(r)
        if err != nil {
            ctx := metrics.AugmentWithResponseAndRouteTag(h.statsCtx,
                http.StatusInternalServerError, routeTag)
            pkgmetrics.RecordBatch(ctx, requestCountM.M(1),
                responseTimeInMsecM.M(float64(latency.Milliseconds())))
            panic(err)
        }
        ctx := metrics.AugmentWithResponseAndRouteTag(h.statsCtx,
            rr.ResponseCode, routeTag)
        pkgmetrics.RecordBatch(ctx, requestCountM.M(1),
            responseTimeInMsecM.M(float64(latency.Milliseconds())))
    }()
    h.next.ServeHTTP(rr, r)
}

Thanks so much for help.

@bonavadeur bonavadeur added the kind/question Further information is requested label Dec 2, 2024
@skonto
Copy link
Contributor

skonto commented Dec 4, 2024

Hi @bonavadeur pls take a look here for a similar question.

You could however do:

diff --git a/pkg/queue/sharedmain/handlers.go b/pkg/queue/sharedmain/handlers.go
index 91aebfc77..8f6453ec9 100644
--- a/pkg/queue/sharedmain/handlers.go
+++ b/pkg/queue/sharedmain/handlers.go
@@ -51,6 +51,10 @@ func mainHandler(
        httpProxy.ErrorHandler = pkghandler.Error(logger)
        httpProxy.BufferPool = netproxy.NewBufferPool()
        httpProxy.FlushInterval = netproxy.FlushInterval
+       httpProxy.ModifyResponse = func(response *http.Response) error {
+               response.Header.Add("test", "test")
+               return nil
+       }
 
        breaker := buildBreaker(logger, env)
        tracingEnabled := env.TracingConfigBackend != tracingconfig.None


$ curl -v -H "Host: helloworld-go.default.example.com" http://192.168.39.148:30874
*   Trying 192.168.39.148:30874...
* Connected to 192.168.39.148 (192.168.39.148) port 30874
> GET / HTTP/1.1
> Host: helloworld-go.default.example.com
> User-Agent: curl/8.6.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< content-length: 20
< content-type: text/plain; charset=utf-8
< date: Wed, 04 Dec 2024 16:44:52 GMT
< test: test
< x-envoy-upstream-service-time: 3
< server: envoy
< 
Hello Go Sample v1!
* Connection #0 to host 192.168.39.148 left intact

There is a similar answer to this here: #15606. Could you pls close the tickets if that works for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants