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

LogQL: add the default sprig template function to logql label/line formatter #6163

Merged
merged 6 commits into from
May 16, 2022

Conversation

jburnham
Copy link
Contributor

What this PR does:
With the default sprig template function, we will be able to make it easier to output a default value if the source string is otherwise empty. Useful for json fields that can be missing, like HTTP headers in a log line that aren't required as in the following:

{job="access_log"} | json | line_format `{{.http_request_headers_x_forwarded_for | default "-"}}`

Previously this could only be done (that I found) with the much more verbose

{{regexReplaceAllLiteral "^$" .http_request_headers_x_forwarded_for "-"}}

Checklist

  • Documentation added
  • Tests updated
  • Is this an important fix or new feature? Add an entry in the CHANGELOG.md.

With the `default` sprig template function, we will be able to make it easier to output a default value if the source string is otherwise empty. Useful for json fields that can be missing, like HTTP headers in a log line that aren't required as in the following:

```
{job="access_log"} | json | line_format `{{.http_request_headers_x_forwarded_for | default "-"}}`
```

Previously this could only be done (that I found) with the much more verbose
```
{{regexReplaceAllLiteral "^$" .http_request_headers_x_forwarded_for "-"}}
```
@CLAassistant
Copy link

CLAassistant commented May 13, 2022

CLA assistant check
All committers have signed the CLA.

@jburnham jburnham marked this pull request as ready for review May 13, 2022 20:44
@jburnham jburnham requested review from KMiller-Grafana and a team as code owners May 13, 2022 20:44
@kavirajk kavirajk changed the title LogQL: add the default sprig template function to logql label/line formatter LogQL: add the default string template function to logql label/line formatter May 16, 2022
@kavirajk kavirajk changed the title LogQL: add the default string template function to logql label/line formatter LogQL: add the default sprig template function to logql label/line formatter May 16, 2022
Copy link
Contributor

@kavirajk kavirajk left a comment

Choose a reason for hiding this comment

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

Thanks @jburnham. LGTM. Nice addition to label/line formatter. Added one minor suggestion.

docs/sources/logql/template_functions.md Outdated Show resolved Hide resolved
Co-authored-by: Kaviraj Kanagaraj <kavirajkanagaraj@gmail.com>
@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%

Copy link
Contributor

@vlad-diachenko vlad-diachenko left a comment

Choose a reason for hiding this comment

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

LGTM. just a small nit

docs/sources/logql/template_functions.md Outdated Show resolved Hide resolved
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.

Nice work, glad to have this!

Co-authored-by: Vladyslav Diachenko <82767850+vlad-diachenko@users.noreply.github.com>
@owen-d owen-d merged commit 97c7d7c into grafana:main May 16, 2022
@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.3%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@osg-grafana osg-grafana added type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories and removed area/docs labels Oct 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/M 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.

8 participants