Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql: use tx with create alert #3776

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions alert/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func (s *Store) UpdateManyAlertStatus(ctx context.Context, status Status, alertI
return updatedIDs, nil
}

func (s *Store) Create(ctx context.Context, a *Alert) (*Alert, error) {
func (s *Store) CreateTx(ctx context.Context, tx *sql.Tx, a *Alert) (*Alert, error) {
n, err := a.Normalize() // validation
if err != nil {
return nil, err
Expand All @@ -489,12 +489,6 @@ func (s *Store) Create(ctx context.Context, a *Alert) (*Alert, error) {
return nil, err
}

tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer sqlutil.Rollback(ctx, "alert: create", tx)

_, err = tx.StmtContext(ctx, s.lockSvc).ExecContext(ctx, n.ServiceID)
if err != nil {
return nil, err
Expand All @@ -507,11 +501,6 @@ func (s *Store) Create(ctx context.Context, a *Alert) (*Alert, error) {

s.logDB.MustLogTx(ctx, tx, n.ID, alertlog.TypeCreated, meta)

err = tx.Commit()
if err != nil {
return nil, err
}

ctx = log.WithFields(ctx, log.Fields{"AlertID": n.ID, "ServiceID": n.ServiceID})
log.Logf(ctx, "Alert created.")
metricCreatedTotal.WithLabelValues(n.ServiceID).Inc()
Expand Down
12 changes: 11 additions & 1 deletion graphql2/graphqlapp/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,17 @@ func (m *Mutation) CreateAlert(ctx context.Context, input graphql2.CreateAlertIn
a.Dedup = alert.NewUserDedup(*input.Dedup)
}

return m.AlertStore.Create(ctx, a)
var newAlert *alert.Alert
err := withContextTx(ctx, m.DB, func(ctx context.Context, tx *sql.Tx) error {
var err error
newAlert, err = m.AlertStore.CreateTx(ctx, tx, a)
return err
})
if err != nil {
return nil, err
}

return newAlert, nil
}

func (a *Alert) NoiseReason(ctx context.Context, raw *alert.Alert) (*string, error) {
Expand Down
Loading