refactor(tracing): tracing & logging

added the custom util functions to handel telemetry
This commit is contained in:
SoXX 2024-08-12 11:46:53 +02:00
parent 24d51a3751
commit 135e09ea8e

View File

@ -3,62 +3,105 @@ package database
import ( import (
"context" "context"
"errors" "errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm" "gorm.io/gorm"
) )
func CreateUserFavorite(ctx context.Context, userFav models.UserFavorite) (models.UserFavorite, error) { func CreateUserFavorite(ctx context.Context, userFav models.UserFavorite) (models.UserFavorite, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavorite")
defer span.End()
localLogger.WithFields(log.Fields{
"user_favorite_id": userFav.ID,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(userFav.ID)),
)
utils.HandleEvent(span, localLogger, "Starting user favorite creation")
if client == nil { if client == nil {
return models.UserFavorite{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
result := client.WithContext(ctx).Create(&userFav) result := client.WithContext(ctx).Create(&userFav)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.UserFavorite{}, &otterError.Database{Reason: otterError.DuplicateKey} return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
} }
return models.UserFavorite{}, result.Error return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, result.Error)
} }
utils.HandleEvent(span, localLogger, "User favorite created successfully")
return userFav, nil return userFav, nil
} }
func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorite, batchSize int) error { func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorite, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavoriteInBatch")
defer span.End()
localLogger.WithFields(log.Fields{
"user_favorite_count": len(userFav),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int64("batch_size", int64(batchSize)),
attribute.Int64("user_favorite_count", int64(len(userFav))),
)
utils.HandleEvent(span, localLogger, "Starting batch user favorite creation")
if client == nil { if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
if userFav == nil { if userFav == nil || len(userFav) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty} return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty})
}
if len(userFav) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty}
} }
if batchSize == 0 { if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
} }
result := client.WithContext(ctx).CreateInBatches(&userFav, batchSize) result := client.WithContext(ctx).CreateInBatches(&userFav, batchSize)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
} }
return result.Error return utils.HandleError(ctx, span, localLogger, result.Error)
} }
utils.HandleEvent(span, localLogger, "Batch user favorites created successfully")
return nil return nil
} }
func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error { func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserFavorite")
defer span.End()
localLogger.WithFields(log.Fields{
"user_favorite_id": userFav.ID,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(userFav.ID)),
)
utils.HandleEvent(span, localLogger, "Starting user favorite update")
if client == nil { if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
if len(userFav.ID) == 0 { if len(userFav.ID) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty})
} }
if !userFav.DeletedAt.Valid { if !userFav.DeletedAt.Valid {
@ -68,58 +111,87 @@ func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error
result := client.WithContext(ctx).Model(&userFav).Update("deleted_at", gorm.DeletedAt{}) result := client.WithContext(ctx).Model(&userFav).Update("deleted_at", gorm.DeletedAt{})
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
} }
return result.Error return utils.HandleError(ctx, span, localLogger, result.Error)
} }
utils.HandleEvent(span, localLogger, "User favorite updated successfully")
return nil return nil
} }
func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models.UserFavorite, error) { func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models.UserFavorite, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserFavoritesByID")
defer span.End()
localLogger.WithFields(log.Fields{
"user_favorite_id": id,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get user favorite by ID")
var userFavorites models.UserFavorite var userFavorites models.UserFavorite
if client == nil { if client == nil {
return models.UserFavorite{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
if len(id) == 0 { if len(id) == 0 {
return models.UserFavorite{}, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty})
} }
result := client.WithContext(ctx).First(&userFavorites, "id = ?", id) result := client.WithContext(ctx).First(&userFavorites, "id = ?", id)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.UserFavorite{}, &otterError.Database{Reason: otterError.NoDataFound} return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
} }
return models.UserFavorite{}, result.Error return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, result.Error)
} }
utils.HandleEvent(span, localLogger, "User favorite retrieved successfully")
return userFavorites, nil return userFavorites, nil
} }
func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error { func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserFavorite")
defer span.End()
localLogger.WithFields(log.Fields{
"user_favorite_id": id,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete user favorite")
var userFavorite models.UserFavorite var userFavorite models.UserFavorite
if client == nil { if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
if len(id) == 0 { if len(id) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty})
} }
if len(id) != 25 { if len(id) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDToShort} return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDToShort})
} }
result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id) result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
} }
return result.Error return utils.HandleError(ctx, span, localLogger, result.Error)
} }
utils.HandleEvent(span, localLogger, "User favorite deleted successfully")
return nil return nil
} }