Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit 3a6bd6c

Browse files
authored
Merge pull request #153 from mailgun/thrawn/logging-cleanup
Now using the logger passed in on initialization
2 parents d0c6d6a + 0af0655 commit 3a6bd6c

File tree

15 files changed

+107
-40
lines changed

15 files changed

+107
-40
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: On Pull Request
2+
3+
on:
4+
pull_request:
5+
branches: [ master, main ]
6+
7+
jobs:
8+
test:
9+
name: test
10+
strategy:
11+
matrix:
12+
go-version:
13+
- 1.18.x
14+
- 1.19.x
15+
os: [ ubuntu-latest ]
16+
runs-on: ${{ matrix.os }}
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@master
20+
21+
- name: Set up Go
22+
uses: actions/setup-go@v2
23+
with:
24+
go-version: ${{ matrix.go-version }}
25+
26+
- run: go env
27+
28+
- name: Cache deps
29+
uses: actions/cache@v2
30+
with:
31+
path: ~/go/pkg/mod
32+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
33+
restore-keys: |
34+
${{ runner.os }}-go-
35+
36+
- name: Install deps
37+
run: go mod download
38+
39+
- name: Test
40+
run: go test -v -race -p=1 -count=1

config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ type Config struct {
9999
DataCenter string
100100

101101
// (Optional) A Logger which implements the declared logger interface (typically *logrus.Entry)
102-
Logger logrus.FieldLogger
102+
Logger FieldLogger
103103

104104
// (Optional) The TLS config used when connecting to gubernator peers
105105
PeerTLS *tls.Config
@@ -218,7 +218,7 @@ type DaemonConfig struct {
218218
Picker PeerPicker
219219

220220
// (Optional) A Logger which implements the declared logger interface (typically *logrus.Entry)
221-
Logger logrus.FieldLogger
221+
Logger FieldLogger
222222

223223
// (Optional) TLS Configuration; SpawnDaemon() will modify the passed TLS config in an
224224
// attempt to build a complete TLS config if one is not provided.

daemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Daemon struct {
4747
HTTPListener net.Listener
4848
V1Server *V1Instance
4949

50-
log logrus.FieldLogger
50+
log FieldLogger
5151
pool PoolInterface
5252
conf DaemonConfig
5353
httpSrv *http.Server

dns.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ type DNSPoolConfig struct {
124124
// (Required) Called when the list of gubernators in the pool updates
125125
OnUpdate UpdateFunc
126126

127-
Logger logrus.FieldLogger
127+
Logger FieldLogger
128128
}
129129

130130
type DNSPool struct {
131-
log logrus.FieldLogger
131+
log FieldLogger
132132
conf DNSPoolConfig
133133
ctx context.Context
134134
cancel context.CancelFunc

etcd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type EtcdPool struct {
4646
ctx context.Context
4747
cancelCtx context.CancelFunc
4848
watchChan etcd.WatchChan
49-
log logrus.FieldLogger
49+
log FieldLogger
5050
watcher etcd.Watcher
5151
conf EtcdPoolConfig
5252
}
@@ -68,7 +68,7 @@ type EtcdPoolConfig struct {
6868
EtcdConfig *etcd.Config
6969

7070
// (Optional) An interface through which logging will occur (Usually *logrus.Entry)
71-
Logger logrus.FieldLogger
71+
Logger FieldLogger
7272
}
7373

7474
func NewEtcdPool(conf EtcdPoolConfig) (*EtcdPool, error) {

flags.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ limitations under the License.
1616

1717
package gubernator
1818

19-
import "github.com/sirupsen/logrus"
20-
2119
const (
2220
FlagOSMetrics MetricFlags = 1 << iota
2321
FlagGolangMetrics
@@ -38,7 +36,7 @@ func (f *MetricFlags) Has(flag MetricFlags) bool {
3836
return *f&flag != 0
3937
}
4038

41-
func getEnvMetricFlags(log logrus.FieldLogger, name string) MetricFlags {
39+
func getEnvMetricFlags(log FieldLogger, name string) MetricFlags {
4240
flags := getEnvSlice(name)
4341
if len(flags) == 0 {
4442
return 0

global.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/mailgun/holster/v4/syncutil"
2626
"github.com/mailgun/holster/v4/tracing"
2727
"github.com/prometheus/client_golang/prometheus"
28-
"github.com/sirupsen/logrus"
2928
"google.golang.org/protobuf/proto"
3029
)
3130

@@ -36,7 +35,7 @@ type globalManager struct {
3635
broadcastQueue chan *RateLimitReq
3736
wg syncutil.WaitGroup
3837
conf BehaviorConfig
39-
log logrus.FieldLogger
38+
log FieldLogger
4039
instance *V1Instance
4140

4241
asyncMetrics prometheus.Summary

gubernator.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type V1Instance struct {
4949
global *globalManager
5050
mutliRegion *mutliRegionManager
5151
peerMutex sync.RWMutex
52-
log logrus.FieldLogger
52+
log FieldLogger
5353
conf Config
5454
isClosed bool
5555
getRateLimitsCounter int64
@@ -175,16 +175,14 @@ func (s *V1Instance) Close() (reterr error) {
175175

176176
err := s.gubernatorPool.Store(ctx)
177177
if err != nil {
178-
logrus.WithContext(ctx).
179-
WithError(err).
178+
s.log.WithError(err).
180179
Error("Error in checkHandlerPool.Store")
181180
return errors.Wrap(err, "Error in checkHandlerPool.Store")
182181
}
183182

184183
err = s.gubernatorPool.Close()
185184
if err != nil {
186-
logrus.WithContext(ctx).
187-
WithError(err).
185+
s.log.WithError(err).
188186
Error("Error in checkHandlerPool.Close")
189187
return errors.Wrap(err, "Error in checkHandlerPool.Close")
190188
}
@@ -356,7 +354,7 @@ func (s *V1Instance) asyncRequests(ctx context.Context, req *AsyncReq) {
356354

357355
for {
358356
if attempts > 5 {
359-
logrus.WithContext(ctx).
357+
s.log.WithContext(ctx).
360358
WithError(err).
361359
WithField("key", req.Key).
362360
Error("GetPeer() returned peer that is not connected")
@@ -366,13 +364,13 @@ func (s *V1Instance) asyncRequests(ctx context.Context, req *AsyncReq) {
366364
break
367365
}
368366

369-
// If we are attempting again, the owner of the this rate limit might have changed to us!
367+
// If we are attempting again, the owner of this rate limit might have changed to us!
370368
if attempts != 0 {
371369
if req.Peer.Info().IsOwner {
372370
getRateLimitCounter.WithLabelValues("local").Add(1)
373371
resp.Resp, err = s.getRateLimit(ctx, req.Req)
374372
if err != nil {
375-
logrus.WithContext(ctx).
373+
s.log.WithContext(ctx).
376374
WithError(err).
377375
WithField("key", req.Key).
378376
Error("Error applying rate limit")
@@ -393,10 +391,7 @@ func (s *V1Instance) asyncRequests(ctx context.Context, req *AsyncReq) {
393391
req.Peer, err = s.GetPeer(ctx, req.Key)
394392
if err != nil {
395393
errPart := fmt.Sprintf("Error finding peer that owns rate limit '%s'", req.Key)
396-
logrus.WithContext(ctx).
397-
WithError(err).
398-
WithField("key", req.Key).
399-
Error(errPart)
394+
s.log.WithContext(ctx).WithError(err).WithField("key", req.Key).Error(errPart)
400395
countError(err, "Error in GetPeer")
401396
err = errors.Wrap(err, errPart)
402397
resp.Resp = &RateLimitResp{Error: err.Error()}
@@ -405,10 +400,6 @@ func (s *V1Instance) asyncRequests(ctx context.Context, req *AsyncReq) {
405400
continue
406401
}
407402

408-
logrus.WithContext(ctx).
409-
WithError(err).
410-
WithField("key", req.Key).
411-
Error("Error fetching rate limit from peer")
412403
// Not calling `countError()` because we expect the remote end to
413404
// report this error.
414405
err = errors.Wrap(err, fmt.Sprintf("Error while fetching rate limit '%s' from peer", req.Key))
@@ -676,6 +667,7 @@ func (s *V1Instance) SetPeers(peerInfo []PeerInfo) {
676667
peer = NewPeerClient(PeerConfig{
677668
TLS: s.conf.PeerTLS,
678669
Behavior: s.conf.Behaviors,
670+
Log: s.log,
679671
Info: info,
680672
})
681673
}
@@ -688,6 +680,7 @@ func (s *V1Instance) SetPeers(peerInfo []PeerInfo) {
688680
peer = NewPeerClient(PeerConfig{
689681
TLS: s.conf.PeerTLS,
690682
Behavior: s.conf.Behaviors,
683+
Log: s.log,
691684
Info: info,
692685
})
693686
}

kubernetes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type K8sPool struct {
3737
informer cache.SharedIndexInformer
3838
client *kubernetes.Clientset
3939
wg syncutil.WaitGroup
40-
log logrus.FieldLogger
40+
log FieldLogger
4141
conf K8sPoolConfig
4242
watchCtx context.Context
4343
watchCancel func()
@@ -65,7 +65,7 @@ func WatchMechanismFromString(mechanism string) (WatchMechanism, error) {
6565
}
6666

6767
type K8sPoolConfig struct {
68-
Logger logrus.FieldLogger
68+
Logger FieldLogger
6969
Mechanism WatchMechanism
7070
OnUpdate UpdateFunc
7171
Namespace string

log.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package gubernator
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/sirupsen/logrus"
8+
)
9+
10+
// The FieldLogger interface generalizes the Entry and Logger types
11+
type FieldLogger interface {
12+
WithField(key string, value interface{}) *logrus.Entry
13+
WithFields(fields logrus.Fields) *logrus.Entry
14+
WithError(err error) *logrus.Entry
15+
WithContext(ctx context.Context) *logrus.Entry
16+
WithTime(t time.Time) *logrus.Entry
17+
18+
Tracef(format string, args ...interface{})
19+
Debugf(format string, args ...interface{})
20+
Infof(format string, args ...interface{})
21+
Printf(format string, args ...interface{})
22+
Warnf(format string, args ...interface{})
23+
Warningf(format string, args ...interface{})
24+
Errorf(format string, args ...interface{})
25+
Fatalf(format string, args ...interface{})
26+
27+
Log(level logrus.Level, args ...interface{})
28+
Debug(args ...interface{})
29+
Info(args ...interface{})
30+
Print(args ...interface{})
31+
Warn(args ...interface{})
32+
Warning(args ...interface{})
33+
Error(args ...interface{})
34+
}

0 commit comments

Comments
 (0)