Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

story(sqsslog): create helpers to standardize log attributes related to sqs #37

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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...)
}
Loading