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

Commit f1fe255

Browse files
authored
Merge pull request #12 from rf/injectable-logging
Allow logger to be injected
2 parents 12f414d + ab572ab commit f1fe255

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

stackdriver.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ import (
3636
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
3737
)
3838

39+
// Logger is the log interface used in go-metrics-stackdriver
40+
type Logger interface {
41+
Printf(format string, v ...interface{})
42+
Println(v ...interface{})
43+
}
44+
3945
// Sink conforms to the metrics.MetricSink interface and is used to transmit
4046
// metrics information to stackdriver.
4147
//
@@ -59,6 +65,8 @@ type Sink struct {
5965

6066
mu sync.Mutex
6167
debugLogs bool
68+
69+
log Logger
6270
}
6371

6472
// Config options for the stackdriver Sink.
@@ -113,6 +121,9 @@ type Config struct {
113121
//
114122
// Setting a nil MonitoredResource will run a defaultMonitoredResource function.
115123
MonitoredResource *monitoredrespb.MonitoredResource
124+
125+
// Logger implements our Logger interface, providing Printf and Println functions
126+
Logger Logger
116127
}
117128

118129
type taskInfo struct {
@@ -185,13 +196,18 @@ func NewSink(client *monitoring.MetricClient, config *Config) *Sink {
185196
TaskID: config.TaskID,
186197
},
187198
debugLogs: config.DebugLogs,
199+
log: config.Logger,
200+
}
201+
202+
if s.log == nil {
203+
s.log = log.New(os.Stderr, "go-metrics-stackdriver: ", log.LstdFlags)
188204
}
189205

190206
if config.Prefix != nil {
191207
if isValidMetricsPrefix(*config.Prefix) {
192208
s.prefix = *config.Prefix
193209
} else {
194-
log.Printf("%s is not valid string to be used as metrics name, using default value 'go-metrics/'", *config.Prefix)
210+
s.log.Printf("%s is not valid string to be used as metrics name, using default value 'go-metrics/'", *config.Prefix)
195211
}
196212
}
197213
// apply defaults if not configured explicitly
@@ -207,15 +223,15 @@ func NewSink(client *monitoring.MetricClient, config *Config) *Sink {
207223
if s.taskInfo.ProjectID == "" {
208224
id, err := metadata.ProjectID()
209225
if err != nil {
210-
log.Printf("could not configure go-metrics stackdriver ProjectID: %s", err)
226+
s.log.Printf("could not configure go-metrics stackdriver ProjectID: %s", err)
211227
}
212228
s.taskInfo.ProjectID = id
213229
}
214230
if s.taskInfo.Location == "" {
215231
// attempt to detect
216232
zone, err := metadata.Zone()
217233
if err != nil {
218-
log.Printf("could not configure go-metric stackdriver location: %s", err)
234+
s.log.Printf("could not configure go-metric stackdriver location: %s", err)
219235
zone = "global"
220236
}
221237
s.taskInfo.Location = zone
@@ -265,7 +281,7 @@ func (s *Sink) flushMetrics(ctx context.Context) {
265281
for {
266282
select {
267283
case <-ctx.Done():
268-
log.Println("stopped flushing metrics")
284+
s.log.Println("stopped flushing metrics")
269285
return
270286
case <-ticker.C:
271287
s.report(ctx)
@@ -327,11 +343,11 @@ func (s *Sink) report(ctx context.Context) {
327343
for _, v := range rCounters {
328344
name, labels, err := v.name.labelMap(s.extractor, "counter")
329345
if err != nil {
330-
log.Printf("Could not extract labels from %s: %v", v.name.hash, err)
346+
s.log.Printf("Could not extract labels from %s: %v", v.name.hash, err)
331347
continue
332348
}
333349
if s.debugLogs {
334-
log.Printf("%v is now %s + (%v)\n", v.name.key, name, labels)
350+
s.log.Printf("%v is now %s + (%v)\n", v.name.key, name, labels)
335351
}
336352
ts = append(ts, &monitoringpb.TimeSeries{
337353
Metric: &metricpb.Metric{
@@ -360,11 +376,11 @@ func (s *Sink) report(ctx context.Context) {
360376
for _, v := range rGauges {
361377
name, labels, err := v.name.labelMap(s.extractor, "gauge")
362378
if err != nil {
363-
log.Printf("Could not extract labels from %s: %v", v.name.hash, err)
379+
s.log.Printf("Could not extract labels from %s: %v", v.name.hash, err)
364380
continue
365381
}
366382
if s.debugLogs {
367-
log.Printf("%v is now %s + (%v)\n", v.name.key, name, labels)
383+
s.log.Printf("%v is now %s + (%v)\n", v.name.key, name, labels)
368384
}
369385
ts = append(ts, &monitoringpb.TimeSeries{
370386
Metric: &metricpb.Metric{
@@ -393,11 +409,11 @@ func (s *Sink) report(ctx context.Context) {
393409
for _, v := range rHistograms {
394410
name, labels, err := v.name.labelMap(s.extractor, "histogram")
395411
if err != nil {
396-
log.Printf("Could not extract labels from %s: %v", v.name.hash, err)
412+
s.log.Printf("Could not extract labels from %s: %v", v.name.hash, err)
397413
continue
398414
}
399415
if s.debugLogs {
400-
log.Printf("%v is now %s + (%v)\n", v.name.key, name, labels)
416+
s.log.Printf("%v is now %s + (%v)\n", v.name.key, name, labels)
401417
}
402418

403419
var count int64
@@ -461,10 +477,10 @@ func (s *Sink) report(ctx context.Context) {
461477
err := s.client.CreateTimeSeries(ctx, req)
462478

463479
if err != nil {
464-
log.Printf("Failed to write time series data: %v", err)
480+
s.log.Printf("Failed to write time series data: %v", err)
465481
if s.debugLogs {
466482
for i, a := range req.TimeSeries {
467-
log.Printf("request timeseries[%d]: %v", i, a)
483+
s.log.Printf("request timeseries[%d]: %v", i, a)
468484
}
469485
}
470486
}

stackdriver_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import (
1717
"context"
1818
"errors"
1919
"fmt"
20+
"log"
2021
"net"
22+
"os"
2123
"sync"
2224
"testing"
2325
"time"
@@ -1041,6 +1043,7 @@ func newTestSink(interval time.Duration, client *monitoring.MetricClient) *Sink
10411043
s.interval = interval
10421044
s.bucketer = DefaultBucketer
10431045
s.extractor = DefaultLabelExtractor
1046+
s.log = log.New(os.Stderr, "go-metrics-stackdriver: ", log.LstdFlags)
10441047
s.reset()
10451048
go s.flushMetrics(context.Background())
10461049
return s

0 commit comments

Comments
 (0)