Skip to content
This repository was archived by the owner on Aug 17, 2025. It is now read-only.

Commit c48167d

Browse files
authored
Change prometheus.New() to accept options rather than fixed arguments (#31)
1 parent d716c2c commit c48167d

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Changes by Version
44
1.2.1 (unreleased)
55
------------------
66

7-
- Nothing yet.
7+
- *breaking* Change prometheus.New() to accept options instead of fixed arguments
88

99

1010
1.2.0 (2017-11-12)

metrics/prometheus/factory.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,41 @@ type Factory struct {
3333
normalizer *strings.Replacer
3434
}
3535

36+
type options struct {
37+
registerer prometheus.Registerer
38+
buckets []float64
39+
}
40+
41+
// Option is a function that sets some option for the Factory constructor.
42+
type Option func(*options)
43+
44+
// WithRegisterer returns an option that sets the registerer.
45+
// If not used we fallback to prometheus.DefaultRegisterer.
46+
func WithRegisterer(registerer prometheus.Registerer) Option {
47+
return func(opts *options) {
48+
opts.registerer = registerer
49+
}
50+
}
51+
52+
// WithBuckets returns an option that sets the default buckets for histogram.
53+
// If not used, we fallback to default Prometheus buckets.
54+
func WithBuckets(buckets []float64) Option {
55+
return func(opts *options) {
56+
opts.buckets = buckets
57+
}
58+
}
59+
60+
func applyOptions(opts []Option) *options {
61+
options := new(options)
62+
for _, o := range opts {
63+
o(options)
64+
}
65+
if options.registerer == nil {
66+
options.registerer = prometheus.DefaultRegisterer
67+
}
68+
return options
69+
}
70+
3671
// New creates a Factory backed by Prometheus registry.
3772
// Typically the first argument should be prometheus.DefaultRegisterer.
3873
//
@@ -41,15 +76,16 @@ type Factory struct {
4176
// values must be sorted in strictly increasing order. There is no need
4277
// to add a highest bucket with +Inf bound, it will be added
4378
// implicitly. The default value is prometheus.DefBuckets.
44-
func New(registerer prometheus.Registerer, buckets []float64) *Factory {
79+
func New(opts ...Option) *Factory {
80+
options := applyOptions(opts)
4581
return newFactory(
4682
&Factory{ // dummy struct to be discarded
47-
cache: newVectorCache(registerer),
48-
buckets: buckets,
83+
cache: newVectorCache(options.registerer),
84+
buckets: options.buckets,
4985
normalizer: strings.NewReplacer(".", "_", "-", "_"),
5086
},
51-
"",
52-
nil)
87+
"", // scope
88+
nil) // tags
5389
}
5490

5591
func newFactory(parent *Factory, scope string, tags map[string]string) *Factory {

metrics/prometheus/factory_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package prometheus
15+
package prometheus_test
1616

1717
import (
1818
"testing"
@@ -24,17 +24,22 @@ import (
2424
"github.com/stretchr/testify/require"
2525

2626
"github.com/uber/jaeger-lib/metrics"
27+
. "github.com/uber/jaeger-lib/metrics/prometheus"
2728
)
2829

2930
var _ metrics.Factory = new(Factory)
3031

32+
func TestOptions(t *testing.T) {
33+
f1 := New()
34+
assert.NotNil(t, f1)
35+
}
36+
3137
func TestCounter(t *testing.T) {
3238
registry := prometheus.NewPedanticRegistry()
33-
f1 := New(registry, nil)
39+
f1 := New(WithRegisterer(registry))
3440
fDummy := f1.Namespace("", nil)
3541
f2 := fDummy.Namespace("bender", map[string]string{"a": "b"})
3642
f3 := f2.Namespace("", nil)
37-
assert.Equal(t, "bender", f3.(*Factory).scope)
3843

3944
c1 := f2.Counter("rodriguez", map[string]string{"x": "y"})
4045
c2 := f2.Counter("rodriguez", map[string]string{"x": "z"})
@@ -56,7 +61,7 @@ func TestCounter(t *testing.T) {
5661

5762
func TestGauge(t *testing.T) {
5863
registry := prometheus.NewPedanticRegistry()
59-
f1 := New(registry, nil)
64+
f1 := New(WithRegisterer(registry))
6065
f2 := f1.Namespace("bender", map[string]string{"a": "b"})
6166
f3 := f2.Namespace("", map[string]string{"a": "b"}) // essentially same as f2
6267
g1 := f2.Gauge("rodriguez", map[string]string{"x": "y"})
@@ -79,7 +84,7 @@ func TestGauge(t *testing.T) {
7984

8085
func TestTimer(t *testing.T) {
8186
registry := prometheus.NewPedanticRegistry()
82-
f1 := New(registry, nil)
87+
f1 := New(WithRegisterer(registry))
8388
f2 := f1.Namespace("bender", map[string]string{"a": "b"})
8489
f3 := f2.Namespace("", map[string]string{"a": "b"}) // essentially same as f2
8590
t1 := f2.Timer("rodriguez", map[string]string{"x": "y"})
@@ -122,7 +127,7 @@ func TestTimer(t *testing.T) {
122127

123128
func TestTimerCustomBuckets(t *testing.T) {
124129
registry := prometheus.NewPedanticRegistry()
125-
f1 := New(registry, []float64{1.5})
130+
f1 := New(WithRegisterer(registry), WithBuckets([]float64{1.5}))
126131
// dot and dash in the metric name will be replaced with underscore
127132
t1 := f1.Timer("bender.bending-rodriguez", map[string]string{"x": "y"})
128133
t1.Record(1 * time.Second)

0 commit comments

Comments
 (0)