Skip to content

feat: update log level discovery from json to detect nested fields #16026

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

Merged
merged 4 commits into from
Jan 31, 2025

Conversation

ashwanthgoli
Copy link
Contributor

What this PR does / why we need it:

Log level discovery for json lines currently only looks at the top level fields, this helps detect level fields that are nested.
It only does a single pass through the log line for detecting the levels using jsonparser.ObjectEach unlike the existing approach that calls Get for each allowed label.

Note: This approach branches away from getValueUsingJSONParser which was common for both level and generic field detection previously and additionally supported jsonpath expressions.

goos: darwin
goarch: arm64
pkg: github.com/grafana/loki/v3/pkg/distributor
cpu: Apple M1 Pro
                                                                    │ /tmp/before.txt │           /tmp/after.txt            │
                                                                    │     sec/op      │   sec/op     vs base                │
_optParseExtractLogLevelFromLogLineJson/deeply_nested_level_field-8     12721.5n ± 2%   183.0n ± 1%  -98.56% (p=0.000 n=10)
_optParseExtractLogLevelFromLogLineJson/level_field_at_start-8          1139.50n ± 1%   59.45n ± 2%  -94.78% (p=0.000 n=10)
_optParseExtractLogLevelFromLogLineJson/level_field_in_middle-8          1225.5n ± 1%   189.0n ± 6%  -84.58% (p=0.000 n=10)
_optParseExtractLogLevelFromLogLineJson/level_field_at_end-8             1335.5n ± 1%   350.4n ± 3%  -73.76% (p=0.000 n=10)
_optParseExtractLogLevelFromLogLineJson/no_level_field-8                 12.822µ ± 7%   1.098µ ± 3%  -91.44% (p=0.000 n=10)
_optParseExtractLogLevelFromLogLineJson/nested_level_field-8           12597.50n ± 1%   97.86n ± 5%  -99.22% (p=0.000 n=10)

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

Special notes for your reviewer:

Checklist

  • Reviewed the CONTRIBUTING.md guide (required)
  • Documentation added
  • Tests updated
  • Title matches the required conventional commits format, see here
    • Note that Promtail is considered to be feature complete, and future development for logs collection will be in Grafana Alloy. As such, feat PRs are unlikely to be accepted unless a case can be made for the feature actually being a bug fix to existing behavior.
  • Changes that require user attention or interaction to upgrade are documented in docs/sources/setup/upgrade/_index.md
  • If the change is deprecating or removing a configuration option, update the deprecated-config.yaml and deleted-config.yaml files respectively in the tools/deprecated-config-checker directory. Example PR
@github-actions github-actions bot added the type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories label Jan 31, 2025
@shantanualsi
Copy link
Contributor

I like the idea!

You're going depth first in your implementation, I think it would make sense to go by level to reduce the memory usage? eg: {"body": { ...5 level deep json... }, "level": INFO}

I think many log formatters do keep log_level shallow, except for a few customised ones.

Copy link
Contributor

github-actions bot commented Jan 31, 2025

💻 Deploy preview deleted.

@ashwanthgoli
Copy link
Contributor Author

i think breath first search is going to incur memory-overhead, currently the parser is presenting data as it apprears - we'd encounter children first in the json line before the next sibling

I think many log formatters do keep log_level shallow, except for a few customised ones.

👍 we could default the maxDepth to 1 to avoid any overhead

Copy link
Contributor

@shantanualsi shantanualsi left a comment

Choose a reason for hiding this comment

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

LGTM

@ashwanthgoli ashwanthgoli changed the title feat: updates log level discovery from json to detect nested fields Jan 31, 2025
@ashwanthgoli ashwanthgoli merged commit 86dc47b into main Jan 31, 2025
61 checks passed
@ashwanthgoli ashwanthgoli deleted the level-detection-nested-json branch January 31, 2025 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories
2 participants