aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/auth/github/github.go6
-rw-r--r--server/auth/gitlab/gitlab.go10
-rw-r--r--server/auth/google/google.go8
-rw-r--r--server/metrics/metrics.go42
4 files changed, 64 insertions, 2 deletions
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.
diff --git a/server/metrics/metrics.go b/server/metrics/metrics.go
new file mode 100644
index 0000000..4102353
--- /dev/null
+++ b/server/metrics/metrics.go
@@ -0,0 +1,42 @@
+package metrics
+
+import (
+ "github.com/prometheus/client_golang/prometheus"
+)
+
+// Metrics collects the metrics.
+type Metrics struct {
+ AuthValid,
+ AuthExchange,
+ Errs *prometheus.CounterVec
+}
+
+// M structure to collect all metrics together.
+var M Metrics
+
+// Register metrics and metrics page.
+func Register() {
+ M = Metrics{
+ AuthValid: prometheus.NewCounterVec(prometheus.CounterOpts{
+ Namespace: "cashier",
+ Subsystem: "auth",
+ Name: "valid_total",
+ Help: "Auth Valid calls",
+ }, []string{"module"}),
+ AuthExchange: prometheus.NewCounterVec(prometheus.CounterOpts{
+ Namespace: "cashier",
+ Subsystem: "auth",
+ Name: "exchange_total",
+ Help: "Auth Exchange calls",
+ }, []string{"module"}),
+ Errs: prometheus.NewCounterVec(prometheus.CounterOpts{
+ Namespace: "cashier",
+ Subsystem: "sys",
+ Name: "error_total",
+ Help: "Error counts by module",
+ }, []string{"module"}),
+ }
+ prometheus.MustRegister(M.Errs)
+ prometheus.MustRegister(M.AuthValid)
+ prometheus.MustRegister(M.AuthExchange)
+}