From ed8bc523fd0d1a66acf3fa449c453508035efdfc Mon Sep 17 00:00:00 2001 From: Kevin Lyda Date: Sun, 12 Feb 2017 14:38:12 +0000 Subject: Initial pass at prometheus support. (#56) --- server/auth/github/github.go | 6 ++++++ server/auth/gitlab/gitlab.go | 10 +++++++++- server/auth/google/google.go | 8 +++++++- 3 files changed, 22 insertions(+), 2 deletions(-) (limited to 'server/auth') diff --git a/server/auth/github/github.go b/server/auth/github/github.go index 46cf76a..d44a49d 100644 --- a/server/auth/github/github.go +++ b/server/auth/github/github.go @@ -7,6 +7,7 @@ import ( "github.com/nsheridan/cashier/server/auth" "github.com/nsheridan/cashier/server/config" + "github.com/nsheridan/cashier/server/metrics" githubapi "github.com/google/go-github/github" "golang.org/x/oauth2" @@ -73,6 +74,7 @@ func (c *Config) Valid(token *oauth2.Token) bool { if c.organization == "" { // There's no organization and the token is valid. Can only reach here // if there's a user whitelist set and the user is in the whitelist. + metrics.M.AuthValid.WithLabelValues("github").Inc() return true } client := githubapi.NewClient(c.newClient(token)) @@ -80,6 +82,9 @@ func (c *Config) Valid(token *oauth2.Token) bool { if err != nil { return false } + if member { + metrics.M.AuthValid.WithLabelValues("github").Inc() + } return member } @@ -108,6 +113,7 @@ func (c *Config) Exchange(code string) (*oauth2.Token, error) { if t.Expiry.Unix() <= 0 { t.Expiry = time.Now().Add(1 * time.Hour) } + metrics.M.AuthExchange.WithLabelValues("github").Inc() return t, nil } diff --git a/server/auth/gitlab/gitlab.go b/server/auth/gitlab/gitlab.go index f76b2e8..2cf2a5c 100644 --- a/server/auth/gitlab/gitlab.go +++ b/server/auth/gitlab/gitlab.go @@ -6,6 +6,7 @@ import ( "github.com/nsheridan/cashier/server/auth" "github.com/nsheridan/cashier/server/config" + "github.com/nsheridan/cashier/server/metrics" gitlabapi "github.com/xanzy/go-gitlab" "golang.org/x/oauth2" @@ -78,6 +79,7 @@ func (c *Config) Valid(token *oauth2.Token) bool { return false } if c.allusers { + metrics.M.AuthValid.WithLabelValues("gitlab").Inc() return true } if len(c.whitelist) > 0 && !c.whitelist[c.Username(token)] { @@ -86,6 +88,7 @@ func (c *Config) Valid(token *oauth2.Token) bool { if c.group == "" { // There's no group and token is valid. Can only reach // here if user whitelist is set and user is in whitelist. + metrics.M.AuthValid.WithLabelValues("gitlab").Inc() return true } client := gitlabapi.NewOAuthClient(nil, token.AccessToken) @@ -96,6 +99,7 @@ func (c *Config) Valid(token *oauth2.Token) bool { } for _, g := range groups { if g.Path == c.group { + metrics.M.AuthValid.WithLabelValues("gitlab").Inc() return true } } @@ -118,7 +122,11 @@ func (c *Config) StartSession(state string) *auth.Session { // Exchange authorizes the session and returns an access token. func (c *Config) Exchange(code string) (*oauth2.Token, error) { - return c.config.Exchange(oauth2.NoContext, code) + t, err := c.config.Exchange(oauth2.NoContext, code) + if err == nil { + metrics.M.AuthExchange.WithLabelValues("gitlab").Inc() + } + return t, err } // Username retrieves the username of the Gitlab user. diff --git a/server/auth/google/google.go b/server/auth/google/google.go index 8c6f53b..9a151f6 100644 --- a/server/auth/google/google.go +++ b/server/auth/google/google.go @@ -8,6 +8,7 @@ import ( "github.com/nsheridan/cashier/server/auth" "github.com/nsheridan/cashier/server/config" + "github.com/nsheridan/cashier/server/metrics" "golang.org/x/oauth2" "golang.org/x/oauth2/google" @@ -90,6 +91,7 @@ func (c *Config) Valid(token *oauth2.Token) bool { if c.domain != "" && ui.Hd != c.domain { return false } + metrics.M.AuthValid.WithLabelValues("google").Inc() return true } @@ -109,7 +111,11 @@ func (c *Config) StartSession(state string) *auth.Session { // Exchange authorizes the session and returns an access token. func (c *Config) Exchange(code string) (*oauth2.Token, error) { - return c.config.Exchange(oauth2.NoContext, code) + t, err := c.config.Exchange(oauth2.NoContext, code) + if err == nil { + metrics.M.AuthExchange.WithLabelValues("google").Inc() + } + return t, err } // Email retrieves the email address of the user. -- cgit v1.2.3