Skip to content

Commit f02f9c2

Browse files
authored
Add json_lines webhook input format (#100)
fluent-bit allows two JSON log formats: a stream of JSON objects not separated at all, and one with a single JSON object per line. None of those was compatible with grok_exporter so far. This change implements the json_lines format that is compatible with fluent-bit.
1 parent 6d86abe commit f02f9c2

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

CONFIG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ input:
183183
# POST body envelope must be a json array "[ <entry>, <entry> ]". Log
184184
# entry text is selected from the value of a json key determined by
185185
# webhook_json_selector.
186+
# json_lines: Webhook POST body contains multiple json log entries, with
187+
# newline-separated log lines holding an individual json object. JSON
188+
# object itself may not contain newlines. For example:
189+
# example:
190+
# { app="foo", stage="prod", log="example log message" }
191+
# { app="bar", stage="dev", log="another line" }
192+
# Log entry text is selected from the value of a json key determined
193+
# by webhook_json_selector.
186194
# Default is `text_single`
187195
webhook_format: json_bulk
188196

@@ -361,7 +369,7 @@ labels:
361369
If you don't want the full path but only the file name, you can use the `base` template function, see next section.
362370
363371
#### extra
364-
The `extra` variable is always present for input type `webhook` with format being either `json_single` or `json_bulk`.
372+
The `extra` variable is always present for input type `webhook` with format being either `json_single`, `json_lines` or `json_bulk`.
365373
It contains the entire JSON object that was parsed.
366374
You can use it like this:
367375

config/v3/configV3.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ func (c *InputConfig) validate() error {
392392
} else if c.WebhookPath[0] != '/' {
393393
return fmt.Errorf("invalid input configuration: 'input.webhook_path' must start with \"/\"")
394394
}
395-
if c.WebhookFormat != "text_single" && c.WebhookFormat != "text_bulk" && c.WebhookFormat != "json_single" && c.WebhookFormat != "json_bulk" {
396-
return fmt.Errorf("invalid input configuration: 'input.webhook_format' must be \"text_single|text_bulk|json_single|json_bulk\"")
395+
if c.WebhookFormat != "text_single" && c.WebhookFormat != "text_bulk" && c.WebhookFormat != "json_single" && c.WebhookFormat != "json_bulk" && c.WebhookFormat != "json_lines" {
396+
return fmt.Errorf("invalid input configuration: 'input.webhook_format' must be \"text_single|text_bulk|json_single|json_bulk|json_lines\"")
397397
}
398398
if c.WebhookJsonSelector == "" {
399399
return fmt.Errorf("invalid input configuration: 'input.webhook_json_selector' is required for input type \"webhook\"")

example/config_logstash_http_input_ipv6.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ input:
2727
# json_single: Webhook POST body is a single json log entry. Log entry
2828
# text is selected from the value of a json key determined by
2929
# webhook_json_selector.
30+
# json_lines: Webhook POST body contains multiple json log entries, with
31+
# newline-separated log lines holding an individual json object. JSON
32+
# object itself may not contain newlines. For example:
33+
# example:
34+
# { app="foo", stage="prod", log="example log message" }
35+
# { app="bar", stage="dev", log="another line" }
36+
# Log entry text is selected from the value of a json key determined
37+
# by webhook_json_selector.
3038
# json_bulk: Webhook POST body contains multiple json log entries. The
3139
# POST body envelope must be a json array "[ <entry>, <entry> ]". Log
3240
# entry text is selected from the value of a json key determined by

tailer/webhookTailer.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package tailer
1616

1717
import (
18+
"bytes"
1819
"errors"
1920
"fmt"
2021
json "github.com/bitly/go-simplejson"
@@ -142,6 +143,33 @@ func WebhookProcessBody(c *configuration.InputConfig, b []byte) []context_string
142143
break
143144
}
144145
strs = append(strs, context_string{line: s, extra: j.MustMap()})
146+
case "json_lines":
147+
if len(c.WebhookJsonSelector) == 0 || c.WebhookJsonSelector[0] != '.' {
148+
logrus.Errorf("%v: invalid webhook json selector", c.WebhookJsonSelector)
149+
break
150+
}
151+
152+
for _, split := range bytes.Split(b, []byte("\n")) {
153+
if len(split) == 0 {
154+
continue
155+
}
156+
j, err := json.NewJson(split)
157+
if err != nil {
158+
logrus.WithFields(logrus.Fields{
159+
"post_body": string(b),
160+
}).Warn("Unable to Parse JSON")
161+
break
162+
}
163+
s, err := processPath(j, c.WebhookJsonSelector)
164+
if err != nil {
165+
logrus.WithFields(logrus.Fields{
166+
"post_body": string(b),
167+
"webhook_json_selector": c.WebhookJsonSelector,
168+
}).Warn("Unable to find selector path")
169+
break
170+
}
171+
strs = append(strs, context_string{line: s, extra: j.MustMap()})
172+
}
145173
case "json_bulk":
146174
if len(c.WebhookJsonSelector) == 0 || c.WebhookJsonSelector[0] != '.' {
147175
logrus.Errorf("%v: invalid webhook json selector", c.WebhookJsonSelector)

tailer/webhookTailer_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,42 @@ func TestWebhookJsonBulk(t *testing.T) {
189189
}
190190
}
191191

192+
func TestWebhookJsonLines(t *testing.T) {
193+
// This test follows the format of Logstash HTTP Non-Bulk Output
194+
// https://www.elastic.co/guide/en/logstash/current/plugins-outputs-http.html
195+
// format="json_batch"
196+
197+
c := &configuration.InputConfig{
198+
Type: "webhook",
199+
WebhookPath: "/webhook",
200+
WebhookFormat: "json_lines",
201+
WebhookJsonSelector: ".message",
202+
WebhookTextBulkSeparator: "",
203+
}
204+
205+
messages := []string{
206+
"2016-04-18 09:33:27 H=(85.214.241.101) [114.37.190.56] F=<[email protected]> rejected RCPT <[email protected]>: relay not permitted",
207+
"2016-04-18 12:28:04 H=(85.214.241.101) [118.161.243.219] F=<[email protected]> rejected RCPT <[email protected]>: relay not permitted",
208+
"2016-04-18 19:16:30 H=(85.214.241.101) [114.24.5.12] F=<[email protected]> rejected RCPT <[email protected]>: relay not permitted",
209+
}
210+
211+
blobs := []string{}
212+
for _, message := range messages {
213+
blobs = append(blobs, strings.Replace(createJsonBlob(message), "\n", " ", -1))
214+
}
215+
s := strings.Join(blobs, "\n")
216+
fmt.Printf("Sending Payload: %v", s)
217+
lines := WebhookProcessBody(c, []byte(s))
218+
if len(lines) != len(messages) {
219+
t.Fatal("Expected number of lines to equal number of messages")
220+
}
221+
for i := range messages {
222+
if messages[i] != lines[i].line {
223+
t.Fatal("Expected line to match")
224+
}
225+
}
226+
}
227+
192228
func TestWebhookJsonBulkNegativeMalformedJson(t *testing.T) {
193229
// This test follows the format of Logstash HTTP Non-Bulk Output
194230
// https://www.elastic.co/guide/en/logstash/current/plugins-outputs-http.html

0 commit comments

Comments
 (0)