Skip to content

Commit

Permalink
Add disabled and prometheus metric providers
Browse files Browse the repository at this point in the history
FAB-12694 #done

Change-Id: I60a243b6f5376f8d24f6856f01dd40e3e053404c
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
sykesm committed Nov 5, 2018
1 parent 8222133 commit 6054cdd
Show file tree
Hide file tree
Showing 8 changed files with 635 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions common/metrics/disabled/disabled_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package disabled_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestDisabled(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Disabled Suite")
}
39 changes: 39 additions & 0 deletions common/metrics/disabled/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package disabled

import (
"github.com/hyperledger/fabric/common/metrics"
)

type Provider struct{}

func (p *Provider) NewCounter(o metrics.CounterOpts) metrics.Counter { return &Counter{} }
func (p *Provider) NewGauge(o metrics.GaugeOpts) metrics.Gauge { return &Gauge{} }
func (p *Provider) NewHistogram(o metrics.HistogramOpts) metrics.Histogram { return &Histogram{} }

type Counter struct{}

func (c *Counter) Add(delta float64) {}
func (c *Counter) With(labelValues ...string) metrics.Counter {
return c
}

type Gauge struct{}

func (g *Gauge) Add(delta float64) {}
func (g *Gauge) Set(delta float64) {}
func (g *Gauge) With(labelValues ...string) metrics.Gauge {
return g
}

type Histogram struct{}

func (h *Histogram) Observe(value float64) {}
func (h *Histogram) With(labelValues ...string) metrics.Histogram {
return h
}
54 changes: 54 additions & 0 deletions common/metrics/disabled/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package disabled_test

import (
"github.com/hyperledger/fabric/common/metrics"
"github.com/hyperledger/fabric/common/metrics/disabled"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Provider", func() {
var p metrics.Provider

BeforeEach(func() {
p = &disabled.Provider{}
})

Describe("NewCounter", func() {
It("creates a no-op counter that doesn't blow up", func() {
c := p.NewCounter(metrics.CounterOpts{})
Expect(c).NotTo(BeNil())

c.Add(1)
c.With("whatever").Add(2)
})
})

Describe("NewGauge", func() {
It("creates a no-op gauge that doesn't blow up", func() {
g := p.NewGauge(metrics.GaugeOpts{})
Expect(g).NotTo(BeNil())

g.Set(1)
g.Add(1)
g.With("whatever").Set(2)
g.With("whatever").Add(2)
})
})

Describe("NewHistogram", func() {
It("creates a no-op histogram that doesn't blow up", func() {
h := p.NewHistogram(metrics.HistogramOpts{})
Expect(h).NotTo(BeNil())

h.Observe(1)
h.With("whatever").Observe(2)
})
})
})
19 changes: 19 additions & 0 deletions common/metrics/prometheus/prometheus_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package prometheus_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestPrometheus(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Prometheus Suite")
}
77 changes: 77 additions & 0 deletions common/metrics/prometheus/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package prometheus

import (
kitmetrics "github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/prometheus"
"github.com/hyperledger/fabric/common/metrics"
prom "github.com/prometheus/client_golang/prometheus"
)

type Provider struct{}

func (p *Provider) NewCounter(o metrics.CounterOpts) metrics.Counter {
return &Counter{
Counter: prometheus.NewCounterFrom(
prom.CounterOpts{
Namespace: o.Namespace,
Subsystem: o.Subsystem,
Name: o.Name,
Help: o.Help,
},
o.LabelNames,
),
}
}

func (p *Provider) NewGauge(o metrics.GaugeOpts) metrics.Gauge {
return &Gauge{
Gauge: prometheus.NewGaugeFrom(
prom.GaugeOpts{
Namespace: o.Namespace,
Subsystem: o.Subsystem,
Name: o.Name,
Help: o.Help,
},
o.LabelNames,
),
}
}

func (p *Provider) NewHistogram(o metrics.HistogramOpts) metrics.Histogram {
return &Histogram{
Histogram: prometheus.NewHistogramFrom(
prom.HistogramOpts{
Namespace: o.Namespace,
Subsystem: o.Subsystem,
Name: o.Name,
Help: o.Help,
Buckets: o.Buckets,
},
o.LabelNames,
),
}
}

type Counter struct{ kitmetrics.Counter }

func (c *Counter) With(labelValues ...string) metrics.Counter {
return &Counter{Counter: c.Counter.With(labelValues...)}
}

type Gauge struct{ kitmetrics.Gauge }

func (g *Gauge) With(labelValues ...string) metrics.Gauge {
return &Gauge{Gauge: g.Gauge.With(labelValues...)}
}

type Histogram struct{ kitmetrics.Histogram }

func (h *Histogram) With(labelValues ...string) metrics.Histogram {
return &Histogram{Histogram: h.Histogram.With(labelValues...)}
}
Loading

0 comments on commit 6054cdd

Please sign in to comment.