refactor(tracing): tracing & logging
added the custom util functions to handel telemetry
This commit is contained in:
parent
9512cd10bc
commit
ffc3164cb7
@ -13,18 +13,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagType) (models.Tag, error) {
|
func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagType) (models.Tag, error) {
|
||||||
ctx, span := tracer.Start(ctx, "CreateTag")
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTag")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
localLogger.WithFields(log.Fields{
|
||||||
|
"tag_name": tagName,
|
||||||
|
"tag_type": tagType,
|
||||||
|
})
|
||||||
|
|
||||||
span.SetAttributes(
|
span.SetAttributes(
|
||||||
attribute.String("tag_name", string(tagName)),
|
attribute.String("tag_name", string(tagName)),
|
||||||
attribute.String("tag_type", string(tagType)),
|
attribute.String("tag_type", string(tagType)),
|
||||||
)
|
)
|
||||||
|
|
||||||
span.AddEvent("Starting tag creation")
|
utils.HandleEvent(span, localLogger, "Starting tag creation")
|
||||||
|
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return models.Tag{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||||
}
|
}
|
||||||
|
|
||||||
tag := models.Tag{
|
tag := models.Tag{
|
||||||
@ -32,131 +37,86 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy
|
|||||||
Type: tagType,
|
Type: tagType,
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_name": tagName,
|
|
||||||
"tag_type": tagType,
|
|
||||||
}).Debug("attempting to create tag")
|
|
||||||
|
|
||||||
result := client.WithContext(ctx).Create(&tag)
|
result := client.WithContext(ctx).Create(&tag)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
||||||
|
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
||||||
loggerFields := log.Fields{
|
|
||||||
"tag_name": tagName,
|
|
||||||
"tag_type": tagType,
|
|
||||||
}
|
}
|
||||||
return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
|
return models.Tag{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
loggerFields := log.Fields{
|
utils.HandleEvent(span, localLogger, "Tag created successfully")
|
||||||
"tag_name": tagName,
|
|
||||||
"tag_type": tagType,
|
|
||||||
}
|
|
||||||
return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_name": tagName,
|
|
||||||
"tag_type": tagType,
|
|
||||||
}).Debug("tag created")
|
|
||||||
span.AddEvent("Tag created successfully")
|
|
||||||
return tag, nil
|
return tag, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) error {
|
func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) error {
|
||||||
ctx, span := tracer.Start(ctx, "CreateTagInBatch")
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagInBatch")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
localLogger.WithFields(log.Fields{
|
||||||
|
"tag_count": len(tags),
|
||||||
|
"batch_size": batchSize,
|
||||||
|
})
|
||||||
|
|
||||||
span.SetAttributes(
|
span.SetAttributes(
|
||||||
attribute.Int64("batch_size", int64(batchSize)),
|
attribute.Int64("batch_size", int64(batchSize)),
|
||||||
attribute.Int64("tag_count", int64(len(tags))),
|
attribute.Int64("tag_count", int64(len(tags))),
|
||||||
)
|
)
|
||||||
|
|
||||||
span.AddEvent("Starting batch tag creation")
|
utils.HandleEvent(span, localLogger, "Starting batch tag creation")
|
||||||
|
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||||
}
|
}
|
||||||
|
|
||||||
if tags == nil || len(tags) == 0 {
|
if tags == nil || len(tags) == 0 {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty})
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty})
|
||||||
}
|
}
|
||||||
|
|
||||||
if batchSize == 0 {
|
if batchSize == 0 {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_length": len(tags),
|
|
||||||
}).Debug("attempting to create tags")
|
|
||||||
|
|
||||||
result := client.WithContext(ctx).CreateInBatches(&tags, batchSize)
|
result := client.WithContext(ctx).CreateInBatches(&tags, batchSize)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
||||||
loggerFields := log.Fields{
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
||||||
"tag_length": len(tags),
|
|
||||||
}
|
}
|
||||||
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
loggerFields := log.Fields{
|
utils.HandleEvent(span, localLogger, "Batch tags created successfully")
|
||||||
"tag_length": len(tags),
|
|
||||||
}
|
|
||||||
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_length": len(tags),
|
|
||||||
}).Debug("tags created")
|
|
||||||
|
|
||||||
span.AddEvent("Batch tags created successfully")
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteTag(ctx context.Context, tagName models.TagName) error {
|
func DeleteTag(ctx context.Context, tagName models.TagName) error {
|
||||||
ctx, span := tracer.Start(ctx, "DeleteTag")
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTag")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
localLogger.WithFields(log.Fields{
|
||||||
|
"tag_name": tagName,
|
||||||
|
})
|
||||||
|
|
||||||
span.SetAttributes(
|
span.SetAttributes(
|
||||||
attribute.String("tag_name", string(tagName)),
|
attribute.String("tag_name", string(tagName)),
|
||||||
)
|
)
|
||||||
|
|
||||||
span.AddEvent("Starting tag deletion")
|
utils.HandleEvent(span, localLogger, "Starting tag deletion")
|
||||||
|
|
||||||
var tag models.Tag
|
var tag models.Tag
|
||||||
|
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tagName) == 0 {
|
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagNameIsEmpty})
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_name": tagName,
|
|
||||||
}).Debug("attempting to delete tag")
|
|
||||||
|
|
||||||
result := client.WithContext(ctx).Delete(&tag, tagName)
|
result := client.WithContext(ctx).Delete(&tag, tagName)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||||
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
||||||
loggerFields := log.Fields{
|
|
||||||
"tag_name": tagName,
|
|
||||||
}
|
}
|
||||||
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound})
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
loggerFields := log.Fields{
|
utils.HandleEvent(span, localLogger, "Tag deleted successfully")
|
||||||
"tag_name": tagName,
|
|
||||||
}
|
|
||||||
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_name": tagName,
|
|
||||||
}).Debug("tag deleted")
|
|
||||||
|
|
||||||
span.AddEvent("Tag deleted successfully")
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -13,18 +13,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) (models.TagAlias, error) {
|
func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) (models.TagAlias, error) {
|
||||||
ctx, span := tracer.Start(ctx, "CreateTagAlias")
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAlias")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
localLogger.WithFields(log.Fields{
|
||||||
|
"tag_alias_name": tagAliasName,
|
||||||
|
"tag_name": tagName,
|
||||||
|
})
|
||||||
|
|
||||||
span.SetAttributes(
|
span.SetAttributes(
|
||||||
attribute.String("tag_alias_name", string(tagAliasName)),
|
attribute.String("tag_alias_name", string(tagAliasName)),
|
||||||
attribute.String("tag_name", string(tagName)),
|
attribute.String("tag_name", string(tagName)),
|
||||||
)
|
)
|
||||||
|
|
||||||
span.AddEvent("Starting tag alias creation")
|
utils.HandleEvent(span, localLogger, "Starting tag alias creation")
|
||||||
|
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return models.TagAlias{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||||
}
|
}
|
||||||
|
|
||||||
tagAlias := models.TagAlias{
|
tagAlias := models.TagAlias{
|
||||||
@ -32,131 +37,86 @@ func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagNa
|
|||||||
TagID: tagName,
|
TagID: tagName,
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_alias_name": tagAliasName,
|
|
||||||
"tag_name": tagName,
|
|
||||||
}).Debug("attempting to create tag alias")
|
|
||||||
|
|
||||||
result := client.WithContext(ctx).Create(&tagAlias)
|
result := client.WithContext(ctx).Create(&tagAlias)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
||||||
|
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
||||||
loggerFields := log.Fields{
|
|
||||||
"tag_alias_name": tagAliasName,
|
|
||||||
"tag_name": tagName,
|
|
||||||
}
|
}
|
||||||
return models.TagAlias{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
|
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
loggerFields := log.Fields{
|
utils.HandleEvent(span, localLogger, "Tag alias created successfully")
|
||||||
"tag_alias_name": tagAliasName,
|
|
||||||
"tag_name": tagName,
|
|
||||||
}
|
|
||||||
return models.TagAlias{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_alias_name": tagAliasName,
|
|
||||||
"tag_name": tagName,
|
|
||||||
}).Debug("tag alias created")
|
|
||||||
span.AddEvent("Tag alias created successfully")
|
|
||||||
return tagAlias, nil
|
return tagAlias, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error {
|
func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error {
|
||||||
ctx, span := tracer.Start(ctx, "CreateTagAliasInBatch")
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
localLogger.WithFields(log.Fields{
|
||||||
|
"tag_aliases_count": len(tagsAliases),
|
||||||
|
"batch_size": batchSize,
|
||||||
|
})
|
||||||
|
|
||||||
span.SetAttributes(
|
span.SetAttributes(
|
||||||
attribute.Int64("batch_size", int64(batchSize)),
|
attribute.Int64("batch_size", int64(batchSize)),
|
||||||
attribute.Int64("tag_aliases_count", int64(len(tagsAliases))),
|
attribute.Int64("tag_aliases_count", int64(len(tagsAliases))),
|
||||||
)
|
)
|
||||||
|
|
||||||
span.AddEvent("Starting batch tag alias creation")
|
utils.HandleEvent(span, localLogger, "Starting batch tag alias creation")
|
||||||
|
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||||
}
|
}
|
||||||
|
|
||||||
if tagsAliases == nil || len(tagsAliases) == 0 {
|
if tagsAliases == nil || len(tagsAliases) == 0 {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty})
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty})
|
||||||
}
|
}
|
||||||
|
|
||||||
if batchSize == 0 {
|
if batchSize == 0 {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_aliases_count": len(tagsAliases),
|
|
||||||
}).Debug("attempting to create tags aliases")
|
|
||||||
|
|
||||||
result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize)
|
result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
||||||
loggerFields := log.Fields{
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
||||||
"tag_aliases_count": len(tagsAliases),
|
|
||||||
}
|
}
|
||||||
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
loggerFields := log.Fields{
|
utils.HandleEvent(span, localLogger, "Batch tags aliases created successfully")
|
||||||
"tag_aliases_count": len(tagsAliases),
|
|
||||||
}
|
|
||||||
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_aliases_count": len(tagsAliases),
|
|
||||||
}).Debug("batch tags aliases created")
|
|
||||||
|
|
||||||
span.AddEvent("Batch tags aliases created successfully")
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error {
|
func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error {
|
||||||
ctx, span := tracer.Start(ctx, "DeleteTagAlias")
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagAlias")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
localLogger.WithFields(log.Fields{
|
||||||
|
"tag_alias_name": tagAliasName,
|
||||||
|
})
|
||||||
|
|
||||||
span.SetAttributes(
|
span.SetAttributes(
|
||||||
attribute.String("tag_alias_name", string(tagAliasName)),
|
attribute.String("tag_alias_name", string(tagAliasName)),
|
||||||
)
|
)
|
||||||
|
|
||||||
span.AddEvent("Starting tag alias deletion")
|
utils.HandleEvent(span, localLogger, "Starting tag alias deletion")
|
||||||
|
|
||||||
var tagAlias models.TagAlias
|
var tagAlias models.TagAlias
|
||||||
|
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tagAliasName) == 0 {
|
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty})
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_alias_name": tagAliasName,
|
|
||||||
}).Debug("attempting to delete tag alias")
|
|
||||||
|
|
||||||
result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName)
|
result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||||
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
||||||
loggerFields := log.Fields{
|
|
||||||
"tag_alias_name": tagAliasName,
|
|
||||||
}
|
}
|
||||||
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound})
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
loggerFields := log.Fields{
|
utils.HandleEvent(span, localLogger, "Tag alias deleted successfully")
|
||||||
"tag_alias_name": tagAliasName,
|
|
||||||
}
|
|
||||||
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
|
||||||
"tag_alias_name": tagAliasName,
|
|
||||||
}).Debug("tag alias deleted")
|
|
||||||
|
|
||||||
span.AddEvent("Tag alias deleted successfully")
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -13,18 +13,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
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")
|
|
||||||
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagGroup")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
localLogger.WithFields(log.Fields{
|
||||||
|
"tag_group_name": tagGroupName,
|
||||||
|
"tag_name": tagName,
|
||||||
|
})
|
||||||
|
|
||||||
span.SetAttributes(
|
span.SetAttributes(
|
||||||
attribute.String("tag_group_name", string(tagGroupName)),
|
attribute.String("tag_group_name", string(tagGroupName)),
|
||||||
attribute.String("tag_name", string(tagName)),
|
attribute.String("tag_name", string(tagName)),
|
||||||
)
|
)
|
||||||
|
|
||||||
span.AddEvent("Starting tag group creation")
|
utils.HandleEvent(span, localLogger, "Starting tag group creation")
|
||||||
|
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return models.TagGroup{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||||
}
|
}
|
||||||
|
|
||||||
tagGroup := models.TagGroup{
|
tagGroup := models.TagGroup{
|
||||||
@ -32,132 +38,92 @@ 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{}, utils.HandleError(ctx, span, localLogger, &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{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
loggerFields := log.Fields{
|
utils.HandleEvent(span, localLogger, "Tag group created successfully")
|
||||||
"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")
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
localLogger.WithFields(log.Fields{
|
||||||
|
"tag_groups_count": len(tagsGroups),
|
||||||
|
"batch_size": batchSize,
|
||||||
|
})
|
||||||
|
|
||||||
span.SetAttributes(
|
span.SetAttributes(
|
||||||
attribute.Int64("batch_size", int64(batchSize)),
|
attribute.Int64("batch_size", int64(batchSize)),
|
||||||
attribute.Int64("tag_group_count", int64(len(tagsGroups))),
|
attribute.Int64("tag_group_count", int64(len(tagsGroups))),
|
||||||
)
|
)
|
||||||
|
|
||||||
span.AddEvent("Starting batch tag group creation")
|
utils.HandleEvent(span, localLogger, "Starting batch tag group creation")
|
||||||
|
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||||
}
|
}
|
||||||
|
|
||||||
if tagsGroups == nil || len(tagsGroups) == 0 {
|
if tagsGroups == nil || len(tagsGroups) == 0 {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty})
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty})
|
||||||
}
|
}
|
||||||
|
|
||||||
if batchSize == 0 {
|
if batchSize == 0 {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
|
return utils.HandleError(ctx, span, localLogger, &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) {
|
||||||
loggerFields := log.Fields{
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
||||||
"tag_group_count": len(tagsGroups),
|
|
||||||
}
|
|
||||||
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
|
|
||||||
|
|
||||||
}
|
}
|
||||||
loggerFields := log.Fields{
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
||||||
"tag_group_count": len(tagsGroups),
|
|
||||||
}
|
|
||||||
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.WithContext(ctx).WithFields(log.Fields{
|
utils.HandleEvent(span, localLogger, "Tag group created successfully")
|
||||||
"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")
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagGroup")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
span.SetAttributes(
|
span.SetAttributes(
|
||||||
attribute.String("tag_group_name", string(tagGroupName)),
|
attribute.String("tag_group_name", string(tagGroupName)),
|
||||||
)
|
)
|
||||||
|
|
||||||
span.AddEvent("Starting tag group deletion")
|
localLogger.WithFields(log.Fields{
|
||||||
|
"tag_group_name": tagGroupName,
|
||||||
|
})
|
||||||
|
|
||||||
|
utils.HandleEvent(span, localLogger, "Starting tag group deletion")
|
||||||
|
|
||||||
var tagGroup models.TagGroup
|
var tagGroup models.TagGroup
|
||||||
|
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tagGroupName) == 0 {
|
if len(tagGroupName) == 0 {
|
||||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagGroupNameIsEmpty})
|
return utils.HandleError(ctx, span, localLogger, &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 utils.HandleError(ctx, span, localLogger, &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 utils.HandleError(ctx, span, localLogger, result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
loggerFields := log.Fields{
|
utils.HandleEvent(span, localLogger, "Tag group deleted successfully")
|
||||||
"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
|
||||||
}
|
}
|
||||||
|
@ -3,77 +3,106 @@ 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 CreateUser(ctx context.Context, id models.UserID) (models.User, error) {
|
func CreateUser(ctx context.Context, user models.User) (models.User, error) {
|
||||||
if client == nil {
|
|
||||||
return models.User{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
|
||||||
}
|
|
||||||
|
|
||||||
user := models.User{
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUser")
|
||||||
BaseModel: models.BaseModel[models.UserID]{
|
defer span.End()
|
||||||
ID: id,
|
|
||||||
},
|
localLogger.WithFields(log.Fields{
|
||||||
|
"user_id": user.ID,
|
||||||
|
})
|
||||||
|
|
||||||
|
span.SetAttributes(
|
||||||
|
attribute.String("user_id", string(user.ID)),
|
||||||
|
)
|
||||||
|
|
||||||
|
utils.HandleEvent(span, localLogger, "Starting user creation")
|
||||||
|
|
||||||
|
if client == nil {
|
||||||
|
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||||
}
|
}
|
||||||
|
|
||||||
result := client.WithContext(ctx).Create(&user)
|
result := client.WithContext(ctx).Create(&user)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
||||||
return models.User{}, &otterError.Database{Reason: otterError.DuplicateKey}
|
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
||||||
}
|
}
|
||||||
return models.User{}, result.Error
|
return models.User{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utils.HandleEvent(span, localLogger, "User created successfully")
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserByID(ctx context.Context, id models.UserID) (models.User, error) {
|
func GetUserByID(ctx context.Context, id models.UserID) (models.User, error) {
|
||||||
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserByID")
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
|
span.SetAttributes(
|
||||||
|
attribute.String("user_id", string(id)),
|
||||||
|
)
|
||||||
|
|
||||||
|
localLogger.WithFields(log.Fields{
|
||||||
|
"user_id": id,
|
||||||
|
})
|
||||||
|
|
||||||
|
utils.HandleEvent(span, localLogger, "Starting user retrieval")
|
||||||
|
|
||||||
var user models.User
|
var user models.User
|
||||||
|
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return models.User{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||||
}
|
|
||||||
|
|
||||||
if len(id) == 0 {
|
|
||||||
return models.User{}, &otterError.EntityValidationFailed{Reason: otterError.UserIDIsEmpty}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(id) != 25 {
|
|
||||||
return models.User{}, &otterError.EntityValidationFailed{Reason: otterError.UserIDToShort}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result := client.WithContext(ctx).First(&user, "id = ?", id)
|
result := client.WithContext(ctx).First(&user, "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.User{}, &otterError.Database{Reason: otterError.NoDataFound}
|
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
||||||
}
|
}
|
||||||
return models.User{}, result.Error
|
return models.User{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utils.HandleEvent(span, localLogger, "User retrieved successfully")
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteUser(ctx context.Context, id models.UserID) error {
|
func DeleteUser(ctx context.Context, id models.UserID) error {
|
||||||
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUser")
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
|
span.SetAttributes(
|
||||||
|
attribute.String("user_id", string(id)),
|
||||||
|
)
|
||||||
|
|
||||||
|
localLogger.WithFields(log.Fields{
|
||||||
|
"user_id": id,
|
||||||
|
})
|
||||||
|
|
||||||
|
utils.HandleEvent(span, localLogger, "Starting user deletion")
|
||||||
|
|
||||||
var user models.User
|
var user models.User
|
||||||
|
|
||||||
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 {
|
|
||||||
return &otterError.EntityValidationFailed{Reason: otterError.UserIDIsEmpty}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result := client.WithContext(ctx).Delete(&user, id)
|
result := client.WithContext(ctx).Delete(&user, 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 deleted successfully")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user