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: update metric (#4705)
ecmadaob6436a1a58ac
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.goindex 1171fcaf11..9218b990ad 100644--- a/backend/api/v1/auth_service.go+++ b/backend/api/v1/auth_service.go@@ -145,14 +145,17 @@ func (s *AuthService) CreateUser(ctx context.Context, request *v1pb.CreateUserRereturn nil, err}- if user.ID == api.PrincipalIDForFirstUser && s.metricReporter != nil {+ if s.metricReporter != nil {+ isFirstUser := user.ID == api.PrincipalIDForFirstUsers.metricReporter.Report(&metric.Metric{- Name: metricAPI.FirstPrincipalMetricName,+ Name: metricAPI.PrincipalRegistrationMetricName,Value: 1,Labels: map[string]interface{}{- "email": user.Email,- "name": user.Name,- "lark_notified": false,+ "email": user.Email,+ "name": user.Name,+ // We only send lark notification for the first principal registration.+ // false means do not notify upfront. Later the notification will be triggered by the scheduler.+ "lark_notified": !isFirstUser,},})}diff --git a/backend/metric/metric.go b/backend/metric/metric.goindex c132ce5342..2cf9bda7fb 100644--- a/backend/metric/metric.go+++ b/backend/metric/metric.go@@ -30,8 +30,16 @@ const (SQLAdviseAPIMetricName metric.Name = "bb.api.sql.advise"// SubscriptionTrialMetricName is the metric name for trial.SubscriptionTrialMetricName metric.Name = "bb.subscription.trial"- // FirstPrincipalMetricName is the metric name for the first principal.- FirstPrincipalMetricName metric.Name = "bb.principal.registration"+ // PrincipalRegistrationMetricName is the metric name for the principal registration event.+ PrincipalRegistrationMetricName metric.Name = "bb.principal.registration"+ // IssueCreateMetricName is the metric name for issue creation event.+ IssueCreateMetricName metric.Name = "bb.issue.create"+ // TaskStatusMetricName is the metric name for task status update event.+ TaskStatusMetricName metric.Name = "bb.task.status"+ // SQLEditorExecutionMetricName is the metric name for SQL Editor execution event.+ SQLEditorExecutionMetricName metric.Name = "bb.sql-editor.execute"+ // PrincipalCreateMetricName is the metric name for principal creation event.+ PrincipalCreateMetricName metric.Name = "bb.principal.create")// InstanceCountMetric is the API message for bb.instance.count.diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.goindex f20e282350..6efd8fb53c 100644--- a/backend/runner/taskrun/scheduler.go+++ b/backend/runner/taskrun/scheduler.go@@ -22,7 +22,10 @@ import ("github.com/bytebase/bytebase/backend/component/state"enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"api "github.com/bytebase/bytebase/backend/legacyapi"+ metricAPI "github.com/bytebase/bytebase/backend/metric"+ "github.com/bytebase/bytebase/backend/plugin/metric""github.com/bytebase/bytebase/backend/runner/apprun"+ "github.com/bytebase/bytebase/backend/runner/metricreport""github.com/bytebase/bytebase/backend/runner/schemasync""github.com/bytebase/bytebase/backend/store""github.com/bytebase/bytebase/backend/utils"@@ -56,7 +59,15 @@ var ()// NewScheduler creates a new task scheduler.-func NewScheduler(store *store.Store, applicationRunner *apprun.Runner, schemaSyncer *schemasync.Syncer, activityManager *activity.Manager, licenseService enterpriseAPI.LicenseService, stateCfg *state.State, profile config.Profile) *Scheduler {+func NewScheduler(+ store *store.Store,+ applicationRunner *apprun.Runner,+ schemaSyncer *schemasync.Syncer,+ activityManager *activity.Manager,+ licenseService enterpriseAPI.LicenseService,+ stateCfg *state.State,+ profile config.Profile,+ metricReporter *metricreport.Reporter) *Scheduler {return &Scheduler{store: store,applicationRunner: applicationRunner,@@ -66,6 +77,7 @@ func NewScheduler(store *store.Store, applicationRunner *apprun.Runner, schemaSyprofile: profile,stateCfg: stateCfg,executorMap: make(map[api.TaskType]Executor),+ metricReporter: metricReporter,}}@@ -79,6 +91,7 @@ type Scheduler struct {stateCfg *state.Stateprofile config.ProfileexecutorMap map[api.TaskType]Executor+ metricReporter *metricreport.Reporter}// Register will register a task executor factory.@@ -328,6 +341,17 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {}}}++ if s.metricReporter != nil {+ s.metricReporter.Report(&metric.Metric{+ Name: metricAPI.TaskStatusMetricName,+ Value: 1,+ Labels: map[string]interface{}{+ "type": task.Type,+ "value": taskStatusPatch.Status,+ },+ })+ }}return}diff --git a/backend/server/issue.go b/backend/server/issue.goindex 93d222a011..6b195b3d6f 100644--- a/backend/server/issue.go+++ b/backend/server/issue.go@@ -20,8 +20,10 @@ import ("github.com/bytebase/bytebase/backend/common/log""github.com/bytebase/bytebase/backend/component/activity"api "github.com/bytebase/bytebase/backend/legacyapi"+ metricAPI "github.com/bytebase/bytebase/backend/metric""github.com/bytebase/bytebase/backend/plugin/db""github.com/bytebase/bytebase/backend/plugin/db/util"+ "github.com/bytebase/bytebase/backend/plugin/metric""github.com/bytebase/bytebase/backend/plugin/vcs""github.com/bytebase/bytebase/backend/store""github.com/bytebase/bytebase/backend/utils"@@ -40,6 +42,16 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create issue").SetInternal(err)}+ if s.MetricReporter != nil {+ s.MetricReporter.Report(&metric.Metric{+ Name: metricAPI.IssueCreateMetricName,+ Value: 1,+ Labels: map[string]interface{}{+ "type": issue.Type,+ },+ })+ }+c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)if err := jsonapi.MarshalPayload(c.Response().Writer, issue); err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create issue response").SetInternal(err)@@ -336,6 +348,7 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {if err != nil {return err}+c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)if err := jsonapi.MarshalPayload(c.Response().Writer, updatedComposedIssue); err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal issue ID response: %v", id)).SetInternal(err)diff --git a/backend/server/principal.go b/backend/server/principal.goindex 7054f6b8ec..08f96d8ff5 100644--- a/backend/server/principal.go+++ b/backend/server/principal.go@@ -8,10 +8,14 @@ import ("github.com/google/jsonapi""github.com/labstack/echo/v4"+ "go.uber.org/zap""golang.org/x/crypto/bcrypt""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/log"api "github.com/bytebase/bytebase/backend/legacyapi"+ metricAPI "github.com/bytebase/bytebase/backend/metric"+ "github.com/bytebase/bytebase/backend/plugin/metric""github.com/bytebase/bytebase/backend/store")@@ -66,6 +70,23 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {principal.ServiceKey = password}+ if s.MetricReporter != nil {+ count, err := s.store.CountUsers(ctx, api.EndUser)+ if err != nil {+ // 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{+ Name: metricAPI.PrincipalCreateMetricName,+ Value: 1,+ Labels: map[string]interface{}{+ "type": principal.Type,+ "role": principal.Role,+ "rank": count,+ },+ })+ }+c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)if err := jsonapi.MarshalPayload(c.Response().Writer, principal); err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create principal response").SetInternal(err)diff --git a/backend/server/server.go b/backend/server/server.goindex 4a8193aaab..c0510278d9 100644--- a/backend/server/server.go+++ b/backend/server/server.go@@ -340,7 +340,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {s.BackupRunner = backuprun.NewRunner(storeInstance, s.dbFactory, s.s3Client, s.stateCfg, &profile)s.RollbackRunner = rollbackrun.NewRunner(storeInstance, s.dbFactory, s.stateCfg)- s.TaskScheduler = taskrun.NewScheduler(storeInstance, s.ApplicationRunner, s.SchemaSyncer, s.ActivityManager, s.licenseService, s.stateCfg, profile)+ s.TaskScheduler = taskrun.NewScheduler(storeInstance, s.ApplicationRunner, s.SchemaSyncer, s.ActivityManager, s.licenseService, s.stateCfg, profile, s.MetricReporter)s.TaskScheduler.Register(api.TaskGeneral, taskrun.NewDefaultExecutor())s.TaskScheduler.Register(api.TaskDatabaseCreate, taskrun.NewDatabaseCreateExecutor(storeInstance, s.dbFactory, s.SchemaSyncer, profile))s.TaskScheduler.Register(api.TaskDatabaseSchemaBaseline, taskrun.NewSchemaBaselineExecutor(storeInstance, s.dbFactory, s.ActivityManager, s.licenseService, s.stateCfg, s.SchemaSyncer, profile))diff --git a/backend/server/sql.go b/backend/server/sql.goindex b0d6a0f24d..7fe07e4787 100644--- a/backend/server/sql.go+++ b/backend/server/sql.go@@ -19,11 +19,13 @@ import ("github.com/bytebase/bytebase/backend/common/log""github.com/bytebase/bytebase/backend/component/activity"api "github.com/bytebase/bytebase/backend/legacyapi"+ metricAPI "github.com/bytebase/bytebase/backend/metric""github.com/bytebase/bytebase/backend/plugin/advisor""github.com/bytebase/bytebase/backend/plugin/advisor/catalog"advisorDB "github.com/bytebase/bytebase/backend/plugin/advisor/db""github.com/bytebase/bytebase/backend/plugin/db""github.com/bytebase/bytebase/backend/plugin/db/util"+ "github.com/bytebase/bytebase/backend/plugin/metric""github.com/bytebase/bytebase/backend/plugin/parser""github.com/bytebase/bytebase/backend/plugin/parser/ast""github.com/bytebase/bytebase/backend/store"@@ -491,6 +493,18 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {}}+ if s.MetricReporter != nil {+ s.MetricReporter.Report(&metric.Metric{+ Name: metricAPI.SQLEditorExecutionMetricName,+ Value: 1,+ Labels: map[string]interface{}{+ "engine": instance.Engine,+ "readonly": exec.Readonly,+ "admin_mode": false,+ },+ })+ }+c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)if err := jsonapi.MarshalPayload(c.Response().Writer, resultSet); err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal sql result set response").SetInternal(err)@@ -594,6 +608,18 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {}}+ if s.MetricReporter != nil {+ s.MetricReporter.Report(&metric.Metric{+ Name: metricAPI.SQLEditorExecutionMetricName,+ Value: 1,+ Labels: map[string]interface{}{+ "engine": instance.Engine,+ "readonly": exec.Readonly,+ "admin_mode": true,+ },+ })+ }+c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)if err := jsonapi.MarshalPayload(c.Response().Writer, resultSet); err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal sql result set response").SetInternal(err)diff --git a/backend/store/metric.go b/backend/store/metric.gonew file mode 100644index 0000000000..bd5b8ee63c--- /dev/null+++ b/backend/store/metric.go@@ -0,0 +1,32 @@+package store++import (+ "context"++ api "github.com/bytebase/bytebase/backend/legacyapi"+)++// CountUsers counts the principal.+func (s *Store) CountUsers(ctx context.Context, userType api.PrincipalType) (int, error) {+ tx, err := s.db.BeginTx(ctx, nil)+ if err != nil {+ return 0, FormatError(err)+ }+ defer tx.Rollback()++ count := 0++ if err := tx.QueryRowContext(ctx, `+ SELECT COUNT(*)+ FROM principal+ WHERE principal.type = $1`,+ userType).Scan(&count); err != nil {+ return 0, FormatError(err)+ }++ if err := tx.Commit(); err != nil {+ return 0, err+ }++ return count, nil+}diff --git a/backend/store/principal.go b/backend/store/principal.goindex 8fa88902ca..a388bbb16d 100644--- a/backend/store/principal.go+++ b/backend/store/principal.go@@ -79,6 +79,7 @@ type FindUserMessage struct {Email *stringRole *api.RoleShowDeleted bool+ Type *api.PrincipalType// IdentityProviderResourceID is the name of the identity provider related with the user.// If set with empty string, then only those users that are not from the idp will be found.IdentityProviderResourceID *string@@ -198,6 +199,9 @@ func (s *Store) listUserImpl(ctx context.Context, tx *Tx, find *FindUserMessage)if v := find.Email; v != nil {where, args = append(where, fmt.Sprintf("principal.email = $%d", len(args)+1)), append(args, *v)}+ if v := find.Type; v != nil {+ where, args = append(where, fmt.Sprintf("principal.type = $%d", len(args)+1)), append(args, *v)+ }if v := find.Role; v != nil {where, args = append(where, fmt.Sprintf("member.role = $%d", len(args)+1)), append(args, *v)}