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

Added Optional Logging support #34

Merged
merged 1 commit into from
Sep 26, 2024
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
4 changes: 3 additions & 1 deletion chanGroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package hotload
import (
"context"
"database/sql/driver"
"sync"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"sync"
)

type testConn struct {
Expand Down Expand Up @@ -50,6 +51,7 @@ var _ = Describe("Driver", func() {
sqlDriver: nil,
mu: sync.RWMutex{},
conns: conns,
log: DefaultLogger{},
}
})

Expand Down
41 changes: 28 additions & 13 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import (
"sort"
"sync"

"github.com/sirupsen/logrus"
"github.com/infobloxopen/hotload/logger"
)

// Strategy is the plugin interface for hotload.
Expand All @@ -78,14 +78,20 @@ var (
sqlDrivers = make(map[string]*driverInstance)
strategies = make(map[string]Strategy)

logger *logrus.Logger
log logger.Logger
)

type driverInstance struct {
driver driver.Driver
options map[string]string
}

type DefaultLogger struct{}

func (d DefaultLogger) Debug(args ...any) {}
func (d DefaultLogger) Info(args ...any) {}
func (d DefaultLogger) Error(args ...any) {}

type driverOption func(*driverInstance)

// WithDriverOptions allows you to specify query parameters to the underlying driver.
Expand Down Expand Up @@ -169,14 +175,8 @@ func Strategies() []string {
return list
}

// SetLogLevel specifies the logrus.Level for the hotload driver's logger
func SetLogLevel(level logrus.Level) {
logger.SetLevel(level)
}

func init() {
sql.Register("hotload", &hdriver{ctx: context.Background(), cgroup: make(map[string]*chanGroup)})
logger = logrus.New()
}

// hdriver is the hotload driver.
Expand All @@ -197,6 +197,7 @@ type chanGroup struct {
mu sync.RWMutex
forceKill bool
conns []*managedConn
log logger.Logger
}

// monitor the location for changes
Expand All @@ -205,15 +206,15 @@ func (cg *chanGroup) run() {
select {
case <-cg.parentCtx.Done():
cg.cancel()
logger.Debug("cancelling chanGroup context")
cg.log.Debug("cancelling chanGroup context")
return
case v := <-cg.values:
if v == cg.value {
// next update is the same, just ignore it
continue
}
cg.valueChanged(v)
logger.Debug("connection information changed")
cg.log.Debug("connection information changed")
}
}
}
Expand Down Expand Up @@ -292,11 +293,11 @@ func (cg *chanGroup) remove(conn *managedConn) {
func (cg *chanGroup) parseValues(vs url.Values) {
cg.mu.Lock()
defer cg.mu.Unlock()
logger.WithFields(logrus.Fields{"urlValues": vs}).Debug("parsing values")
cg.log.Debug("parsing values", vs)
if v, ok := vs[forceKill]; ok {
firstValue := v[0]
cg.forceKill = firstValue == "true"
logger.Debug("forceKill set to true")
cg.log.Debug("forceKill set to true")
}
}

Expand All @@ -315,7 +316,6 @@ func (h *hdriver) Open(name string) (driver.Conn, error) {
if !ok {
return nil, ErrUnsupportedStrategy
}

sqlDriver, ok := sqlDrivers[uri.Host]
if !ok {
return nil, ErrUnknownDriver
Expand All @@ -334,10 +334,25 @@ func (h *hdriver) Open(name string) (driver.Conn, error) {
cancel: cancel,
sqlDriver: sqlDriver,
conns: make([]*managedConn, 0),
log: GetLogger(),
}
cgroup.parseValues(queryParams)
h.cgroup[name] = cgroup
go cgroup.run()
}
return cgroup.Open()
}

func WithLogger(l logger.Logger) {
log = l
if log == nil {
log = DefaultLogger{}
}
}

func GetLogger() logger.Logger {
if log == nil {
return DefaultLogger{}
}
return log
}
12 changes: 6 additions & 6 deletions fsnotify/filewatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fsnotify

import (
"context"
"log"
"net/url"
"os"
"strings"
Expand All @@ -19,6 +18,7 @@ func init() {
}

var resyncPeriod = time.Second * 2
var log = hotload.GetLogger()

// NewStrategy implements a hotload strategy that monitors config changes
// in a file using fsnotify.
Expand Down Expand Up @@ -52,7 +52,7 @@ func readConfigFile(path string) (v []byte, err error) {
}

func resync(w watcher, pth string) (string, error) {
log.Printf("fsnotify: Path Name-Resync=%s", pth)
log.Debug("fsnotify: Path Name-Resync ", pth)
err := w.Remove(pth)
if err != nil && !errors.Is(err, rfsnotify.ErrNonExistentWatch) {
return "", err
Expand All @@ -69,7 +69,7 @@ func (s *Strategy) run() {
for {
select {
case e := <-s.watcher.GetEvents():
log.Printf("fsnotify: Path Name-Run=%s", e.Name)
log.Debug("fsnotify: Path Name-Run ", e.Name)
if e.Op != rfsnotify.Write && e.Op != rfsnotify.Remove {
continue
}
Expand All @@ -82,7 +82,7 @@ func (s *Strategy) run() {

s.setVal(e.Name, val)
case e := <-s.watcher.GetErrors():
log.Printf("got error: %s", e)
log.Debug("got error: ", e)
break
case <-time.After(resyncPeriod):
var fixedPaths []string
Expand All @@ -104,7 +104,7 @@ func (s *Strategy) setVal(pth string, val string) {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.paths[pth]; !ok {
log.Printf("fsnotify: Path not in map=%s", pth)
log.Debug("fsnotify: Path not in map ", pth)
return
}
s.paths[pth].value = val
Expand All @@ -129,7 +129,7 @@ func (s *Strategy) Watch(ctx context.Context, pth string, options url.Values) (v
}
notifier, found := s.paths[pth]
if !found {
log.Printf("fsnotify: Path Name-Init=%s", pth)
log.Debug("fsnotify: Path Name-Init ", pth)
if err := s.watcher.Add(pth); err != nil {
return "", nil, err
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/onsi/gomega v1.27.6
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.18.0
github.com/sirupsen/logrus v1.9.0
)

require (
Expand Down
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
Expand All @@ -63,12 +62,8 @@ github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGy
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand All @@ -93,7 +88,6 @@ golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down Expand Up @@ -125,6 +119,5 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 8 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package logger

// Logger defines the interface for logging.
type Logger interface {
Info(args ...interface{})
Error(args ...interface{})
Debug(args ...interface{})
}
Loading