From 5ae5500b51231262becd7b1aa7df2291fee307af Mon Sep 17 00:00:00 2001 From: lukyer Date: Mon, 9 Sep 2024 14:52:41 +0200 Subject: [PATCH 1/2] Fix Timer stopping in http.send Fixes incorrect `Timer` measuring in case of error. Without calling `Stop()` method when e.g. `eval_cancel_error` happens the last delta is not added to the accumulated `Timer` value. I discovered it by investigating `eval_cancel_error` when the caller gave up but according to OPA metrics it looked like `http.send` did not cause it (I would expect near 10s value in `timer_rego_builtin_http_send_ns` too): ``` {"counter_rego_builtin_http_send_interquery_cache_hits":1,"counter_server_query_cache_hit":1,"timer_rego_builtin_http_send_ns":124580,"timer_rego_input_parse_ns":4771,"timer_rego_query_eval_ns":9770617400,"timer_server_handler_ns":9770659804} ``` Signed-off-by: lukyer --- topdown/http.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/topdown/http.go b/topdown/http.go index 9d01bc14b2..8858eb881b 100644 --- a/topdown/http.go +++ b/topdown/http.go @@ -168,6 +168,7 @@ func generateRaiseErrorResult(err error) *ast.Term { func getHTTPResponse(bctx BuiltinContext, req ast.Object) (*ast.Term, error) { bctx.Metrics.Timer(httpSendLatencyMetricKey).Start() + defer bctx.Metrics.Timer(httpSendLatencyMetricKey).Stop() key, err := getKeyFromRequest(req) if err != nil { @@ -199,7 +200,6 @@ func getHTTPResponse(bctx BuiltinContext, req ast.Object) (*ast.Term, error) { } } - bctx.Metrics.Timer(httpSendLatencyMetricKey).Stop() return ast.NewTerm(resp), nil } From f6a7976ccc6cb60601a51ac33afb63b89de6ac4b Mon Sep 17 00:00:00 2001 From: lukyer Date: Mon, 9 Sep 2024 15:00:45 +0200 Subject: [PATCH 2/2] Update topdown/http.go Signed-off-by: lukyer --- topdown/http.go | 1 - 1 file changed, 1 deletion(-) diff --git a/topdown/http.go b/topdown/http.go index 8858eb881b..a84faf0851 100644 --- a/topdown/http.go +++ b/topdown/http.go @@ -200,7 +200,6 @@ func getHTTPResponse(bctx BuiltinContext, req ast.Object) (*ast.Term, error) { } } - return ast.NewTerm(resp), nil }