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

Add JSON mode to Loki client #36

Merged
merged 1 commit into from
Feb 5, 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: 2 additions & 0 deletions dnsutils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ type Config struct {
Enable bool `yaml:"enable"`
ServerURL string `yaml:"server-url"`
JobName string `yaml:"job-name"`
Mode string `yaml:"mode"`
FlushInterval int `yaml:"flush-interval"`
BatchSize int `yaml:"batch-size"`
RetryInterval int `yaml:"retry-interval"`
Expand Down Expand Up @@ -378,6 +379,7 @@ func (c *Config) SetDefault() {
c.Loggers.LokiClient.Enable = false
c.Loggers.LokiClient.ServerURL = "http://localhost:3100/loki/api/v1/push"
c.Loggers.LokiClient.JobName = "dnscollector"
c.Loggers.LokiClient.Mode = "text"
c.Loggers.LokiClient.FlushInterval = 5
c.Loggers.LokiClient.BatchSize = 1024 * 1024
c.Loggers.LokiClient.RetryInterval = 10
Expand Down
13 changes: 12 additions & 1 deletion loggers/lokiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -110,6 +111,7 @@ func (o *LokiClient) Stop() {

func (o *LokiClient) Run() {
o.LogInfo("running in background...")
buffer := new(bytes.Buffer)

// prepare stream with label name

Expand All @@ -133,7 +135,16 @@ LOOP:
// prepare entry
entry := logproto.Entry{}
entry.Timestamp = time.Unix(int64(dm.DnsTap.TimeSec), int64(dm.DnsTap.TimeNsec))
entry.Line = dm.String(o.textFormat)

switch o.config.Loggers.LokiClient.Mode {
case "text":
delimiter := ""
entry.Line = string(dm.Bytes(o.textFormat, delimiter))
case "json":
json.NewEncoder(buffer).Encode(dm)
entry.Line = buffer.String()
buffer.Reset()
}
o.sizeentries += len(entry.Line)

// append entry to the stream
Expand Down