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

Fix(query-frontend): /label/<name>/values endpoint to use right set of middlewares #6072

Merged
merged 3 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/querier/queryrange/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func getOperation(path string) string {
return QueryRangeOp
case strings.HasSuffix(path, "/series"):
return SeriesOp
case strings.HasSuffix(path, "/labels") || strings.HasSuffix(path, "/label"):
case strings.HasSuffix(path, "/labels") || strings.HasSuffix(path, "/label") || strings.HasSuffix(path, "/values"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function feels a bit flimsy, and could introduce some interesting bugs in the future.
We should refactor this at some point

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding a test!

Copy link
Contributor Author

@kavirajk kavirajk May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Does anyone know why middeware chain was disabled for /labels/<name>/values endpoint on the query frontend?

We in fact have a test to lock that behaviour.
https://github.com/grafana/loki/blob/main/pkg/querier/queryrange/roundtrip_test.go#L390-L400 (which is the only test failing for this PR)

I wanted this support so that middlewares like metrics.go stats collector can work on this endpoints too. (#5971)

Any thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no protest, I can remove that test!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bet it was omitted by mistake

return LabelNamesOp
case strings.HasSuffix(path, "/v1/query"):
return InstantQueryOp
Expand Down
107 changes: 72 additions & 35 deletions pkg/querier/queryrange/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/parser"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/weaveworks/common/httpgrpc"
"github.com/weaveworks/common/middleware"
Expand Down Expand Up @@ -377,30 +378,6 @@ func TestLogNoRegex(t *testing.T) {
require.NoError(t, err)
}

func TestUnhandledPath(t *testing.T) {
tpw, stopper, err := NewTripperware(testConfig, util_log.Logger, fakeLimits{}, config.SchemaConfig{}, nil, nil)
if stopper != nil {
defer stopper.Stop()
}
require.NoError(t, err)
rt, err := newfakeRoundTripper()
require.NoError(t, err)
defer rt.Close()

ctx := user.InjectOrgID(context.Background(), "1")
req, err := http.NewRequest(http.MethodGet, "/loki/api/v1/labels/foo/values", nil)
require.NoError(t, err)
req = req.WithContext(ctx)
err = user.InjectOrgIDIntoHTTPRequest(ctx, req)
require.NoError(t, err)

count, h := errorResult()
rt.setHandler(h)
_, err = tpw(rt).RoundTrip(req)
require.Equal(t, 1, *count)
require.NoError(t, err)
}

func TestRegexpParamsSupport(t *testing.T) {
l := WithSplitByLimits(fakeLimits{maxSeries: 1, maxQueryParallelism: 2}, 4*time.Hour)
tpw, stopper, err := NewTripperware(testConfig, util_log.Logger, l, config.SchemaConfig{}, nil, nil)
Expand Down Expand Up @@ -547,6 +524,77 @@ func TestEntriesLimitWithZeroTripperware(t *testing.T) {
require.NoError(t, err)
}

func Test_getOperation(t *testing.T) {
cases := []struct {
name string
path string
expectedOp string
}{
{
name: "instant_query",
path: "/loki/api/v1/query",
expectedOp: InstantQueryOp,
},
{
name: "range_query_prom",
path: "/prom/query",
expectedOp: QueryRangeOp,
},
{
name: "range_query",
path: "/loki/api/v1/query_range",
expectedOp: QueryRangeOp,
},
{
name: "series_query",
path: "/loki/api/v1/series",
expectedOp: SeriesOp,
},
{
name: "series_query_prom",
path: "/prom/series",
expectedOp: SeriesOp,
},
{
name: "labels_query",
path: "/loki/api/v1/labels",
expectedOp: LabelNamesOp,
},
{
name: "labels_query_prom",
path: "/prom/labels",
expectedOp: LabelNamesOp,
},
{
name: "label_query",
path: "/loki/api/v1/label",
expectedOp: LabelNamesOp,
},
{
name: "labels_query_prom",
path: "/prom/label",
expectedOp: LabelNamesOp,
},
{
name: "label_values_query",
path: "/loki/api/v1/label/__name__/values",
expectedOp: LabelNamesOp,
},
{
name: "label_values_query_prom",
path: "/prom/label/__name__/values",
expectedOp: LabelNamesOp,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := getOperation(tc.path)
assert.Equal(t, tc.expectedOp, got)
})
}
}

type fakeLimits struct {
maxQueryLength time.Duration
maxQueryParallelism int
Expand Down Expand Up @@ -605,17 +653,6 @@ func counter() (*int, http.Handler) {
})
}

func errorResult() (*int, http.Handler) {
count := 0
var lock sync.Mutex
return &count, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lock.Lock()
defer lock.Unlock()
count++
w.WriteHeader(http.StatusInternalServerError)
})
}

func promqlResult(v parser.Value) (*int, http.Handler) {
count := 0
var lock sync.Mutex
Expand Down