Skip to content

Commit

Permalink
Add metadata interpolation for all keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffail committed Jul 26, 2018
1 parent 490d02c commit c29ad12
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/config_interpolation.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ referred to will depend on the context of where the function is called.
If a message contains the metadata key/value pair `foo: bar` the function
`${!metadata:foo}` would resolve to `bar`.

If the argument is ommited then all key/value pairs within the metadata will be
printed in the following comma-separated format: `key1:value1,key2:value2,...`.

Message metadata can be modified using the
[metadata processor](./processors/README.md#metadata).

Expand Down
15 changes: 15 additions & 0 deletions lib/util/text/function_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"bytes"
"os"
"regexp"
"sort"
"strconv"
"strings"
"sync"
Expand All @@ -40,6 +41,7 @@ type Message interface {
Get(p int) []byte
GetJSON(p int) (interface{}, error)
GetMetadata(key string) string
IterMetadata(func(k, v string) error) error
Len() int
}

Expand Down Expand Up @@ -134,6 +136,19 @@ var functionVars = map[string]func(msg Message, arg string) []byte{
},
"json_field": jsonFieldFunction,
"metadata": func(m Message, arg string) []byte {
if len(arg) == 0 {
keys := []string{}
m.IterMetadata(func(k, _ string) error {
keys = append(keys, k)
return nil
})
sort.Strings(keys)
kvs := []string{}
for _, k := range keys {
kvs = append(kvs, k+":"+m.GetMetadata(k))
}
return []byte(strings.Join(kvs, ","))
}
return []byte(m.GetMetadata(arg))
},
}
Expand Down
8 changes: 8 additions & 0 deletions lib/util/text/function_vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestFunctionVarDetection(t *testing.T) {
func TestMetadataFunction(t *testing.T) {
msg := types.NewMessage(nil)
msg.SetMetadata("foo", "bar")
msg.SetMetadata("baz", "qux")

act := string(ReplaceFunctionVariables(
msg, []byte(`foo ${!metadata:foo} baz`),
Expand All @@ -69,6 +70,13 @@ func TestMetadataFunction(t *testing.T) {
if exp := "foo baz"; act != exp {
t.Errorf("Wrong result: %v != %v", act, exp)
}

act = string(ReplaceFunctionVariables(
msg, []byte(`${!metadata}`),
))
if exp := "baz:qux,foo:bar"; act != exp {
t.Errorf("Wrong result: %v != %v", act, exp)
}
}

func TestJSONFunction(t *testing.T) {
Expand Down

0 comments on commit c29ad12

Please sign in to comment.