diff --git a/pkg/infra/usagestats/statscollector/service.go b/pkg/infra/usagestats/statscollector/service.go index 603c19d022f..49acd8eb0e8 100644 --- a/pkg/infra/usagestats/statscollector/service.go +++ b/pkg/infra/usagestats/statscollector/service.go @@ -261,14 +261,12 @@ func (s *Service) collectElasticStats(ctx context.Context) (map[string]interface s.log.Error("Failed to get elasticsearch json data", "error", err) return nil, err } - for _, data := range esDataSourcesQuery.Result { - esVersion, err := data.JsonData.Get("esVersion").Int() + esVersion, err := data.JsonData.Get("esVersion").String() if err != nil { continue } - - statName := fmt.Sprintf("stats.ds.elasticsearch.v%d.count", esVersion) + statName := fmt.Sprintf("stats.ds.elasticsearch.v%s.count", strings.ReplaceAll(esVersion, ".", "_")) count, _ := m[statName].(int64) diff --git a/pkg/infra/usagestats/statscollector/service_test.go b/pkg/infra/usagestats/statscollector/service_test.go index 1550fb265a6..73e60b87b73 100644 --- a/pkg/infra/usagestats/statscollector/service_test.go +++ b/pkg/infra/usagestats/statscollector/service_test.go @@ -121,6 +121,24 @@ func TestFeatureUsageStats(t *testing.T) { func TestCollectingUsageStats(t *testing.T) { sqlStore := mockstore.NewSQLStoreMock() + sqlStore.ExpectedDataSources = []*datasources.DataSource{ + { + JsonData: simplejson.NewFromAny(map[string]interface{}{ + "esVersion": "2.0.0", + }), + }, + { + JsonData: simplejson.NewFromAny(map[string]interface{}{ + "esVersion": "2.0.0", + }), + }, + { + JsonData: simplejson.NewFromAny(map[string]interface{}{ + "esVersion": "70.1.1", + }), + }, + } + s := createService(t, &setting.Cfg{ ReportingEnabled: true, BuildVersion: "5.0.0", @@ -130,7 +148,8 @@ func TestCollectingUsageStats(t *testing.T) { AuthProxyEnabled: true, Packaging: "deb", ReportingDistributor: "hosted-grafana", - }, sqlStore) + }, sqlStore, + withDatasources(mockDatasourceService{datasources: sqlStore.ExpectedDataSources})) s.startTime = time.Now().Add(-1 * time.Minute) @@ -182,6 +201,45 @@ func TestCollectingUsageStats(t *testing.T) { assert.InDelta(t, int64(65), metrics["stats.uptime"], 6) } +func TestElasticStats(t *testing.T) { + sqlStore := mockstore.NewSQLStoreMock() + + s := createService(t, &setting.Cfg{ + ReportingEnabled: true, + BuildVersion: "5.0.0", + AnonymousEnabled: true, + BasicAuthEnabled: true, + LDAPEnabled: true, + AuthProxyEnabled: true, + Packaging: "deb", + ReportingDistributor: "hosted-grafana", + }, sqlStore, + withDatasources(mockDatasourceService{datasources: sqlStore.ExpectedDataSources})) + + sqlStore.ExpectedDataSources = []*datasources.DataSource{ + { + JsonData: simplejson.NewFromAny(map[string]interface{}{ + "esVersion": "2.0.0", + }), + }, + { + JsonData: simplejson.NewFromAny(map[string]interface{}{ + "esVersion": "2.0.0", + }), + }, + { + JsonData: simplejson.NewFromAny(map[string]interface{}{ + "esVersion": "70.1.1", + }), + }, + } + + metrics, err := s.collectElasticStats(context.Background()) + require.NoError(t, err) + + assert.EqualValues(t, 2, metrics["stats.ds."+datasources.DS_ES+".v2_0_0.count"]) + assert.EqualValues(t, 1, metrics["stats.ds."+datasources.DS_ES+".v70_1_1.count"]) +} func TestDatasourceStats(t *testing.T) { sqlStore := mockstore.NewSQLStoreMock() s := createService(t, &setting.Cfg{}, sqlStore) diff --git a/pkg/services/sqlstore/mockstore/mockstore.go b/pkg/services/sqlstore/mockstore/mockstore.go index 90d8206b2a8..6dbb757d040 100644 --- a/pkg/services/sqlstore/mockstore/mockstore.go +++ b/pkg/services/sqlstore/mockstore/mockstore.go @@ -423,11 +423,12 @@ func (m SQLStoreMock) GetDataSource(ctx context.Context, query *datasources.GetD } func (m *SQLStoreMock) GetDataSources(ctx context.Context, query *datasources.GetDataSourcesQuery) error { - query.Result = m.ExpectedDatasources + query.Result = m.ExpectedDataSources return m.ExpectedError } func (m *SQLStoreMock) GetDataSourcesByType(ctx context.Context, query *datasources.GetDataSourcesByTypeQuery) error { + query.Result = m.ExpectedDataSources return m.ExpectedError }