Skip to content

Commit

Permalink
Merge pull request #8 from go-flexible/feature/custom-logger-support
Browse files Browse the repository at this point in the history
feat: allow passing in custom logger
  • Loading branch information
ladydascalie authored Apr 16, 2022
2 parents 6c83f1a + f8f7c91 commit e36c83e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
20 changes: 16 additions & 4 deletions flexmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package flexmetrics

import (
"context"
"log"
"net"
"net/http"
"net/http/pprof"
Expand Down Expand Up @@ -62,6 +61,18 @@ func WithServer(server *http.Server) Option {
}
}

// Logger defines any logger able to call Printf.
type Logger interface {
Printf(format string, v ...interface{})
}

// WithLogger allows you to set a logger for the server.
func WithLogger(l Logger) Option {
return func(s *Server) {
s.logger = l
}
}

// New creates a new default metrics server.
func New(options ...Option) *Server {
path := os.Getenv("METRICS_PROMETHEUS_PATH")
Expand Down Expand Up @@ -94,8 +105,9 @@ func New(options ...Option) *Server {

// Server represents a prometheus metrics server.
type Server struct {
Path string
logger Logger
Server *http.Server
Path string
}

// Run will start the metrics server.
Expand All @@ -114,12 +126,12 @@ func (s *Server) Run(_ context.Context) error {
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)

s.Server.Handler = mux
log.Printf("serving profiling and prometheus metrics over http on http://%s%s", s.Server.Addr, s.Path)
s.logger.Printf("serving profiling and prometheus metrics over http on http://%s%s", s.Server.Addr, s.Path)
return s.Server.Serve(lis)
}

// Halt will attempt to gracefully shut down the server.
func (s *Server) Halt(ctx context.Context) error {
log.Printf("stopping serving profiling and prometheus metrics over http on http://%s...", s.Server.Addr)
s.logger.Printf("stopping serving profiling and prometheus metrics over http on http://%s...", s.Server.Addr)
return s.Server.Shutdown(ctx)
}
31 changes: 31 additions & 0 deletions flexmetrics_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package flexmetrics_test

import (
"bytes"
"context"
"io"
"log"
"net/http"
"os"
"strings"
"testing"

"github.com/go-flexible/flexmetrics"
Expand Down Expand Up @@ -84,3 +88,30 @@ func TestOption_WithServer(t *testing.T) {
t.Error("WithServer option should set the provided http server")
}
}

func TestOption_WithLogger(t *testing.T) {
var buf bytes.Buffer

w := io.MultiWriter(&buf, os.Stderr) // so we get console output.
logger := log.New(w, "TEST_LOGGER: ", 0) // so we get consistent output.

metrics := flexmetrics.New(
flexmetrics.WithAddr("127.0.0.1:"),
flexmetrics.WithLogger(logger),
)

ctx, cancel := context.WithCancel(context.Background())
cancel()

go func() {
_ = metrics.Run(ctx)
}()
_ = metrics.Halt(ctx)

t.Log(buf.String())

// ugly? yes, but, it will do.
if !strings.Contains(buf.String(), "TEST_LOGGER: ") {
t.Fatal("expected log message to contain prefix")
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ require (
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.26.0-rc.1 // indirect
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
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=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
Expand Down

0 comments on commit e36c83e

Please sign in to comment.