Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: move metric reporting to a package (#3726)
Danny Xu413752a2bb08
diff --git a/api/principal.go b/api/principal.goindex daa4da0ca0..9df9c0d3f0 100644--- a/api/principal.go+++ b/api/principal.go@@ -20,6 +20,9 @@ const (// SystemBot is the principal type for SYSTEM_BOT.// SystemBot represents the internal system bot performing operations.SystemBot PrincipalType = "SYSTEM_BOT"++ // PrincipalIDForFirstUser is the principal id for the first user in workspace.+ PrincipalIDForFirstUser = 101)// PrincipalAuthProvider is the type of an authentication provider.diff --git a/server/auth.go b/server/auth.goindex b5dbdf6253..92a5d4ff4d 100644--- a/server/auth.go+++ b/server/auth.go@@ -212,8 +212,8 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate access token").SetInternal(err)}- if user.ID == principalIDForFirstUser && s.MetricReporter != nil {- s.MetricReporter.report(&metric.Metric{+ if user.ID == api.PrincipalIDForFirstUser && s.MetricReporter != nil {+ s.MetricReporter.Report(&metric.Metric{Name: metricAPI.FirstPrincipalMetricName,Value: 1,Labels: map[string]interface{}{diff --git a/server/config.go b/server/config.godeleted file mode 100644index 9f63512acb..0000000000--- a/server/config.go+++ /dev/null@@ -1,6 +0,0 @@-package server--const (- // secretLength is the length for the secret used to sign the JWT auto token.- secretLength = 32-)diff --git a/server/openapi_metric.go b/server/openapi_metric.goindex 88cf147b8c..d6abee691d 100644--- a/server/openapi_metric.go+++ b/server/openapi_metric.go@@ -21,7 +21,7 @@ func openAPIMetricMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFuncresponseCode := c.Response().Statusif s.MetricReporter != nil {- s.MetricReporter.report(&metric.Metric{+ s.MetricReporter.Report(&metric.Metric{Name: metricAPI.OpenAPIMetricName,Value: 1,Labels: map[string]interface{}{diff --git a/server/openapi_sql.go b/server/openapi_sql.goindex e3a85b1d32..13eee42e2c 100644--- a/server/openapi_sql.go+++ b/server/openapi_sql.go@@ -154,7 +154,7 @@ func (s *Server) sqlCheckController(c echo.Context) error {}if s.MetricReporter != nil {- s.MetricReporter.report(&metric.Metric{+ s.MetricReporter.Report(&metric.Metric{Name: metricAPI.SQLAdviseAPIMetricName,Value: 1,Labels: map[string]interface{}{diff --git a/server/metric_reporter.go b/server/runner/metricreport/reporter.gosimilarity index 79%rename from server/metric_reporter.gorename to server/runner/metricreport/reporter.goindex 2277735331..a3d39fd88b 100644--- a/server/metric_reporter.go+++ b/server/runner/metricreport/reporter.go@@ -1,4 +1,5 @@-package server+// Package metricreport is a runner reporting metrics.+package metricreportimport ("context"@@ -8,6 +9,7 @@ import ("github.com/pkg/errors"+ "github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/common/log"enterpriseAPI "github.com/bytebase/bytebase/enterprise/api""github.com/bytebase/bytebase/plugin/metric"@@ -28,12 +30,10 @@ const (identifyTraitForOrgName = "org_name"// identifyTraitForVersion is the trait key for Bytebase version.identifyTraitForVersion = "version"- // principalIDForFirstUser is the principal id for the first user in workspace.- principalIDForFirstUser = 101)-// MetricReporter is the metric reporter.-type MetricReporter struct {+// Reporter is the metric reporter.+type Reporter struct {licenseService enterpriseAPI.LicenseService// Version is the bytebase's versionversion string@@ -43,11 +43,11 @@ type MetricReporter struct {store *store.Store}-// NewMetricReporter creates a new metric scheduler.-func NewMetricReporter(store *store.Store, licenseService enterpriseAPI.LicenseService, profile config.Profile, workspaceID string) *MetricReporter {+// NewReporter creates a new metric scheduler.+func NewReporter(store *store.Store, licenseService enterpriseAPI.LicenseService, profile config.Profile, workspaceID string) *Reporter {r := segment.NewReporter(profile.MetricConnectionKey, workspaceID)- return &MetricReporter{+ return &Reporter{licenseService: licenseService,version: profile.Version,workspaceID: workspaceID,@@ -58,7 +58,7 @@ func NewMetricReporter(store *store.Store, licenseService enterpriseAPI.LicenseS}// Run will run the metric reporter.-func (m *MetricReporter) Run(ctx context.Context, wg *sync.WaitGroup) {+func (m *Reporter) Run(ctx context.Context, wg *sync.WaitGroup) {ticker := time.NewTicker(metricSchedulerInterval)defer ticker.Stop()defer wg.Done()@@ -81,7 +81,7 @@ func (m *MetricReporter) Run(ctx context.Context, wg *sync.WaitGroup) {ctx := context.Background()// identify will be triggered in every schedule loop so that we can update the latest workspace profile such as subscription plan.- m.identify(ctx)+ m.Identify(ctx)for name, collector := range m.collectors {log.Debug("Run metric collector", zap.String("collector", name))@@ -96,7 +96,7 @@ func (m *MetricReporter) Run(ctx context.Context, wg *sync.WaitGroup) {}for _, metric := range metricList {- m.report(metric)+ m.Report(metric)}}}()@@ -107,25 +107,25 @@ func (m *MetricReporter) Run(ctx context.Context, wg *sync.WaitGroup) {}// Close will close the metric reporter.-func (m *MetricReporter) Close() {+func (m *Reporter) Close() {m.reporter.Close()}// Register will register a metric collector.-func (m *MetricReporter) Register(metricName metric.Name, collector metric.Collector) {+func (m *Reporter) Register(metricName metric.Name, collector metric.Collector) {m.collectors[string(metricName)] = collector}// Identify will identify the workspace and update the subscription plan.-func (m *MetricReporter) identify(ctx context.Context) {+func (m *Reporter) Identify(ctx context.Context) {subscription := m.licenseService.LoadSubscription(ctx)plan := subscription.Plan.String()orgID := subscription.OrgIDorgName := subscription.OrgName- principal, err := m.store.GetPrincipalByID(ctx, principalIDForFirstUser)+ principal, err := m.store.GetPrincipalByID(ctx, api.PrincipalIDForFirstUser)if err != nil {- log.Debug("unable to get the first principal user", zap.Int("id", principalIDForFirstUser), zap.Error(err))+ log.Debug("unable to get the first principal user", zap.Int("id", api.PrincipalIDForFirstUser), zap.Error(err))}email := ""if principal != nil {@@ -147,7 +147,8 @@ func (m *MetricReporter) identify(ctx context.Context) {}}-func (m *MetricReporter) report(metric *metric.Metric) {+// Report will report a metric.+func (m *Reporter) Report(metric *metric.Metric) {if err := m.reporter.Report(metric); err != nil {log.Error("Failed to report metric",diff --git a/server/server.go b/server/server.goindex b9ed5546b6..a2e62649cb 100644--- a/server/server.go+++ b/server/server.go@@ -42,6 +42,7 @@ import ("github.com/bytebase/bytebase/server/runner/anomaly""github.com/bytebase/bytebase/server/runner/apprun""github.com/bytebase/bytebase/server/runner/backuprun"+ "github.com/bytebase/bytebase/server/runner/metricreport""github.com/bytebase/bytebase/server/runner/rollbackrun""github.com/bytebase/bytebase/server/runner/schemasync""github.com/bytebase/bytebase/server/runner/taskcheck"@@ -93,7 +94,7 @@ type Server struct {// Asynchronous runners.TaskScheduler *TaskSchedulerTaskCheckScheduler *taskcheck.Scheduler- MetricReporter *MetricReporter+ MetricReporter *metricreport.ReporterSchemaSyncer *schemasync.SyncerBackupRunner *backuprun.RunnerAnomalyScanner *anomaly.Scanner@@ -434,7 +435,7 @@ func (s *Server) registerOpenAPIRoutes(e *echo.Echo, ce *casbin.Enforcer, prof cfunc (s *Server) initMetricReporter(workspaceID string) {enabled := s.profile.Mode == common.ReleaseModeProd && !s.profile.Demo && !s.profile.DisableMetricif enabled {- metricReporter := NewMetricReporter(s.store, s.licenseService, s.profile, workspaceID)+ metricReporter := metricreport.NewReporter(s.store, s.licenseService, s.profile, workspaceID)metricReporter.Register(metric.InstanceCountMetricName, metricCollector.NewInstanceCountCollector(s.store))metricReporter.Register(metric.IssueCountMetricName, metricCollector.NewIssueCountCollector(s.store))metricReporter.Register(metric.ProjectCountMetricName, metricCollector.NewProjectCountCollector(s.store))@@ -456,6 +457,9 @@ type workspaceConfig struct {}func getInitSetting(ctx context.Context, store *store.Store) (*workspaceConfig, error) {+ // secretLength is the length for the secret used to sign the JWT auto token.+ const secretLength = 32+// initial brandingif _, _, err := store.CreateSettingIfNotExist(ctx, &api.SettingCreate{CreatorID: api.SystemBotID,diff --git a/server/subscription.go b/server/subscription.goindex 7285d57102..ed28f84552 100644--- a/server/subscription.go+++ b/server/subscription.go@@ -138,7 +138,7 @@ func (s *Server) registerSubscriptionRoutes(g *echo.Group) {subscription = s.licenseService.LoadSubscription(ctx)currentPlan := subscription.Planif s.MetricReporter != nil {- s.MetricReporter.report(&metric.Metric{+ s.MetricReporter.Report(&metric.Metric{Name: metricAPI.SubscriptionTrialMetricName,Value: 1,Labels: map[string]interface{}{