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: Improve template format #2822

Merged
merged 40 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
6715d03
Add error labels on failure and new functions for templates.
cyriltovena Oct 27, 2020
94adcd9
Updates docs.
cyriltovena Oct 27, 2020
f2b0e99
Update docs/sources/logql/_index.md
cyriltovena Nov 5, 2020
d28320b
Update docs/sources/logql/_index.md
cyriltovena Nov 5, 2020
b54c7a6
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
cfec17d
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
1084637
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
2767d24
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
bf42dbc
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
46904dd
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
4a1cc24
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
5ed2cff
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
be9be60
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
f32becf
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
1dfda03
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
38ddece
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
96623a7
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
a92c661
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
d8ad1b5
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
d24ff39
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
f5d1608
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
dfdda5c
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
2cc17bd
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
e4f63a7
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
565d17d
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
2fe24d8
Improve docs based off review feedback.
cyriltovena Nov 5, 2020
8c55441
Review feedback.
cyriltovena Nov 5, 2020
0fb3dcc
Fixes panic out of boundary
cyriltovena Nov 5, 2020
0bfbc1c
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
4c18044
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
7bd59ec
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
84231af
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
257947e
Update docs/sources/logql/_index.md
cyriltovena Nov 5, 2020
128ae1f
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
641543f
Update docs/sources/logql/functions.md
cyriltovena Nov 5, 2020
3be39f3
Update docs/sources/logql/_index.md
cyriltovena Nov 5, 2020
a9a7579
Apply suggestions from code review
cyriltovena Nov 5, 2020
aaee685
Apply suggestions from code review
cyriltovena Nov 5, 2020
c97fcf2
Apply suggestions from code review
cyriltovena Nov 5, 2020
54b6898
Merge remote-tracking branch 'upstream/master' into improve-format
cyriltovena Nov 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 3 additions & 78 deletions docs/sources/logql/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,97 +295,22 @@ Will extract and rewrite the log line to only contains the query and the duratio

You can use double quoted string for the template or single backtick \``\{{.label_name}}`\` to avoid the need to escape special characters.

See [functions](#Template-functions) to learn about available functions in the template format.
See [functions](functions/) to learn about available functions in the template format.

#### Labels Format Expression

The `| label_format` expression can renamed, modify or add labels. It takes as parameter a comma separated list of equality operations, enabling multiple operations at once.

When both side are label identifiers, for example `dst=src`, the operation will rename the `src` label into `dst`.

The left side can alternatively be a template string (double quoted or backtick), for example `dst="{{.status}} {{.query}}"`, in which case the `dst` label value will be replace by the result of the [text/template](https://golang.org/pkg/text/template/) evaluation. This is the same template engine as the `| line_format` expression, this mean labels are available as variables and you can use the same list of [functions](#Template-functions).
The left side can alternatively be a template string (double quoted or backtick), for example `dst="{{.status}} {{.query}}"`, in which case the `dst` label value is replaced by the result of the [text/template](https://golang.org/pkg/text/template/) evaluation. This is the same template engine as the `| line_format` expression, which means labels are available as variables and you can use the same list of [functions](functions/).

In both case if the destination label doesn't exist a new one will be created.
In both cases, if the destination label doesn't exist, then a new one is created.

The renaming form `dst=src` will _drop_ the `src` label after remapping it to the `dst` label. However, the _template_ form will preserve the referenced labels, such that `dst="{{.src}}"` results in both `dst` and `src` having the same value.

> A single label name can only appear once per expression. This means `| label_format foo=bar,foo="new"` is not allowed but you can use two expressions for the desired effect: `| label_format foo=bar | label_format foo="new"`

#### Template functions

The text template format used in `| line_format` and `| label_format` support functions the following list of functions.

##### ToLower & ToUpper

Convert the entire string to lowercase or uppercase:

Examples:

```template
"{{.request_method | ToLower}}"
"{{.request_method | ToUpper}}"
`{{ToUpper "This is a string" | ToLower}}`
```

##### Replace

Perform simple string replacement.

It takes three arguments:

- string to replace
- string to replace with
- source string

Example:

```template
`"This is a string" | Replace " " "-"`
```

The above will produce `This-is-a-string`

##### Trim

`Trim` returns a slice of the string s with all leading and
trailing Unicode code points contained in cutset removed.

`TrimLeft` and `TrimRight` are the same as `Trim` except that it respectively trim only leading and trailing characters.

```template
`{{ Trim .query ",. " }}`
`{{ TrimLeft .uri ":" }}`
`{{ TrimRight .path "/" }}`
```

`TrimSpace` TrimSpace returns string s with all leading
and trailing white space removed, as defined by Unicode.

```template
{{ TrimSpace .latency }}
```

`TrimPrefix` and `TrimSuffix` will trim respectively the prefix or suffix supplied.

```template
{{ TrimPrefix .path "/" }}
```

##### Regex

`regexReplaceAll` returns a copy of the input string, replacing matches of the Regexp with the replacement string replacement. Inside string replacement, $ signs are interpreted as in Expand, so for instance $1 represents the text of the first sub-match. See the golang [docs](https://golang.org/pkg/regexp/#Regexp.ReplaceAll) for detailed examples.

```template
`{{ regexReplaceAllLiteral "(a*)bc" .some_label "${1}a" }}`
```

`regexReplaceAllLiteral` returns a copy of the input string, replacing matches of the Regexp with the replacement string replacement The replacement string is substituted directly, without using Expand.

```template
`{{ regexReplaceAllLiteral "(ts=)" .timestamp "timestamp=" }}`
```

You can combine multiple function using pipe, for example if you want to strip out spaces and make the request method in capital you would write the following template `{{ .request_method | TrimSpace | ToUpper }}`.

### Log Queries Examples

Expand Down
Loading