Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

add support to change the prefix format #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 14 additions & 14 deletions examples/client-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"fmt"
"github.com/afiskon/promtail-client/promtail"
"log"
"os"
"time"
"github.com/afiskon/promtail-client/promtail"
)

func displayUsage() {
Expand All @@ -20,13 +20,13 @@ func displayInvalidName(arg string) {

func nameIsValid(name string) bool {
for _, c := range name {
if !((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
(c == '-') || (c == '_')) {
return false
}
}
if !((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
(c == '-') || (c == '_')) {
return false
}
}
return true
}

Expand All @@ -50,19 +50,19 @@ func main() {
displayInvalidName("job-name")
}

labels := "{source=\""+source_name+"\",job=\""+job_name+"\"}"
labels := "{source=\"" + source_name + "\",job=\"" + job_name + "\"}"
conf := promtail.ClientConfig{
PushURL: "http://localhost:3100/api/prom/push",
Labels: labels,
BatchWait: 5 * time.Second,
BatchEntriesNumber: 10000,
SendLevel: promtail.INFO,
PrintLevel: promtail.ERROR,
SendLevel: promtail.INFO,
PrintLevel: promtail.ERROR,
}

var (
loki promtail.Client
err error
err error
)

if format == "proto" {
Expand All @@ -78,8 +78,8 @@ func main() {

for i := 1; i < 5; i++ {
tstamp := time.Now().String()
loki.Debugf("source = %s time = %s, i = %d\n", source_name, tstamp, i)
loki.Infof("source = %s, time = %s, i = %d\n", source_name, tstamp, i)
loki.Debugf("source=%s time = %s, i = %d\n", source_name, tstamp, i)
loki.Infof("source=%s, time = %s, i = %d\n", source_name, tstamp, i)
loki.Warnf("source = %s, time = %s, i = %d\n", source_name, tstamp, i)
loki.Errorf("source = %s, time = %s, i = %d\n", source_name, tstamp, i)
time.Sleep(1 * time.Second)
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/afiskon/promtail-client

go 1.17

require (
github.com/golang/protobuf v1.5.2
github.com/golang/snappy v0.0.4
)

require google.golang.org/protobuf v1.26.0 // indirect
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
4 changes: 3 additions & 1 deletion promtail/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type ClientConfig struct {
SendLevel LogLevel
// Logs are printed to stdout if the entry level is >= PrintLevel
PrintLevel LogLevel
// fmt string to express the prefix
PrefixFormat string
}

type Client interface {
Expand Down Expand Up @@ -67,4 +69,4 @@ func (client *httpClient) sendJsonReq(method, url string, ctype string, reqBody
}

return resp, resBody, nil
}
}
39 changes: 25 additions & 14 deletions promtail/jsonclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type jsonLogEntry struct {
Ts time.Time `json:"ts"`
Line string `json:"line"`
level LogLevel // not used in JSON
level LogLevel // not used in JSON
}

type promtailStream struct {
Expand All @@ -24,19 +24,26 @@ type promtailMsg struct {
}

type clientJson struct {
config *ClientConfig
quit chan struct{}
entries chan *jsonLogEntry
waitGroup sync.WaitGroup
client httpClient
config *ClientConfig
quit chan struct{}
entries chan *jsonLogEntry
waitGroup sync.WaitGroup
client httpClient
prefixFormat string
}

func NewClientJson(conf ClientConfig) (Client, error) {
prefixFormat := "%s: "
if conf.PrefixFormat != "" {
prefixFormat = conf.PrefixFormat
}

client := clientJson{
config: &conf,
quit: make(chan struct{}),
entries: make(chan *jsonLogEntry, LOG_ENTRIES_CHAN_SIZE),
client: httpClient{},
config: &conf,
quit: make(chan struct{}),
entries: make(chan *jsonLogEntry, LOG_ENTRIES_CHAN_SIZE),
client: httpClient{},
prefixFormat: prefixFormat,
}

client.waitGroup.Add(1)
Expand All @@ -46,19 +53,23 @@ func NewClientJson(conf ClientConfig) (Client, error) {
}

func (c *clientJson) Debugf(format string, args ...interface{}) {
c.log(format, DEBUG, "Debug: ", args...)
prefix := fmt.Sprintf(c.prefixFormat, "Debug")
c.log(format, DEBUG, prefix, args...)
}

func (c *clientJson) Infof(format string, args ...interface{}) {
c.log(format, INFO, "Info: ", args...)
prefix := fmt.Sprintf(c.prefixFormat, "Info")
c.log(format, INFO, prefix, args...)
}

func (c *clientJson) Warnf(format string, args ...interface{}) {
c.log(format, WARN, "Warn: ", args...)
prefix := fmt.Sprintf(c.prefixFormat, "Warn")
c.log(format, WARN, prefix, args...)
}

func (c *clientJson) Errorf(format string, args ...interface{}) {
c.log(format, ERROR, "Error: ", args...)
prefix := fmt.Sprintf(c.prefixFormat, "Error")
c.log(format, ERROR, prefix, args...)
}

func (c *clientJson) log(format string, level LogLevel, prefix string, args ...interface{}) {
Expand Down
39 changes: 25 additions & 14 deletions promtail/protoclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package promtail

import (
"fmt"
"github.com/afiskon/promtail-client/logproto"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/golang/snappy"
"github.com/afiskon/promtail-client/logproto"
"log"
"sync"
"time"
Expand All @@ -17,19 +17,26 @@ type protoLogEntry struct {
}

type clientProto struct {
config *ClientConfig
quit chan struct{}
entries chan protoLogEntry
waitGroup sync.WaitGroup
client httpClient
config *ClientConfig
quit chan struct{}
entries chan protoLogEntry
waitGroup sync.WaitGroup
client httpClient
prefixFormat string
}

func NewClientProto(conf ClientConfig) (Client, error) {
prefixFormat := "%s: "
if conf.PrefixFormat != "" {
prefixFormat = conf.PrefixFormat
}

client := clientProto{
config: &conf,
quit: make(chan struct{}),
entries: make(chan protoLogEntry, LOG_ENTRIES_CHAN_SIZE),
client: httpClient{},
config: &conf,
quit: make(chan struct{}),
entries: make(chan protoLogEntry, LOG_ENTRIES_CHAN_SIZE),
client: httpClient{},
prefixFormat: prefixFormat,
}

client.waitGroup.Add(1)
Expand All @@ -39,19 +46,23 @@ func NewClientProto(conf ClientConfig) (Client, error) {
}

func (c *clientProto) Debugf(format string, args ...interface{}) {
c.log(format, DEBUG, "Debug: ", args...)
prefix := fmt.Sprintf(c.prefixFormat, "Debug")
c.log(format, DEBUG, prefix, args...)
}

func (c *clientProto) Infof(format string, args ...interface{}) {
c.log(format, INFO, "Info: ", args...)
prefix := fmt.Sprintf(c.prefixFormat, "Info")
c.log(format, INFO, prefix, args...)
}

func (c *clientProto) Warnf(format string, args ...interface{}) {
c.log(format, WARN, "Warn: ", args...)
prefix := fmt.Sprintf(c.prefixFormat, "Warn")
c.log(format, WARN, prefix, args...)
}

func (c *clientProto) Errorf(format string, args ...interface{}) {
c.log(format, ERROR, "Error: ", args...)
prefix := fmt.Sprintf(c.prefixFormat, "Error")
c.log(format, ERROR, prefix, args...)
}

func (c *clientProto) log(format string, level LogLevel, prefix string, args ...interface{}) {
Expand Down