Skip to content

Commit 6054cdd

Browse files
committed
Add disabled and prometheus metric providers
FAB-12694 #done Change-Id: I60a243b6f5376f8d24f6856f01dd40e3e053404c Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 8222133 commit 6054cdd

File tree

8 files changed

+635
-1
lines changed

8 files changed

+635
-1
lines changed

Gopkg.lock

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package disabled_test
8+
9+
import (
10+
"testing"
11+
12+
. "github.com/onsi/ginkgo"
13+
. "github.com/onsi/gomega"
14+
)
15+
16+
func TestDisabled(t *testing.T) {
17+
RegisterFailHandler(Fail)
18+
RunSpecs(t, "Disabled Suite")
19+
}

common/metrics/disabled/provider.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package disabled
8+
9+
import (
10+
"github.com/hyperledger/fabric/common/metrics"
11+
)
12+
13+
type Provider struct{}
14+
15+
func (p *Provider) NewCounter(o metrics.CounterOpts) metrics.Counter { return &Counter{} }
16+
func (p *Provider) NewGauge(o metrics.GaugeOpts) metrics.Gauge { return &Gauge{} }
17+
func (p *Provider) NewHistogram(o metrics.HistogramOpts) metrics.Histogram { return &Histogram{} }
18+
19+
type Counter struct{}
20+
21+
func (c *Counter) Add(delta float64) {}
22+
func (c *Counter) With(labelValues ...string) metrics.Counter {
23+
return c
24+
}
25+
26+
type Gauge struct{}
27+
28+
func (g *Gauge) Add(delta float64) {}
29+
func (g *Gauge) Set(delta float64) {}
30+
func (g *Gauge) With(labelValues ...string) metrics.Gauge {
31+
return g
32+
}
33+
34+
type Histogram struct{}
35+
36+
func (h *Histogram) Observe(value float64) {}
37+
func (h *Histogram) With(labelValues ...string) metrics.Histogram {
38+
return h
39+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package disabled_test
8+
9+
import (
10+
"github.com/hyperledger/fabric/common/metrics"
11+
"github.com/hyperledger/fabric/common/metrics/disabled"
12+
. "github.com/onsi/ginkgo"
13+
. "github.com/onsi/gomega"
14+
)
15+
16+
var _ = Describe("Provider", func() {
17+
var p metrics.Provider
18+
19+
BeforeEach(func() {
20+
p = &disabled.Provider{}
21+
})
22+
23+
Describe("NewCounter", func() {
24+
It("creates a no-op counter that doesn't blow up", func() {
25+
c := p.NewCounter(metrics.CounterOpts{})
26+
Expect(c).NotTo(BeNil())
27+
28+
c.Add(1)
29+
c.With("whatever").Add(2)
30+
})
31+
})
32+
33+
Describe("NewGauge", func() {
34+
It("creates a no-op gauge that doesn't blow up", func() {
35+
g := p.NewGauge(metrics.GaugeOpts{})
36+
Expect(g).NotTo(BeNil())
37+
38+
g.Set(1)
39+
g.Add(1)
40+
g.With("whatever").Set(2)
41+
g.With("whatever").Add(2)
42+
})
43+
})
44+
45+
Describe("NewHistogram", func() {
46+
It("creates a no-op histogram that doesn't blow up", func() {
47+
h := p.NewHistogram(metrics.HistogramOpts{})
48+
Expect(h).NotTo(BeNil())
49+
50+
h.Observe(1)
51+
h.With("whatever").Observe(2)
52+
})
53+
})
54+
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package prometheus_test
8+
9+
import (
10+
"testing"
11+
12+
. "github.com/onsi/ginkgo"
13+
. "github.com/onsi/gomega"
14+
)
15+
16+
func TestPrometheus(t *testing.T) {
17+
RegisterFailHandler(Fail)
18+
RunSpecs(t, "Prometheus Suite")
19+
}

common/metrics/prometheus/provider.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package prometheus
8+
9+
import (
10+
kitmetrics "github.com/go-kit/kit/metrics"
11+
"github.com/go-kit/kit/metrics/prometheus"
12+
"github.com/hyperledger/fabric/common/metrics"
13+
prom "github.com/prometheus/client_golang/prometheus"
14+
)
15+
16+
type Provider struct{}
17+
18+
func (p *Provider) NewCounter(o metrics.CounterOpts) metrics.Counter {
19+
return &Counter{
20+
Counter: prometheus.NewCounterFrom(
21+
prom.CounterOpts{
22+
Namespace: o.Namespace,
23+
Subsystem: o.Subsystem,
24+
Name: o.Name,
25+
Help: o.Help,
26+
},
27+
o.LabelNames,
28+
),
29+
}
30+
}
31+
32+
func (p *Provider) NewGauge(o metrics.GaugeOpts) metrics.Gauge {
33+
return &Gauge{
34+
Gauge: prometheus.NewGaugeFrom(
35+
prom.GaugeOpts{
36+
Namespace: o.Namespace,
37+
Subsystem: o.Subsystem,
38+
Name: o.Name,
39+
Help: o.Help,
40+
},
41+
o.LabelNames,
42+
),
43+
}
44+
}
45+
46+
func (p *Provider) NewHistogram(o metrics.HistogramOpts) metrics.Histogram {
47+
return &Histogram{
48+
Histogram: prometheus.NewHistogramFrom(
49+
prom.HistogramOpts{
50+
Namespace: o.Namespace,
51+
Subsystem: o.Subsystem,
52+
Name: o.Name,
53+
Help: o.Help,
54+
Buckets: o.Buckets,
55+
},
56+
o.LabelNames,
57+
),
58+
}
59+
}
60+
61+
type Counter struct{ kitmetrics.Counter }
62+
63+
func (c *Counter) With(labelValues ...string) metrics.Counter {
64+
return &Counter{Counter: c.Counter.With(labelValues...)}
65+
}
66+
67+
type Gauge struct{ kitmetrics.Gauge }
68+
69+
func (g *Gauge) With(labelValues ...string) metrics.Gauge {
70+
return &Gauge{Gauge: g.Gauge.With(labelValues...)}
71+
}
72+
73+
type Histogram struct{ kitmetrics.Histogram }
74+
75+
func (h *Histogram) With(labelValues ...string) metrics.Histogram {
76+
return &Histogram{Histogram: h.Histogram.With(labelValues...)}
77+
}

0 commit comments

Comments
 (0)