Skip to content

Commit

Permalink
chore(prometheus): don't use global prometheus registry
Browse files Browse the repository at this point in the history
And add tests.
  • Loading branch information
mark-rushakoff committed Jul 11, 2018
1 parent 037eca4 commit 11d5e76
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 7 deletions.
16 changes: 9 additions & 7 deletions prometheus/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,14 @@ func NewAuthorizationService() *AuthorizationService {
}, []string{"method", "error"}),
}

prometheus.MustRegister(
s.requestCount,
s.requestDuration,
)

return s
}

// FindAuthorizationByID returns an authorization given a id, records function call latency, and counts function calls.
func (s *AuthorizationService) FindAuthorizationByID(ctx context.Context, id platform.ID) (a *platform.Authorization, err error) {
defer func(start time.Time) {
labels := prometheus.Labels{
"method": "FindAuthorizationsByID",
"method": "FindAuthorizationByID",
"error": fmt.Sprint(err != nil),
}
s.requestCount.With(labels).Add(1)
Expand All @@ -63,7 +58,7 @@ func (s *AuthorizationService) FindAuthorizationByID(ctx context.Context, id pla
func (s *AuthorizationService) FindAuthorizationByToken(ctx context.Context, t string) (a *platform.Authorization, err error) {
defer func(start time.Time) {
labels := prometheus.Labels{
"method": "FindAuthorizationsByToken",
"method": "FindAuthorizationByToken",
"error": fmt.Sprint(err != nil),
}
s.requestCount.With(labels).Add(1)
Expand Down Expand Up @@ -113,3 +108,10 @@ func (s *AuthorizationService) DeleteAuthorization(ctx context.Context, id platf

return s.AuthorizationService.DeleteAuthorization(ctx, id)
}

func (s *AuthorizationService) PrometheusCollectors() []prometheus.Collector {
return []prometheus.Collector{
s.requestCount,
s.requestDuration,
}
}
144 changes: 144 additions & 0 deletions prometheus/auth_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package prometheus_test

import (
"context"
"errors"
"testing"

"github.com/influxdata/platform"
"github.com/influxdata/platform/kit/prom"
"github.com/influxdata/platform/kit/prom/promtest"
"github.com/influxdata/platform/prometheus"
)

// authzSvc is a test helper that returns its Err from every method on the AuthorizationService interface.
type authzSvc struct {
Err error
}

var _ platform.AuthorizationService = (*authzSvc)(nil)

func (a *authzSvc) FindAuthorizationByID(context.Context, platform.ID) (*platform.Authorization, error) {
return nil, a.Err
}

func (a *authzSvc) FindAuthorizationByToken(context.Context, string) (*platform.Authorization, error) {
return nil, a.Err
}

func (a *authzSvc) FindAuthorizations(context.Context, platform.AuthorizationFilter, ...platform.FindOptions) ([]*platform.Authorization, int, error) {
return nil, 0, a.Err
}

func (a *authzSvc) CreateAuthorization(context.Context, *platform.Authorization) error {
return a.Err
}

func (a *authzSvc) DeleteAuthorization(context.Context, platform.ID) error {
return a.Err
}

func TestAuthorizationService_Metrics(t *testing.T) {
a := new(authzSvc)

svc := prometheus.NewAuthorizationService()
svc.AuthorizationService = a
reg := prom.NewRegistry()
reg.MustRegister(svc.PrometheusCollectors()...)

ctx := context.Background()
id := platform.ID{1}

if _, err := svc.FindAuthorizationByID(ctx, id); err != nil {
t.Fatal(err)
}
mfs := promtest.MustGather(t, reg)
m := promtest.MustFindMetric(t, mfs, "auth_prometheus_requests_total", map[string]string{"method": "FindAuthorizationByID", "error": "false"})
if got := m.GetCounter().GetValue(); got != 1 {
t.Fatalf("exp 1 request, got %v", got)
}

if _, err := svc.FindAuthorizationByToken(ctx, ""); err != nil {
t.Fatal(err)
}
mfs = promtest.MustGather(t, reg)
m = promtest.MustFindMetric(t, mfs, "auth_prometheus_requests_total", map[string]string{"method": "FindAuthorizationByToken", "error": "false"})
if got := m.GetCounter().GetValue(); got != 1 {
t.Fatalf("exp 1 request, got %v", got)
}

if _, _, err := svc.FindAuthorizations(ctx, platform.AuthorizationFilter{}); err != nil {
t.Fatal(err)
}
mfs = promtest.MustGather(t, reg)
m = promtest.MustFindMetric(t, mfs, "auth_prometheus_requests_total", map[string]string{"method": "FindAuthorizations", "error": "false"})
if got := m.GetCounter().GetValue(); got != 1 {
t.Fatalf("exp 1 request, got %v", got)
}

if err := svc.CreateAuthorization(ctx, nil); err != nil {
t.Fatal(err)
}
mfs = promtest.MustGather(t, reg)
m = promtest.MustFindMetric(t, mfs, "auth_prometheus_requests_total", map[string]string{"method": "CreateAuthorization", "error": "false"})
if got := m.GetCounter().GetValue(); got != 1 {
t.Fatalf("exp 1 request, got %v", got)
}

if err := svc.DeleteAuthorization(ctx, nil); err != nil {
t.Fatal(err)
}
mfs = promtest.MustGather(t, reg)
m = promtest.MustFindMetric(t, mfs, "auth_prometheus_requests_total", map[string]string{"method": "DeleteAuthorization", "error": "false"})
if got := m.GetCounter().GetValue(); got != 1 {
t.Fatalf("exp 1 request, got %v", got)
}

forced := errors.New("forced error")
a.Err = forced

if _, err := svc.FindAuthorizationByID(ctx, id); err != forced {
t.Fatalf("expected forced error, got %v", err)
}
mfs = promtest.MustGather(t, reg)
m = promtest.MustFindMetric(t, mfs, "auth_prometheus_requests_total", map[string]string{"method": "FindAuthorizationByID", "error": "true"})
if got := m.GetCounter().GetValue(); got != 1 {
t.Fatalf("exp 1 request, got %v", got)
}

if _, err := svc.FindAuthorizationByToken(ctx, ""); err != forced {
t.Fatalf("expected forced error, got %v", err)
}
mfs = promtest.MustGather(t, reg)
m = promtest.MustFindMetric(t, mfs, "auth_prometheus_requests_total", map[string]string{"method": "FindAuthorizationByToken", "error": "true"})
if got := m.GetCounter().GetValue(); got != 1 {
t.Fatalf("exp 1 request, got %v", got)
}

if _, _, err := svc.FindAuthorizations(ctx, platform.AuthorizationFilter{}); err != forced {
t.Fatalf("expected forced error, got %v", err)
}
mfs = promtest.MustGather(t, reg)
m = promtest.MustFindMetric(t, mfs, "auth_prometheus_requests_total", map[string]string{"method": "FindAuthorizations", "error": "true"})
if got := m.GetCounter().GetValue(); got != 1 {
t.Fatalf("exp 1 request, got %v", got)
}

if err := svc.CreateAuthorization(ctx, nil); err != forced {
t.Fatalf("expected forced error, got %v", err)
}
mfs = promtest.MustGather(t, reg)
m = promtest.MustFindMetric(t, mfs, "auth_prometheus_requests_total", map[string]string{"method": "CreateAuthorization", "error": "true"})
if got := m.GetCounter().GetValue(); got != 1 {
t.Fatalf("exp 1 request, got %v", got)
}

if err := svc.DeleteAuthorization(ctx, nil); err != forced {
t.Fatalf("expected forced error, got %v", err)
}
mfs = promtest.MustGather(t, reg)
m = promtest.MustFindMetric(t, mfs, "auth_prometheus_requests_total", map[string]string{"method": "DeleteAuthorization", "error": "true"})
if got := m.GetCounter().GetValue(); got != 1 {
t.Fatalf("exp 1 request, got %v", got)
}
}

0 comments on commit 11d5e76

Please sign in to comment.