Commit 1d66f9a4 by Marcus Efraimsson

anonymous usage stats for authentication types

parent a5bcd4b8
...@@ -350,7 +350,7 @@ func getEdition() string { ...@@ -350,7 +350,7 @@ func getEdition() string {
} }
} }
func sendUsageStats() { func sendUsageStats(oauthProviders map[string]bool) {
if !setting.ReportingEnabled { if !setting.ReportingEnabled {
return return
} }
...@@ -450,6 +450,24 @@ func sendUsageStats() { ...@@ -450,6 +450,24 @@ func sendUsageStats() {
metrics["stats.alert_notifiers."+stats.Type+".count"] = stats.Count metrics["stats.alert_notifiers."+stats.Type+".count"] = stats.Count
} }
authTypes := map[string]bool{}
authTypes["anonymous"] = setting.AnonymousEnabled
authTypes["basic_auth"] = setting.BasicAuthEnabled
authTypes["ldap"] = setting.LdapEnabled
authTypes["auth_proxy"] = setting.AuthProxyEnabled
for provider, enabled := range oauthProviders {
authTypes["oauth_"+provider] = enabled
}
for authType, enabled := range authTypes {
enabledValue := 0
if enabled {
enabledValue = 1
}
metrics["stats.auth_enabled."+authType+".count"] = enabledValue
}
out, _ := json.MarshalIndent(report, "", " ") out, _ := json.MarshalIndent(report, "", " ")
data := bytes.NewBuffer(out) data := bytes.NewBuffer(out)
......
...@@ -147,11 +147,19 @@ func TestMetrics(t *testing.T) { ...@@ -147,11 +147,19 @@ func TestMetrics(t *testing.T) {
})) }))
usageStatsURL = ts.URL usageStatsURL = ts.URL
sendUsageStats() oauthProviders := map[string]bool{
"github": true,
"gitlab": true,
"google": true,
"generic_oauth": true,
"grafana_com": true,
}
sendUsageStats(oauthProviders)
Convey("Given reporting not enabled and sending usage stats", func() { Convey("Given reporting not enabled and sending usage stats", func() {
setting.ReportingEnabled = false setting.ReportingEnabled = false
sendUsageStats() sendUsageStats(oauthProviders)
Convey("Should not gather stats or call http endpoint", func() { Convey("Should not gather stats or call http endpoint", func() {
So(getSystemStatsQuery, ShouldBeNil) So(getSystemStatsQuery, ShouldBeNil)
...@@ -164,8 +172,13 @@ func TestMetrics(t *testing.T) { ...@@ -164,8 +172,13 @@ func TestMetrics(t *testing.T) {
Convey("Given reporting enabled and sending usage stats", func() { Convey("Given reporting enabled and sending usage stats", func() {
setting.ReportingEnabled = true setting.ReportingEnabled = true
setting.BuildVersion = "5.0.0" setting.BuildVersion = "5.0.0"
setting.AnonymousEnabled = true
setting.BasicAuthEnabled = true
setting.LdapEnabled = true
setting.AuthProxyEnabled = true
wg.Add(1) wg.Add(1)
sendUsageStats() sendUsageStats(oauthProviders)
Convey("Should gather stats and call http endpoint", func() { Convey("Should gather stats and call http endpoint", func() {
if waitTimeout(&wg, 2*time.Second) { if waitTimeout(&wg, 2*time.Second) {
...@@ -220,6 +233,16 @@ func TestMetrics(t *testing.T) { ...@@ -220,6 +233,16 @@ func TestMetrics(t *testing.T) {
So(metrics.Get("stats.alert_notifiers.slack.count").MustInt(), ShouldEqual, 1) So(metrics.Get("stats.alert_notifiers.slack.count").MustInt(), ShouldEqual, 1)
So(metrics.Get("stats.alert_notifiers.webhook.count").MustInt(), ShouldEqual, 2) So(metrics.Get("stats.alert_notifiers.webhook.count").MustInt(), ShouldEqual, 2)
So(metrics.Get("stats.auth_enabled.anonymous.count").MustInt(), ShouldEqual, 1)
So(metrics.Get("stats.auth_enabled.basic_auth.count").MustInt(), ShouldEqual, 1)
So(metrics.Get("stats.auth_enabled.ldap.count").MustInt(), ShouldEqual, 1)
So(metrics.Get("stats.auth_enabled.auth_proxy.count").MustInt(), ShouldEqual, 1)
So(metrics.Get("stats.auth_enabled.oauth_github.count").MustInt(), ShouldEqual, 1)
So(metrics.Get("stats.auth_enabled.oauth_gitlab.count").MustInt(), ShouldEqual, 1)
So(metrics.Get("stats.auth_enabled.oauth_google.count").MustInt(), ShouldEqual, 1)
So(metrics.Get("stats.auth_enabled.oauth_generic_oauth.count").MustInt(), ShouldEqual, 1)
So(metrics.Get("stats.auth_enabled.oauth_grafana_com.count").MustInt(), ShouldEqual, 1)
}) })
}) })
......
...@@ -31,6 +31,7 @@ type InternalMetricsService struct { ...@@ -31,6 +31,7 @@ type InternalMetricsService struct {
enabled bool enabled bool
intervalSeconds int64 intervalSeconds int64
graphiteCfg *graphitebridge.Config graphiteCfg *graphitebridge.Config
oauthProviders map[string]bool
} }
func (im *InternalMetricsService) Init() error { func (im *InternalMetricsService) Init() error {
...@@ -61,7 +62,7 @@ func (im *InternalMetricsService) Run(ctx context.Context) error { ...@@ -61,7 +62,7 @@ func (im *InternalMetricsService) Run(ctx context.Context) error {
for { for {
select { select {
case <-onceEveryDayTick.C: case <-onceEveryDayTick.C:
sendUsageStats() sendUsageStats(im.oauthProviders)
case <-everyMinuteTicker.C: case <-everyMinuteTicker.C:
updateTotalStats() updateTotalStats()
case <-ctx.Done(): case <-ctx.Done():
......
...@@ -5,6 +5,8 @@ import ( ...@@ -5,6 +5,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/grafana/grafana/pkg/social"
"github.com/grafana/grafana/pkg/metrics/graphitebridge" "github.com/grafana/grafana/pkg/metrics/graphitebridge"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
...@@ -27,6 +29,8 @@ func (im *InternalMetricsService) readSettings() error { ...@@ -27,6 +29,8 @@ func (im *InternalMetricsService) readSettings() error {
return fmt.Errorf("Unable to parse metrics graphite section, %v", err) return fmt.Errorf("Unable to parse metrics graphite section, %v", err)
} }
im.oauthProviders = social.GetOAuthProviders(im.Cfg)
return nil return nil
} }
......
...@@ -49,14 +49,13 @@ func (e *Error) Error() string { ...@@ -49,14 +49,13 @@ func (e *Error) Error() string {
var ( var (
SocialBaseUrl = "/login/" SocialBaseUrl = "/login/"
SocialMap = make(map[string]SocialConnector) SocialMap = make(map[string]SocialConnector)
allOauthes = []string{"github", "gitlab", "google", "generic_oauth", "grafananet", "grafana_com"}
) )
func NewOAuthService() { func NewOAuthService() {
setting.OAuthService = &setting.OAuther{} setting.OAuthService = &setting.OAuther{}
setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo) setting.OAuthService.OAuthInfos = make(map[string]*setting.OAuthInfo)
allOauthes := []string{"github", "gitlab", "google", "generic_oauth", "grafananet", "grafana_com"}
for _, name := range allOauthes { for _, name := range allOauthes {
sec := setting.Raw.Section("auth." + name) sec := setting.Raw.Section("auth." + name)
info := &setting.OAuthInfo{ info := &setting.OAuthInfo{
...@@ -184,3 +183,26 @@ func NewOAuthService() { ...@@ -184,3 +183,26 @@ func NewOAuthService() {
} }
} }
} }
// GetOAuthProviders returns available oauth providers and if they're enabled or not
var GetOAuthProviders = func(cfg *setting.Cfg) map[string]bool {
result := map[string]bool{}
if cfg == nil || cfg.Raw == nil {
return result
}
for _, name := range allOauthes {
if name == "grafananet" {
name = "grafana_com"
}
sec := cfg.Raw.Section("auth." + name)
if sec == nil {
continue
}
result[name] = sec.Key("enabled").MustBool()
}
return result
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment