feat(tracing): Add tracing, logging, and error handling to tagGroup database functions
This commit is contained in:
parent
098ecda869
commit
3f92bdb325
@ -3,14 +3,28 @@ 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 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 {
|
||||
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{
|
||||
@ -18,63 +32,132 @@ func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagNa
|
||||
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)
|
||||
if result.Error != nil {
|
||||
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{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
|
||||
}
|
||||
return models.TagGroup{}, result.Error
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||
}
|
||||
|
||||
if tagsGroups == nil {
|
||||
return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty}
|
||||
}
|
||||
|
||||
if len(tagsGroups) == 0 {
|
||||
return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty}
|
||||
if tagsGroups == nil || len(tagsGroups) == 0 {
|
||||
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty})
|
||||
}
|
||||
|
||||
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)
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
||||
return &otterError.Database{Reason: otterError.DuplicateKey}
|
||||
loggerFields := log.Fields{
|
||||
"tag_group_count": len(tagsGroups),
|
||||
}
|
||||
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
|
||||
|
||||
}
|
||||
return result.Error
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
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 {
|
||||
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)
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return &otterError.Database{Reason: otterError.NoDataFound}
|
||||
|
||||
loggerFields := log.Fields{
|
||||
"tag_group_name": tagGroupName,
|
||||
}
|
||||
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound})
|
||||
}
|
||||
return result.Error
|
||||
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user