From 5e9b4ff5327fbef4c768a4df7f358b8a62252bc9 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 13 Aug 2024 15:43:45 +0200 Subject: [PATCH] refactor(tracing): Refactor source ID logging and tracing --- pkg/database/source.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/pkg/database/source.go b/pkg/database/source.go index 06fc8a7..d2e56b8 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -16,14 +16,6 @@ func CreateSource(ctx context.Context, source models.Source) (models.Source, err ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSource") 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") 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) } + 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") return source, nil } @@ -82,6 +82,11 @@ func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize 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 { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateSource") defer span.End() @@ -105,12 +110,15 @@ func UpdateSource(ctx context.Context, source models.Source) error { } updateSource := models.Source{ + BaseModel: models.BaseModel[models.SourceID]{ + ID: source.ID, + }, DisplayName: source.DisplayName, Domain: source.Domain, 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 errors.Is(result.Error, gorm.ErrRecordNotFound) { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})