Skip to content

Commit

Permalink
Merge pull request #80 from getamis/prometheus-registerer-interface
Browse files Browse the repository at this point in the history
metrics: implement prometheus Registerer interface
  • Loading branch information
markya0616 authored Jul 6, 2021
2 parents 8b38612 + 022ac51 commit 6660f4f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ deps:
docker pull amazon/dynamodb-local:latest
docker pull rabbitmq:3.6.2-management
docker pull redis:3-alpine
docker pull redis:6-alpine
docker pull vault:1.0.3

clean:
Expand Down
5 changes: 1 addition & 4 deletions kv/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ func makeRedisClient() store.Store {
}

func TestRedisStore(t *testing.T) {
container, err := test.NewRedisContainer()
if err != nil {
t.Fatal("failed to new REDIS container")
}
container := test.NewRedisContainer(test.LoadRedisOptions())
assert.NoError(t, container.Start())
defer container.Stop()
kv := makeRedisClient()
Expand Down
16 changes: 16 additions & 0 deletions metrics/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/http"
"time"

prom "github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
)

Expand All @@ -29,6 +30,21 @@ func NewDummyRegistry() *DummyRegistry {
return &DummyRegistry{}
}

// prom.Registerer interface
func (d *DummyRegistry) Register(prom.Collector) error {
return nil
}

// prom.Registerer interface
func (d *DummyRegistry) MustRegister(...prom.Collector) {

}

// prom.Registerer interface
func (d *DummyRegistry) Unregister(prom.Collector) bool {
return false
}

func (d *DummyRegistry) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("Not support metrics.\n"))
Expand Down
21 changes: 11 additions & 10 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (
)

type PrometheusRegistry struct {
*prom.Registry

namespace string
labels map[string]string
registry *prom.Registry
httpHandler http.Handler
}

Expand All @@ -40,7 +41,7 @@ func NewPrometheusRegistry() *PrometheusRegistry {
registry := prom.NewRegistry()
registry.MustRegister(prom.NewGoCollector())
return &PrometheusRegistry{
registry: registry,
Registry: registry,
labels: defaultLabels,
httpHandler: promhttp.HandlerFor(registry, promhttp.HandlerOpts{}),
}
Expand Down Expand Up @@ -70,7 +71,7 @@ func (p *PrometheusRegistry) NewHttpServerMetrics(opts ...Option) HttpServerMetr
}
httpMetrics := NewHttpMetrics(ToGRPCPromCounterOption(options))
httpMetrics.EnableHandlingTimeHistogram(ToGRPCPromHistogramOption(options))
err := p.registry.Register(httpMetrics)
err := p.Register(httpMetrics)
if err != nil {
reg, ok := err.(prom.AlreadyRegisteredError)
if ok {
Expand All @@ -88,7 +89,7 @@ func (p *PrometheusRegistry) NewServerMetrics(opts ...Option) ServerMetrics {
}
grpcMetrics := grpcProm.NewServerMetrics(ToGRPCPromCounterOption(options))
grpcMetrics.EnableHandlingTimeHistogram(ToGRPCPromHistogramOption(options))
err := p.registry.Register(grpcMetrics)
err := p.Register(grpcMetrics)
if err != nil {
reg, ok := err.(prom.AlreadyRegisteredError)
if ok {
Expand All @@ -111,7 +112,7 @@ func (p *PrometheusRegistry) NewCounter(key string, opts ...Option) Counter {
Help: key,
ConstLabels: prom.Labels(options.Labels),
})
err := p.registry.Register(cnt)
err := p.Register(cnt)
if err != nil {
reg, ok := err.(prom.AlreadyRegisteredError)
if ok {
Expand All @@ -134,7 +135,7 @@ func (p *PrometheusRegistry) NewGauge(key string, opts ...Option) Gauge {
Help: key,
ConstLabels: prom.Labels(options.Labels),
})
err := p.registry.Register(g)
err := p.Register(g)
if err != nil {
reg, ok := err.(prom.AlreadyRegisteredError)
if ok {
Expand All @@ -157,7 +158,7 @@ func (p *PrometheusRegistry) NewGaugeVec(key string, labelNames []string, opts .
Help: key,
ConstLabels: prom.Labels(options.Labels),
}, labelNames)
err := p.registry.Register(cnt)
err := p.Register(cnt)
if err != nil {
reg, ok := err.(prom.AlreadyRegisteredError)
if ok {
Expand All @@ -181,7 +182,7 @@ func (p *PrometheusRegistry) NewHistogram(key string, opts ...Option) Histogram
Help: key,
ConstLabels: prom.Labels(options.Labels),
})
err := p.registry.Register(h)
err := p.Register(h)
if err != nil {
reg, ok := err.(prom.AlreadyRegisteredError)
if ok {
Expand All @@ -204,7 +205,7 @@ func (p *PrometheusRegistry) NewHistogramVec(key string, labels []string, opts .
Help: key,
ConstLabels: prom.Labels(options.Labels),
}, labels)
err := p.registry.Register(hv)
err := p.Register(hv)
if err != nil {
reg, ok := err.(prom.AlreadyRegisteredError)
if ok {
Expand Down Expand Up @@ -246,7 +247,7 @@ func (p *PrometheusRegistry) NewCounterVec(key string, labelNames []string, opts
Help: key,
ConstLabels: prom.Labels(options.Labels),
}, labelNames)
err := p.registry.Register(cnt)
err := p.Register(cnt)
if err != nil {
reg, ok := err.(prom.AlreadyRegisteredError)
if ok {
Expand Down
3 changes: 3 additions & 0 deletions metrics/registery.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

"github.com/getamis/sirius/log"
prom "github.com/prometheus/client_golang/prometheus"
)

const MetricsEnabledFlag = "metrics"
Expand All @@ -40,6 +41,8 @@ func init() {

// Registry is a metrics gather
type Registry interface {
prom.Registerer

NewHttpServerMetrics(opts ...Option) HttpServerMetrics
NewServerMetrics(opts ...Option) ServerMetrics
NewCounter(key string, opts ...Option) Counter
Expand Down

0 comments on commit 6660f4f

Please sign in to comment.