Skip to content

Commit 04ec16c

Browse files
backward compatible metadata keys (#4738)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a new constant for header prefix to improve metadata handling. - **Improvements** - Enhanced key retrieval logic for better backward compatibility in metadata access. - Improved error logging in message handling to include subscriber name for better traceability. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent b647737 commit 04ec16c

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

pkg/lib/envelope/metadata.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package envelope
22

33
import (
44
"strconv"
5+
"strings"
56
"time"
67
)
78

89
// Metadata keys
910
const (
11+
HeaderPrefix = "Bacalhau-"
1012
KeyMessageType = "Bacalhau-Type"
1113
KeyPayloadEncoding = "Bacalhau-PayloadEncoding"
1214
)
@@ -50,7 +52,15 @@ func NewMetadataFromMapCopy(m map[string]string) *Metadata {
5052

5153
// Get returns the value for a given key, or an empty string if the key doesn't exist
5254
func (m Metadata) Get(key string) string {
53-
return m[key]
55+
if v, ok := m[key]; ok {
56+
return v
57+
}
58+
// backward compatible with old headers
59+
if key == KeyMessageType || key == KeyPayloadEncoding {
60+
// return keys excluding header prefix
61+
return m[strings.TrimPrefix(key, HeaderPrefix)]
62+
}
63+
return ""
5464
}
5565

5666
// Has checks if a key exists in the metadata

pkg/lib/ncl/subscriber.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (s *subscriber) Subscribe(ctx context.Context, subjects ...string) error {
7575
// messageHandler is the callback function for message processing
7676
func (s *subscriber) handleNatsMessage(m *nats.Msg) {
7777
if err := s.processMessage(m); err != nil {
78-
log.Error().Err(err).Msg("failed to process message")
78+
log.Error().Err(err).Str("handler", s.config.Name).Msg("failed to process message")
7979

8080
s.consecutiveFailures += 1
8181
delay := s.config.Backoff.BackoffDuration(s.consecutiveFailures)

0 commit comments

Comments
 (0)