UsageStats: fixed elasticsearch version number to semver (#49054)

* UsageStats: fixed elasticsearch version number

- The version numbering was changed from plain numbers to a semver-ish approach

* added missing version assertion

* adapted tests
pull/51694/head
Sven Grossmann 3 years ago committed by GitHub
parent f233a74b70
commit b7e22c37a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      pkg/infra/usagestats/statscollector/service.go
  2. 60
      pkg/infra/usagestats/statscollector/service_test.go
  3. 3
      pkg/services/sqlstore/mockstore/mockstore.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)

@ -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)

@ -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
}

Loading…
Cancel
Save