refactor(tracing): tracing & logging
added the custom util functions to handel telemetry
This commit is contained in:
parent
d994ead6f6
commit
24d51a3751
@ -3,84 +3,141 @@ package database
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils"
|
||||
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func CreatePost(ctx context.Context, post models.Post) (models.Post, error) {
|
||||
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePost")
|
||||
defer span.End()
|
||||
|
||||
localLogger.WithFields(log.Fields{
|
||||
"post_id": post.ID,
|
||||
})
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("post_id", string(post.ID)),
|
||||
)
|
||||
|
||||
utils.HandleEvent(span, localLogger, "Starting post creation")
|
||||
|
||||
if client == nil {
|
||||
return models.Post{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
||||
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||
}
|
||||
|
||||
result := client.WithContext(ctx).Create(&post)
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
||||
return models.Post{}, &otterError.Database{Reason: otterError.DuplicateKey}
|
||||
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
||||
}
|
||||
return models.Post{}, result.Error
|
||||
return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
||||
}
|
||||
|
||||
utils.HandleEvent(span, localLogger, "Post created successfully")
|
||||
return post, nil
|
||||
}
|
||||
|
||||
func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) error {
|
||||
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePostInBatch")
|
||||
defer span.End()
|
||||
|
||||
localLogger.WithFields(log.Fields{
|
||||
"post_count": len(post),
|
||||
"batch_size": batchSize,
|
||||
})
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.Int64("batch_size", int64(batchSize)),
|
||||
attribute.Int64("post_count", int64(len(post))),
|
||||
)
|
||||
|
||||
utils.HandleEvent(span, localLogger, "Starting batch post creation")
|
||||
|
||||
if client == nil {
|
||||
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
||||
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||
}
|
||||
|
||||
if post == nil {
|
||||
return &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty}
|
||||
}
|
||||
|
||||
if len(post) == 0 {
|
||||
return &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty}
|
||||
if post == nil || len(post) == 0 {
|
||||
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty})
|
||||
}
|
||||
|
||||
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(&post, batchSize)
|
||||
if result.Error != nil {
|
||||
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 posts created successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) {
|
||||
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetPostByID")
|
||||
defer span.End()
|
||||
|
||||
localLogger.WithFields(log.Fields{
|
||||
"post_id": id,
|
||||
})
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("post_id", string(id)),
|
||||
)
|
||||
|
||||
utils.HandleEvent(span, localLogger, "Starting get post by ID")
|
||||
|
||||
var post models.Post
|
||||
|
||||
if client == nil {
|
||||
return models.Post{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
||||
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||
}
|
||||
|
||||
if len(id) == 0 {
|
||||
return models.Post{}, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty}
|
||||
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
|
||||
}
|
||||
|
||||
result := client.WithContext(ctx).First(&post, "id = ?", id)
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return models.Post{}, &otterError.Database{Reason: otterError.NoDataFound}
|
||||
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
||||
}
|
||||
return models.Post{}, result.Error
|
||||
return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
||||
}
|
||||
|
||||
utils.HandleEvent(span, localLogger, "Post retrieved successfully")
|
||||
return post, nil
|
||||
}
|
||||
|
||||
func UpdatePost(ctx context.Context, anthrovePost models.Post) error {
|
||||
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePost")
|
||||
defer span.End()
|
||||
|
||||
localLogger.WithFields(log.Fields{
|
||||
"post_id": anthrovePost.ID,
|
||||
})
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("post_id", string(anthrovePost.ID)),
|
||||
)
|
||||
|
||||
utils.HandleEvent(span, localLogger, "Starting post update")
|
||||
|
||||
if client == nil {
|
||||
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
||||
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||
}
|
||||
|
||||
if len(anthrovePost.ID) == 0 {
|
||||
return &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty}
|
||||
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
|
||||
}
|
||||
|
||||
updatePost := models.Post{
|
||||
@ -92,36 +149,51 @@ func UpdatePost(ctx context.Context, anthrovePost models.Post) error {
|
||||
result := client.WithContext(ctx).Model(&updatePost).Update("deleted_at", gorm.DeletedAt{})
|
||||
if result.Error != nil {
|
||||
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, "Post updated successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeletePost(ctx context.Context, id models.PostID) error {
|
||||
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePost")
|
||||
defer span.End()
|
||||
|
||||
localLogger.WithFields(log.Fields{
|
||||
"post_id": id,
|
||||
})
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("post_id", string(id)),
|
||||
)
|
||||
|
||||
utils.HandleEvent(span, localLogger, "Starting delete post")
|
||||
|
||||
var userFavorite models.UserFavorite
|
||||
|
||||
if client == nil {
|
||||
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
||||
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||
}
|
||||
|
||||
if len(id) == 0 {
|
||||
return &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty}
|
||||
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
|
||||
}
|
||||
|
||||
if len(id) != 25 {
|
||||
return &otterError.EntityValidationFailed{Reason: otterError.PostIDToShort}
|
||||
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDToShort})
|
||||
}
|
||||
|
||||
result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id)
|
||||
if result.Error != nil {
|
||||
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, "Post deleted successfully")
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user