Skip to content

loki: time out query_range requests so a hung one is retried - #4757

Open
NoahSurprenant wants to merge 1 commit into
crowdsecurity:masterfrom
NoahSurprenant:fix/loki-request-timeout
Open

NoahSurprenant wants to merge 1 commit into
crowdsecurity:masterfrom
NoahSurprenant:fix/loki-request-timeout

Conversation

@NoahSurprenant

Copy link
Copy Markdown

What and why

The Loki datasource's client used http.DefaultClient, which has no timeout. A query_range that Loki accepts but never answers blocks that source forever: no error, no log, and max_failure_duration never applies. We hit this three times. Each time, a Loki rollout lost the request (grafana/loki#25153) and the traefik/unifi sources read nothing until the agent was restarted, once for 4.5 days.

Adds request_timeout (yaml and DSN) for the Loki client's HTTP requests, default 2m: above Loki's own 1m query timeout, so Loki gives up on a slow query first. A timed-out request logs a warning and is retried from the same position through the existing shouldRetry backoff, so no lines are lost and it counts against max_failure_duration like any other error: two failed polls in a row more than max_failure_duration (default 30s) apart make the source give up, as already happens when Loki is unreachable. A request that never returns now ends that way instead of hanging forever. The websocket tail dialer gets the same value as its handshake timeout.

How it was tested

New tests in pkg/acquisition/modules/loki/loki_test.go. TestRequestTimeoutRecovers serves a query_range that hangs the first time and answers after that. It checks the source reads every line in tail and one-shot mode. TestRequestTimeoutConfig covers the defaults, explicit and invalid values, and the DSN.

$ go test -count=1 -run TestRequestTimeout -v ./pkg/acquisition/modules/loki/...
--- PASS: TestRequestTimeoutRecovers (2.21s)
--- PASS: TestRequestTimeoutConfig (0.00s)
ok      github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/loki  2.218s

The same test with the old loki_client.go put back (from the parent commit) fails, so it catches the hang:

--- FAIL: TestRequestTimeoutRecovers (10.01s)
    --- FAIL: TestRequestTimeoutRecovers/streaming (5.01s)
        loki_test.go:534: got 0 lines; source stuck on the unanswered request
    --- FAIL: TestRequestTimeoutRecovers/one_shot (5.00s)
        loki_test.go:534: got 0 lines; source stuck on the unanswered request
$ TEST_LOCAL_ONLY=1 go test -count=1 ./pkg/acquisition/modules/loki/...
ok      github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/loki  2.230s

golangci-lint (v2.13.0): 0 issues for linux, windows and freebsd.

Checklist

  • One concern only.
  • No break to LAPI/CAPI payloads, database schema, config keys, or cscli -o json|raw output.
  • A human has reviewed this diff line by line.
  • A human has tested this change, not only an agent.

AI assistance used: mostly

🤖 Generated with Claude Code

The Loki client used http.DefaultClient, which has no timeout. A
query_range request that Loki accepts but never answers (e.g. a query
frontend losing a sub-query when a query-scheduler restarts) blocked the
source forever: nothing logged, retry logic never reached.

Add request_timeout (yaml and DSN; default 2m, above Loki's 1m query timeout). A timed
out request logs a warning and goes through the existing
shouldRetry/backoff path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

@NoahSurprenant: There are no 'kind' label on this PR. You need a 'kind' label to generate the release automatically.

  • /kind feature
  • /kind enhancement
  • /kind refactoring
  • /kind fix
  • /kind chore
  • /kind dependencies
Details

I am a bot created to help the crowdsecurity developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.

@github-actions

Copy link
Copy Markdown

@NoahSurprenant: There are no area labels on this PR. You can add as many areas as you see fit.

  • /area agent
  • /area local-api
  • /area cscli
  • /area appsec
  • /area security
  • /area configuration
Details

I am a bot created to help the crowdsecurity developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.

@NoahSurprenant

Copy link
Copy Markdown
Author

/area agent
/kind fix

@NoahSurprenant

Copy link
Copy Markdown
Author

The Loki side is being fixed too (grafana/loki#25154 fixes the other end), but a client timeout is still needed: older Loki versions, a proxy in front of Loki, or a dropped connection can leave a request unanswered the same way.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant