grafana/pkg/metrics/publish.go

103 lines
2.6 KiB
Go
Raw Normal View History

2015-03-22 14:14:00 -05:00
package metrics
import (
"bytes"
"encoding/json"
"net/http"
2015-03-22 15:13:16 -05:00
"strings"
2015-03-22 14:14:00 -05:00
"time"
"github.com/grafana/grafana/pkg/bus"
2015-03-22 14:45:13 -05:00
"github.com/grafana/grafana/pkg/log"
m "github.com/grafana/grafana/pkg/models"
2016-04-10 17:36:54 -05:00
"github.com/grafana/grafana/pkg/plugins"
2015-03-22 14:14:00 -05:00
"github.com/grafana/grafana/pkg/setting"
)
func Init() {
settings := readSettings()
initMetricVars(settings)
go instrumentationLoop(settings)
}
func instrumentationLoop(settings *MetricSettings) chan struct{} {
2015-03-22 14:14:00 -05:00
M_Instance_Start.Inc(1)
onceEveryDayTick := time.NewTicker(time.Hour * 24)
secondTicker := time.NewTicker(time.Second * time.Duration(settings.IntervalSeconds))
2015-03-22 14:14:00 -05:00
for {
select {
case <-onceEveryDayTick.C:
2015-03-22 14:14:00 -05:00
sendUsageStats()
case <-secondTicker.C:
if settings.Enabled {
sendMetrics(settings)
}
2015-03-22 14:14:00 -05:00
}
}
}
func sendMetrics(settings *MetricSettings) {
metrics := MetricStats.GetSnapshots()
for _, publisher := range settings.Publishers {
publisher.Publish(metrics)
}
}
2015-03-22 14:14:00 -05:00
func sendUsageStats() {
if !setting.ReportingEnabled {
return
}
2015-03-22 14:45:13 -05:00
log.Trace("Sending anonymous usage stats to stats.grafana.org")
2015-03-22 15:13:16 -05:00
version := strings.Replace(setting.BuildVersion, ".", "_", -1)
2015-03-22 14:14:00 -05:00
metrics := map[string]interface{}{}
report := map[string]interface{}{
2015-03-22 15:13:16 -05:00
"version": version,
2015-03-22 14:14:00 -05:00
"metrics": metrics,
}
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.users.count"] = statsQuery.Result.UserCount
metrics["stats.orgs.count"] = statsQuery.Result.OrgCount
2016-01-25 14:39:04 -06:00
metrics["stats.playlist.count"] = statsQuery.Result.PlaylistCount
2016-04-10 17:36:54 -05:00
metrics["stats.plugins.apps.count"] = len(plugins.Apps)
metrics["stats.plugins.panels.count"] = len(plugins.Panels)
metrics["stats.plugins.datasources.count"] = len(plugins.DataSources)
2015-03-22 14:14:00 -05:00
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.IsKnownDataSourcePlugin(dsStat.Type) {
metrics["stats.ds."+dsStat.Type+".count"] = dsStat.Count
} else {
dsOtherCount += dsStat.Count
}
}
metrics["stats.ds.other.count"] = dsOtherCount
out, _ := json.MarshalIndent(report, "", " ")
2015-03-22 14:14:00 -05:00
data := bytes.NewBuffer(out)
client := http.Client{Timeout: time.Duration(5 * time.Second)}
2015-03-22 14:24:35 -05:00
go client.Post("https://stats.grafana.org/grafana-usage-report", "application/json", data)
2015-03-22 14:14:00 -05:00
}