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: move metric api into the api package (#1339)
ecmadao0b2bf7953bcb
diff --git a/api/metric.go b/api/metric.godeleted file mode 100644index c012ab44ae..0000000000--- a/api/metric.go+++ /dev/null@@ -1,16 +0,0 @@-package api--import "github.com/bytebase/bytebase/plugin/db"--// InstanceCountMetric is the API message for instance count metric-type InstanceCountMetric struct {- Engine db.Type- EnvironmentID int- Count int-}--// IssueCountMetric is the API message for issue count metric-type IssueCountMetric struct {- Type IssueType- Count int-}diff --git a/plugin/metric/collector/instance_count.go b/metric/collector/instance_count.gosimilarity index 70%rename from plugin/metric/collector/instance_count.gorename to metric/collector/instance_count.goindex 9e52afa591..5e960160ee 100644--- a/plugin/metric/collector/instance_count.go+++ b/metric/collector/instance_count.go@@ -4,6 +4,9 @@ import ("context""github.com/bytebase/bytebase/api"+ metricAPI "github.com/bytebase/bytebase/metric"+ "github.com/bytebase/bytebase/plugin/metric"+ "github.com/bytebase/bytebase/plugin/metric/collector""github.com/bytebase/bytebase/store""go.uber.org/zap")@@ -15,7 +18,7 @@ type instanceCollector struct {}// NewInstanceCollector creates a new instance of instanceCollector-func NewInstanceCollector(l *zap.Logger, store *store.Store) MetricCollector {+func NewInstanceCollector(l *zap.Logger, store *store.Store) collector.MetricCollector {return &instanceCollector{l: l,store: store,@@ -23,8 +26,8 @@ func NewInstanceCollector(l *zap.Logger, store *store.Store) MetricCollector {}// Collect will collect the netric for instance-func (c *instanceCollector) Collect(ctx context.Context) ([]*Metric, error) {- var res []*Metric+func (c *instanceCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {+ var res []*metric.MetricinstanceCountMetricList, err := c.store.CountInstanceGroupByEngineAndEnvironmentID(ctx, api.Normal)if err != nil {@@ -38,8 +41,8 @@ func (c *instanceCollector) Collect(ctx context.Context) ([]*Metric, error) {continue}- res = append(res, &Metric{- Name: instanceCountMetricName,+ res = append(res, &metric.Metric{+ Name: metricAPI.InstanceCountMetricName,Value: instanceCountMetric.Count,Labels: map[string]string{"engine": string(instanceCountMetric.Engine),diff --git a/plugin/metric/collector/issue_count.go b/metric/collector/issue_count.gosimilarity index 62%rename from plugin/metric/collector/issue_count.gorename to metric/collector/issue_count.goindex 6b634c1d92..7e82aa7636 100644--- a/plugin/metric/collector/issue_count.go+++ b/metric/collector/issue_count.go@@ -3,6 +3,9 @@ package collectorimport ("context"+ metricAPI "github.com/bytebase/bytebase/metric"+ "github.com/bytebase/bytebase/plugin/metric"+ "github.com/bytebase/bytebase/plugin/metric/collector""github.com/bytebase/bytebase/store""go.uber.org/zap")@@ -14,7 +17,7 @@ type issueCollector struct {}// NewIssueCollector creates a new instance of issueCollector-func NewIssueCollector(l *zap.Logger, store *store.Store) MetricCollector {+func NewIssueCollector(l *zap.Logger, store *store.Store) collector.MetricCollector {return &issueCollector{l: l,store: store,@@ -22,8 +25,8 @@ func NewIssueCollector(l *zap.Logger, store *store.Store) MetricCollector {}// Collect will collect the metric for issue-func (c *issueCollector) Collect(ctx context.Context) ([]*Metric, error) {- var res []*Metric+func (c *issueCollector) Collect(ctx context.Context) ([]*metric.Metric, error) {+ var res []*metric.MetricissueCountMetricList, err := c.store.CountIssueGroupByType(ctx)if err != nil {@@ -31,8 +34,8 @@ func (c *issueCollector) Collect(ctx context.Context) ([]*Metric, error) {}for _, issueCountMetric := range issueCountMetricList {- res = append(res, &Metric{- Name: issueCountMetricName,+ res = append(res, &metric.Metric{+ Name: metricAPI.IssueCountMetricName,Value: issueCountMetric.Count,Labels: map[string]string{"type": string(issueCountMetric.Type),diff --git a/metric/metric.go b/metric/metric.gonew file mode 100644index 0000000000..3430fbb7f9--- /dev/null+++ b/metric/metric.go@@ -0,0 +1,27 @@+package metric++import (+ "github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/plugin/db"+ "github.com/bytebase/bytebase/plugin/metric"+)++var (+ // InstanceCountMetricName is the metric name for instance count+ InstanceCountMetricName metric.Name = "bb.instance.count"+ // IssueCountMetricName is the metric name for issue count+ IssueCountMetricName metric.Name = "bb.issue.count"+)++// InstanceCountMetric is the API message for bb.instance.count+type InstanceCountMetric struct {+ Engine db.Type+ EnvironmentID int+ Count int+}++// IssueCountMetric is the API message for bb.issue.count+type IssueCountMetric struct {+ Type api.IssueType+ Count int+}diff --git a/plugin/metric/collector/collector.go b/plugin/metric/collector/collector.goindex 02dc4ed4b8..2d562d24ef 100644--- a/plugin/metric/collector/collector.go+++ b/plugin/metric/collector/collector.go@@ -1,25 +1,12 @@package collector-import "context"+import (+ "context"-// MetricName is the metric name.-type MetricName string--var (- // instanceMetricName is the MetricName for instance count- instanceCountMetricName MetricName = "bb.instance.count"- // issueCountMetricName is the MetricName for issue count- issueCountMetricName MetricName = "bb.issue.count"+ "github.com/bytebase/bytebase/plugin/metric")-// Metric is the API message for metric-type Metric struct {- Name MetricName- Value int- Labels map[string]string-}-// MetricCollector is the API message for metric collector.type MetricCollector interface {- Collect(ctx context.Context) ([]*Metric, error)+ Collect(ctx context.Context) ([]*metric.Metric, error)}diff --git a/plugin/metric/metric.go b/plugin/metric/metric.gonew file mode 100644index 0000000000..34bd985e09--- /dev/null+++ b/plugin/metric/metric.go@@ -0,0 +1,17 @@+package metric++// Name is the metric name.+type Name string++// Metric is the API message for metric.+type Metric struct {+ Name Name+ Value int+ Labels map[string]string+}++// Identifier is the identifier for metric.+type Identifier struct {+ ID string+ Labels map[string]string+}diff --git a/plugin/metric/reporter/reporter.go b/plugin/metric/reporter/reporter.goindex d374b0f8b6..fc92936a22 100644--- a/plugin/metric/reporter/reporter.go+++ b/plugin/metric/reporter/reporter.go@@ -1,13 +1,12 @@package reporterimport (- "github.com/bytebase/bytebase/api"- "github.com/bytebase/bytebase/plugin/metric/collector"+ "github.com/bytebase/bytebase/plugin/metric")// MetricReporter is the API message for metric reporter.type MetricReporter interface {Close()- Report(metric *collector.Metric) error- Identify(workspace *api.Workspace) error+ Report(metric *metric.Metric) error+ Identify(identifier *metric.Identifier) error}diff --git a/plugin/metric/reporter/segment.go b/plugin/metric/reporter/segment.goindex 944ed422db..ac7111bb68 100644--- a/plugin/metric/reporter/segment.go+++ b/plugin/metric/reporter/segment.go@@ -3,8 +3,7 @@ package reporterimport ("time"- "github.com/bytebase/bytebase/api"- "github.com/bytebase/bytebase/plugin/metric/collector"+ "github.com/bytebase/bytebase/plugin/metric""github.com/segmentio/analytics-go""go.uber.org/zap"@@ -18,8 +17,6 @@ type segment struct {}const (- // identifyTraitForPlan is the trait key for subscription plan.- identifyTraitForPlan = "plan"// metricValueField is the property key for valuemetricValueField = "value")@@ -41,7 +38,7 @@ func (s *segment) Close() {}// Report will exec all the segment reporter.-func (s *segment) Report(metric *collector.Metric) error {+func (s *segment) Report(metric *metric.Metric) error {properties := analytics.NewProperties().Set(metricValueField, metric.Value)@@ -58,10 +55,15 @@ func (s *segment) Report(metric *collector.Metric) error {}// Identify will identify the workspace with license.-func (s *segment) Identify(workspace *api.Workspace) error {+func (s *segment) Identify(identifier *metric.Identifier) error {+ traits := analytics.NewTraits()+ for key, value := range identifier.Labels {+ traits.Set(key, value)+ }+return s.client.Enqueue(analytics.Identify{UserId: s.identifier,- Traits: analytics.NewTraits().Set(identifyTraitForPlan, workspace.Plan),+ Traits: traits,Timestamp: time.Now().UTC(),})}diff --git a/server/metric_reporter.go b/server/metric_reporter.goindex 36267ad8e7..8781574e8e 100644--- a/server/metric_reporter.go+++ b/server/metric_reporter.go@@ -3,12 +3,12 @@ package serverimport ("context""fmt"- "reflect""sync""time""github.com/bytebase/bytebase/api"enterpriseAPI "github.com/bytebase/bytebase/enterprise/api"+ metricAPI "github.com/bytebase/bytebase/plugin/metric""github.com/bytebase/bytebase/plugin/metric/collector""github.com/bytebase/bytebase/plugin/metric/reporter"@@ -17,6 +17,8 @@ import (const (metricSchedulerInterval = time.Duration(1) * time.Hour+ // identifyTraitForPlan is the trait key for subscription plan.+ identifyTraitForPlan = "plan")// MetricReporter is the metric reporter.@@ -25,26 +27,22 @@ type MetricReporter struct {// subscription is the pointer to the server.subscription.// the subscription can be updated by users so we need the pointer to get the latest value.- subscription *enterpriseAPI.Subscription- reporter reporter.MetricReporter- workspaceID string- collectorList []collector.MetricCollector+ subscription *enterpriseAPI.Subscription+ workspaceID string+ reporter reporter.MetricReporter+ collectors map[string]collector.MetricCollector}// NewMetricReporter creates a new metric scheduler.func NewMetricReporter(logger *zap.Logger, server *Server, workspaceID string) *MetricReporter {- reporter := reporter.NewSegmentReporter(logger, server.profile.MetricConnectionKey, workspaceID)- collectorList := []collector.MetricCollector{- collector.NewInstanceCollector(logger, server.store),- collector.NewIssueCollector(logger, server.store),- }+ r := reporter.NewSegmentReporter(logger, server.profile.MetricConnectionKey, workspaceID)return &MetricReporter{- l: logger,- subscription: &server.subscription,- workspaceID: workspaceID,- reporter: reporter,- collectorList: collectorList,+ l: logger,+ subscription: &server.subscription,+ workspaceID: workspaceID,+ reporter: r,+ collectors: make(map[string]collector.MetricCollector),}}@@ -74,19 +72,19 @@ func (m *MetricReporter) Run(ctx context.Context, wg *sync.WaitGroup) {}()ctx := context.Background()- for _, collector := range m.collectorList {- collectorName := reflect.TypeOf(collector).String()- m.l.Debug("Run metric collector", zap.String("collector", collectorName))+ for name, collector := range m.collectors {+ m.l.Debug("Run metric collector", zap.String("collector", name))metricList, err := collector.Collect(ctx)if err != nil {m.l.Error("Failed to collect metric",- zap.String("collector", collectorName),+ zap.String("collector", name),zap.Error(err),)continue}+for _, metric := range metricList {if err := m.reporter.Report(metric); err != nil {m.l.Error(@@ -109,15 +107,22 @@ func (m *MetricReporter) Close() {m.reporter.Close()}+// Register will register a metric collector.+func (m *MetricReporter) Register(metricName metricAPI.Name, collector collector.MetricCollector) {+ m.collectors[string(metricName)] = collector+}+// Identify will identify the workspace and update the subscription plan.func (m *MetricReporter) identify() {plan := api.FREE.String()if m.subscription != nil {plan = m.subscription.Plan.String()}- if err := m.reporter.Identify(&api.Workspace{- Plan: plan,- ID: m.workspaceID,+ if err := m.reporter.Identify(&metricAPI.Identifier{+ ID: m.workspaceID,+ Labels: map[string]string{+ identifyTraitForPlan: plan,+ },}); err != nil {m.l.Debug("reporter identify failed", zap.Error(err))}diff --git a/server/server.go b/server/server.goindex 5fe8719cda..2a5f8328b0 100644--- a/server/server.go+++ b/server/server.go@@ -10,6 +10,8 @@ import ("time""github.com/bytebase/bytebase/common"+ "github.com/bytebase/bytebase/metric"+ metricCollector "github.com/bytebase/bytebase/metric/collector""github.com/bytebase/bytebase/store""github.com/google/uuid"@@ -315,6 +317,8 @@ func (server *Server) initMetricReporter(workspaceID string) {enabled := server.profile.Mode == common.ReleaseModeProd && !server.profile.Demoif enabled {metricReporter := NewMetricReporter(server.l, server, workspaceID)+ metricReporter.Register(metric.InstanceCountMetricName, metricCollector.NewInstanceCollector(server.l, server.store))+ metricReporter.Register(metric.IssueCountMetricName, metricCollector.NewIssueCollector(server.l, server.store))server.MetricReporter = metricReporter}}diff --git a/store/instance.go b/store/instance.goindex 63fd854e08..d5dff9ca07 100644--- a/store/instance.go+++ b/store/instance.go@@ -8,6 +8,7 @@ import ("github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/common"+ "github.com/bytebase/bytebase/metric""github.com/bytebase/bytebase/plugin/db")@@ -154,7 +155,7 @@ func (s *Store) CountInstance(ctx context.Context, find *api.InstanceFind) (int,// CountInstanceGroupByEngineAndEnvironmentID counts the number of instances and group by engine and environment_id.// Used by the metric collector.-func (s *Store) CountInstanceGroupByEngineAndEnvironmentID(ctx context.Context, rowStatus api.RowStatus) ([]*api.InstanceCountMetric, error) {+func (s *Store) CountInstanceGroupByEngineAndEnvironmentID(ctx context.Context, rowStatus api.RowStatus) ([]*metric.InstanceCountMetric, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -173,10 +174,10 @@ func (s *Store) CountInstanceGroupByEngineAndEnvironmentID(ctx context.Context,}defer rows.Close()- var res []*api.InstanceCountMetric+ var res []*metric.InstanceCountMetricfor rows.Next() {- var metric api.InstanceCountMetric+ var metric metric.InstanceCountMetricif err := rows.Scan(&metric.Engine, &metric.EnvironmentID, &metric.Count); err != nil {return nil, FormatError(err)}diff --git a/store/issue.go b/store/issue.goindex aa6900c85b..777efeb141 100644--- a/store/issue.go+++ b/store/issue.go@@ -11,6 +11,7 @@ import ("github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/common"+ "github.com/bytebase/bytebase/metric")// issueRaw is the store model for an Issue.@@ -142,7 +143,7 @@ func (s *Store) PatchIssue(ctx context.Context, patch *api.IssuePatch) (*api.Iss// CountIssueGroupByType counts the number of issue and group by type.// Used by the metric collector.-func (s *Store) CountIssueGroupByType(ctx context.Context) ([]*api.IssueCountMetric, error) {+func (s *Store) CountIssueGroupByType(ctx context.Context) ([]*metric.IssueCountMetric, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -159,10 +160,10 @@ func (s *Store) CountIssueGroupByType(ctx context.Context) ([]*api.IssueCountMet}defer rows.Close()- var res []*api.IssueCountMetric+ var res []*metric.IssueCountMetricfor rows.Next() {- var metric api.IssueCountMetric+ var metric metric.IssueCountMetricif err := rows.Scan(&metric.Type, &metric.Count); err != nil {return nil, FormatError(err)}