From 0e4b0b693922245df741b09eb08368b2b78ce442 Mon Sep 17 00:00:00 2001 From: mkraft Date: Fri, 8 Apr 2022 11:25:40 -0400 Subject: [PATCH] MM-43082: Added new telemetry values for groups. (#19918) --- services/telemetry/telemetry.go | 12 ++++++++++++ services/telemetry/telemetry_test.go | 2 ++ store/opentracinglayer/opentracinglayer.go | 18 ++++++++++++++++++ store/retrylayer/retrylayer.go | 21 +++++++++++++++++++++ store/sqlstore/group_store.go | 4 ++++ store/store.go | 2 ++ store/storetest/mocks/GroupStore.go | 21 +++++++++++++++++++++ store/timerlayer/timerlayer.go | 16 ++++++++++++++++ 8 files changed, 96 insertions(+) diff --git a/services/telemetry/telemetry.go b/services/telemetry/telemetry.go index b7a4109dab..25d5cc5148 100644 --- a/services/telemetry/telemetry.go +++ b/services/telemetry/telemetry.go @@ -1106,6 +1106,16 @@ func (ts *TelemetryService) trackGroups() { mlog.Debug("Could not get group_count", mlog.Err(err)) } + ldapGroupCount, err := ts.dbStore.Group().GroupCountBySource(model.GroupSourceLdap) + if err != nil { + mlog.Debug("Could not get group_count", mlog.Err(err)) + } + + customGroupCount, err := ts.dbStore.Group().GroupCountBySource(model.GroupSourceCustom) + if err != nil { + mlog.Debug("Could not get group_count", mlog.Err(err)) + } + groupTeamCount, err := ts.dbStore.Group().GroupTeamCount() if err != nil { mlog.Debug("Could not get group_team_count", mlog.Err(err)) @@ -1143,6 +1153,8 @@ func (ts *TelemetryService) trackGroups() { ts.SendTelemetry(TrackGroups, map[string]interface{}{ "group_count": groupCount, + "ldap_group_count": ldapGroupCount, + "custom_group_count": customGroupCount, "group_team_count": groupTeamCount, "group_channel_count": groupChannelCount, "group_synced_team_count": groupSyncedTeamCount, diff --git a/services/telemetry/telemetry_test.go b/services/telemetry/telemetry_test.go index 11adbcd958..60985f2577 100644 --- a/services/telemetry/telemetry_test.go +++ b/services/telemetry/telemetry_test.go @@ -130,6 +130,8 @@ func initializeMocks(cfg *model.Config) (*mocks.ServerIface, *storeMocks.Store, groupStore.On("GroupMemberCount").Return(int64(32), nil) groupStore.On("DistinctGroupMemberCount").Return(int64(22), nil) groupStore.On("GroupCountWithAllowReference").Return(int64(13), nil) + groupStore.On("GroupCountBySource", model.GroupSourceCustom).Return(int64(10), nil) + groupStore.On("GroupCountBySource", model.GroupSourceLdap).Return(int64(2), nil) schemeStore := storeMocks.SchemeStore{} schemeStore.On("CountByScope", "channel").Return(int64(8), nil) diff --git a/store/opentracinglayer/opentracinglayer.go b/store/opentracinglayer/opentracinglayer.go index 5ae97d9564..2090b17801 100644 --- a/store/opentracinglayer/opentracinglayer.go +++ b/store/opentracinglayer/opentracinglayer.go @@ -4213,6 +4213,24 @@ func (s *OpenTracingLayerGroupStore) GroupCount() (int64, error) { return result, err } +func (s *OpenTracingLayerGroupStore) GroupCountBySource(source model.GroupSource) (int64, error) { + origCtx := s.Root.Store.Context() + span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "GroupStore.GroupCountBySource") + s.Root.Store.SetContext(newCtx) + defer func() { + s.Root.Store.SetContext(origCtx) + }() + + defer span.Finish() + result, err := s.GroupStore.GroupCountBySource(source) + if err != nil { + span.LogFields(spanlog.Error(err)) + ext.Error.Set(span, true) + } + + return result, err +} + func (s *OpenTracingLayerGroupStore) GroupCountWithAllowReference() (int64, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "GroupStore.GroupCountWithAllowReference") diff --git a/store/retrylayer/retrylayer.go b/store/retrylayer/retrylayer.go index 8e8154b2ae..7442161038 100644 --- a/store/retrylayer/retrylayer.go +++ b/store/retrylayer/retrylayer.go @@ -4750,6 +4750,27 @@ func (s *RetryLayerGroupStore) GroupCount() (int64, error) { } +func (s *RetryLayerGroupStore) GroupCountBySource(source model.GroupSource) (int64, error) { + + tries := 0 + for { + result, err := s.GroupStore.GroupCountBySource(source) + if err == nil { + return result, nil + } + if !isRepeatableError(err) { + return result, err + } + tries++ + if tries >= 3 { + err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures") + return result, err + } + timepkg.Sleep(100 * timepkg.Millisecond) + } + +} + func (s *RetryLayerGroupStore) GroupCountWithAllowReference() (int64, error) { tries := 0 diff --git a/store/sqlstore/group_store.go b/store/sqlstore/group_store.go index 11f2f22b17..5911720792 100644 --- a/store/sqlstore/group_store.go +++ b/store/sqlstore/group_store.go @@ -1720,6 +1720,10 @@ func (s *SqlGroupStore) GroupCount() (int64, error) { return s.countTable("UserGroups") } +func (s *SqlGroupStore) GroupCountBySource(source model.GroupSource) (int64, error) { + return s.countTableWithSelectAndWhere("COUNT(*)", "UserGroups", sq.Eq{"Source": source, "DeleteAt": 0}) +} + func (s *SqlGroupStore) GroupTeamCount() (int64, error) { return s.countTable("GroupTeams") } diff --git a/store/store.go b/store/store.go index 212d40acb0..3123cff9ab 100644 --- a/store/store.go +++ b/store/store.go @@ -853,6 +853,8 @@ type GroupStore interface { // GroupCount returns the total count of records in the UserGroups table. GroupCount() (int64, error) + GroupCountBySource(source model.GroupSource) (int64, error) + // GroupTeamCount returns the total count of records in the GroupTeams table. GroupTeamCount() (int64, error) diff --git a/store/storetest/mocks/GroupStore.go b/store/storetest/mocks/GroupStore.go index 02b4285d24..85498100dd 100644 --- a/store/storetest/mocks/GroupStore.go +++ b/store/storetest/mocks/GroupStore.go @@ -849,6 +849,27 @@ func (_m *GroupStore) GroupCount() (int64, error) { return r0, r1 } +// GroupCountBySource provides a mock function with given fields: source +func (_m *GroupStore) GroupCountBySource(source model.GroupSource) (int64, error) { + ret := _m.Called(source) + + var r0 int64 + if rf, ok := ret.Get(0).(func(model.GroupSource) int64); ok { + r0 = rf(source) + } else { + r0 = ret.Get(0).(int64) + } + + var r1 error + if rf, ok := ret.Get(1).(func(model.GroupSource) error); ok { + r1 = rf(source) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // GroupCountWithAllowReference provides a mock function with given fields: func (_m *GroupStore) GroupCountWithAllowReference() (int64, error) { ret := _m.Called() diff --git a/store/timerlayer/timerlayer.go b/store/timerlayer/timerlayer.go index 60c4e8378b..38af85bd9b 100644 --- a/store/timerlayer/timerlayer.go +++ b/store/timerlayer/timerlayer.go @@ -3835,6 +3835,22 @@ func (s *TimerLayerGroupStore) GroupCount() (int64, error) { return result, err } +func (s *TimerLayerGroupStore) GroupCountBySource(source model.GroupSource) (int64, error) { + start := timemodule.Now() + + result, err := s.GroupStore.GroupCountBySource(source) + + elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second) + if s.Root.Metrics != nil { + success := "false" + if err == nil { + success = "true" + } + s.Root.Metrics.ObserveStoreMethodDuration("GroupStore.GroupCountBySource", success, elapsed) + } + return result, err +} + func (s *TimerLayerGroupStore) GroupCountWithAllowReference() (int64, error) { start := timemodule.Now()