Like Prometheus, but for logs.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
loki/pkg/logql/blocker_test.go

163 lines
3.7 KiB

package logql
import (
"context"
"fmt"
"testing"
"time"
"github.com/go-kit/log"
"github.com/grafana/dskit/user"
"github.com/stretchr/testify/require"
"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/loki/v3/pkg/logqlmodel"
"github.com/grafana/loki/v3/pkg/util"
"github.com/grafana/loki/v3/pkg/util/validation"
)
func TestEngine_ExecWithBlockedQueries(t *testing.T) {
limits := &fakeLimits{maxSeries: 10}
eng := NewEngine(EngineOpts{}, getLocalQuerier(100000), limits, log.NewNopLogger())
defaultQuery := `topk(1,rate(({app=~"foo|bar"})[1m]))`
for _, test := range []struct {
name string
q string
blocked []*validation.BlockedQuery
expectedErr error
}{
{
"exact match all types",
defaultQuery, []*validation.BlockedQuery{
{
Pattern: defaultQuery,
},
}, logqlmodel.ErrBlocked,
},
{
"exact match all types with surrounding whitespace trimmed",
defaultQuery, []*validation.BlockedQuery{
{
Pattern: fmt.Sprintf(" %s ", defaultQuery),
},
}, logqlmodel.ErrBlocked,
},
{
"exact match filter type only",
`{app=~"foo|bar"} |= "baz"`, []*validation.BlockedQuery{
{
Pattern: `{app=~"foo|bar"} |= "baz"`,
Types: []string{QueryTypeFilter},
},
}, logqlmodel.ErrBlocked,
},
{
"match from multiple patterns",
`{app=~"foo|bar"} |= "baz"`, []*validation.BlockedQuery{
// won't match
{
Pattern: `.*"buzz".*`,
Regex: true,
},
// will match
{
Pattern: `{app=~"foo|bar"} |= "baz"`,
Types: []string{QueryTypeFilter},
},
}, logqlmodel.ErrBlocked,
},
{
"no block: exact match not matching filter type",
`{app=~"foo|bar"} | json`, []*validation.BlockedQuery{
{
Pattern: `{app=~"foo|bar"} | json`, // "limited" query
Types: []string{QueryTypeFilter},
},
}, nil,
},
{
"regex match all types",
defaultQuery, []*validation.BlockedQuery{
{
Pattern: ".*foo.*",
Regex: true,
},
}, logqlmodel.ErrBlocked,
},
{
"regex match multiple types",
defaultQuery, []*validation.BlockedQuery{
{
Pattern: ".*foo.*",
Regex: true,
Types: []string{QueryTypeFilter, QueryTypeMetric},
},
}, logqlmodel.ErrBlocked,
},
{
"match all queries by type",
defaultQuery, []*validation.BlockedQuery{
{
Types: []string{QueryTypeFilter, QueryTypeMetric},
},
}, logqlmodel.ErrBlocked,
},
{
"no block: match all queries by type",
defaultQuery, []*validation.BlockedQuery{
{
Types: []string{QueryTypeLimited},
},
}, nil,
},
{
"regex does not compile",
defaultQuery, []*validation.BlockedQuery{
{
Pattern: "[.*",
Regex: true,
Types: []string{QueryTypeFilter, QueryTypeMetric},
},
}, nil,
},
{
"correct FNV32 hash matches",
defaultQuery, []*validation.BlockedQuery{
{
inflight-logging: Add extra metadata to inflight requests logging (#11243) **What this PR does / why we need it**: logging: Add extra metadata to inflight requests This adds extra metadata (similar to what we have in `metrics.go`) but for queries in in-flight (both started and retrying) Changes: Adds following data 1. Query Hash 2. Start and End time 3. Start and End delta 4. Length of the query 5. Moved the helper util to `queryutil` package because of cyclic dependencies with `logql` package. **Which issue(s) this PR fixes**: Fixes #<issue number> **Special notes for your reviewer**: Find the screenshots of log entries looks like (both in `retry.go` and `roundtrip.go`) ![Screenshot 2023-11-16 at 13 01 32](https://github.com/grafana/loki/assets/3735252/177e97ed-6ee8-41dd-b088-2e4f49562ba0) ![Screenshot 2023-11-16 at 13 02 15](https://github.com/grafana/loki/assets/3735252/fb328a37-dbe3-483e-b083-f21327858029) **Checklist** - [x] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [ ] Documentation added - [x] Tests updated - [x] `CHANGELOG.md` updated - [ ] If the change is worth mentioning in the release notes, add `add-to-release-notes` label - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/setup/upgrade/_index.md` - [ ] For Helm chart changes bump the Helm chart version in `production/helm/loki/Chart.yaml` and update `production/helm/loki/CHANGELOG.md` and `production/helm/loki/README.md`. [Example PR](https://github.com/grafana/loki/commit/d10549e3ece02120974929894ee333d07755d213) - [ ] If the change is deprecating or removing a configuration option, update the `deprecated-config.yaml` and `deleted-config.yaml` files respectively in the `tools/deprecated-config-checker` directory. [Example PR](https://github.com/grafana/loki/pull/10840/commits/0d4416a4b03739583349934b96f272fb4f685d15) --------- Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com>
2 years ago
Hash: util.HashedQuery(defaultQuery),
},
}, logqlmodel.ErrBlocked,
},
{
"incorrect FNV32 hash does not match",
defaultQuery, []*validation.BlockedQuery{
{
inflight-logging: Add extra metadata to inflight requests logging (#11243) **What this PR does / why we need it**: logging: Add extra metadata to inflight requests This adds extra metadata (similar to what we have in `metrics.go`) but for queries in in-flight (both started and retrying) Changes: Adds following data 1. Query Hash 2. Start and End time 3. Start and End delta 4. Length of the query 5. Moved the helper util to `queryutil` package because of cyclic dependencies with `logql` package. **Which issue(s) this PR fixes**: Fixes #<issue number> **Special notes for your reviewer**: Find the screenshots of log entries looks like (both in `retry.go` and `roundtrip.go`) ![Screenshot 2023-11-16 at 13 01 32](https://github.com/grafana/loki/assets/3735252/177e97ed-6ee8-41dd-b088-2e4f49562ba0) ![Screenshot 2023-11-16 at 13 02 15](https://github.com/grafana/loki/assets/3735252/fb328a37-dbe3-483e-b083-f21327858029) **Checklist** - [x] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [ ] Documentation added - [x] Tests updated - [x] `CHANGELOG.md` updated - [ ] If the change is worth mentioning in the release notes, add `add-to-release-notes` label - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/setup/upgrade/_index.md` - [ ] For Helm chart changes bump the Helm chart version in `production/helm/loki/Chart.yaml` and update `production/helm/loki/CHANGELOG.md` and `production/helm/loki/README.md`. [Example PR](https://github.com/grafana/loki/commit/d10549e3ece02120974929894ee333d07755d213) - [ ] If the change is deprecating or removing a configuration option, update the `deprecated-config.yaml` and `deleted-config.yaml` files respectively in the `tools/deprecated-config-checker` directory. [Example PR](https://github.com/grafana/loki/pull/10840/commits/0d4416a4b03739583349934b96f272fb4f685d15) --------- Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com>
2 years ago
Hash: util.HashedQuery(defaultQuery) + 1,
},
}, nil,
},
{
"no blocked queries",
defaultQuery, []*validation.BlockedQuery{}, nil,
},
} {
t.Run(test.name, func(t *testing.T) {
limits.blockedQueries = test.blocked
params, err := NewLiteralParams(test.q, time.Unix(0, 0), time.Unix(100000, 0), 60*time.Second, 0, logproto.FORWARD, 1000, nil, nil)
Send query plan to querier. (#11246) **What this PR does / why we need it**: Following https://github.com/grafana/loki/pull/11123 and in order to enable https://github.com/grafana/loki/pull/10417 the query frontend should send the serialized LogQL AST instead of the query string to the queriers. This enables the frontend to change the AST and inject expressions that are not expressible in LogQL. **Checklist** - [ ] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [ ] Documentation added - [x] Tests updated - [ ] `CHANGELOG.md` updated - [ ] If the change is worth mentioning in the release notes, add `add-to-release-notes` label - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/setup/upgrade/_index.md` - [ ] For Helm chart changes bump the Helm chart version in `production/helm/loki/Chart.yaml` and update `production/helm/loki/CHANGELOG.md` and `production/helm/loki/README.md`. [Example PR](https://github.com/grafana/loki/commit/d10549e3ece02120974929894ee333d07755d213) - [ ] If the change is deprecating or removing a configuration option, update the `deprecated-config.yaml` and `deleted-config.yaml` files respectively in the `tools/deprecated-config-checker` directory. [Example PR](https://github.com/grafana/loki/pull/10840/commits/0d4416a4b03739583349934b96f272fb4f685d15) --------- Signed-off-by: Callum Styan <callumstyan@gmail.com> Co-authored-by: Callum Styan <callumstyan@gmail.com>
2 years ago
require.NoError(t, err)
q := eng.Query(params)
_, err = q.Exec(user.InjectOrgID(context.Background(), "fake"))
if test.expectedErr == nil {
require.NoError(t, err)
return
}
require.Error(t, err)
require.Equal(t, err.Error(), test.expectedErr.Error())
})
}
}