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: get workspace id from setting (#5414)
ecmadaoad9fdc12fc39
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.goindex 06e8f42f4b..33e69cbde4 100644--- a/backend/api/v1/auth_service.go+++ b/backend/api/v1/auth_service.go@@ -163,7 +163,7 @@ func (s *AuthService) CreateUser(ctx context.Context, request *v1pb.CreateUserRe}isFirstUser := user.ID == api.PrincipalIDForFirstUser- s.metricReporter.Report(&metric.Metric{+ s.metricReporter.Report(ctx, &metric.Metric{Name: metricAPI.PrincipalRegistrationMetricName,Value: 1,Labels: map[string]any{diff --git a/backend/api/v1/subscription_service.go b/backend/api/v1/subscription_service.goindex e94222891c..121e865d1e 100644--- a/backend/api/v1/subscription_service.go+++ b/backend/api/v1/subscription_service.go@@ -160,7 +160,7 @@ func (s *SubscriptionService) TrialSubscription(ctx context.Context, request *v1s.licenseService.RefreshCache(ctx)subscription = s.licenseService.LoadSubscription(ctx)currentPlan := subscription.Plan- s.metricReporter.Report(&metric.Metric{+ s.metricReporter.Report(ctx, &metric.Metric{Name: metricAPI.SubscriptionTrialMetricName,Value: 1,Labels: map[string]any{diff --git a/backend/plugin/metric/reporter.go b/backend/plugin/metric/reporter.goindex 1dbd8981b3..e7ec855d3c 100644--- a/backend/plugin/metric/reporter.go+++ b/backend/plugin/metric/reporter.go@@ -3,6 +3,6 @@ package metric// Reporter is the API message for metric reporter.type Reporter interface {Close()- Report(metric *Metric) error+ Report(id string, metric *Metric) errorIdentify(identifier *Identifier) error}diff --git a/backend/plugin/metric/segment/mock.go b/backend/plugin/metric/segment/mock.goindex 2075ef18d4..ffe02a4276 100644--- a/backend/plugin/metric/segment/mock.go+++ b/backend/plugin/metric/segment/mock.go@@ -16,7 +16,7 @@ func (*mockreporter) Close() {}// Report will exec all the segment reporter.-func (*mockreporter) Report(_ *metric.Metric) error {+func (*mockreporter) Report(_ string, _ *metric.Metric) error {return nil}diff --git a/backend/plugin/metric/segment/reporter.go b/backend/plugin/metric/segment/reporter.goindex fc645c610b..b0753fc6cd 100644--- a/backend/plugin/metric/segment/reporter.go+++ b/backend/plugin/metric/segment/reporter.go@@ -13,8 +13,7 @@ var _ metric.Reporter = (*reporter)(nil)// reporter is the metrics collector https://segment.com/.type reporter struct {- identifier string- client analytics.Client+ client analytics.Client}const (@@ -23,12 +22,11 @@ const ()// NewReporter creates a new instance of segment.-func NewReporter(key string, identifier string) metric.Reporter {+func NewReporter(key string) metric.Reporter {client := analytics.New(key)return &reporter{- identifier: identifier,- client: client,+ client: client,}}@@ -38,7 +36,7 @@ func (r *reporter) Close() {}// Report will exec all the segment reporter.-func (r *reporter) Report(metric *metric.Metric) error {+func (r *reporter) Report(id string, metric *metric.Metric) error {properties := analytics.NewProperties().Set(metricValueField, metric.Value)@@ -48,7 +46,7 @@ func (r *reporter) Report(metric *metric.Metric) error {return r.client.Enqueue(analytics.Track{Event: string(metric.Name),- UserId: r.identifier,+ UserId: id,Properties: properties,Timestamp: time.Now().UTC(),})@@ -64,7 +62,7 @@ func (r *reporter) Identify(identifier *metric.Identifier) error {}return r.client.Enqueue(analytics.Identify{- UserId: r.identifier,+ UserId: identifier.ID,Traits: traits,Timestamp: time.Now().UTC(),})diff --git a/backend/runner/metricreport/reporter.go b/backend/runner/metricreport/reporter.goindex 3a63f03e9a..bcf919e9f1 100644--- a/backend/runner/metricreport/reporter.go+++ b/backend/runner/metricreport/reporter.go@@ -36,18 +36,17 @@ const (type Reporter struct {licenseService enterpriseAPI.LicenseService// Version is the bytebase's version- version string- workspaceID string- reporter metric.Reporter- collectors map[string]metric.Collector- store *store.Store+ version string+ reporter metric.Reporter+ collectors map[string]metric.Collector+ store *store.Store}// NewReporter creates a new metric scheduler.-func NewReporter(store *store.Store, licenseService enterpriseAPI.LicenseService, profile config.Profile, workspaceID string, enabled bool) *Reporter {+func NewReporter(store *store.Store, licenseService enterpriseAPI.LicenseService, profile config.Profile, enabled bool) *Reporter {var r metric.Reporterif enabled {- r = segment.NewReporter(profile.MetricConnectionKey, workspaceID)+ r = segment.NewReporter(profile.MetricConnectionKey)} else {r = segment.NewMockReporter()}@@ -55,7 +54,6 @@ func NewReporter(store *store.Store, licenseService enterpriseAPI.LicenseServicereturn &Reporter{licenseService: licenseService,version: profile.Version,- workspaceID: workspaceID,reporter: r,collectors: make(map[string]metric.Collector),store: store,@@ -73,7 +71,7 @@ func (m *Reporter) Run(ctx context.Context, wg *sync.WaitGroup) {for {select {case <-ticker.C:- func() {+ go func() {defer func() {if r := recover(); r != nil {err, ok := r.(error)@@ -86,7 +84,12 @@ func (m *Reporter) 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)+ workspaceID, err := m.identify(ctx)+ if err != nil {+ log.Error("failed to report identifier", zap.Error(err))+ return+ }+for name, collector := range m.collectors {log.Debug("Run metric collector", zap.String("collector", name))@@ -101,7 +104,7 @@ func (m *Reporter) Run(ctx context.Context, wg *sync.WaitGroup) {}for _, metric := range metricList {- m.Report(metric)+ m.reportMetric(workspaceID, metric)}}}()@@ -121,8 +124,36 @@ func (m *Reporter) Register(metricName metric.Name, collector metric.Collector)m.collectors[string(metricName)] = collector}+func (m *Reporter) getWorkspaceID(ctx context.Context) (string, error) {+ settingName := api.SettingWorkspaceID+ setting, err := m.store.GetSettingV2(ctx, &store.FindSettingMessage{+ Name: &settingName,+ })+ if err != nil {+ return "", errors.Wrapf(err, "failed to get setting %s", settingName)+ }+ if setting == nil {+ return "", errors.Errorf("cannot find setting %v", settingName)+ }+ return setting.Value, nil+}++func (m *Reporter) reportMetric(id string, metric *metric.Metric) {+ if err := m.reporter.Report(id, metric); err != nil {+ log.Error(+ "Failed to report metric",+ zap.String("metric", string(metric.Name)),+ zap.Error(err),+ )+ }+}+// Identify will identify the workspace and update the subscription plan.-func (m *Reporter) identify(ctx context.Context) {+func (m *Reporter) identify(ctx context.Context) (string, error) {+ workspaceID, err := m.getWorkspaceID(ctx)+ if err != nil {+ return "", err+ }subscription := m.licenseService.LoadSubscription(ctx)plan := subscription.Plan.String()orgID := subscription.OrgID@@ -140,7 +171,7 @@ func (m *Reporter) identify(ctx context.Context) {}if err := m.reporter.Identify(&metric.Identifier{- ID: m.workspaceID,+ ID: workspaceID,Email: email,Name: name,Labels: map[string]string{@@ -150,17 +181,20 @@ func (m *Reporter) identify(ctx context.Context) {identifyTraitForOrgName: orgName,},}); err != nil {- log.Debug("reporter identify failed", zap.Error(err))+ return workspaceID, err}++ return workspaceID, nil}// 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",- zap.String("metric", string(metric.Name)),- zap.Error(err),- )- }+func (m *Reporter) Report(ctx context.Context, metric *metric.Metric) {+ go func() {+ workspaceID, err := m.getWorkspaceID(ctx)+ if err != nil {+ log.Error("failed to find the workspace id", zap.Error(err))+ return+ }+ m.reportMetric(workspaceID, metric)+ }()}diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.goindex fe0b7c1f6b..532624d5f4 100644--- a/backend/runner/taskrun/scheduler.go+++ b/backend/runner/taskrun/scheduler.go@@ -342,7 +342,7 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {}}- s.metricReporter.Report(&metric.Metric{+ s.metricReporter.Report(ctx, &metric.Metric{Name: metricAPI.TaskStatusMetricName,Value: 1,Labels: map[string]any{diff --git a/backend/server/issue.go b/backend/server/issue.goindex ced5532ae7..f3fbf80fdc 100644--- a/backend/server/issue.go+++ b/backend/server/issue.go@@ -44,7 +44,7 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create issue").SetInternal(err)}- s.MetricReporter.Report(&metric.Metric{+ s.MetricReporter.Report(ctx, &metric.Metric{Name: metricAPI.IssueCreateMetricName,Value: 1,Labels: map[string]any{diff --git a/backend/server/openapi_metric.go b/backend/server/openapi_metric.goindex 724eabc661..345d99c64b 100644--- a/backend/server/openapi_metric.go+++ b/backend/server/openapi_metric.go@@ -19,8 +19,9 @@ func openAPIMetricMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFuncrequestMethod := c.Request().MethodrequestPath := c.Path()responseCode := c.Response().Status+ ctx := c.Request().Context()- s.MetricReporter.Report(&metric.Metric{+ s.MetricReporter.Report(ctx, &metric.Metric{Name: metricAPI.OpenAPIMetricName,Value: 1,Labels: map[string]any{diff --git a/backend/server/openapi_sql.go b/backend/server/openapi_sql.goindex 840c9a530f..01c89c9e3a 100644--- a/backend/server/openapi_sql.go+++ b/backend/server/openapi_sql.go@@ -175,7 +175,7 @@ func (s *Server) sqlCheckController(c echo.Context) error {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to run sql check").SetInternal(err)}- s.MetricReporter.Report(&metric.Metric{+ s.MetricReporter.Report(ctx, &metric.Metric{Name: metricAPI.SQLAdviseAPIMetricName,Value: 1,Labels: map[string]any{diff --git a/backend/server/principal.go b/backend/server/principal.goindex abe94b90b7..0fe0c7ba1f 100644--- a/backend/server/principal.go+++ b/backend/server/principal.go@@ -88,7 +88,7 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {// it's okay to ignore the error to avoid workflow broken.log.Debug("failed to count end users", zap.Error(err))}- s.MetricReporter.Report(&metric.Metric{+ s.MetricReporter.Report(ctx, &metric.Metric{Name: metricAPI.PrincipalCreateMetricName,Value: 1,Labels: map[string]any{diff --git a/backend/server/server.go b/backend/server/server.goindex d11eecafaf..e4b136f6c4 100644--- a/backend/server/server.go+++ b/backend/server/server.go@@ -364,7 +364,9 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {return}s.profile.LastActiveTs = time.Now().Unix()- s.MetricReporter.Report(&metricPlugin.Metric{+ ctx := c.Request().Context()++ s.MetricReporter.Report(ctx, &metricPlugin.Metric{Name: metric.APIRequestMetricName,Value: 1,Labels: map[string]any{@@ -392,7 +394,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {s.s3Client = s3Client}- s.MetricReporter = metricreport.NewReporter(s.store, s.licenseService, s.profile, s.GetWorkspaceID(), false)+ s.MetricReporter = metricreport.NewReporter(s.store, s.licenseService, s.profile, false)if !profile.Readonly {s.SchemaSyncer = schemasync.NewSyncer(storeInstance, s.dbFactory, s.stateCfg, profile)// TODO(p0ny): enable Feishu provider only when it is needed.@@ -438,7 +440,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {s.AnomalyScanner = anomaly.NewScanner(storeInstance, s.dbFactory, s.licenseService)// Metric reporter- s.initMetricReporter(config.workspaceID)+ s.initMetricReporter()}// Middleware@@ -649,9 +651,9 @@ func (s *Server) registerOpenAPIRoutes(e *echo.Echo, ce *casbin.Enforcer, prof c}// initMetricReporter will initial the metric scheduler.-func (s *Server) initMetricReporter(workspaceID string) {+func (s *Server) initMetricReporter() {enabled := s.profile.Mode == common.ReleaseModeProd && s.profile.DemoName != "" && !s.profile.DisableMetric- metricReporter := metricreport.NewReporter(s.store, s.licenseService, s.profile, workspaceID, enabled)+ metricReporter := metricreport.NewReporter(s.store, s.licenseService, s.profile, enabled)metricReporter.Register(metric.InstanceCountMetricName, metricCollector.NewInstanceCountCollector(s.store))metricReporter.Register(metric.IssueCountMetricName, metricCollector.NewIssueCountCollector(s.store))metricReporter.Register(metric.ProjectCountMetricName, metricCollector.NewProjectCountCollector(s.store))diff --git a/backend/server/sql.go b/backend/server/sql.goindex d1190f489d..430dbf1e22 100644--- a/backend/server/sql.go+++ b/backend/server/sql.go@@ -558,7 +558,7 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {}}- s.MetricReporter.Report(&metric.Metric{+ s.MetricReporter.Report(ctx, &metric.Metric{Name: metricAPI.SQLEditorExecutionMetricName,Value: 1,Labels: map[string]any{@@ -760,7 +760,7 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {}}- s.MetricReporter.Report(&metric.Metric{+ s.MetricReporter.Report(ctx, &metric.Metric{Name: metricAPI.SQLEditorExecutionMetricName,Value: 1,Labels: map[string]any{diff --git a/backend/sql-server/metric.go b/backend/sql-server/metric.goindex dbb6a2356f..d317e74115 100644--- a/backend/sql-server/metric.go+++ b/backend/sql-server/metric.go@@ -22,7 +22,7 @@ func (m *metricReporter) Report(metric *metric.Metric) {return}- if err := m.reporter.Report(metric); err != nil {+ if err := m.reporter.Report(m.workspaceID, metric); err != nil {log.Error("Failed to report metric",zap.String("metric", string(metricAPI.OpenAPIMetricName)),diff --git a/backend/sql-server/server.go b/backend/sql-server/server.goindex 1f71813225..aa3f10d3d3 100644--- a/backend/sql-server/server.go+++ b/backend/sql-server/server.go@@ -55,7 +55,7 @@ func NewServer(ctx context.Context, prof Profile) (*Server, error) {startedTs: time.Now().Unix(),metricReporter: &metricReporter{workspaceID: prof.WorkspaceID,- reporter: segment.NewReporter(prof.MetricConnectionKey, prof.WorkspaceID),+ reporter: segment.NewReporter(prof.MetricConnectionKey),},}