fix(observabillity): propagate ctx to every log call and add resource to logging
This commit was merged in pull request #187.
This commit is contained in:
@@ -47,7 +47,7 @@ func (s TransactionImpl) Add(ctx context.Context, tx *sqlx.Tx, user *types.User,
|
||||
if tx == nil {
|
||||
ownsTransaction = true
|
||||
tx, err = s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transaction Add", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Add", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -66,7 +66,7 @@ func (s TransactionImpl) Add(ctx context.Context, tx *sqlx.Tx, user *types.User,
|
||||
party, description, error, created_at, created_by)
|
||||
VALUES (:id, :user_id, :account_id, :treasure_chest_id, :value, :timestamp,
|
||||
:party, :description, :error, :created_at, :created_by)`, transaction)
|
||||
err = db.TransformAndLogDbError("transaction Insert", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Insert", r, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -76,7 +76,7 @@ func (s TransactionImpl) Add(ctx context.Context, tx *sqlx.Tx, user *types.User,
|
||||
UPDATE account
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction Add", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Add", r, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -87,7 +87,7 @@ func (s TransactionImpl) Add(ctx context.Context, tx *sqlx.Tx, user *types.User,
|
||||
UPDATE treasure_chest
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction Add", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Add", r, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -95,7 +95,7 @@ func (s TransactionImpl) Add(ctx context.Context, tx *sqlx.Tx, user *types.User,
|
||||
|
||||
if ownsTransaction {
|
||||
err = tx.Commit()
|
||||
err = db.TransformAndLogDbError("transaction Add", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Add", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -110,7 +110,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *types.User, input typ
|
||||
}
|
||||
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transaction Update", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Update", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -120,7 +120,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *types.User, input typ
|
||||
|
||||
transaction := &types.Transaction{}
|
||||
err = tx.GetContext(ctx, transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, input.Id)
|
||||
err = db.TransformAndLogDbError("transaction Update", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Update", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, fmt.Errorf("transaction %v not found: %w", input.Id, ErrBadRequest)
|
||||
@@ -133,7 +133,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *types.User, input typ
|
||||
UPDATE account
|
||||
SET current_balance = current_balance - ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction Update", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Update", r, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -143,7 +143,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *types.User, input typ
|
||||
UPDATE treasure_chest
|
||||
SET current_balance = current_balance - ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction Update", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Update", r, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -159,7 +159,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *types.User, input typ
|
||||
UPDATE account
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction Update", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Update", r, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -169,7 +169,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *types.User, input typ
|
||||
UPDATE treasure_chest
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction Update", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Update", r, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -189,13 +189,13 @@ func (s TransactionImpl) Update(ctx context.Context, user *types.User, input typ
|
||||
updated_by = :updated_by
|
||||
WHERE id = :id
|
||||
AND user_id = :user_id`, transaction)
|
||||
err = db.TransformAndLogDbError("transaction Update", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Update", r, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
err = db.TransformAndLogDbError("transaction Update", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Update", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -209,13 +209,13 @@ func (s TransactionImpl) Get(ctx context.Context, user *types.User, id string) (
|
||||
}
|
||||
uuid, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
slog.Error("transaction get", "err", err)
|
||||
slog.ErrorContext(ctx, "transaction get", "err", err)
|
||||
return nil, fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
var transaction types.Transaction
|
||||
err = s.db.GetContext(ctx, &transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("transaction Get", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Get", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, fmt.Errorf("transaction %v not found: %w", id, ErrBadRequest)
|
||||
@@ -247,7 +247,7 @@ func (s TransactionImpl) GetAll(ctx context.Context, user *types.User, filter ty
|
||||
filter.AccountId, filter.AccountId,
|
||||
filter.TreasureChestId, filter.TreasureChestId,
|
||||
filter.Error, filter.Error, filter.Error)
|
||||
err = db.TransformAndLogDbError("transaction GetAll", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction GetAll", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -261,12 +261,12 @@ func (s TransactionImpl) Delete(ctx context.Context, user *types.User, id string
|
||||
}
|
||||
uuid, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
slog.Error("transaction delete", "err", err)
|
||||
slog.ErrorContext(ctx, "transaction delete", "err", err)
|
||||
return fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transaction Delete", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Delete", nil, err)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -276,7 +276,7 @@ func (s TransactionImpl) Delete(ctx context.Context, user *types.User, id string
|
||||
|
||||
var transaction types.Transaction
|
||||
err = tx.GetContext(ctx, &transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("transaction Delete", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Delete", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -287,7 +287,7 @@ func (s TransactionImpl) Delete(ctx context.Context, user *types.User, id string
|
||||
SET current_balance = current_balance - ?
|
||||
WHERE id = ?
|
||||
AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction Delete", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Delete", r, err)
|
||||
if err != nil && !errors.Is(err, db.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
@@ -299,20 +299,20 @@ func (s TransactionImpl) Delete(ctx context.Context, user *types.User, id string
|
||||
SET current_balance = current_balance - ?
|
||||
WHERE id = ?
|
||||
AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction Delete", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Delete", r, err)
|
||||
if err != nil && !errors.Is(err, db.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
r, err := tx.ExecContext(ctx, "DELETE FROM \"transaction\" WHERE id = ? AND user_id = ?", uuid, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction Delete", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Delete", r, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
err = db.TransformAndLogDbError("transaction Delete", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction Delete", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -326,7 +326,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *types.Us
|
||||
}
|
||||
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError("transaction RecalculateBalances", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -338,7 +338,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *types.Us
|
||||
UPDATE account
|
||||
SET current_balance = 0
|
||||
WHERE user_id = ?`, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction RecalculateBalances", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
|
||||
if err != nil && !errors.Is(err, db.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
@@ -347,7 +347,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *types.Us
|
||||
UPDATE treasure_chest
|
||||
SET current_balance = 0
|
||||
WHERE user_id = ?`, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction RecalculateBalances", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
|
||||
if err != nil && !errors.Is(err, db.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
@@ -356,21 +356,21 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *types.Us
|
||||
SELECT *
|
||||
FROM "transaction"
|
||||
WHERE user_id = ?`, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction RecalculateBalances", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
|
||||
if err != nil && !errors.Is(err, db.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
err := rows.Close()
|
||||
if err != nil {
|
||||
slog.Error("transaction RecalculateBalances", "err", err)
|
||||
slog.ErrorContext(ctx, "transaction RecalculateBalances", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
var transaction types.Transaction
|
||||
for rows.Next() {
|
||||
err = rows.StructScan(&transaction)
|
||||
err = db.TransformAndLogDbError("transaction RecalculateBalances", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -381,7 +381,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *types.Us
|
||||
SET error = ?
|
||||
WHERE user_id = ?
|
||||
AND id = ?`, transaction.Error, user.Id, transaction.Id)
|
||||
err = db.TransformAndLogDbError("transaction RecalculateBalances", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -395,7 +395,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *types.Us
|
||||
UPDATE account
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction RecalculateBalances", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -405,7 +405,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *types.Us
|
||||
UPDATE treasure_chest
|
||||
SET current_balance = current_balance + ?
|
||||
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
|
||||
err = db.TransformAndLogDbError("transaction RecalculateBalances", r, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -413,7 +413,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *types.Us
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
err = db.TransformAndLogDbError("transaction RecalculateBalances", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -434,7 +434,7 @@ func (s TransactionImpl) validateAndEnrichTransaction(ctx context.Context, tx *s
|
||||
)
|
||||
|
||||
if oldTransaction == nil {
|
||||
id, err = s.random.UUID()
|
||||
id, err = s.random.UUID(ctx)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
@@ -451,12 +451,12 @@ func (s TransactionImpl) validateAndEnrichTransaction(ctx context.Context, tx *s
|
||||
|
||||
if input.AccountId != nil {
|
||||
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, input.AccountId, userId)
|
||||
err = db.TransformAndLogDbError("transaction validate", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction validate", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rowCount == 0 {
|
||||
slog.Error("transaction validate", "err", err)
|
||||
slog.ErrorContext(ctx, "transaction validate", "err", err)
|
||||
return nil, fmt.Errorf("account not found: %w", ErrBadRequest)
|
||||
}
|
||||
}
|
||||
@@ -464,7 +464,7 @@ func (s TransactionImpl) validateAndEnrichTransaction(ctx context.Context, tx *s
|
||||
if input.TreasureChestId != nil {
|
||||
var treasureChest types.TreasureChest
|
||||
err = tx.GetContext(ctx, &treasureChest, `SELECT * FROM treasure_chest WHERE id = ? AND user_id = ?`, input.TreasureChestId, userId)
|
||||
err = db.TransformAndLogDbError("transaction validate", nil, err)
|
||||
err = db.TransformAndLogDbError(ctx, "transaction validate", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, fmt.Errorf("treasure chest not found: %w", ErrBadRequest)
|
||||
|
||||
Reference in New Issue
Block a user