Skip to content

Conversation

@UmarFarooq-MP
Copy link

@UmarFarooq-MP UmarFarooq-MP commented Sep 20, 2025

What this PR does

This PR implements centralized error handling for the proxy layer in lokxy.

Key Changes

  • Added a new pkg/proxy/errors package with sentinel errors (e.g. ErrNoUpstream, ErrReadBodyFailed, ErrTailRequiresWebSocket).
  • Introduced WriteJSONError utility so that all handlers return consistent JSON error responses instead of raw text/plain.
  • Updated all proxy handlers (query, labels, series, stats, tail) to use the centralized error definitions.
  • Ensured successful paths still forward valid Loki JSON responses without modification.
  • Added shell command examples in README.md so users can easily test both success and failure cases for every endpoint.

Why this matters

  • Users (and Grafana) now receive predictable JSON error payloads instead of raw error strings.
  • Easier debugging: each error is clearly named, and log messages match user-facing error responses.
  • Cleaner code: handlers no longer duplicate ad-hoc error handling logic.

Testing the Proxy Endpoints

To verify that lokxy is correctly proxying requests to Loki, you can run a few simple curl and websocat commands.
These examples assume:

  • lokxy is running on http://localhost:3100
  • Loki is running on http://localhost:9090
  • You already have logs (e.g., from the included log-generator container)

Query Range (Success)

START=$(( ($(date +%s) - 300) * 1000000000 ))

curl -sG \
  --data-urlencode 'query={container="lokxy-log-generator-1"}' \
  --data-urlencode "limit=5" \
  --data-urlencode "start=$START" \
  http://localhost:3100/loki/api/v1/query_range | jq .

curl -sG \
  --data-urlencode 'query={app=~".*"}' \
  --data-urlencode "limit=5" \
  --data-urlencode "start=$START" \
  http://localhost:3100/loki/api/v1/query_range

curl -s http://localhost:3100/loki/api/v1/labels | jq .

curl -sG \
  --data-urlencode 'match[]={container="lokxy-log-generator-1"}' \
  http://localhost:3100/loki/api/v1/series | jq .

websocat "ws://localhost:3100/loki/api/v1/tail?query={container=\"lokxy-log-generator-1\"}&limit=5"

This way your test.sh remains local for you, and contributors can still see exactly how to test without needing a script.

Closes #89

UmarFarooq-MP and others added 4 commits September 21, 2025 08:06
Signed-off-by: UmarFarooq-MP <[email protected]>
Add spell check workflow

Signed-off-by: Hélia Barroso <[email protected]>
Signed-off-by: UmarFarooq-MP <[email protected]>
…lojmdias#91)

Bumps [github.com/grafana/loki/v3](https://github.com/grafana/loki) from
3.5.4 to 3.5.5.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/grafana/loki/releases">github.com/grafana/loki/v3's
releases</a>.</em></p>
<blockquote>
<h2>v3.5.5</h2>
<h2><a
href="https://github.com/grafana/loki/compare/v3.5.4...v3.5.5">3.5.5</a>
(2025-09-11)</h2>
<h3>Bug Fixes</h3>
<ul>
<li><strong>release-3.5.x:</strong> backport fix to correctly calculate
entry metadata size (<a
href="https://redirect.github.com/grafana/loki/issues/19163">#19163</a>)
(<a
href="https://github.com/grafana/loki/commit/5aa8bd27946a1ce5267790ebe9210f93cd7111b2">5aa8bd2</a>)</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/grafana/loki/commit/5aa8bd27946a1ce5267790ebe9210f93cd7111b2"><code>5aa8bd2</code></a>
fix(release-3.5.x): backport fix to correctly calculate entry metadata
size (...</li>
<li><a
href="https://github.com/grafana/loki/commit/58c0ca12feaaf99a90a28563389e68f47bd075bd"><code>58c0ca1</code></a>
docs: Backport 19151 (<a
href="https://redirect.github.com/grafana/loki/issues/19158">#19158</a>)</li>
<li><a
href="https://github.com/grafana/loki/commit/78f9c6f18455dff0a0a1dc0600225d98cc5d133c"><code>78f9c6f</code></a>
docs: Add breaking change to upgrade (backport release-3.5.x) (<a
href="https://redirect.github.com/grafana/loki/issues/19147">#19147</a>)</li>
<li><a
href="https://github.com/grafana/loki/commit/ecdf1ea7826be513a3cbf91875459e656cad3ca8"><code>ecdf1ea</code></a>
docs: manual backport 3.5 (<a
href="https://redirect.github.com/grafana/loki/issues/19123">#19123</a>)</li>
<li><a
href="https://github.com/grafana/loki/commit/2e0b55d02dc6dc5cf4b6133e5dbeb00e0f01d72b"><code>2e0b55d</code></a>
docs: Release notes for 3.5.4 (<a
href="https://redirect.github.com/grafana/loki/issues/19113">#19113</a>)</li>
<li><a
href="https://github.com/grafana/loki/commit/b4075c0a307b68183366d9a2d618948199135e67"><code>b4075c0</code></a>
chore(release-3.5.x): release 3.5.4 (<a
href="https://redirect.github.com/grafana/loki/issues/18680">#18680</a>)</li>
<li>See full diff in <a
href="https://github.com/grafana/loki/compare/v3.5.4...v3.5.5">compare
view</a></li>
</ul>
</details>
<br />

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=github.com/grafana/loki/v3&package-manager=go_modules&previous-version=3.5.4&new-version=3.5.5)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)

</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: UmarFarooq-MP <[email protected]>
Signed-off-by: UmarFarooq-MP <[email protected]>
@UmarFarooq-MP UmarFarooq-MP force-pushed the feat/89-proxy-error-handling branch from f44590c to 8347b0b Compare September 21, 2025 03:07
@UmarFarooq-MP
Copy link
Author

is it wise to update the tool chain to latest on 1.24 but CI needs to update for that otherwise CI will keep on failing. @heliapb

@paulojmdias
Copy link
Owner

@UmarFarooq-MP thanks for your PR! Can we just focus on returning back the errors returned by Loki? Currently it seems a big refactor

@UmarFarooq-MP
Copy link
Author

UmarFarooq-MP commented Sep 22, 2025

I think you are right let me revert them. I added a response pattern with sentinel errors. @paulojmdias

Signed-off-by: UmarFarooq-MP <[email protected]>
@UmarFarooq-MP UmarFarooq-MP force-pushed the feat/89-proxy-error-handling branch from 4f77a7f to 04ed207 Compare September 22, 2025 12:58
@UmarFarooq-MP
Copy link
Author

The PR is updated @paulojmdias @heliapb

// Log the raw body for debugging
level.Debug(logger).Log("msg", "Received body for label values", "body", string(bodyBytes))

// Unmarshal into a struct that matches the actual response format
Copy link
Owner

Choose a reason for hiding this comment

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

@UmarFarooq-MP can we keep all the existing comments to make this PR easier to review?

Copy link
Author

Choose a reason for hiding this comment

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

sure @paulojmdias , was caught up in thing will try to do it in this week.

Copy link
Owner

Choose a reason for hiding this comment

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

@UmarFarooq-MP any chance to came back on this ? 🙏

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backend errors not properly handled

3 participants