feat(tracing): Add tracing, logging, and error handling to tagGroup database functions

This commit is contained in:
SoXX 2024-08-12 10:07:42 +02:00
parent 098ecda869
commit 3f92bdb325

View File

@ -3,14 +3,28 @@ 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 CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagName models.TagName) (models.TagGroup, error) { func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagName models.TagName) (models.TagGroup, error) {
ctx, span := tracer.Start(ctx, "CreateTagGroup")
defer span.End()
span.SetAttributes(
attribute.String("tag_group_name", string(tagGroupName)),
attribute.String("tag_name", string(tagName)),
)
span.AddEvent("Starting tag group creation")
if client == nil { if client == nil {
return models.TagGroup{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return models.TagGroup{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
tagGroup := models.TagGroup{ tagGroup := models.TagGroup{
@ -18,63 +32,132 @@ func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagNa
TagID: tagName, TagID: tagName,
} }
logger.WithContext(ctx).WithFields(log.Fields{
"tag_group_name": tagGroupName,
"tag_name": tagName,
}).Debug("attempting to create tag group")
result := client.WithContext(ctx).Create(&tagGroup) result := client.WithContext(ctx).Create(&tagGroup)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.TagGroup{}, &otterError.Database{Reason: otterError.DuplicateKey}
loggerFields := log.Fields{
"tag_group_name": tagGroupName,
"tag_name": tagName,
} }
return models.TagGroup{}, result.Error return models.TagGroup{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
} }
loggerFields := log.Fields{
"tag_group_name": tagGroupName,
"tag_name": tagName,
}
return models.TagGroup{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
logger.WithContext(ctx).WithFields(log.Fields{
"tag_group_name": tagGroupName,
"tag_name": tagName,
}).Debug("tag group created")
span.AddEvent("Tag group created successfully")
return tagGroup, nil return tagGroup, nil
} }
func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error { func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error {
ctx, span := tracer.Start(ctx, "CreateTagAliasInBatch")
defer span.End()
span.SetAttributes(
attribute.Int64("batch_size", int64(batchSize)),
attribute.Int64("tag_group_count", int64(len(tagsGroups))),
)
span.AddEvent("Starting batch tag group creation")
if client == nil { if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
if tagsGroups == nil { if tagsGroups == nil || len(tagsGroups) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty} return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty})
}
if len(tagsGroups) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty}
} }
if batchSize == 0 { if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
} }
logger.WithContext(ctx).WithFields(log.Fields{
"tag_groups_count": len(tagsGroups),
}).Debug("attempting to create tags groups")
result := client.WithContext(ctx).CreateInBatches(&tagsGroups, batchSize) result := client.WithContext(ctx).CreateInBatches(&tagsGroups, 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} loggerFields := log.Fields{
"tag_group_count": len(tagsGroups),
} }
return result.Error return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
}
loggerFields := log.Fields{
"tag_group_count": len(tagsGroups),
}
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
} }
logger.WithContext(ctx).WithFields(log.Fields{
"tag_group_count": len(tagsGroups),
}).Debug("batch tags groups created")
span.AddEvent("Batch tags groups created successfully")
return nil return nil
} }
func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error { func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error {
ctx, span := tracer.Start(ctx, "DeleteTagGroup")
defer span.End()
span.SetAttributes(
attribute.String("tag_group_name", string(tagGroupName)),
)
span.AddEvent("Starting tag group deletion")
var tagGroup models.TagGroup var tagGroup models.TagGroup
if client == nil { if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
if len(tagGroupName) == 0 { if len(tagGroupName) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.TagGroupNameIsEmpty} return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagGroupNameIsEmpty})
} }
logger.WithContext(ctx).WithFields(log.Fields{
"tag_group_name": tagGroupName,
}).Debug("attempting to delete tag group")
result := client.WithContext(ctx).Delete(&tagGroup, tagGroupName) result := client.WithContext(ctx).Delete(&tagGroup, tagGroupName)
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}
loggerFields := log.Fields{
"tag_group_name": tagGroupName,
} }
return result.Error return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound})
} }
loggerFields := log.Fields{
"tag_group_name": tagGroupName,
}
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
logger.WithContext(ctx).WithFields(log.Fields{
"tag_group_name": tagGroupName,
}).Debug("tag group deleted")
span.AddEvent("Tag group deleted successfully")
return nil return nil
} }