[Issue #7] Add event metadata support #33

Merged
HugoNijhuis merged 1 commits from issue-7-event-metadata-support into main 2026-01-09 17:14:39 +00:00
Owner

Summary

Add optional metadata field to Event for correlation IDs, causation IDs, and user context to support distributed tracing and auditing.

Changes

  • Add Metadata field (map[string]string) to Event struct with omitempty JSON tag
  • Add metadata key constants: MetadataKeyCorrelationID, MetadataKeyCausationID, MetadataKeyUserID, MetadataKeyTraceID, MetadataKeySpanID
  • Add helper methods for setting/getting common metadata:
    • SetMetadata(key, value) / GetMetadata(key)
    • SetCorrelationID / GetCorrelationID
    • SetCausationID / GetCausationID
    • SetUserID / GetUserID
    • SetTraceID / GetTraceID
    • SetSpanID / GetSpanID
  • Add WithMetadataFrom(source) helper for copying metadata between events (useful for event chaining)
  • Add comprehensive unit tests for metadata serialization and helper methods
  • Add store tests verifying metadata is persisted and retrieved correctly

Acceptance Criteria

  • Add Metadata field to Event struct (map[string]string or similar)
  • Metadata is persisted and retrieved correctly
  • Metadata is optional (nil/empty allowed)
  • Update serialization to include metadata
  • Add helper methods for common metadata (correlation ID, causation ID)

Closes #7

## Summary Add optional metadata field to Event for correlation IDs, causation IDs, and user context to support distributed tracing and auditing. ## Changes - Add `Metadata` field (`map[string]string`) to Event struct with `omitempty` JSON tag - Add metadata key constants: `MetadataKeyCorrelationID`, `MetadataKeyCausationID`, `MetadataKeyUserID`, `MetadataKeyTraceID`, `MetadataKeySpanID` - Add helper methods for setting/getting common metadata: - `SetMetadata(key, value)` / `GetMetadata(key)` - `SetCorrelationID` / `GetCorrelationID` - `SetCausationID` / `GetCausationID` - `SetUserID` / `GetUserID` - `SetTraceID` / `GetTraceID` - `SetSpanID` / `GetSpanID` - Add `WithMetadataFrom(source)` helper for copying metadata between events (useful for event chaining) - Add comprehensive unit tests for metadata serialization and helper methods - Add store tests verifying metadata is persisted and retrieved correctly ## Acceptance Criteria - [x] Add Metadata field to Event struct (map[string]string or similar) - [x] Metadata is persisted and retrieved correctly - [x] Metadata is optional (nil/empty allowed) - [x] Update serialization to include metadata - [x] Add helper methods for common metadata (correlation ID, causation ID) Closes #7
HugoNijhuis added 1 commit 2026-01-09 16:53:20 +00:00
Add event metadata support for distributed tracing and auditing
All checks were successful
CI / build (pull_request) Successful in 15s
97ff1c3346
- Add Metadata field (map[string]string) to Event struct with omitempty
- Add helper methods for common metadata: SetCorrelationID/GetCorrelationID,
  SetCausationID/GetCausationID, SetUserID/GetUserID, SetTraceID/GetTraceID,
  SetSpanID/GetSpanID
- Add WithMetadataFrom helper for copying metadata between events
- Add metadata key constants for standard fields
- Add comprehensive unit tests for metadata serialization and helpers
- Add store tests verifying metadata persistence

Closes #7

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Author
Owner

AI Code Review

This is an automated review generated by the code-reviewer agent.

Summary

This PR adds comprehensive event metadata support for distributed tracing and auditing. The implementation is clean, well-tested, and follows Go best practices. The feature enables correlation of events across services using standard metadata fields like correlation ID, causation ID, user ID, and OpenTelemetry trace/span IDs.

Note: This PR branch includes commits from other pending PRs (hashring tests, eventstore tests, snapshot tests). Only the most recent commit (97ff1c3) addresses issue #7.

Findings

Code Quality

Excellent:

  • Clean API design with helper methods for common metadata fields
  • Proper initialization handling in SetMetadata (nil-safe)
  • Good use of constants for standard metadata keys
  • Clear, concise method implementations
  • Comprehensive documentation in comments

Minor observation:

  • The CommandID field in Event struct (line 36 in event.go) serves a similar purpose to CorrelationID in metadata. Consider documenting the relationship or deprecating CommandID in favor of metadata.correlationId for consistency.

Potential Bugs

No issues found. The implementation correctly handles:

  • Nil metadata maps (defensive programming in GetMetadata/WithMetadataFrom)
  • Empty metadata maps
  • Map initialization in SetMetadata
  • Deep copy in WithMetadataFrom (copies values, not reference)

Security Concerns

No security concerns identified.

  • Metadata is map[string]string, preventing injection of complex types
  • No user input is directly processed by these methods
  • Metadata is properly serialized/deserialized through JSON

Style Notes

Consistent with codebase conventions:

  • Follows Go naming conventions (exported constants, camelCase JSON tags)
  • Uses omitempty JSON tag appropriately for optional field
  • Method naming follows Go getter/setter patterns
  • Comments follow godoc standards

Test Coverage

Outstanding test coverage:

  • JSON serialization/deserialization with metadata
  • omitempty behavior verification
  • All helper methods tested (Get/Set for each metadata type)
  • Nil and empty metadata handling
  • WithMetadataFrom functionality including edge cases
  • Store persistence verification for InMemoryEventStore
  • 468 lines of test code in event_test.go
  • 219 lines of store integration tests in store/memory_test.go

Test quality highlights:

  • Tests verify both behavior and implementation details (e.g., checking correct metadata key constants)
  • Edge cases covered (nil source, empty metadata, nil metadata)
  • Integration tests verify metadata survives persistence layer

Verdict

LGTM

This implementation is production-ready. The code is clean, well-documented, comprehensive test coverage validates all functionality, and the feature enables important distributed tracing capabilities. The minor observation about CommandID vs CorrelationID is worth considering for future cleanup but is not blocking.

Great work on the comprehensive test coverage and thoughtful API design!

## AI Code Review > This is an automated review generated by the code-reviewer agent. ### Summary This PR adds comprehensive event metadata support for distributed tracing and auditing. The implementation is clean, well-tested, and follows Go best practices. The feature enables correlation of events across services using standard metadata fields like correlation ID, causation ID, user ID, and OpenTelemetry trace/span IDs. **Note:** This PR branch includes commits from other pending PRs (hashring tests, eventstore tests, snapshot tests). Only the most recent commit (97ff1c3) addresses issue #7. ### Findings #### Code Quality **Excellent:** - Clean API design with helper methods for common metadata fields - Proper initialization handling in SetMetadata (nil-safe) - Good use of constants for standard metadata keys - Clear, concise method implementations - Comprehensive documentation in comments **Minor observation:** - The `CommandID` field in Event struct (line 36 in event.go) serves a similar purpose to `CorrelationID` in metadata. Consider documenting the relationship or deprecating CommandID in favor of metadata.correlationId for consistency. #### Potential Bugs **No issues found.** The implementation correctly handles: - Nil metadata maps (defensive programming in GetMetadata/WithMetadataFrom) - Empty metadata maps - Map initialization in SetMetadata - Deep copy in WithMetadataFrom (copies values, not reference) #### Security Concerns **No security concerns identified.** - Metadata is map[string]string, preventing injection of complex types - No user input is directly processed by these methods - Metadata is properly serialized/deserialized through JSON #### Style Notes **Consistent with codebase conventions:** - Follows Go naming conventions (exported constants, camelCase JSON tags) - Uses `omitempty` JSON tag appropriately for optional field - Method naming follows Go getter/setter patterns - Comments follow godoc standards #### Test Coverage **Outstanding test coverage:** - JSON serialization/deserialization with metadata - omitempty behavior verification - All helper methods tested (Get/Set for each metadata type) - Nil and empty metadata handling - WithMetadataFrom functionality including edge cases - Store persistence verification for InMemoryEventStore - 468 lines of test code in event_test.go - 219 lines of store integration tests in store/memory_test.go **Test quality highlights:** - Tests verify both behavior and implementation details (e.g., checking correct metadata key constants) - Edge cases covered (nil source, empty metadata, nil metadata) - Integration tests verify metadata survives persistence layer ### Verdict **LGTM** This implementation is production-ready. The code is clean, well-documented, comprehensive test coverage validates all functionality, and the feature enables important distributed tracing capabilities. The minor observation about CommandID vs CorrelationID is worth considering for future cleanup but is not blocking. Great work on the comprehensive test coverage and thoughtful API design!
HugoNijhuis merged commit 7d3acd89ed into main 2026-01-09 17:14:39 +00:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: flowmade-one/aether#33