Skip to content

Commit

Permalink
pkg/oauth2: dependency-inject grants total counter metric
Browse files Browse the repository at this point in the history
  • Loading branch information
s-urbaniak committed Jun 12, 2019
1 parent 36c72d7 commit b10ffcf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
13 changes: 12 additions & 1 deletion cmd/telemeter-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"

oidc "github.com/coreos/go-oidc"
"github.com/oklog/run"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -250,8 +252,17 @@ func (o *Options) Run() error {
Endpoint: provider.Endpoint(),
}

grantsTotal := prometheus.NewCounter(
prometheus.CounterOpts{
Name: "telemeter_password_credentials_grants_total",
Help: "Tracks the number of resource owner password credential grants.",
},
)

prometheus.MustRegister(grantsTotal)

src := telemeter_oauth2.NewPasswordCredentialsTokenSource(
ctx, &cfg,
ctx, &cfg, grantsTotal.Inc,
o.AuthorizeUsername, o.AuthorizePassword,
)

Expand Down
28 changes: 8 additions & 20 deletions pkg/oauth2/token_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,14 @@ import (
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
"golang.org/x/oauth2"
)

var (
grantsTotal = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "password_credentials_grants_total",
Help: "Tracks the number of resource owner password credential grants.",
},
)
)

func init() {
prometheus.MustRegister(grantsTotal)
}

type passwordCredentialsTokenSource struct {
ctx context.Context
cfg *oauth2.Config
username, password string
grantsCounter func()

mu sync.Mutex // protects the fields below
refreshToken *oauth2.Token
Expand All @@ -47,12 +34,13 @@ type passwordCredentialsTokenSource struct {
// using the given resource owner and password.
//
// It is safe for concurrent use.
func NewPasswordCredentialsTokenSource(ctx context.Context, cfg *oauth2.Config, username, password string) *passwordCredentialsTokenSource {
func NewPasswordCredentialsTokenSource(ctx context.Context, cfg *oauth2.Config, grantsCounter func(), username, password string) *passwordCredentialsTokenSource {
return &passwordCredentialsTokenSource{
ctx: ctx,
username: username,
password: password,
cfg: cfg,
ctx: ctx,
username: username,
password: password,
cfg: cfg,
grantsCounter: grantsCounter,
}
}

Expand Down Expand Up @@ -95,7 +83,7 @@ func (c *passwordCredentialsTokenSource) Token() (*oauth2.Token, error) {
}

func (c *passwordCredentialsTokenSource) passwordCredentialsToken() (*oauth2.Token, error) {
grantsTotal.Inc()
c.grantsCounter()

tok, err := c.cfg.PasswordCredentialsToken(c.ctx, c.username, c.password)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/oauth2/token_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ func TestPasswordCredentialsTokenSource(t *testing.T) {
},
)

src := NewPasswordCredentialsTokenSource(ctx, conf, username, password)
nop := func() {}
src := NewPasswordCredentialsTokenSource(ctx, conf, nop, username, password)
return src, tr
}
}
Expand Down

0 comments on commit b10ffcf

Please sign in to comment.