From 216f8543e00792f514ec351192ff81c99fd2fef3 Mon Sep 17 00:00:00 2001 From: schromp Date: Sun, 25 Jan 2026 18:02:01 +0100 Subject: [PATCH] complete manual song instrumentation --- backend/internal/store/song.go | 69 +++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/backend/internal/store/song.go b/backend/internal/store/song.go index 68c6c7a..cb4097e 100644 --- a/backend/internal/store/song.go +++ b/backend/internal/store/song.go @@ -8,13 +8,22 @@ import ( "log/slog" m "echsen.club/radio/internal/models" + "echsen.club/radio/internal/otel" "github.com/google/uuid" "github.com/lib/pq" + "github.com/samber/lo" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/codes" ) var ErrSongExists = errors.New("song already exists") func (store *Store) CreateSong(ctx context.Context, owner_id uuid.UUID, filename string) (*m.Song, error) { + rootSpan := otel.GetRootSpan(ctx) + rootSpan.SetAttributes( + attribute.String("song.owner.id", owner_id.String()), + attribute.String("song.filename", filename), + ) var song m.Song query := ` @@ -29,26 +38,33 @@ func (store *Store) CreateSong(ctx context.Context, owner_id uuid.UUID, filename if errors.As(err, &pqErr) { if pqErr.Code == "23505" { - slog.Info("song already exists") + rootSpan.SetAttributes(attribute.String("song.status", "already_exists")) return nil, ErrSongExists } } - slog.Error("Error inserting song into database", "error", err) + rootSpan.RecordError(err) + rootSpan.SetStatus(codes.Error, "song_create_failed") return nil, fmt.Errorf("failed to insert song into database") } - slog.Info("Insert song into database") + + rootSpan.SetAttributes( + attribute.String("song.id", song.ID.String()), + attribute.String("song.created_at", song.CreatedAt.String()), + ) return &song, nil } func (store *Store) DeleteSong(ctx context.Context, song_id uuid.UUID) error { + rootSpan := otel.GetRootSpan(ctx) query := ` DELETE FROM songs WHERE id=$1; ` _, err := store.db.ExecContext(ctx, query, song_id) if err != nil { - slog.Error("couldnt delete song from database") + rootSpan.RecordError(err) + rootSpan.SetStatus(codes.Error, "song_delete_failed") return fmt.Errorf("couldnt delete song") } @@ -58,6 +74,9 @@ func (store *Store) DeleteSong(ctx context.Context, song_id uuid.UUID) error { var ErrSongDoesntExist = errors.New("song doesnt exist") func (store *Store) GetSong(ctx context.Context, song_id uuid.UUID) (*m.Song, error) { + rootSpan := otel.GetRootSpan(ctx) + rootSpan.SetAttributes(attribute.String("song.id", song_id.String())) + var s m.Song query := ` SELECT id, owner_id, filename, name, created_at, modified_at @@ -76,24 +95,62 @@ func (store *Store) GetSong(ctx context.Context, song_id uuid.UUID) (*m.Song, er if err != nil { if err == sql.ErrNoRows { + rootSpan.SetAttributes(attribute.String("song.status", "not_found")) return nil, ErrSongDoesntExist } + rootSpan.RecordError(err) + rootSpan.SetStatus(codes.Error, "song_get_failed") return nil, fmt.Errorf("failed to get song: %w", err) } + rootSpan.SetAttributes( + attribute.String("song.name", s.Name), + attribute.String("song.filename", s.Filename), + attribute.String("song.owner.id", s.OwnerID.String()), + attribute.String("song.created_at", s.CreatedAt.String()), + attribute.String("song.modified_at", s.ModifiedAt.String()), + ) + return &s, nil } func (s *Store) SongList(ctx context.Context) ([]m.Song, error) { + rootSpan := otel.GetRootSpan(ctx) + var songs []m.Song err := s.db.SelectContext(ctx, &songs, `SELECT * FROM songs`) - return songs, err + if err != nil { + rootSpan.RecordError(err) + rootSpan.SetStatus(codes.Error, "song_list_failed") + return nil, err + } + + rootSpan.SetAttributes( + attribute.StringSlice("song.list.ids", lo.Map(songs, func(s m.Song, _ int) string { return s.ID.String() })), + attribute.Int("song.list.count", len(songs)), + ) + + return songs, nil } func (s *Store) SongListByUser(ctx context.Context, id uuid.UUID) ([]m.Song, error) { + rootSpan := otel.GetRootSpan(ctx) + rootSpan.SetAttributes(attribute.String("song.owner.id", id.String())) + var songs []m.Song err := s.db.SelectContext(ctx, &songs, `SELECT * FROM songs WHERE owner_id = $1`, id) - return songs, err + if err != nil { + rootSpan.RecordError(err) + rootSpan.SetStatus(codes.Error, "song_list_by_user_failed") + return nil, err + } + + rootSpan.SetAttributes( + attribute.StringSlice("song.list.ids", lo.Map(songs, func(s m.Song, _ int) string { return s.ID.String() })), + attribute.Int("song.list.count", len(songs)), + ) + + return songs, nil } -- 2.51.2