Skip to content

Commit 7db7154

Browse files
committed
refactor(querytee): move response comparator files to dedicated package
This change relocates response comparator logic to its own package to prevent cyclic imports and improve code organization. No functional changes.
1 parent 06da42a commit 7db7154

File tree

7 files changed

+1167
-22
lines changed

7 files changed

+1167
-22
lines changed

cmd/querytee/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/grafana/dskit/log"
1010
"github.com/grafana/dskit/server"
1111
"github.com/grafana/dskit/tracing"
12+
"github.com/grafana/loki/v3/tools/querytee/response_comparator"
1213
"github.com/prometheus/client_golang/prometheus"
1314
"github.com/prometheus/client_golang/prometheus/collectors"
1415

@@ -85,7 +86,7 @@ func exit(code int) {
8586
}
8687

8788
func lokiReadRoutes(cfg Config) []querytee.Route {
88-
samplesComparator := querytee.NewSamplesComparator(querytee.SampleComparisonOptions{
89+
samplesComparator := response_comparator.NewSamplesComparator(response_comparator.SampleComparisonOptions{
8990
Tolerance: cfg.ProxyConfig.ValueComparisonTolerance,
9091
UseRelativeError: cfg.ProxyConfig.UseRelativeError,
9192
SkipRecentSamples: cfg.ProxyConfig.SkipRecentSamples,

tools/querytee/proxy.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/gorilla/mux"
2020
"github.com/grafana/dskit/flagext"
2121
"github.com/grafana/dskit/middleware"
22+
"github.com/grafana/loki/v3/tools/querytee/response_comparator"
2223
"github.com/pkg/errors"
2324
"github.com/prometheus/client_golang/prometheus"
2425

@@ -71,7 +72,7 @@ type Route struct {
7172
Path string
7273
RouteName string
7374
Methods []string
74-
ResponseComparator ResponsesComparator
75+
ResponseComparator response_comparator.ResponsesComparator
7576
}
7677

7778
type Proxy struct {
@@ -240,7 +241,7 @@ func (p *Proxy) Start() error {
240241

241242
// register read routes
242243
for _, route := range p.readRoutes {
243-
var comparator ResponsesComparator
244+
var comparator response_comparator.ResponsesComparator
244245
if p.cfg.CompareResponses {
245246
comparator = route.ResponseComparator
246247
}

tools/querytee/proxy_endpoint.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,14 @@ import (
1414
"github.com/go-kit/log"
1515
"github.com/go-kit/log/level"
1616
"github.com/grafana/loki/v3/tools/querytee/goldfish"
17+
"github.com/grafana/loki/v3/tools/querytee/response_comparator"
1718
)
1819

19-
type ResponsesComparator interface {
20-
Compare(expected, actual []byte, queryEvaluationTime time.Time) (*ComparisonSummary, error)
21-
}
22-
23-
type ComparisonSummary struct {
24-
skipped bool
25-
missingMetrics int
26-
}
27-
2820
type ProxyEndpoint struct {
2921
backends []*ProxyBackend
3022
metrics *ProxyMetrics
3123
logger log.Logger
32-
comparator ResponsesComparator
24+
comparator response_comparator.ResponsesComparator
3325

3426
instrumentCompares bool
3527

@@ -43,7 +35,7 @@ type ProxyEndpoint struct {
4335
goldfishManager *goldfish.Manager
4436
}
4537

46-
func NewProxyEndpoint(backends []*ProxyBackend, routeName string, metrics *ProxyMetrics, logger log.Logger, comparator ResponsesComparator, instrumentCompares bool) *ProxyEndpoint {
38+
func NewProxyEndpoint(backends []*ProxyBackend, routeName string, metrics *ProxyMetrics, logger log.Logger, comparator response_comparator.ResponsesComparator, instrumentCompares bool) *ProxyEndpoint {
4739
hasPreferredBackend := false
4840
for _, backend := range backends {
4941
if backend.preferred {
@@ -195,12 +187,12 @@ func (p *ProxyEndpoint) executeBackendRequests(r *http.Request, resCh chan *Back
195187
"route-name", p.routeName,
196188
"query", r.URL.RawQuery, "err", err)
197189
result = comparisonFailed
198-
} else if summary != nil && summary.skipped {
190+
} else if summary != nil && summary.Skipped {
199191
result = comparisonSkipped
200192
}
201193

202194
if p.instrumentCompares && summary != nil {
203-
p.metrics.missingMetrics.WithLabelValues(p.backends[i].name, p.routeName, result, issuer).Observe(float64(summary.missingMetrics))
195+
p.metrics.missingMetrics.WithLabelValues(p.backends[i].name, p.routeName, result, issuer).Observe(float64(summary.MissingMetrics))
204196
}
205197
p.metrics.responsesComparedTotal.WithLabelValues(p.backends[i].name, p.routeName, result, issuer).Inc()
206198
}
@@ -275,9 +267,9 @@ func (p *ProxyEndpoint) waitBackendResponseForDownstream(resCh chan *BackendResp
275267
return responses[0]
276268
}
277269

278-
func (p *ProxyEndpoint) compareResponses(expectedResponse, actualResponse *BackendResponse, queryEvalTime time.Time) (*ComparisonSummary, error) {
270+
func (p *ProxyEndpoint) compareResponses(expectedResponse, actualResponse *BackendResponse, queryEvalTime time.Time) (*response_comparator.ComparisonSummary, error) {
279271
if expectedResponse.err != nil {
280-
return &ComparisonSummary{skipped: true}, nil
272+
return &response_comparator.ComparisonSummary{Skipped: true}, nil
281273
}
282274

283275
if actualResponse.err != nil {
@@ -286,7 +278,7 @@ func (p *ProxyEndpoint) compareResponses(expectedResponse, actualResponse *Backe
286278

287279
// compare response body only if we get a 200
288280
if expectedResponse.status != 200 {
289-
return &ComparisonSummary{skipped: true}, nil
281+
return &response_comparator.ComparisonSummary{Skipped: true}, nil
290282
}
291283

292284
if actualResponse.status != 200 {

tools/querytee/proxy_endpoint_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/go-kit/log"
1616
"github.com/grafana/loki/v3/pkg/goldfish"
1717
querytee_goldfish "github.com/grafana/loki/v3/tools/querytee/goldfish"
18+
"github.com/grafana/loki/v3/tools/querytee/response_comparator"
1819
"github.com/pkg/errors"
1920
"github.com/prometheus/client_golang/prometheus"
2021
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
@@ -425,8 +426,8 @@ func Test_BackendResponse_statusCode(t *testing.T) {
425426

426427
type mockComparator struct{}
427428

428-
func (c *mockComparator) Compare(_, _ []byte, _ time.Time) (*ComparisonSummary, error) {
429-
return &ComparisonSummary{missingMetrics: 12}, nil
429+
func (c *mockComparator) Compare(_, _ []byte, _ time.Time) (*response_comparator.ComparisonSummary, error) {
430+
return &response_comparator.ComparisonSummary{MissingMetrics: 12}, nil
430431
}
431432

432433
func Test_endToEnd_traceIDFlow(t *testing.T) {

tools/querytee/proxy_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/go-kit/log"
1515
"github.com/gorilla/mux"
16+
"github.com/grafana/loki/v3/tools/querytee/response_comparator"
1617
"github.com/stretchr/testify/assert"
1718
"github.com/stretchr/testify/require"
1819
)
@@ -25,7 +26,9 @@ var testWriteRoutes = []Route{}
2526

2627
type testComparator struct{}
2728

28-
func (testComparator) Compare(_, _ []byte, _ time.Time) (*ComparisonSummary, error) { return nil, nil }
29+
func (testComparator) Compare(_, _ []byte, _ time.Time) (*response_comparator.ComparisonSummary, error) {
30+
return nil, nil
31+
}
2932

3033
func Test_NewProxy(t *testing.T) {
3134
cfg := ProxyConfig{}

0 commit comments

Comments
 (0)