Skip to content
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
53 changes: 53 additions & 0 deletions .github/workflows/fuzz.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Parser fuzz

# Exploratory only: started by hand from the Actions tab, never a merge gate.
# A crasher fails the run and is uploaded as an artifact; commit it under the
# target's testdata/fuzz/FuzzParse directory so it replays as a plain test.
on:
workflow_dispatch:
inputs:
head:
description: Query head to fuzz
type: choice
options:
- promql
- logql
- traceql
duration:
description: How long to fuzz, as a Go duration (e.g. 60s, 10m)
type: string
default: 60s

permissions:
contents: read

jobs:
fuzz:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Fuzz
env:
FUZZ_HEAD: ${{ inputs.head }}
FUZZ_DURATION: ${{ inputs.duration }}
run: |
# -fuzztime takes a duration such as 60s or 10m, or an iteration count such as 1000x.
if ! [[ "$FUZZ_DURATION" =~ ^[0-9]+(ns|us|ms|s|m|h|x)$ ]]; then
echo "invalid duration: $FUZZ_DURATION" >&2
exit 1
fi
make fuzz HEAD="$FUZZ_HEAD" DURATION="$FUZZ_DURATION"

- name: Upload crashers
if: failure()
uses: actions/upload-artifact@v4
with:
name: fuzz-crashers-${{ inputs.head }}
path: reader/${{ inputs.head }}/${{ inputs.head }}_parser/testdata/fuzz/
if-no-files-found: ignore
31 changes: 26 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ on:
pull_request:
branches: [ "master" ]

permissions:
contents: read

# Every lane runs through a Makefile recipe, so CI and local runs are the same
# commands.
jobs:

build:
Expand All @@ -17,13 +22,29 @@ jobs:
go-version-file: go.mod

- name: gofmt
run: test -z "$(gofmt -l .)" || { gofmt -l .; exit 1; }
run: make fmt-check

- name: Build Gigapipe
run: |
go mod tidy
CGO_ENABLED=0 go build -ldflags="-extldflags=-static" -o gigapipe cmd/gigapipe/main.go
run: make build

- name: Unit tests
run: go test ./...
run: make test-unit

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
# --new-from-rev needs the commit it compares against in the history.
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod

# Only new issues fail the job (since the pull request's base commit, or
# since the previous commit on a push), so adoption doesn't require
# cleaning up pre-existing findings first.
- name: golangci-lint
run: make lint LINT_NEW_FROM_REV=${{ github.event.pull_request.base.sha || github.event.before }}
62 changes: 62 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# golangci-lint configuration (schema v2).
#
# Goal for this pass: a reasonable, not-overly-strict baseline appropriate for
# a mid-size *existing* codebase, not a from-scratch project. Linter choice
# and tuning below were derived by actually running golangci-lint against
# this repo and looking at what fired:
#
# - errcheck is intentionally NOT enabled. This codebase idiomatically
# ignores errors from things like http.ResponseWriter.Write and
# rows/conn.Close() in defers; enabling it flags ~170 pre-existing call
# sites that are not bugs, just this codebase's style.
# - govet's "structtag" and "composites" checks are disabled. Both parsers
# built on github.com/alecthomas/participle encode grammar rules as
# struct tags (e.g. `@@`, `"(" @@ ")"`) that are not real reflect
# struct tags, and lexer rule tables use unkeyed composite literals by
# participle's own convention. These would fire on every new grammar
# rule added to the PromQL/LogQL/TraceQL parsers.
# - staticcheck is restricted to the SA* (actual bug/deprecation) checks.
# The ST*/S*/QF* style and quickfix checks bundled into staticcheck by
# default add ~90 pre-existing stylistic findings (e.g. "could use
# strings.ReplaceAll", implicit type on declaration) that are not worth
# a hard gate right now.
#
# ineffassign, unused, misspell, unconvert, gofmt and goimports are enabled
# as-is. CI runs `make lint`, which reports only issues new since the pull
# request's base commit (--new-from-rev), so this initial adoption gates
# new/changed code without requiring a full pre-existing-debt cleanup as a
# prerequisite.
version: "2"

