From f7acdb13db229d4736a9db98113d9339d1873db1 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 23 Jan 2023 22:22:05 -0800 Subject: [PATCH] app/{vmagent,vminsert}: follow-up for 1cfa183c2bba07f914f091967dcd920054b7a12b - Call httpserver.GetQuotedRemoteAddr() and httpserver.GetRequestURI() only when the error occurs. This saves CPU time on fast path when there are no parsing errors. - Create a helper function - httpserver.LogError() - for logging the error with the request uri and remote addr context. --- app/vmagent/prometheusimport/request_handler.go | 5 +---- app/vmagent/prometheusimport/request_handler_test.go | 5 +++-- app/vminsert/prometheusimport/request_handler.go | 5 +---- lib/httpserver/httpserver.go | 7 +++++++ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/vmagent/prometheusimport/request_handler.go b/app/vmagent/prometheusimport/request_handler.go index 84ede2e1b..ed5d53a85 100644 --- a/app/vmagent/prometheusimport/request_handler.go +++ b/app/vmagent/prometheusimport/request_handler.go @@ -7,7 +7,6 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite" "github.com/VictoriaMetrics/VictoriaMetrics/lib/auth" "github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver" - "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal" parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common" parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/prometheus" @@ -31,13 +30,11 @@ func InsertHandler(at *auth.Token, req *http.Request) error { if err != nil { return err } - remoteAddr := httpserver.GetQuotedRemoteAddr(req) - uri := httpserver.GetRequestURI(req) isGzipped := req.Header.Get("Content-Encoding") == "gzip" return parser.ParseStream(req.Body, defaultTimestamp, isGzipped, func(rows []parser.Row) error { return insertRows(at, rows, extraLabels) }, func(s string) { - logger.Errorf("error parsing prometheus text protocol, uri - %s, remote address - %q: %s", uri, remoteAddr, s) + httpserver.LogError(req, s) }) } diff --git a/app/vmagent/prometheusimport/request_handler_test.go b/app/vmagent/prometheusimport/request_handler_test.go index 6883da2b6..c09e66ff8 100644 --- a/app/vmagent/prometheusimport/request_handler_test.go +++ b/app/vmagent/prometheusimport/request_handler_test.go @@ -27,8 +27,9 @@ go_memstats_alloc_bytes_total 1`)) if err := InsertHandler(nil, req); err != nil { t.Errorf("unxepected error %s", err) } - if msg := "error parsing prometheus text protocol"; !strings.Contains(testOutput.String(), msg) { - t.Errorf("output %q should contain %q", testOutput.String(), msg) + expectedMsg := "cannot unmarshal Prometheus line" + if !strings.Contains(testOutput.String(), expectedMsg) { + t.Errorf("output %q should contain %q", testOutput.String(), expectedMsg) } } diff --git a/app/vminsert/prometheusimport/request_handler.go b/app/vminsert/prometheusimport/request_handler.go index 12b1fe90e..1ef5abb08 100644 --- a/app/vminsert/prometheusimport/request_handler.go +++ b/app/vminsert/prometheusimport/request_handler.go @@ -6,7 +6,6 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common" "github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel" "github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver" - "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal" parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common" parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/prometheus" @@ -28,13 +27,11 @@ func InsertHandler(req *http.Request) error { if err != nil { return err } - remoteAddr := httpserver.GetQuotedRemoteAddr(req) - uri := httpserver.GetRequestURI(req) isGzipped := req.Header.Get("Content-Encoding") == "gzip" return parser.ParseStream(req.Body, defaultTimestamp, isGzipped, func(rows []parser.Row) error { return insertRows(rows, extraLabels) }, func(s string) { - logger.Errorf("error parsing prometheus text protocol, uri - %s, remote address - %q: %s", uri, remoteAddr, s) + httpserver.LogError(req, s) }) } diff --git a/lib/httpserver/httpserver.go b/lib/httpserver/httpserver.go index d3aca6d77..9e154276c 100644 --- a/lib/httpserver/httpserver.go +++ b/lib/httpserver/httpserver.go @@ -705,3 +705,10 @@ func Redirect(w http.ResponseWriter, url string) { // This may require browser cache cleaning after the incorrect redirect is fixed. w.WriteHeader(http.StatusFound) } + +// LogError logs the errStr with the context from req. +func LogError(req *http.Request, errStr string) { + uri := GetRequestURI(req) + remoteAddr := GetQuotedRemoteAddr(req) + logger.Errorf("uri: %s, remote address: %q: %s", uri, remoteAddr, errStr) +}