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

Alerting: Switch to snappy-compressed-protobuf for outgoing push requests to Loki #65077

Merged
merged 10 commits into from
Mar 21, 2023

Conversation

alexweav
Copy link
Contributor

What is this feature?

Loki's HTTP API accepts exactly two formats:

  • snappy-compressed-protobuf
  • uncompressed JSON.

Promtail, the canonical Loki client, uses snappy-compressed-protobuf only. This is not configurable.
Many "loki-like" tools also seem to only accept snappy-compressed-protobuf.

This PR does the same - the default becomes snappy-compressed-protobuf. Right now this is configurable at the code level, when using the client as a library, but not at the grafana configuration level. We can evaluate adding this config option to grafana .ini in the future, but there's precedent to not do so in Promtail.

Why do we need this feature?

Consistency with Promtail. Interoperability with other Loki-like tools. Closer to the main code path in Loki.

Also, we send fewer bytes over the network.

Who is this feature for?

Operators and users who want to run state history against some Loki-like tools that aren't necessarily Loki.

Which issue(s) does this PR fix?:

n/a

Special notes for your reviewer:

Please check that:

  • It works as expected from a user's perspective.
  • If this is a pre-GA feature, it is behind a feature toggle.
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.
  • There are no known compatibility issues with older supported versions of Grafana, or plugins.
  • It passes the Hosted Grafana feature readiness review for observability, scalability, performance, and security.

@alexweav alexweav added this to the 9.4.8 milestone Mar 20, 2023
@alexweav alexweav requested a review from a team March 20, 2023 21:32
@grafanabot
Copy link
Contributor

Hello @alexweav!
Backport pull requests need to be either:

  • Pull requests which address bugs,
  • Urgent fixes which need product approval, in order to get merged,
  • Docs changes.

Please, if the current pull request addresses a bug fix, label it with the type/bug label.
If it already has the product approval, please add the product-approved label. For docs changes, please add the type/docs label.
If none of the above applies, please consider removing the backport label and target the next major/minor release.
Thanks!

Copy link
Contributor

@gotjosh gotjosh left a comment

Choose a reason for hiding this comment

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

LGTM

"golang.org/x/exp/slices"
)

type JsonEncoder struct{}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: these structs are public but their methods are private, not sure if they should be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made the types public so the anything wishing to use this as a library can pass in the right encoding. Such systems should not need to call any encoder methods directly, only pass one in

Comment on lines +28 to +35
// encoder serializes log streams to some byte format.
type encoder interface {
// encode serializes a set of log streams to bytes.
encode(s []stream) ([]byte, error)
// headers returns a set of HTTP-style headers that describes the encoding scheme used.
headers() map[string]string
}

Copy link
Contributor

@gotjosh gotjosh Mar 21, 2023

Choose a reason for hiding this comment

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

nit: you're using the concrete implementation in tests anyways - I understand the "programming practice" but YAGNI it's a thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We already do need it though - the main path uses snappyProto (since that's the main encoding) and tests use json (since it's human readable) so even within this PR we're already swapping out the implementation.

I originally tried it without the interface - enum for encoding type and if statements to call the right encoding method. The result felt pretty bug prone to me, as we needed branches in a number of places to get it right.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense, I missed that detail!

Copy link
Member

@JohnnyQQQQ JohnnyQQQQ left a comment

Choose a reason for hiding this comment

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

LGTM

@alexweav alexweav merged commit bf54f26 into main Mar 21, 2023
@alexweav alexweav deleted the alexweav/snappy-compression branch March 21, 2023 18:38
@grafanabot
Copy link
Contributor

The backport to v9.4.x failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new branch
git switch --create backport-65077-to-v9.4.x origin/v9.4.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x bf54f2672ef45d2737cd2ef5ea43f82b7312d12b
# Push it to GitHub
git push --set-upstream origin backport-65077-to-v9.4.x
git switch main
# Remove the local backport branch
git branch -D backport-65077-to-v9.4.x

Then, create a pull request where the base branch is v9.4.x and the compare/head branch is backport-65077-to-v9.4.x.

@grafanabot grafanabot added the backport-failed Failed to generate backport PR. Please resolve conflicts and create one manually. label Mar 21, 2023
alexweav added a commit that referenced this pull request Mar 21, 2023
…ests to Loki (#65077)

* Encode with snappy, always

* JSON encoder type

* Headers

* Copy labels formatter from promtail

* Implement snappy-proto encoding

* Create encoder interface, test both encoders, choose snappy-proto by default

* Make encoder configurable at the LokiCfg level

* Export both encoders

* Touch up comment and tests

* Drop unnecessary conversions after move to plain strings to appease linter

(cherry picked from commit bf54f26)
@armandgrillet armandgrillet added the product-approved Pull requests that are approved by product/managers and are allowed to be backported label Mar 21, 2023
alexweav added a commit that referenced this pull request Mar 21, 2023
…ests to Loki (#65137)

* Alerting: Switch to snappy-compressed-protobuf for outgoing push requests to Loki (#65077)

* Encode with snappy, always

* JSON encoder type

* Headers

* Copy labels formatter from promtail

* Implement snappy-proto encoding

* Create encoder interface, test both encoders, choose snappy-proto by default

* Make encoder configurable at the LokiCfg level

* Export both encoders

* Touch up comment and tests

* Drop unnecessary conversions after move to plain strings to appease linter

(cherry picked from commit bf54f26)

* Sample fields got renamed between 9.4 and main
gotjosh pushed a commit that referenced this pull request Mar 27, 2023
…ests to Loki (#65077)

* Encode with snappy, always

* JSON encoder type

* Headers

* Copy labels formatter from promtail

* Implement snappy-proto encoding

* Create encoder interface, test both encoders, choose snappy-proto by default

* Make encoder configurable at the LokiCfg level

* Export both encoders

* Touch up comment and tests

* Drop unnecessary conversions after move to plain strings to appease linter
@zerok zerok modified the milestones: 9.4.x, 9.4.8, 9.5.0 Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add to changelog area/alerting Grafana Alerting area/backend backport v9.4.x Mark PR for automatic backport to v9.4.x backport-failed Failed to generate backport PR. Please resolve conflicts and create one manually. kata:state-history product-approved Pull requests that are approved by product/managers and are allowed to be backported
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

6 participants