linters:
default: none
enable:
- govet
- ineffassign
- unused
- staticcheck
- misspell
- unconvert
settings:
govet:
disable:
- structtag
- composites
staticcheck:
checks:
- "SA*"
exclusions:
paths:
# Ported/vendored-style algorithm implementations (two independent
# copies pre-date this config); not code we want a style gate on.
- reader/utils/cityhash102
- writer/utils/helputils/cityhash102

formatters:
enable:
- gofmt
- goimports
exclusions:
paths:
- reader/utils/cityhash102
- writer/utils/helputils/cityhash102
44 changes: 44 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,53 @@ E2E_TESTS_DIR ?= ./deps/gigapipe-tests
# server (via compose interpolation) and the test runner see the same value.
export QRYN_RULER_ENABLED ?= true

# Query heads with a parser fuzz target.
heads := promql logql traceql

# HEAD/DURATION are set on the command line, e.g. `make fuzz HEAD=promql`.
HEAD ?=
DURATION ?= 60s

# Only issues introduced since this revision are reported. CI passes the pull
# request's base commit.
LINT_NEW_FROM_REV ?= origin/master

.PHONY: docker e2e-deps e2e-build e2e-test e2e-cleanup e2e-full e2e-ci \
fmt-check build test-unit lint fuzz

docker:
docker build -f scripts/deploy/docker/Dockerfile -t gigapipe .

# Fail if any Go file is not gofmt-formatted, listing the offending files.
fmt-check:
@test -z "$$(gofmt -l .)" || { gofmt -l .; exit 1; }

# Build the gigapipe binary the same way CI does.
build:
go mod tidy
CGO_ENABLED=0 go build -ldflags="-extldflags=-static" -o gigapipe cmd/gigapipe/main.go

# Run the full unit-test suite with the race detector, matching CI.
test-unit:
go test -race ./...

# Run golangci-lint with the repo's committed config, gating only issues
# introduced since LINT_NEW_FROM_REV so this doesn't fail on pre-existing
# repo-wide debt. golangci-lint is deliberately unpinned: @latest keeps up with
# new Go releases without a version to bump by hand, and the linters it runs
# are listed explicitly in .golangci.yml.
lint:
go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest run --new-from-rev=$(LINT_NEW_FROM_REV)

# Run the native Go parser fuzz target for one query head:
# make fuzz HEAD=promql|logql|traceql [DURATION=60s]
fuzz:
@case " $(heads) " in \
*" $(HEAD) "*) ;; \
*) echo "fuzz: unknown or missing HEAD '$(HEAD)' (expected one of: $(heads)); usage: make fuzz HEAD=promql [DURATION=60s]" >&2; exit 1 ;; \
esac
go test "./reader/$(HEAD)/$(HEAD)_parser" -run '^$$' -fuzz "FuzzParse" -fuzztime "$(DURATION)"

