Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: simple metrics (#15214)
Danny Xu3fae2b5a4e35
diff --git a/backend/metric/collector/issue_count.go b/backend/metric/collector/issue_count.goindex dd71177ff2..6a24da44ec 100644--- a/backend/metric/collector/issue_count.go+++ b/backend/metric/collector/issue_count.go@@ -24,23 +24,14 @@ func NewIssueCountCollector(store *store.Store) metric.Collector {// Collect will collect the metric for issue.func (c *issueCountCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {- var res []*metric.Metric-- issueCountMetricList, err := c.store.CountIssueGroupByTypeAndStatus(ctx)+ count, err := c.store.CountIssues(ctx)if err != nil {return nil, err}-- for _, issueCountMetric := range issueCountMetricList {- res = append(res, &metric.Metric{+ return []*metric.Metric{+ {Name: metricapi.IssueCountMetricName,- Value: issueCountMetric.Count,- Labels: map[string]any{- "type": string(issueCountMetric.Type),- "status": string(issueCountMetric.Status),- },- })- }-- return res, nil+ Value: count,+ },+ }, nil}diff --git a/backend/metric/collector/member_count.go b/backend/metric/collector/member_count.godeleted file mode 100644index b9307fde16..0000000000--- a/backend/metric/collector/member_count.go+++ /dev/null@@ -1,50 +0,0 @@-package collector--import (- "context"-- metricapi "github.com/bytebase/bytebase/backend/metric"- "github.com/bytebase/bytebase/backend/plugin/metric"- "github.com/bytebase/bytebase/backend/store"-)--var _ metric.Collector = (*memberCountCollector)(nil)--// memberCountCollector is the metric data collector for member.-type memberCountCollector struct {- store *store.Store-}--// NewMemberCountCollector creates a new instance of memberCountCollector.-func NewMemberCountCollector(store *store.Store) metric.Collector {- return &memberCountCollector{- store: store,- }-}--// Collect will collect the metric for issue.-func (c *memberCountCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {- var res []*metric.Metric-- workspaceIAMPolicy, err := c.store.GetWorkspaceIamPolicy(ctx)- if err != nil {- return nil, err- }-- roleMap := map[string]int{}- for _, binding := range workspaceIAMPolicy.Policy.Bindings {- roleMap[binding.Role] += len(binding.Members)- }-- for role, count := range roleMap {- res = append(res, &metric.Metric{- Name: metricapi.MemberCountMetricName,- Value: count,- Labels: map[string]any{- "role": role,- },- })- }-- return res, nil-}diff --git a/backend/metric/collector/sheet_count.go b/backend/metric/collector/sheet_count.godeleted file mode 100644index ecb774f243..0000000000--- a/backend/metric/collector/sheet_count.go+++ /dev/null@@ -1,45 +0,0 @@-package collector--import (- "context"-- metricapi "github.com/bytebase/bytebase/backend/metric"- "github.com/bytebase/bytebase/backend/plugin/metric"- "github.com/bytebase/bytebase/backend/store"-)--var _ metric.Collector = (*sheetCountCollector)(nil)--// sheetCountCollector is the metric data collector for sheet.-type sheetCountCollector struct {- store *store.Store-}--// NewSheetCountCollector creates a new instance of sheetCountCollector.-func NewSheetCountCollector(store *store.Store) metric.Collector {- return &sheetCountCollector{- store: store,- }-}--// Collect will collect the metric for sheet.-func (c *sheetCountCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {- var res []*metric.Metric-- sheetCountMetricList, err := c.store.CountSheetGroupByRowstatusVisibilitySourceAndType(ctx)- if err != nil {- return nil, err- }-- for _, sheetCountMetric := range sheetCountMetricList {- res = append(res, &metric.Metric{- Name: metricapi.SheetCountMetricName,- Value: sheetCountMetric.Count,- Labels: map[string]any{- "visibility": string(sheetCountMetric.Visibility),- },- })- }-- return res, nil-}diff --git a/backend/metric/collector/user_count.go b/backend/metric/collector/user_count.gonew file mode 100644index 0000000000..cf8d581399--- /dev/null+++ b/backend/metric/collector/user_count.go@@ -0,0 +1,38 @@+package collector++import (+ "context"++ metricapi "github.com/bytebase/bytebase/backend/metric"+ "github.com/bytebase/bytebase/backend/plugin/metric"+ "github.com/bytebase/bytebase/backend/store"+)++var _ metric.Collector = (*userCountCollector)(nil)++// userCountCollector is the metric data collector for user.+type userCountCollector struct {+ store *store.Store+}++// NewUserCountCollector creates a new instance of userCountCollector.+func NewUserCountCollector(store *store.Store) metric.Collector {+ return &userCountCollector{+ store: store,+ }+}++// Collect will collect the metric for user.+func (c *userCountCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {+ count, err := c.store.CountActiveUsers(ctx)+ if err != nil {+ return nil, err+ }++ return []*metric.Metric{+ {+ Name: metricapi.UserCountMetricName,+ Value: count,+ },+ }, nil+}diff --git a/backend/metric/metric.go b/backend/metric/metric.goindex 9e575a44ed..5ff5461481 100644--- a/backend/metric/metric.go+++ b/backend/metric/metric.go@@ -14,12 +14,8 @@ const (IssueCountMetricName metric.Name = "bb.issue.count"// ProjectCountMetricName is the metric name for project count.ProjectCountMetricName metric.Name = "bb.project.count"- // DatabaseCountMetricName is the metric name for database count.- DatabaseCountMetricName metric.Name = "bb.database.count"- // SheetCountMetricName is the metric name for sheet count.- SheetCountMetricName metric.Name = "bb.sheet.count"- // MemberCountMetricName is the metric name for member count.- MemberCountMetricName metric.Name = "bb.member.count"+ // UserCountMetricName is the metric name for user count.+ UserCountMetricName metric.Name = "bb.user.count"// OpenAPIMetricName is the metric name for OpenAPI.OpenAPIMetricName metric.Name = "bb.api.call"// SQLAdviseAPIMetricName is the metric name for SQL check API.@@ -53,9 +49,7 @@ type InstanceCountMetric struct {// IssueCountMetric is the API message for bb.issue.count.type IssueCountMetric struct {- Type api.IssueType- Status api.IssueStatus- Count int+ Count int}// ProjectCountMetric is the API message for project count metric.diff --git a/backend/server/init.go b/backend/server/init.goindex 750f13c20e..93575b3f02 100644--- a/backend/server/init.go+++ b/backend/server/init.go@@ -241,7 +241,6 @@ func (s *Server) initMetricReporter() {metricReporter.Register(metric.InstanceCountMetricName, metriccollector.NewInstanceCountCollector(s.store))metricReporter.Register(metric.IssueCountMetricName, metriccollector.NewIssueCountCollector(s.store))metricReporter.Register(metric.ProjectCountMetricName, metriccollector.NewProjectCountCollector(s.store))- metricReporter.Register(metric.SheetCountMetricName, metriccollector.NewSheetCountCollector(s.store))- metricReporter.Register(metric.MemberCountMetricName, metriccollector.NewMemberCountCollector(s.store))+ metricReporter.Register(metric.UserCountMetricName, metriccollector.NewUserCountCollector(s.store))s.metricReporter = metricReporter}diff --git a/backend/store/stats.go b/backend/store/stats.goindex 2d4f66b954..da31e41662 100644--- a/backend/store/stats.go+++ b/backend/store/stats.go@@ -122,42 +122,27 @@ func (s *Store) CountProjects(ctx context.Context) (int, error) {return count, nil}-// CountIssueGroupByTypeAndStatus counts the number of issue and group by type and status.+// CountIssues counts the number of issues.// Used by the metric collector.-func (s *Store) CountIssueGroupByTypeAndStatus(ctx context.Context) ([]*metric.IssueCountMetric, error) {+func (s *Store) CountIssues(ctx context.Context) (int, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {- return nil, err+ return 0, err}defer tx.Rollback()- rows, err := tx.QueryContext(ctx, `- SELECT type, status, COUNT(*)+ var count int+ if err := tx.QueryRowContext(ctx, `+ SELECT COUNT(1)FROM issueWHERE id > 101- GROUP BY type, status`,- )- if err != nil {- return nil, err- }- defer rows.Close()-- var res []*metric.IssueCountMetric-- for rows.Next() {- var metric metric.IssueCountMetric- if err := rows.Scan(&metric.Type, &metric.Status, &metric.Count); err != nil {- return nil, err- }- res = append(res, &metric)- }- if err := rows.Err(); err != nil {- return nil, err+ `).Scan(&count); err != nil {+ return 0, err}if err := tx.Commit(); err != nil {- return nil, err+ return 0, err}- return res, nil+ return count, nil}// CountInstanceGroupByEngineAndEnvironmentID counts the number of instances and group by engine and environment.