refactor(tracing): Refactor source ID logging and tracing

This commit is contained in:
SoXX 2024-08-13 15:43:45 +02:00
parent 450e06eb25
commit 5e9b4ff532

View File

@ -16,14 +16,6 @@ func CreateSource(ctx context.Context, source models.Source) (models.Source, err
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSource") ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSource")
defer span.End() defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"source_id": source.ID,
})
span.SetAttributes(
attribute.String("source_id", string(source.ID)),
)
utils.HandleEvent(span, localLogger, "Starting source creation") utils.HandleEvent(span, localLogger, "Starting source creation")
if client == nil { if client == nil {
@ -38,6 +30,14 @@ func CreateSource(ctx context.Context, source models.Source) (models.Source, err
return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error) return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error)
} }
localLogger = localLogger.WithFields(log.Fields{
"source_id": source.ID,
})
span.SetAttributes(
attribute.String("source_id", string(source.ID)),
)
utils.HandleEvent(span, localLogger, "Source created successfully") utils.HandleEvent(span, localLogger, "Source created successfully")
return source, nil return source, nil
} }
@ -82,6 +82,11 @@ func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize
return nil return nil
} }
// UpdateSource updates th source information in the database.
// Only a few parameter can be updated:
// - DisplayName
// - Domain
// - Icon
func UpdateSource(ctx context.Context, source models.Source) error { func UpdateSource(ctx context.Context, source models.Source) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateSource") ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateSource")
defer span.End() defer span.End()
@ -105,12 +110,15 @@ func UpdateSource(ctx context.Context, source models.Source) error {
} }
updateSource := models.Source{ updateSource := models.Source{
BaseModel: models.BaseModel[models.SourceID]{
ID: source.ID,
},
DisplayName: source.DisplayName, DisplayName: source.DisplayName,
Domain: source.Domain, Domain: source.Domain,
Icon: source.Icon, Icon: source.Icon,
} }
result := client.WithContext(ctx).Model(&updateSource).Update("deleted_at", gorm.DeletedAt{}) result := client.WithContext(ctx).Updates(&updateSource)
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}) return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})