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 sprig text/template functions to template stage. #3515

Merged
merged 3 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion docs/sources/clients/promtail/stages/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ The snippet above will for instance prepend the log line with the application na

## Supported Functions

> All [sprig functions](http://masterminds.github.io/sprig/) have been added to the template stage in Loki 2.3(along with function described below).

### ToLower & ToUpper

ToLower and ToUpper convert the entire string respectively to lowercase and uppercase.
Expand Down Expand Up @@ -201,7 +203,7 @@ and trailing white space removed, as defined by Unicode.
template: '{{ Hash .Value "salt" }}'
```

Alternatively, you can use `Sha2Hash` for calculating the Sha2_256 of the string. Sha2_256 is faster and requires less CPU than Sha3_256, however it is less secure.
Alternatively, you can use `Sha2Hash` for calculating the Sha2_256 of the string. Sha2_256 is faster and requires less CPU than Sha3_256, however it is less secure.

We recommend using `Hash` as it has a stronger hashing algorithm which we plan to keep strong over time without requiring client config changes.

Expand Down
64 changes: 35 additions & 29 deletions pkg/logentry/stages/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"text/template"
"time"

"github.com/Masterminds/sprig/v3"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/mitchellh/mapstructure"
Expand All @@ -25,35 +26,41 @@ const (
ErrTemplateSourceRequired = "template source value is required"
)

var (
functionMap = template.FuncMap{
"ToLower": strings.ToLower,
"ToUpper": strings.ToUpper,
"Replace": strings.Replace,
"Trim": strings.Trim,
"TrimLeft": strings.TrimLeft,
"TrimRight": strings.TrimRight,
"TrimPrefix": strings.TrimPrefix,
"TrimSuffix": strings.TrimSuffix,
"TrimSpace": strings.TrimSpace,
"Hash": func(salt string, input string) string {
hash := sha3.Sum256([]byte(salt + input))
return hex.EncodeToString(hash[:])
},
"Sha2Hash": func(salt string, input string) string {
hash := sha256.Sum256([]byte(salt + input))
return hex.EncodeToString(hash[:])
},
"regexReplaceAll": func(regex string, s string, repl string) string {
r := regexp.MustCompile(regex)
return r.ReplaceAllString(s, repl)
},
"regexReplaceAllLiteral": func(regex string, s string, repl string) string {
r := regexp.MustCompile(regex)
return r.ReplaceAllLiteralString(s, repl)
},
var extraFunctionMap = template.FuncMap{
"ToLower": strings.ToLower,
"ToUpper": strings.ToUpper,
"Replace": strings.Replace,
"Trim": strings.Trim,
"TrimLeft": strings.TrimLeft,
"TrimRight": strings.TrimRight,
"TrimPrefix": strings.TrimPrefix,
"TrimSuffix": strings.TrimSuffix,
"TrimSpace": strings.TrimSpace,
"Hash": func(salt string, input string) string {
hash := sha3.Sum256([]byte(salt + input))
return hex.EncodeToString(hash[:])
},
"Sha2Hash": func(salt string, input string) string {
hash := sha256.Sum256([]byte(salt + input))
return hex.EncodeToString(hash[:])
},
"regexReplaceAll": func(regex string, s string, repl string) string {
r := regexp.MustCompile(regex)
return r.ReplaceAllString(s, repl)
},
"regexReplaceAllLiteral": func(regex string, s string, repl string) string {
r := regexp.MustCompile(regex)
return r.ReplaceAllLiteralString(s, repl)
},
}

var functionMap = sprig.TxtFuncMap()

func init() {
for k, v := range extraFunctionMap {
functionMap[k] = v
}
)
}

// TemplateConfig configures template value extraction
type TemplateConfig struct {
Expand Down Expand Up @@ -135,7 +142,6 @@ func (o *templateStage) Process(labels model.LabelSet, extracted map[string]inte
} else {
extracted[o.cfgs.Source] = st
}

}

// Name implements Stage
Expand Down
12 changes: 12 additions & 0 deletions pkg/logentry/stages/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ func TestTemplateStage_Process(t *testing.T) {
"testval": "value",
},
},
"sprig": {
TemplateConfig{
Source: "testval",
Template: "{{ add 7 3 }}",
},
map[string]interface{}{
"testval": "Value",
},
map[string]interface{}{
"testval": "10",
},
},
"ToLowerParams": {
TemplateConfig{
Source: "testval",
Expand Down