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

Add logfmt selective label extraction #6675

Merged
merged 22 commits into from
Feb 22, 2023

Conversation

btaani
Copy link
Contributor

@btaani btaani commented Jul 13, 2022

What this PR does / why we need it:
This PR introduces extracting labels from a log line in logfmt, with an extra option to rename them.

For example, this query:

{app=”foo”} | logfmt msg,status

will extract the labels msg and status from the following logfmt line:

level=error ts=2021-02-12T19:18:10.037940878Z caller=client.go:294 component=client host=observability-loki-gateway msg="final error sending batch" status=400 error="server returned HTTP status 400 Bad Request (400): entry with timestamp 2021-02-12 19:18:08.917452 +0000 UTC ignored, reason: 'entry out of order' for stream..."

With the following results:

msg="final error sending batch"
status=”400”

Another possible scenario with label renaming:

{app=”foo”} | logfmt message="msg", status

That produces the following results:

message="final error sending batch"
status=”400”

Which issue(s) this PR fixes:
Fixes #3355

Special notes for your reviewer:

Checklist

  • Documentation added
  • Tests updated
  • Is this an important fix or new feature? Add an entry in the CHANGELOG.md.
  • Changes that require user attention or interaction to upgrade are documented in docs/sources/upgrading/_index.md

@btaani btaani changed the title adding logfmt label extraction syntax adding logfmt selective label extraction Jul 14, 2022
@btaani btaani changed the title adding logfmt selective label extraction Add logfmt selective label extraction Jul 14, 2022
@btaani btaani force-pushed the logql-label-extraction branch 3 times, most recently from c3d7b3b to 0ab797d Compare July 22, 2022 11:23
@stale
Copy link

stale bot commented Sep 21, 2022

Hi! This issue has been automatically marked as stale because it has not had any
activity in the past 30 days.

We use a stalebot among other tools to help manage the state of issues in this project.
A stalebot can be very useful in closing issues in a number of cases; the most common
is closing issues or PRs where the original reporter has not responded.

Stalebots are also emotionless and cruel and can close issues which are still very relevant.

If this issue is important to you, please add a comment to keep it open. More importantly, please add a thumbs-up to the original issue entry.

We regularly sort for closed issues which have a stale label sorted by thumbs up.

We may also:

  • Mark issues as revivable if we think it's a valid issue but isn't something we are likely
    to prioritize in the future (the issue will still remain closed).
  • Add a keepalive label to silence the stalebot if the issue is very common/popular/important.

We are doing our best to respond, organize, and prioritize all issues but it can be a challenging task,
our sincere apologies if you find yourself at the mercy of the stalebot.

@stale stale bot added the stale A stale issue or PR that will automatically be closed. label Sep 21, 2022
@btaani
Copy link
Contributor Author

btaani commented Sep 21, 2022

/keepalive

@stale stale bot removed the stale A stale issue or PR that will automatically be closed. label Sep 21, 2022
@github-actions github-actions bot added the type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories label Sep 26, 2022
@btaani btaani force-pushed the logql-label-extraction branch 7 times, most recently from 768c85f to 8275502 Compare September 28, 2022 14:49
@btaani btaani marked this pull request as ready for review September 28, 2022 17:10
@btaani btaani requested a review from a team as a code owner September 28, 2022 17:10
@slim-bean
Copy link
Collaborator

Oh my, so sorry to let this hit the stalebot 😬

We've talked about adding this for a long time!!

Thanks so much for the PR, we've been super busy lately but will try to take a look at this as soon as we can!!!

@dannykopping dannykopping added the keepalive An issue or PR that will be kept alive and never marked as stale. label Sep 28, 2022
Copy link
Contributor

@chaudum chaudum left a comment

Choose a reason for hiding this comment

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

Looks great! Just one question regarding the <label>_extracted labels.

docs/sources/logql/log_queries.md Outdated Show resolved Hide resolved
docs/sources/logql/log_queries.md Outdated Show resolved Hide resolved
pkg/logql/log/parser.go Outdated Show resolved Hide resolved
pkg/logql/log/parser_test.go Outdated Show resolved Hide resolved
pkg/logql/log/parser.go Outdated Show resolved Hide resolved
pkg/logql/log/parser.go Outdated Show resolved Hide resolved
pkg/logql/log/parser_test.go Outdated Show resolved Hide resolved
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@btaani
Copy link
Contributor Author

btaani commented Sep 29, 2022

/hold
I still need to add the benchmark tests

@btaani
Copy link
Contributor Author

btaani commented Oct 4, 2022

Benchmark test added.
Results:

$ go test -run=NONE -bench='Logfmt' -benchtime 5s -count 5 .
goos: linux
goarch: amd64
pkg: github.com/grafana/loki/pkg/logql/log
cpu: Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz
BenchmarkLogfmtParser-8             	 1919780	      3389 ns/op	     264 B/op	      17 allocs/op
BenchmarkLogfmtParser-8             	 1765072	      3433 ns/op	     264 B/op	      17 allocs/op
BenchmarkLogfmtParser-8             	 1748881	      3428 ns/op	     264 B/op	      17 allocs/op
BenchmarkLogfmtParser-8             	 1749825	      3406 ns/op	     264 B/op	      17 allocs/op
BenchmarkLogfmtParser-8             	 1757325	      3372 ns/op	     264 B/op	      17 allocs/op
BenchmarkLogfmtExpressionParser-8   	  652922	      9622 ns/op	    5411 B/op	      68 allocs/op
BenchmarkLogfmtExpressionParser-8   	  615289	      9584 ns/op	    5411 B/op	      68 allocs/op
BenchmarkLogfmtExpressionParser-8   	  621687	      9592 ns/op	    5411 B/op	      68 allocs/op
BenchmarkLogfmtExpressionParser-8   	  620328	      9693 ns/op	    5412 B/op	      68 allocs/op
BenchmarkLogfmtExpressionParser-8   	  600560	      9659 ns/op	    5411 B/op	      68 allocs/op
PASS
ok  	github.com/grafana/loki/pkg/logql/log	77.919s

Copy link
Contributor

@chaudum chaudum left a comment

Choose a reason for hiding this comment

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

I think we are good to go! 🚢

Copy link
Member

@owen-d owen-d left a comment

Choose a reason for hiding this comment

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

This LGTM. Really nice work, apologies it took so long.

@owen-d owen-d merged commit 68a9fd6 into grafana:main Feb 22, 2023
@btaani btaani deleted the logql-label-extraction branch February 23, 2023 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keepalive An issue or PR that will be kept alive and never marked as stale. size/XL type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LogQL: Selective label extraction with logfmt pipeline
8 participants