Skip to content

Commit

Permalink
UsageStats: fixed elasticsearch version number to semver (#49054)
Browse files Browse the repository at this point in the history
* UsageStats: fixed elasticsearch version number

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

* added missing version assertion

* adapted tests
  • Loading branch information
svennergr committed Jul 4, 2022
1 parent f233a74 commit b7e22c3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
6 changes: 2 additions & 4 deletions pkg/infra/usagestats/statscollector/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
60 changes: 59 additions & 1 deletion pkg/infra/usagestats/statscollector/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/services/sqlstore/mockstore/mockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit b7e22c3

Please sign in to comment.