Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update Loki output to reflect the new API (version before of Loki <1 … #356

Merged
merged 1 commit into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func getConfig() *types.Configuration {
v.SetDefault("Loki.MutualTLS", false)
v.SetDefault("Loki.CheckCert", true)
v.SetDefault("Loki.Tenant", "")
v.SetDefault("Loki.Endpoint", "/api/prom/push")
v.SetDefault("Loki.Endpoint", "/loki/api/v1/push")
v.SetDefault("Loki.ExtraLabels", "")

v.SetDefault("AWS.AccessKeyID", "")
Expand Down
2 changes: 1 addition & 1 deletion config_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ loki:
# mutualtls: false # if true, checkcert flag will be ignored (server cert will always be checked)
# checkcert: true # check if ssl certificate of the output is valid (default: true)
# tenant: "" # Add the tenant header if needed. Tenant header is enabled only if not empty
# endpoint: "/api/prom/push" # The endpoint URL path, default is "/api/prom/push" more info : https://grafana.com/docs/loki/latest/api/#post-apiprompush
# endpoint: "/loki/api/v1/push" # The endpoint URL path, default is "/loki/api/v1/push" more info : https://grafana.com/docs/loki/latest/api/#post-apiprompush
# extralabels: "" # comma separated list of fields to use as labels additionally to rule, source, priority, tags and custom_fields

nats:
Expand Down
37 changes: 17 additions & 20 deletions outputs/loki.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package outputs

import (
"fmt"
"log"
"strings"
"time"

"github.com/falcosecurity/falcosidekick/types"
)
Expand All @@ -13,34 +13,32 @@ type lokiPayload struct {
}

type lokiStream struct {
Labels string `json:"labels"`
Entries []lokiEntry `json:"entries"`
Stream map[string]string `json:"stream"`
Values []lokiValue `json:"values"`
}

type lokiEntry struct {
Ts string `json:"ts"`
Line string `json:"line"`
}
type lokiValue = []string

// The Content-Type to send along with the request
const LokiContentType = "application/json"

func newLokiPayload(falcopayload types.FalcoPayload, config *types.Configuration) lokiPayload {
le := lokiEntry{Ts: falcopayload.Time.Format(time.RFC3339), Line: falcopayload.Output}
ls := lokiStream{Entries: []lokiEntry{le}}
s := make(map[string]string, 3+len(falcopayload.OutputFields)+len(config.Loki.ExtraLabelsList)+len(falcopayload.Tags))
s["rule"] = falcopayload.Rule
s["source"] = falcopayload.Source
s["priority"] = falcopayload.Priority.String()

var s string
for i, j := range falcopayload.OutputFields {
switch v := j.(type) {
case string:
for k := range config.Customfields {
if i == k {
s += strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(i, ".", ""), "]", ""), "[", "") + "=\"" + strings.ReplaceAll(v, "\"", "") + "\","
s[strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(i, ".", ""), "]", ""), "[", "")] = strings.ReplaceAll(v, "\"", "")
}
}
for _, k := range config.Loki.ExtraLabelsList {
if i == k {
s += strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(i, ".", ""), "]", ""), "[", "") + "=\"" + strings.ReplaceAll(v, "\"", "") + "\","
s[strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(i, ".", ""), "]", ""), "[", "")] = strings.ReplaceAll(v, "\"", "")
}
}
default:
Expand All @@ -49,16 +47,15 @@ func newLokiPayload(falcopayload types.FalcoPayload, config *types.Configuration
}

if len(falcopayload.Tags) != 0 {
s += "tags=\"" + strings.Join(falcopayload.Tags, ",") + "\","
s["tags"] = strings.Join(falcopayload.Tags, ",")
}

s += "rule=\"" + falcopayload.Rule + "\","
s += "source=\"" + falcopayload.Source + "\","
s += "priority=\"" + falcopayload.Priority.String() + "\""

ls.Labels = "{" + s + "}"

return lokiPayload{Streams: []lokiStream{ls}}
return lokiPayload{Streams: []lokiStream{
{
Stream: s,
Values: []lokiValue{[]string{fmt.Sprintf("%v", falcopayload.Time.UnixNano()), falcopayload.Output}},
},
}}
}

// LokiPost posts event to Loki
Expand Down
12 changes: 6 additions & 6 deletions outputs/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ func TestNewLokiPayload(t *testing.T) {
expectedOutput := lokiPayload{
Streams: []lokiStream{
{
Labels: "{tags=\"test,example\",rule=\"Test rule\",source=\"syscalls\",priority=\"Debug\"}",
Entries: []lokiEntry{
{
Ts: "2001-01-01T01:10:00Z",
Line: "This is a test from falcosidekick",
},
Stream: map[string]string{
"tags": "test,example",
"rule": "Test rule",
"source": "syscalls",
"priority": "Debug",
},
Values: []lokiValue{[]string{"978311400000000000", "This is a test from falcosidekick"}},
},
},
}
Expand Down