Skip to content

Commit 84da73d

Browse files
mergify[bot]efd6
andauthored
[8.18](backport #45810) x-pack/filebeat/input/httpjson: allow template termination without logging error (#45993)
* x-pack/filebeat/input/httpjson: allow template termination without logging error (#45810) This adds a terminate helper that can be used to signal that a template should terminate without an error. The termination can (should) be used to report the reason for the termination which will be logged at DEBUG level, but otherwise ignored. (cherry picked from commit f324f29) # Conflicts: # docs/reference/filebeat/filebeat-input-httpjson.md # x-pack/filebeat/input/httpjson/value_tpl.go * remove irrelevant changelog entries * remove markdown * add asciidoc documentation * resolve conflicts --------- Co-authored-by: Dan Kortschak <dan.kortschak@elastic.co>
1 parent 6489b4d commit 84da73d

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
241241
- Jounrald input now supports filtering by facilities {pull}41061[41061]
242242
- Add ability to remove request trace logs from http_endpoint input. {pull}40005[40005]
243243
- Add ability to remove request trace logs from entityanalytics input. {pull}40004[40004]
244+
- Add mechanism to allow HTTP JSON templates to terminate without logging an error. {issue}45664[45664] {pull}45810[45810]
244245

245246
*Auditbeat*
246247

x-pack/filebeat/docs/inputs/input-httpjson.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ Some built-in helper functions are provided to work with the input state inside
230230
- `parseTimestamp`: parses a timestamp in seconds and returns a `time.Time` in UTC. Example: `[[parseTimestamp 1604582732]]` returns `2020-11-05 13:25:32 +0000 UTC`.
231231
- `replaceAll(old, new, s)`: replaces all non-overlapping instances of `old` with `new` in `s`. Example: `[[ replaceAll "some" "my" "some value" ]]` returns `my value`.
232232
- `sprintf`: formats according to a format specifier and returns the resulting string. Refer to https://pkg.go.dev/fmt#Sprintf[the Go docs] for usage. Example: `[[sprintf "%d:%q" 34 "quote this"]]`
233+
- `terminate`: exits the template without falling back to the default value and without causing an error. It takes a single string argument that is logged in debug logging.
233234
- `toInt`: converts a value of any type to an integer when possible. Returns 0 if the conversion fails.
234235
- `toJSON`: converts a value to a JSON string. This can be used with `value_type: json` to create an object from a template. Example: `[[ toJSON .last_response.body.pagingIdentifiers ]]`.
235236
- `urlEncode`: URL encodes the supplied string. Example `[[urlEncode "string1"]]`. Example `[[urlEncode "<string1>"]]` will return `%3Cstring1%3E`.

x-pack/filebeat/input/httpjson/request.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ func evaluateResponse(expression *valueTpl, data []byte, log *logp.Logger) (bool
386386
if err != nil {
387387
return false, fmt.Errorf("error while evaluating expression: %w", err)
388388
}
389+
if val == "" {
390+
return false, nil
391+
}
389392
result, err := strconv.ParseBool(val)
390393
if err != nil {
391394
return false, fmt.Errorf("error while parsing boolean value of string: %w", err)

x-pack/filebeat/input/httpjson/value_tpl.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func (t *valueTpl) Unpack(in string) error {
8282
"urlEncode": urlEncode,
8383
"userAgent": userAgentString,
8484
"uuid": uuidString,
85+
"terminate": func(s string) (any, error) { return nil, &errTerminate{s} },
8586
}).
8687
Delims(leftDelim, rightDelim).
8788
Parse(in)
@@ -94,6 +95,17 @@ func (t *valueTpl) Unpack(in string) error {
9495
return nil
9596
}
9697

98+
type errTerminate struct {
99+
Reason string
100+
}
101+
102+
func (e *errTerminate) Error() string {
103+
if e.Reason != "" {
104+
return "terminated template: " + e.Reason
105+
}
106+
return "terminated template"
107+
}
108+
97109
func (t *valueTpl) Execute(trCtx *transformContext, tr transformable, targetName string, defaultVal *valueTpl, log *logp.Logger) (val string, err error) {
98110
fallback := func(err error) (string, error) {
99111
if defaultVal != nil {
@@ -128,6 +140,11 @@ func (t *valueTpl) Execute(trCtx *transformContext, tr transformable, targetName
128140
}
129141

130142
if err := t.Template.Execute(buf, data); err != nil {
143+
var termErr *errTerminate
144+
if errors.As(err, &termErr) {
145+
log.Debugw("template execution terminated", "target", targetName, "reason", termErr.Reason)
146+
return "", nil
147+
}
131148
return fallback(err)
132149
}
133150

x-pack/filebeat/input/httpjson/value_tpl_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ func TestValueTpl(t *testing.T) {
7474
paramDefVal: "25",
7575
expectedVal: "25",
7676
},
77+
{
78+
name: "terminate",
79+
value: `[[if false]]ok[[else]][[terminate "because reasons"]][[end]]`,
80+
paramCtx: emptyTransformContext(),
81+
paramTr: transformable{},
82+
paramDefVal: "this should not be seen",
83+
expectedVal: "",
84+
},
7785
{
7886
name: "returns error if result is empty and no default is set",
7987
value: "",

0 commit comments

Comments
 (0)