-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QFE: only log slow query, if it is a query endpoint
Signed-off-by: Pedro Tanaka <[email protected]>
- Loading branch information
1 parent
731e460
commit d0cf961
Showing
2 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package transport | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type fakeRoundTripper struct { | ||
response *http.Response | ||
err error | ||
requestLatency time.Duration | ||
} | ||
|
||
func (f *fakeRoundTripper) RoundTrip(_ *http.Request) (*http.Response, error) { | ||
time.Sleep(f.requestLatency) | ||
return f.response, f.err | ||
} | ||
|
||
func TestHandler_SlowQueryLog(t *testing.T) { | ||
cfg := HandlerConfig{ | ||
QueryStatsEnabled: true, | ||
LogQueriesLongerThan: 1 * time.Second, | ||
} | ||
fakeRoundTripper := &fakeRoundTripper{ | ||
requestLatency: 2 * time.Second, | ||
response: &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Header: http.Header{ | ||
"Content-Type": []string{"application/json"}, | ||
"Server-Timing": []string{"querier;dur=1.23"}, | ||
}, | ||
Body: io.NopCloser(bytes.NewBufferString(`{}`)), | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
url string | ||
logParts []string | ||
}{ | ||
{ | ||
name: "Basic query", | ||
url: "/api/v1/query?query=absent(up)&start=1714262400&end=1714266000", | ||
logParts: []string{ | ||
"slow query detected", | ||
"time_taken=2", | ||
"path=/api/v1/query", | ||
"param_query=absent(up)", | ||
"param_start=1714262400", | ||
"param_end=1714266000", | ||
}, | ||
}, | ||
{ | ||
name: "Query with different parameters", | ||
url: "/api/v1/query_range?query=rate(http_requests_total[5m])&start=1714262400&end=1714266000&step=15", | ||
logParts: []string{ | ||
"slow query detected", | ||
"time_taken=2", | ||
"path=/api/v1/query_range", | ||
"param_query=rate(http_requests_total[5m])", | ||
"param_start=1714262400", | ||
"param_end=1714266000", | ||
"param_step=15", | ||
}, | ||
}, | ||
{ | ||
name: "Non-query endpoint", | ||
url: "/api/v1/status/config", | ||
// No slow query log for non-query endpoints | ||
logParts: []string{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
logWriter := &bytes.Buffer{} | ||
logger := log.NewLogfmtLogger(log.NewSyncWriter(logWriter)) | ||
|
||
handler := NewHandler(cfg, fakeRoundTripper, logger, prometheus.NewRegistry()) | ||
|
||
handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", tt.url, nil)) | ||
|
||
for _, part := range tt.logParts { | ||
require.Contains(t, logWriter.String(), part) | ||
} | ||
}) | ||
} | ||
} |