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

Getting rid of sirupsen/logrus and log.Interface #359

Merged
merged 8 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* [CHANGE] gRPC client: use default connect timeout of 5s, and therefore enable default connect backoff max delay of 5s. #332
* [CHANGE] Remove `github.com/grafana/dskit/errors` in favour of Go's `errors` package. #357
* [CHANGE] Remove `grpcutil.IsGRPCContextCanceled()` in favour of `grpcutil.IsCanceled()`. #357
* [CHANGE] Remove `logrus` and `log.Interface`. #359
* [FEATURE] Cache: Add support for configuring a Redis cache backend. #268 #271 #276
* [FEATURE] Add support for waiting on the rate limiter using the new `WaitN` method. #279
* [FEATURE] Add `log.BufferedLogger` type. #338
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ require (
github.com/prometheus/common v0.44.0
github.com/prometheus/exporter-toolkit v0.10.1-0.20230714054209-2f4150c63f97
github.com/sercand/kuberesolver/v4 v4.0.0
github.com/sirupsen/logrus v1.8.1
github.com/soheilhy/cmux v0.1.5
github.com/stretchr/testify v1.8.1
github.com/uber/jaeger-client-go v2.28.0+incompatible
github.com/uber/jaeger-lib v2.2.0+incompatible
github.com/weaveworks/promrus v1.2.0
go.etcd.io/etcd/api/v3 v3.5.0
go.etcd.io/etcd/client/pkg/v3 v3.5.0
go.etcd.io/etcd/client/v3 v3.5.0
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -793,8 +793,6 @@ github.com/sercand/kuberesolver/v4 v4.0.0/go.mod h1:F4RGyuRmMAjeXHKL+w4P7AwUnPce
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down Expand Up @@ -823,8 +821,6 @@ github.com/uber/jaeger-client-go v2.28.0+incompatible h1:G4QSBfvPKvg5ZM2j9MrJFdf
github.com/uber/jaeger-client-go v2.28.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
github.com/uber/jaeger-lib v2.2.0+incompatible h1:MxZXOiR2JuoANZ3J6DE/U0kSFv/eJ/GfSYVCjK7dyaw=
github.com/uber/jaeger-lib v2.2.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/weaveworks/promrus v1.2.0 h1:jOLf6pe6/vss4qGHjXmGz4oDJQA+AOCqEL3FvvZGz7M=
github.com/weaveworks/promrus v1.2.0/go.mod h1:SaE82+OJ91yqjrE1rsvBWVzNZKcHYFtMUyS1+Ogs/KA=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
7 changes: 5 additions & 2 deletions httpgrpc/httpgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ package httpgrpc
import (
"fmt"

"github.com/go-kit/log/level"

spb "github.com/gogo/googleapis/google/rpc"
"github.com/gogo/protobuf/types"
"github.com/gogo/status"
log "github.com/sirupsen/logrus"

"github.com/grafana/dskit/log"
)

// Errorf returns a HTTP gRPC error than is correctly forwarded over
Expand Down Expand Up @@ -51,7 +54,7 @@ func HTTPResponseFromError(err error) (*HTTPResponse, bool) {

var resp HTTPResponse
if err := types.UnmarshalAny(status.Details[0], &resp); err != nil {
log.Errorf("Got error containing non-response: %v", err)
level.Error(log.Global()).Log("msg", "got error containing non-response", "err", err)
return nil, false
}

Expand Down
3 changes: 2 additions & 1 deletion httpgrpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/url"
"strings"

"github.com/go-kit/log/level"
otgrpc "github.com/opentracing-contrib/go-grpc"
"github.com/opentracing/opentracing-go"
"github.com/sercand/kuberesolver/v4"
Expand Down Expand Up @@ -189,7 +190,7 @@ func (c *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if tracer := opentracing.GlobalTracer(); tracer != nil {
if span := opentracing.SpanFromContext(r.Context()); span != nil {
if err := tracer.Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header)); err != nil {
log.Global().Warnf("Failed to inject tracing headers into request: %v", err)
level.Warn(log.Global()).Log("msg", "failed to inject tracing headers into request", "err", err)
}
}
}
Expand Down
56 changes: 0 additions & 56 deletions log/format.go

This file was deleted.

57 changes: 8 additions & 49 deletions log/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,18 @@

package log

var global = Noop()
import (
"github.com/go-kit/log"
)

var global = log.NewNopLogger()

// Global returns the global logger.
func Global() Interface {
func Global() log.Logger {
return global
}

// SetGlobal sets the global logger.
func SetGlobal(i Interface) {
global = i
}

// Debugf convenience function calls the global loggerr.
func Debugf(format string, args ...interface{}) {
global.Debugf(format, args...)
}

// Debugln convenience function calls the global logger.
func Debugln(args ...interface{}) {
global.Debugln(args...)
}

// Infof convenience function calls the global logger.
func Infof(format string, args ...interface{}) {
global.Infof(format, args...)
}

// Infoln convenience function calls the global logger.
func Infoln(args ...interface{}) {
global.Infoln(args...)
}

// Warnf convenience function calls the global logger.
func Warnf(format string, args ...interface{}) {
global.Warnf(format, args...)
}

// Warnln convenience function calls the global logger.
func Warnln(args ...interface{}) {
global.Warnln(args...)
}

// Errorf convenience function calls the global logger.
func Errorf(format string, args ...interface{}) {
global.Errorf(format, args...)
}

// Errorln convenience function calls the global logger.
func Errorln(args ...interface{}) {
global.Errorln(args...)
}

// WithField convenience function calls the global logger.
func WithField(key string, value interface{}) Interface {
return global.WithField(key, value)
func SetGlobal(logger log.Logger) {
global = logger
}
134 changes: 63 additions & 71 deletions log/gokit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,101 +6,93 @@ package log

import (
"fmt"
"io"
"os"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
)

// NewGoKitFormat creates a new Interface backed by a GoKit logger
// format can be "json" or defaults to logfmt
func NewGoKitFormat(l Level, f Format) Interface {
var logger log.Logger
if f.s == "json" {
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
} else {
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
}
return addStandardFields(logger, l)
}

// stand-alone for test purposes
func addStandardFields(logger log.Logger, l Level) Interface {
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.Caller(5))
logger = level.NewFilter(logger, l.Gokit)
return gokit{logger}
}

// NewGoKit creates a new Interface backed by a GoKit logger
func NewGoKit(l Level) Interface {
return NewGoKitFormat(l, Format{s: "logfmt"})
}

// GoKit wraps an existing gokit Logger.
func GoKit(logger log.Logger) Interface {
return gokit{logger}
}
const (
LogfmtFormat = "logfmt"
)

type gokit struct {
log.Logger
}
type Fields []interface{}

// Helper to defer sprintf until it is needed.
type sprintf struct {
duricanikolic marked this conversation as resolved.
Show resolved Hide resolved
format string
args []interface{}
func NewFields(fields ...interface{}) Fields {
return fields
}

func (s *sprintf) String() string {
return fmt.Sprintf(s.format, s.args...)
func (f Fields) empty() bool {
return len(f) == 0
}

// Helper to defer sprint until it is needed.
// Note we don't use Sprintln because the output is passed to go-kit as one value among many on a line
type sprint struct {
args []interface{}
// NewGoKit creates a new GoKit logger with the given level, format and writer,
// enriched with the standard "ts" and "caller" fields.
// If the given writer is nil, os.Stderr is used.
// If the given format is nil, logfmt is used.
duricanikolic marked this conversation as resolved.
Show resolved Hide resolved
func NewGoKit(lvl Level, format string, writer io.Writer) log.Logger {
duricanikolic marked this conversation as resolved.
Show resolved Hide resolved
logger := newGoKit(format, writer, nil)
logger = addStandardFields(logger)
return level.NewFilter(logger, lvl.Option)
}

func (s *sprint) String() string {
return fmt.Sprint(s.args...)
// NewGoKitLogFmt creates a new GoKit logger with the given level and writer,
// enriched with the standard "ts" and "caller" fields.
// The "logfmt" format is used.
// If the given writer is nil, os.Stderr is used.
func NewGoKitLogFmt(l Level, writer io.Writer) log.Logger {
return NewGoKit(l, "logfmt", writer)
duricanikolic marked this conversation as resolved.
Show resolved Hide resolved
}

func (g gokit) Debugf(format string, args ...interface{}) {
level.Debug(g.Logger).Log("msg", &sprintf{format: format, args: args})
}
func (g gokit) Debugln(args ...interface{}) {
level.Debug(g.Logger).Log("msg", &sprint{args: args})
// NewGoKitWithFields creates a new GoKit logger configured with the given level, format, writer and
// rate limit-related configuration, enriched with the given fields.
// If the given format is nil, logfmt is used.
duricanikolic marked this conversation as resolved.
Show resolved Hide resolved
// If the given writer is nil, os.Stderr is used.
// If the given rate limit configuration is nil, no rate limited logger is created.
// If the given fields are empty, no fields are added.
func NewGoKitWithFields(lvl Level, format string, writer io.Writer, rateLimitedCfg *RateLimitedLoggerCfg, fields Fields) log.Logger {
duricanikolic marked this conversation as resolved.
Show resolved Hide resolved
logger := newGoKit(format, writer, rateLimitedCfg)
if !fields.empty() {
logger = log.With(logger, fields...)
}
return level.NewFilter(logger, lvl.Option)
}

func (g gokit) Infof(format string, args ...interface{}) {
level.Info(g.Logger).Log("msg", &sprintf{format: format, args: args})
}
func (g gokit) Infoln(args ...interface{}) {
level.Info(g.Logger).Log("msg", &sprint{args: args})
func newGoKit(format string, writer io.Writer, rateLimitedCfg *RateLimitedLoggerCfg) log.Logger {
var logger log.Logger
if writer == nil {
writer = log.NewSyncWriter(os.Stderr)
}
if format == "json" {
duricanikolic marked this conversation as resolved.
Show resolved Hide resolved
logger = log.NewJSONLogger(writer)
} else {
logger = log.NewLogfmtLogger(writer)
}
if rateLimitedCfg != nil {
logger = NewRateLimitedLogger(logger, *rateLimitedCfg)
}
return logger
}

func (g gokit) Warnf(format string, args ...interface{}) {
level.Warn(g.Logger).Log("msg", &sprintf{format: format, args: args})
}
func (g gokit) Warnln(args ...interface{}) {
level.Warn(g.Logger).Log("msg", &sprint{args: args})
// stand-alone for test purposes
func addStandardFields(logger log.Logger) log.Logger {
fields := NewFields("ts", log.DefaultTimestampUTC, "caller", log.Caller(5))
return log.With(logger, fields...)
}

func (g gokit) Errorf(format string, args ...interface{}) {
level.Error(g.Logger).Log("msg", &sprintf{format: format, args: args})
}
func (g gokit) Errorln(args ...interface{}) {
level.Error(g.Logger).Log("msg", &sprint{args: args})
type Sprintf struct {
format string
args []interface{}
}

func (g gokit) WithField(key string, value interface{}) Interface {
return gokit{log.With(g.Logger, key, value)}
func LazySprintf(format string, args ...interface{}) *Sprintf {
return &Sprintf{
format: format,
args: args,
}
}

func (g gokit) WithFields(fields Fields) Interface {
logger := g.Logger
for k, v := range fields {
logger = log.With(logger, k, v)
}
return gokit{logger}
func (s *Sprintf) String() string {
return fmt.Sprintf(s.format, s.args...)
}
Loading