Sfoglia il codice sorgente

feat(usage stats): added data source count stats

Torkel Ödegaard 10 anni fa
parent
commit
c816ed2527

+ 26 - 7
pkg/metrics/report_usage.go

@@ -36,12 +36,6 @@ func sendUsageStats() {
 		"metrics": metrics,
 		"metrics": metrics,
 	}
 	}
 
 
-	statsQuery := m.GetSystemStatsQuery{}
-	if err := bus.Dispatch(&statsQuery); err != nil {
-		log.Error(3, "Failed to get system stats", err)
-		return
-	}
-
 	UsageStats.Each(func(name string, i interface{}) {
 	UsageStats.Each(func(name string, i interface{}) {
 		switch metric := i.(type) {
 		switch metric := i.(type) {
 		case Counter:
 		case Counter:
@@ -52,11 +46,36 @@ func sendUsageStats() {
 		}
 		}
 	})
 	})
 
 
+	statsQuery := m.GetSystemStatsQuery{}
+	if err := bus.Dispatch(&statsQuery); err != nil {
+		log.Error(3, "Failed to get system stats", err)
+		return
+	}
+
 	metrics["stats.dashboards.count"] = statsQuery.Result.DashboardCount
 	metrics["stats.dashboards.count"] = statsQuery.Result.DashboardCount
 	metrics["stats.users.count"] = statsQuery.Result.UserCount
 	metrics["stats.users.count"] = statsQuery.Result.UserCount
 	metrics["stats.orgs.count"] = statsQuery.Result.OrgCount
 	metrics["stats.orgs.count"] = statsQuery.Result.OrgCount
 
 
-	out, _ := json.Marshal(report)
+	dsStats := m.GetDataSourceStatsQuery{}
+	if err := bus.Dispatch(&dsStats); err != nil {
+		log.Error(3, "Failed to get datasource stats", err)
+		return
+	}
+
+	// send counters for each data source
+	// but ignore any custom data sources
+	// as sending that name could be sensitive information
+	dsOtherCount := 0
+	for _, dsStat := range dsStats.Result {
+		if m.IsStandardDataSource(dsStat.Type) {
+			metrics["stats.ds."+dsStat.Type+".count"] = dsStat.Count
+		} else {
+			dsOtherCount += dsStat.Count
+		}
+	}
+	metrics["stats.ds.other.count"] = dsOtherCount
+
+	out, _ := json.MarshalIndent(report, "", " ")
 	data := bytes.NewBuffer(out)
 	data := bytes.NewBuffer(out)
 
 
 	client := http.Client{Timeout: time.Duration(5 * time.Second)}
 	client := http.Client{Timeout: time.Duration(5 * time.Second)}

+ 21 - 0
pkg/models/datasource.go

@@ -12,6 +12,8 @@ const (
 	DS_ES            = "elasticsearch"
 	DS_ES            = "elasticsearch"
 	DS_OPENTSDB      = "opentsdb"
 	DS_OPENTSDB      = "opentsdb"
 	DS_CLOUDWATCH    = "cloudwatch"
 	DS_CLOUDWATCH    = "cloudwatch"
+	DS_KAIROSDB      = "kairosdb"
+	DS_PROMETHEUS    = "prometheus"
 	DS_ACCESS_DIRECT = "direct"
 	DS_ACCESS_DIRECT = "direct"
 	DS_ACCESS_PROXY  = "proxy"
 	DS_ACCESS_PROXY  = "proxy"
 )
 )
@@ -45,6 +47,25 @@ type DataSource struct {
 	Updated time.Time
 	Updated time.Time
 }
 }
 
 
+func IsStandardDataSource(dsType string) bool {
+	switch dsType {
+	case DS_ES:
+		return true
+	case DS_INFLUXDB:
+		return true
+	case DS_OPENTSDB:
+		return true
+	case DS_CLOUDWATCH:
+		return true
+	case DS_PROMETHEUS:
+		return true
+	case DS_GRAPHITE:
+		return true
+	default:
+		return false
+	}
+}
+
 // ----------------------
 // ----------------------
 // COMMANDS
 // COMMANDS
 
 

+ 9 - 0
pkg/models/stats.go

@@ -6,6 +6,15 @@ type SystemStats struct {
 	OrgCount       int
 	OrgCount       int
 }
 }
 
 
+type DataSourceStats struct {
+	Count int
+	Type  string
+}
+
 type GetSystemStatsQuery struct {
 type GetSystemStatsQuery struct {
 	Result *SystemStats
 	Result *SystemStats
 }
 }
+
+type GetDataSourceStatsQuery struct {
+	Result []*DataSourceStats
+}

+ 12 - 0
pkg/services/sqlstore/stats.go

@@ -7,6 +7,18 @@ import (
 
 
 func init() {
 func init() {
 	bus.AddHandler("sql", GetSystemStats)
 	bus.AddHandler("sql", GetSystemStats)
+	bus.AddHandler("sql", GetDataSourceStats)
+}
+
+func GetDataSourceStats(query *m.GetDataSourceStatsQuery) error {
+	var rawSql = `SELECT COUNT(*) as count, type FROM data_source GROUP BY type`
+	query.Result = make([]*m.DataSourceStats, 0)
+	err := x.Sql(rawSql).Find(&query.Result)
+	if err != nil {
+		return err
+	}
+
+	return err
 }
 }
 
 
 func GetSystemStats(query *m.GetSystemStatsQuery) error {
 func GetSystemStats(query *m.GetSystemStatsQuery) error {