diff --git a/examples/client-example/main.go b/examples/client-example/main.go index 5b38187..2c399ed 100644 --- a/examples/client-example/main.go +++ b/examples/client-example/main.go @@ -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() { @@ -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 } @@ -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" { @@ -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) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d9568f1 --- /dev/null +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d0ca355 --- /dev/null +++ b/go.sum @@ -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= diff --git a/promtail/common.go b/promtail/common.go index f44774d..21c4e54 100644 --- a/promtail/common.go +++ b/promtail/common.go @@ -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 { @@ -67,4 +69,4 @@ func (client *httpClient) sendJsonReq(method, url string, ctype string, reqBody } return resp, resBody, nil -} \ No newline at end of file +} diff --git a/promtail/jsonclient.go b/promtail/jsonclient.go index 6ee022e..fbef7ae 100644 --- a/promtail/jsonclient.go +++ b/promtail/jsonclient.go @@ -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 { @@ -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) @@ -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{}) { diff --git a/promtail/protoclient.go b/promtail/protoclient.go index 6764e0f..db0adbc 100644 --- a/promtail/protoclient.go +++ b/promtail/protoclient.go @@ -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" @@ -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) @@ -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{}) {