diff --git a/file/readfile.go b/file/readfile.go index 7fb8d3b82..93054c8e3 100644 --- a/file/readfile.go +++ b/file/readfile.go @@ -158,12 +158,18 @@ func toFloat(key string) (float64, error) { return strconv.ParseFloat(key, 64) } +func indent(spaces int, v string) string { + pad := strings.Repeat(" ", spaces) + return strings.Replace(v, "\n", "\n"+pad, -1) +} + func renderTemplate(content string) (string, error) { t := template.New("state").Funcs(template.FuncMap{ "env": getPrefixedEnvVar, "toBool": toBool, "toInt": toInt, "toFloat": toFloat, + "indent": indent, }).Delims("${{", "}}") t, err := t.Parse(content) if err != nil { diff --git a/file/readfile_test.go b/file/readfile_test.go index 385132763..df2a49285 100644 --- a/file/readfile_test.go +++ b/file/readfile_test.go @@ -172,6 +172,17 @@ func Test_getContent(t *testing.T) { args: args{[]string{"testdata/file.yaml"}}, envVars: map[string]string{ "DECK_SVC2_HOST": "2.example.com", + "DECK_FILE_LOG_FUNCTION": ` +function parse_traceid(str)str = string.sub(str,1,8) + local uint = 0 + for i = 1, #str do + uint = uint + str:byte(i) * 0x100^(i-1) + end + return string.format("%.0f", uint) +end + +kong.log.set_serialize_value("trace_id", parse_traceid(ngx.ctx.KONG_SPANS[1].trace_id)) +kong.log.set_serialize_value("span_id", parse_traceid(ngx.ctx.KONG_SPANS[1].span_id))`, }, want: &Content{ Services: []FService{ @@ -197,6 +208,25 @@ func Test_getContent(t *testing.T) { Name: kong.String("prometheus"), }, }, + { + Plugin: kong.Plugin{ + Name: kong.String("pre-function"), + Config: kong.Configuration{ + "log": ` +function parse_traceid(str)str = string.sub(str,1,8) + local uint = 0 + for i = 1, #str do + uint = uint + str:byte(i) * 0x100^(i-1) + end + return string.format("%.0f", uint) +end + +kong.log.set_serialize_value("trace_id", parse_traceid(ngx.ctx.KONG_SPANS[1].trace_id)) +kong.log.set_serialize_value("span_id", parse_traceid(ngx.ctx.KONG_SPANS[1].span_id)) +`, + }, + }, + }, }, }, wantErr: false, @@ -220,7 +250,8 @@ func Test_getContent(t *testing.T) { name: "multiple files", args: args{[]string{"testdata/file.yaml", "testdata/file.json"}}, envVars: map[string]string{ - "DECK_SVC2_HOST": "2.example.com", + "DECK_SVC2_HOST": "2.example.com", + "DECK_FILE_LOG_FUNCTION": "kong.log.set_serialize_value('trace_id', 1))", }, want: &Content{ Services: []FService{ @@ -246,6 +277,14 @@ func Test_getContent(t *testing.T) { Name: kong.String("prometheus"), }, }, + { + Plugin: kong.Plugin{ + Name: kong.String("pre-function"), + Config: kong.Configuration{ + "log": "kong.log.set_serialize_value('trace_id', 1))\n", + }, + }, + }, }, Consumers: []FConsumer{ { diff --git a/file/testdata/file.yaml b/file/testdata/file.yaml index 4d9d0ff62..05d6fa8b6 100644 --- a/file/testdata/file.yaml +++ b/file/testdata/file.yaml @@ -9,3 +9,7 @@ services: - '<' # verifies that the templating engine does not perform character escaping plugins: - name: prometheus +- name: pre-function + config: + log: | + ${{ env "DECK_FILE_LOG_FUNCTION" | indent 8 }}