refactor(tracing): tracing & logging

added the custom util functions to handel telemetry
This commit is contained in:
SoXX 2024-08-12 11:32:53 +02:00
parent ffc3164cb7
commit 4d262c8fff

View File

@ -3,34 +3,65 @@ 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 CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserSource")
defer span.End()
localLogger.WithFields(log.Fields{
"user_source_id": userSource.ID,
})
span.SetAttributes(
attribute.String("user_source_id", string(userSource.SourceID)),
)
utils.HandleEvent(span, localLogger, "Starting user source creation")
if client == nil {
return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).Create(&userSource)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.UserSource{}, &otterError.Database{Reason: otterError.DuplicateKey}
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.UserSource{}, result.Error
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User source created successfully")
return userSource, nil
}
func UpdateUserSource(ctx context.Context, userSource models.UserSource) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserSource")
defer span.End()
localLogger.WithFields(log.Fields{
"user_source_id": userSource.ID,
})
span.SetAttributes(
attribute.String("user_source_id", string(userSource.ID)),
)
utils.HandleEvent(span, localLogger, "Starting user source update")
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(userSource.ID) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty}
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.UserSourceIDIsEmpty})
}
updatedUserSource := models.UserSource{
@ -47,62 +78,89 @@ func UpdateUserSource(ctx context.Context, userSource models.UserSource) error {
result := client.WithContext(ctx).Updates(&updatedUserSource)
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, "User source updated successfully")
return nil
}
func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserSourceByID")
defer span.End()
localLogger.WithFields(log.Fields{
"user_source_id": id,
})
span.SetAttributes(
attribute.String("user_source_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get user source by ID")
var user models.UserSource
if client == nil {
return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty}
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty})
}
if len(id) != 25 {
return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort})
}
result := client.WithContext(ctx).First(&user, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound}
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, otterError.Database{Reason: otterError.NoDataFound})
}
return models.UserSource{}, result.Error
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User source retrieved successfully")
return user, nil
}
func DeleteUserSource(ctx context.Context, id models.UserSourceID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserSource")
defer span.End()
localLogger.WithFields(log.Fields{
"user_source_id": id,
})
span.SetAttributes(
attribute.String("user_source_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete user source")
var user models.UserSource
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.UserSourceIDIsEmpty}
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty})
}
if len(id) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort})
}
result := client.WithContext(ctx).Delete(&user, 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
}
utils.HandleEvent(span, localLogger, "User source deleted successfully")
return nil
}