Skip to content

Commit

Permalink
story(sqsslog): create helpers to standardize log attributes related …
Browse files Browse the repository at this point in the history
…to sqs (#37)

* feat(issue-25): add helper package for standardizing sqs log messages

* refactor(issue-25): use helper package to ensure consistent log field names
  • Loading branch information
Zaba505 authored Dec 16, 2023
1 parent bb2ab98 commit 54c4682
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
11 changes: 10 additions & 1 deletion queue/sqs/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/z5labs/bedrock/pkg/otelslog"
"github.com/z5labs/bedrock/pkg/slogfield"
"github.com/z5labs/bedrock/queue"
"github.com/z5labs/bedrock/queue/sqs/sqsslog"

"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
Expand Down Expand Up @@ -242,7 +243,7 @@ func (p *BatchDeleteProcessor) Process(ctx context.Context, msgs []types.Message
p.log.ErrorContext(
spanCtx,
"failed to delete message",
slogfield.String("sqs_message_id", *entry.Id),
sqsslog.MessageId(deref(entry.Id)),
slogfield.String("sqs_error_code", *entry.Code),
slogfield.String("sqs_error_message", *entry.Message),
slogfield.Bool("sqs_sender_fault", entry.SenderFault),
Expand All @@ -251,3 +252,11 @@ func (p *BatchDeleteProcessor) Process(ctx context.Context, msgs []types.Message
}
return nil
}

func deref[T any](t *T) T {
var zero T
if t == nil {
return zero
}
return *t
}
27 changes: 27 additions & 0 deletions queue/sqs/sqsslog/sqsslog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2023 Z5Labs and Contributors
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

package sqsslog

import "log/slog"

// MessageId
func MessageId(s string) slog.Attr {
return slog.String("sqs_message_id", s)
}

// ReceiptHandle
func ReceiptHandle(s string) slog.Attr {
return slog.String("sqs_receipt_handle", s)
}

// MessageAttributes
func MessageAttributes(m map[string]string) slog.Attr {
attrs := make([]any, len(m))
for key, val := range m {
attrs = append(attrs, slog.String(key, val))
}
return slog.Group("sqs_message_attributes", attrs...)
}

0 comments on commit 54c4682

Please sign in to comment.