From bd76f838ec568ea6f1443e5db01cd572952b5956 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 12 Aug 2024 15:53:45 +0200 Subject: [PATCH] refactor(tracing): fixed minor issues & added debug flag --- pkg/database/client.go | 16 ++++++++++------ pkg/database/favorite.go | 10 +++++----- pkg/database/post.go | 10 +++++----- pkg/database/source.go | 12 ++++++------ pkg/database/tag.go | 6 +++--- pkg/database/tagAlias.go | 6 +++--- pkg/database/tagGroup.go | 6 +++--- pkg/database/user.go | 6 +++--- pkg/database/userSource.go | 8 ++++---- 9 files changed, 42 insertions(+), 38 deletions(-) diff --git a/pkg/database/client.go b/pkg/database/client.go index 901ae9f..2f4c362 100644 --- a/pkg/database/client.go +++ b/pkg/database/client.go @@ -29,10 +29,16 @@ var ( ) func Connect(ctx context.Context, config models.DatabaseConfig) error { + setupTelemetry() + + if config.Debug { + log.SetLevel(log.DebugLevel) + } + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "Connect") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "endpoint": config.Endpoint, "port": config.Port, "database": config.Database, @@ -64,8 +70,6 @@ func Connect(ctx context.Context, config models.DatabaseConfig) error { return utils.HandleError(ctx, span, localLogger, err) } - setupTelemetry() - client = sqlDB utils.HandleEvent(span, localLogger, "Database connected successfully") return nil @@ -75,7 +79,7 @@ func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.Databas ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "migrateDatabase") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "database": config.Database, }) @@ -100,9 +104,9 @@ func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.Databas if config.Debug { if n != 0 { - localLogger.Infof("postgres migration: applied %d migrations!", n) + localLogger.Debug("applied %d migrations!", n) } else { - localLogger.Info("postgres migration: nothing to migrate") + localLogger.Debug("nothing to migrate") } } diff --git a/pkg/database/favorite.go b/pkg/database/favorite.go index c730176..335cf65 100644 --- a/pkg/database/favorite.go +++ b/pkg/database/favorite.go @@ -16,7 +16,7 @@ func CreateUserFavorite(ctx context.Context, userFav models.UserFavorite) (model ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavorite") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_favorite_id": userFav.ID, }) @@ -46,7 +46,7 @@ func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorit ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavoriteInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_favorite_count": len(userFav), "batch_size": batchSize, }) @@ -86,7 +86,7 @@ func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserFavorite") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_favorite_id": userFav.ID, }) @@ -124,7 +124,7 @@ func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserFavoritesByID") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_favorite_id": id, }) @@ -160,7 +160,7 @@ func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserFavorite") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_favorite_id": id, }) diff --git a/pkg/database/post.go b/pkg/database/post.go index 623995c..a92d231 100644 --- a/pkg/database/post.go +++ b/pkg/database/post.go @@ -16,7 +16,7 @@ func CreatePost(ctx context.Context, post models.Post) (models.Post, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePost") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "post_id": post.ID, }) @@ -46,7 +46,7 @@ func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) e ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePostInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "post_count": len(post), "batch_size": batchSize, }) @@ -86,7 +86,7 @@ func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetPostByID") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "post_id": id, }) @@ -122,7 +122,7 @@ func UpdatePost(ctx context.Context, anthrovePost models.Post) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePost") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "post_id": anthrovePost.ID, }) @@ -162,7 +162,7 @@ func DeletePost(ctx context.Context, id models.PostID) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePost") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "post_id": id, }) diff --git a/pkg/database/source.go b/pkg/database/source.go index eab4530..06fc8a7 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -16,7 +16,7 @@ func CreateSource(ctx context.Context, source models.Source) (models.Source, err ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_id": source.ID, }) @@ -46,7 +46,7 @@ func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSourceInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_count": len(source), "batch_size": batchSize, }) @@ -86,7 +86,7 @@ func UpdateSource(ctx context.Context, source models.Source) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_id": source.ID, }) @@ -126,7 +126,7 @@ func GetSourceByID(ctx context.Context, id models.SourceID) (models.Source, erro ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetSourceByID") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_id": id, }) @@ -166,7 +166,7 @@ func GetSourceByDomain(ctx context.Context, sourceDomain models.SourceDomain) (m ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetSourceByDomain") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_domain": sourceDomain, }) @@ -202,7 +202,7 @@ func DeleteSource(ctx context.Context, id models.SourceID) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "source_id": id, }) diff --git a/pkg/database/tag.go b/pkg/database/tag.go index 342285a..cdb7a14 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -16,7 +16,7 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTag") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_name": tagName, "tag_type": tagType, }) @@ -53,7 +53,7 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) err ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_count": len(tags), "batch_size": batchSize, }) @@ -93,7 +93,7 @@ func DeleteTag(ctx context.Context, tagName models.TagName) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTag") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_name": tagName, }) diff --git a/pkg/database/tagAlias.go b/pkg/database/tagAlias.go index e422672..f7cfc74 100644 --- a/pkg/database/tagAlias.go +++ b/pkg/database/tagAlias.go @@ -16,7 +16,7 @@ func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagNa ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAlias") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_alias_name": tagAliasName, "tag_name": tagName, }) @@ -53,7 +53,7 @@ func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, b ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_aliases_count": len(tagsAliases), "batch_size": batchSize, }) @@ -93,7 +93,7 @@ func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagAlias") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_alias_name": tagAliasName, }) diff --git a/pkg/database/tagGroup.go b/pkg/database/tagGroup.go index 39940b6..f90a7a6 100644 --- a/pkg/database/tagGroup.go +++ b/pkg/database/tagGroup.go @@ -17,7 +17,7 @@ func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagNa ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagGroup") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_group_name": tagGroupName, "tag_name": tagName, }) @@ -54,7 +54,7 @@ func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, ba ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_groups_count": len(tagsGroups), "batch_size": batchSize, }) @@ -100,7 +100,7 @@ func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error attribute.String("tag_group_name", string(tagGroupName)), ) - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "tag_group_name": tagGroupName, }) diff --git a/pkg/database/user.go b/pkg/database/user.go index c7346e6..840c54e 100644 --- a/pkg/database/user.go +++ b/pkg/database/user.go @@ -17,7 +17,7 @@ func CreateUser(ctx context.Context, user models.User) (models.User, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUser") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_id": user.ID, }) @@ -51,7 +51,7 @@ func GetUserByID(ctx context.Context, id models.UserID) (models.User, error) { attribute.String("user_id", string(id)), ) - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_id": id, }) @@ -83,7 +83,7 @@ func DeleteUser(ctx context.Context, id models.UserID) error { attribute.String("user_id", string(id)), ) - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_id": id, }) diff --git a/pkg/database/userSource.go b/pkg/database/userSource.go index 6576128..dc18af0 100644 --- a/pkg/database/userSource.go +++ b/pkg/database/userSource.go @@ -16,7 +16,7 @@ func CreateUserSource(ctx context.Context, userSource models.UserSource) (models ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_source_id": userSource.ID, }) @@ -46,7 +46,7 @@ func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_source_id": userSource.ID, }) @@ -91,7 +91,7 @@ func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.User ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserSourceByID") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_source_id": id, }) @@ -130,7 +130,7 @@ func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserSource") defer span.End() - localLogger.WithFields(log.Fields{ + localLogger = localLogger.WithFields(log.Fields{ "user_source_id": id, })