Skip to content

Commit

Permalink
Add sprig text/template function to template stage.
Browse files Browse the repository at this point in the history
Fixes grafana#3129

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
  • Loading branch information
cyriltovena committed Mar 19, 2021
1 parent b06d970 commit bb30f2f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 43 deletions.
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
26 changes: 13 additions & 13 deletions vendor/github.com/Masterminds/sprig/v3/functions.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bb30f2f

Please sign in to comment.