better logging
This commit is contained in:
@@ -23,6 +23,9 @@ type Repository struct {
|
||||
}
|
||||
|
||||
func New(logger mlogger.Logger, conn *db.MongoConnection) (*Repository, error) {
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
if conn == nil {
|
||||
return nil, merrors.InvalidArgument("mongo connection is nil")
|
||||
}
|
||||
@@ -30,10 +33,19 @@ func New(logger mlogger.Logger, conn *db.MongoConnection) (*Repository, error) {
|
||||
if client == nil {
|
||||
return nil, merrors.Internal("mongo client is not initialised")
|
||||
}
|
||||
db := conn.Database()
|
||||
if db == nil {
|
||||
return nil, merrors.Internal("mongo database is not initialised")
|
||||
}
|
||||
dbName := db.Name()
|
||||
logger = logger.Named("storage").Named("mongo")
|
||||
if dbName != "" {
|
||||
logger = logger.With(zap.String("database", dbName))
|
||||
}
|
||||
result := &Repository{
|
||||
logger: logger.Named("storage").Named("mongo"),
|
||||
logger: logger,
|
||||
conn: conn,
|
||||
db: conn.Database(),
|
||||
db: db,
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
@@ -43,12 +55,12 @@ func New(logger mlogger.Logger, conn *db.MongoConnection) (*Repository, error) {
|
||||
}
|
||||
paymentsStore, err := store.NewPayments(result.logger, result.db)
|
||||
if err != nil {
|
||||
result.logger.Error("Failed to initialise payments store", zap.Error(err))
|
||||
result.logger.Error("Failed to initialise payments store", zap.Error(err), zap.String("store", "payments"))
|
||||
return nil, err
|
||||
}
|
||||
tgStore, err := store.NewTelegramConfirmations(result.logger, result.db)
|
||||
if err != nil {
|
||||
result.logger.Error("Failed to initialise telegram confirmations store", zap.Error(err))
|
||||
result.logger.Error("Failed to initialise telegram confirmations store", zap.Error(err), zap.String("store", "telegram_confirmations"))
|
||||
return nil, err
|
||||
}
|
||||
result.payments = paymentsStore
|
||||
|
||||
@@ -2,16 +2,18 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/tgsettle/storage"
|
||||
"github.com/tech/sendico/gateway/tgsettle/storage/model"
|
||||
"github.com/tech/sendico/pkg/db/repository"
|
||||
ri "github.com/tech/sendico/pkg/db/repository/index"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -29,18 +31,25 @@ func NewPayments(logger mlogger.Logger, db *mongo.Database) (*Payments, error) {
|
||||
if db == nil {
|
||||
return nil, merrors.InvalidArgument("mongo database is nil")
|
||||
}
|
||||
p := &Payments{
|
||||
logger: logger.Named("payments"),
|
||||
coll: db.Collection(paymentsCollection),
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
_, err := p.coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{
|
||||
Keys: bson.D{{Key: fieldIdempotencyKey, Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
})
|
||||
if err != nil {
|
||||
p.logger.Error("Failed to create payments idempotency index", zap.Error(err))
|
||||
logger = logger.Named("payments").With(zap.String("collection", paymentsCollection))
|
||||
|
||||
repo := repository.CreateMongoRepository(db, paymentsCollection)
|
||||
if err := repo.CreateIndex(&ri.Definition{
|
||||
Keys: []ri.Key{{Field: fieldIdempotencyKey, Sort: ri.Asc}},
|
||||
Unique: true,
|
||||
}); err != nil {
|
||||
logger.Error("Failed to create payments idempotency index", zap.Error(err), zap.String("index_field", fieldIdempotencyKey))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p := &Payments{
|
||||
logger: logger,
|
||||
coll: db.Collection(paymentsCollection),
|
||||
}
|
||||
p.logger.Debug("Payments store initialised")
|
||||
return p, nil
|
||||
}
|
||||
|
||||
@@ -55,6 +64,9 @@ func (p *Payments) FindByIdempotencyKey(ctx context.Context, key string) (*model
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
|
||||
p.logger.Warn("Payment execution lookup failed", zap.String("idempotency_key", key), zap.Error(err))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
@@ -74,6 +86,13 @@ func (p *Payments) InsertExecution(ctx context.Context, exec *model.PaymentExecu
|
||||
if mongo.IsDuplicateKeyError(err) {
|
||||
return storage.ErrDuplicate
|
||||
}
|
||||
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
|
||||
p.logger.Warn("Failed to insert payment execution",
|
||||
zap.String("idempotency_key", exec.IdempotencyKey),
|
||||
zap.String("payment_intent_id", exec.PaymentIntentID),
|
||||
zap.String("quote_ref", exec.QuoteRef),
|
||||
zap.Error(err))
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -2,11 +2,14 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/tgsettle/storage"
|
||||
"github.com/tech/sendico/gateway/tgsettle/storage/model"
|
||||
"github.com/tech/sendico/pkg/db/repository"
|
||||
ri "github.com/tech/sendico/pkg/db/repository/index"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
@@ -29,18 +32,25 @@ func NewTelegramConfirmations(logger mlogger.Logger, db *mongo.Database) (*Teleg
|
||||
if db == nil {
|
||||
return nil, merrors.InvalidArgument("mongo database is nil")
|
||||
}
|
||||
t := &TelegramConfirmations{
|
||||
logger: logger.Named("telegram_confirmations"),
|
||||
coll: db.Collection(telegramCollection),
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
_, err := t.coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{
|
||||
Keys: bson.D{{Key: fieldRequestID, Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
})
|
||||
if err != nil {
|
||||
t.logger.Error("Failed to create telegram confirmations request_id index", zap.Error(err))
|
||||
logger = logger.Named("telegram_confirmations").With(zap.String("collection", telegramCollection))
|
||||
|
||||
repo := repository.CreateMongoRepository(db, telegramCollection)
|
||||
if err := repo.CreateIndex(&ri.Definition{
|
||||
Keys: []ri.Key{{Field: fieldRequestID, Sort: ri.Asc}},
|
||||
Unique: true,
|
||||
}); err != nil {
|
||||
logger.Error("Failed to create telegram confirmations request_id index", zap.Error(err), zap.String("index_field", fieldRequestID))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
t := &TelegramConfirmations{
|
||||
logger: logger,
|
||||
coll: db.Collection(telegramCollection),
|
||||
}
|
||||
t.logger.Debug("Telegram confirmations store initialised")
|
||||
return t, nil
|
||||
}
|
||||
|
||||
@@ -61,6 +71,16 @@ func (t *TelegramConfirmations) Upsert(ctx context.Context, record *model.Telegr
|
||||
"$set": record,
|
||||
}
|
||||
_, err := t.coll.UpdateOne(ctx, bson.M{fieldRequestID: record.RequestID}, update, options.Update().SetUpsert(true))
|
||||
if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
|
||||
fields := []zap.Field{zap.String("request_id", record.RequestID)}
|
||||
if record.PaymentIntentID != "" {
|
||||
fields = append(fields, zap.String("payment_intent_id", record.PaymentIntentID))
|
||||
}
|
||||
if record.QuoteRef != "" {
|
||||
fields = append(fields, zap.String("quote_ref", record.QuoteRef))
|
||||
}
|
||||
t.logger.Warn("Failed to upsert telegram confirmation", append(fields, zap.Error(err))...)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user