Skip to content

Commit 42c8e32

Browse files
Merge pull request #26 from platinummonkey/fix-metrics
fixes metric registry issues with nil point on registered metrics
2 parents e651817 + 86cbdce commit 42c8e32

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

metric_registry/datadog/registry.go

+36-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package datadog
33

44
import (
5+
"fmt"
56
"strings"
67
"sync"
78
"time"
@@ -78,10 +79,41 @@ func NewMetricRegistry(addr string, prefix string, pollFrequency time.Duration)
7879
return nil, err
7980
}
8081
return &MetricRegistry{
81-
client: client,
82-
prefix: prefix,
83-
pollFrequency: pollFrequency,
84-
stopper: make(chan bool, 1),
82+
client: client,
83+
prefix: prefix,
84+
pollFrequency: pollFrequency,
85+
stopper: make(chan bool, 1),
86+
registeredGauges: make(map[string]*metricPoller, 0),
87+
registeredListeners: make(map[string]*metricSampleListener, 0),
88+
}, nil
89+
}
90+
91+
// NewMetricRegistryWithClient will create a new Datadog MetricRegistry with the provided client instead.
92+
// This registry reports metrics to datadog using the datadog dogstatsd forwarding.
93+
func NewMetricRegistryWithClient(
94+
client *dogstatsd.Client,
95+
prefix string,
96+
pollFrequency time.Duration,
97+
) (*MetricRegistry, error) {
98+
if client == nil {
99+
return nil, fmt.Errorf("client is nil")
100+
}
101+
102+
if !strings.HasSuffix(prefix, ".") {
103+
prefix = prefix + "."
104+
}
105+
106+
if pollFrequency == 0 {
107+
pollFrequency = defaultPollFrequency
108+
}
109+
110+
return &MetricRegistry{
111+
client: client,
112+
prefix: prefix,
113+
pollFrequency: pollFrequency,
114+
stopper: make(chan bool, 1),
115+
registeredGauges: make(map[string]*metricPoller, 0),
116+
registeredListeners: make(map[string]*metricSampleListener, 0),
85117
}, nil
86118
}
87119

metric_registry/gometrics/registry.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ func NewGoMetricsMetricRegistry(
8787
}
8888

8989
return &MetricRegistry{
90-
registry: registry,
91-
prefix: prefix,
92-
pollFrequency: pollFrequency,
93-
stopper: make(chan bool, 1),
90+
registry: registry,
91+
prefix: prefix,
92+
pollFrequency: pollFrequency,
93+
stopper: make(chan bool, 1),
94+
registeredGauges: make(map[string]*gometricsMetricPoller, 0),
95+
registeredListeners: make(map[string]*metricSampleListener, 0),
9496
}, nil
9597
}
9698

0 commit comments

Comments
 (0)