e2e-deps:
if [ ! -d $(E2E_TESTS_DIR) ]; then git clone $(E2E_TESTS_REPO) $(E2E_TESTS_DIR); fi
cd $(E2E_TESTS_DIR) && git pull && git checkout main && git pull;
Expand Down
16 changes: 16 additions & 0 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ For end-to-end tests:
make e2e-full
```

The Makefile is the entry point for every lane CI runs (CI calls these same
recipes), plus parser-fuzz helpers for the query transpilers:

```bash
make fmt-check # fail if any Go file is not gofmt-formatted
make build # build the gigapipe binary the way CI does
make test-unit # go test -race ./..., matching CI
make lint # golangci-lint, gated to issues new since origin/master
make fuzz HEAD=promql DURATION=60s # run the native Go parser fuzz target for a query head
```

`make lint` builds the latest golangci-lint release, so nothing needs to be
installed first. If your fork's `origin/master` is behind,
compare against another revision with `make lint LINT_NEW_FROM_REV=upstream/master`.
Fuzzing can also be started from the "Parser fuzz" workflow in the Actions tab.

## Submitting a Pull Request

1. Create a branch:
Expand Down
152 changes: 152 additions & 0 deletions reader/logql/logql_parser/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package logql_parser

import "testing"

// FuzzParse asserts that Parse never panics on arbitrary input, and that any
// query it accepts round-trips: rendering it, parsing the rendering and
// rendering that again gives the same text. It makes no claim about which
// queries should be accepted.
//
// The seed corpus below (and any inputs under testdata/fuzz/FuzzParse) runs
// as an ordinary test under `go test ./...`, so CI checks it on every change.
// Fuzzing beyond the seeds is not part of CI: it runs with
// `make fuzz HEAD=logql DURATION=<duration>`, locally or from the manually
// dispatched "Parser fuzz" workflow.
func FuzzParse(f *testing.F) {
for _, seed := range []string{
`{app="x"}`,
`{app="x", env="prod"}`,
`{app="x"} |= "GET"`,
`{app="x"} |~ "2[0-9]$"`,
`{app="x"} != "error" != "debug"`,
`{app="x"} | json`,
`{app="x"} | json field="value"`,
`{app="x"} | logfmt`,
`{app="x"} | regexp "^(?<e>[^0-9]+)[0-9]+$"`,
`rate({app="x"} |= "GET" [5m])`,
`sum by (app) (rate({app="x"} [1m]))`,
`sum_over_time({app="x"} | json | unwrap value [1m]) by (app)`,
`{app="x"} | line_format "{{.field}}"`,
`{app="x"} | freq > 4 and (status="4" or status==2)`,

// The rest of this seed corpus is drawn from grafana/loki's
// pkg/logql/bench query registry — a broader spread of LogQL grammar
// (drop/unwrap-conversion-function/detected_level, empty by()/
// without(), chained label filters) than the hand-written seeds
// above cover alone. Several exercise constructs this parser
// currently rejects — useful fuzz input either way, since rejected
// input must still not panic.
`{app="x"} | detected_level="error"`,
`{app="x"} | logfmt | detected_level="error"`,
`{app="x"} | detected_level="error" | logfmt`,
`{app="x"} | json | detected_level="error"`,
`{app="x"} | detected_level="error" | json`,
`{app="x"} |= "level"`,
`{app="x"} |~ "(?i)error"`,
`{app="x"} !~ "(?i)debug"`,
`{app="x"} | field = "value"`,
`{app="x"} |= "this will not hit any line"`,
`sum(count_over_time({app="x"}[5m]))`,
`sum(count_over_time({app="x"} | detected_level=~"error|warn" [5m]))`,
`sum(rate({app="x"} | detected_level=~"error|warn" [5m]))`,
`sum(count_over_time({app="x"} |= "level" [5m]))`,
`sum(avg_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`sum(avg_over_time({app="x"} |= "level" | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`sum(min_over_time({app="x"} |= "level" | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`sum(max_over_time({app="x"} |= "level" | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`sum by (pod) (count_over_time({app="x"}[5m]))`,
`sum by (namespace) (count_over_time({app="x"}[5m]))`,
`sum by (service_name) (count_over_time({app="x"}[5m]))`,
`sum by (detected_level) (count_over_time({app="x"}[5m]))`,
`sum by (cluster, namespace) (count_over_time({app="x"}[5m]))`,
`sum by (service_name, container) (count_over_time({app="x"}[5m]))`,
`sum by (level) (sum_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`sum by (level) (sum_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m]))`,
`max by (level) (min_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m]))`,
`avg by (level) (avg_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m]))`,
`max by () (avg_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m]))`,
`max by (level) (avg_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m]) without (service_name))`,
`max without () (sum_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m]))`,
`avg_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m])`,
`sum_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m])`,
`rate({app="x"} | json | unwrap duration_ms [5m])`,
`sum by (level) (rate({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`rate({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m])`,
`sum_over_time({app="x"} | json | unwrap duration_ms [5m])`,
`sum(sum_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`sum by (level, namespace) (sum_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`sum without (namespace) (sum_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`sum by (level) (sum_over_time({app="x"} | logfmt | unwrap bytes [5m]))`,
`avg_over_time({app="x"} | json | unwrap duration_ms [5m])`,
`avg(avg_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`avg by (level) (avg_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`max_over_time({app="x"} | json | unwrap duration_ms [5m])`,
`max(max_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`max by (level) (max_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`max by (level) (max_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m]))`,
`min_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m])`,
`min(min_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`min by (level) (min_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`min by (level) (min_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m]))`,
`sum by (detected_level) (sum_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`stdvar_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m])`,
`avg by (level) (stdvar_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`stddev_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m])`,
`avg by (level) (stddev_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`quantile_over_time(0.5, {app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m])`,
`quantile_over_time(0.90, {app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m])`,
`quantile_over_time(0.95, {app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m])`,
`quantile_over_time(0.99, {app="x"} | json | unwrap duration_ms [5m])`,
`avg by (level) (quantile_over_time(0.99, {app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`quantile_over_time(0.99, {app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m])`,
`sum_over_time({app="x"} | logfmt | size != "" | unwrap size [5m])`,
`max by () (max_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`sum without (namespace, cluster) (sum_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`max(avg by (level) (avg_over_time({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m])))`,
`sum(sum_over_time({app="x"} |= "level" | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`avg(avg_over_time({app="x"} |~ "(?i)error|warn" | logfmt | duration != "" | unwrap duration(duration) [5m]))`,
`sum by (level) (sum_over_time({app="x"} | logfmt | level != "" | duration != "" | unwrap duration(duration) [5m]))`,
`{app="x"} |~ "error|exception" | detected_level="error"`,
`{app="x"} | json | duration_seconds > 0.1 | detected_level!="debug"`,
`{app="x"} | logfmt | level="error" | detected_level="error"`,
`{app="x"} |~ "(?i)error" | json | logfmt | drop __error__, __error_details__ | status_code >= 500`,
`{app="x"} | detected_level="error" |~ "(?i)failed"`,
`{app="x"} | detected_level=~"error|warn" |~ "(?i)refused"`,
`{app="x"} | json | logfmt | drop __error__, __error_details__`,
`{app="x"} | json | logfmt | drop __error__, __error_details__ | level="error"`,
`{app="x"} | json | logfmt | drop __error__, __error_details__ | level="error" or level="warn"`,
`sum by (detected_level) (avg_over_time({app="x"} | json | logfmt | duration != "" | drop __error__, __error_details__ | unwrap duration_seconds(duration) [5m]))`,
`sum by (detected_level) (count_over_time({app="x"} | detected_level="debug" or detected_level="info" or detected_level="warn" |~ "(?i)(?i)duration" | json | logfmt | drop __error__, __error_details__ | level=~"(?i)INFO" [5m]))`,
`rate({app="x"} | logfmt | duration != "" | unwrap duration(duration) [5m])`,
`sum by (level) (avg_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m]))`,
`sum(sum_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m]))`,
`avg(avg_over_time({app="x"} | logfmt | duration != "" | unwrap duration_seconds(duration) [5m]))`,
`sum(sum_over_time({app="x"} | logfmt | unwrap bytes [5m]))`,
`sum(sum_over_time({app="x"} | logfmt | size != "" | unwrap size [5m]))`,
`avg(avg_over_time({app="x"} | logfmt | size != "" | unwrap size [5m]))`,
`avg(avg_over_time({app="x"} | logfmt | unwrap status [5m]))`,
`sum by (level) (count_over_time({app="x"} | json [5m]))`,
} {
f.Add(seed)
}

f.Fuzz(func(t *testing.T, query string) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("panicked on %q: %v", query, r)
}
}()
script, err := Parse(query)
if err != nil {
return
}
rendered := script.String()
again, err := Parse(rendered)
if err != nil {
t.Fatalf("%q renders as %q, which does not parse: %v", query, rendered, err)
}
if got := again.String(); got != rendered {
t.Fatalf("%q renders as %q, which renders as %q", query, rendered, got)
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("{A=\"\"}|json|logfmt|drop A000|A=\"\"or")
Loading
Loading