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

Log domain events #2817

Merged
merged 17 commits into from
Aug 2, 2024
Merged

Log domain events #2817

merged 17 commits into from
Aug 2, 2024

Conversation

nelsonkopliku
Copy link
Member

@nelsonkopliku nelsonkopliku commented Jul 24, 2024

Description

This PR extends Activity Logging capabilities so that all domain events can be tracked as activity log entries.

How was this tested?

Automated tests.

@nelsonkopliku nelsonkopliku self-assigned this Jul 24, 2024
@nelsonkopliku nelsonkopliku added enhancement New feature or request user story elixir Pull requests that update Elixir code env Create an ephimeral environment for the pr branch labels Jul 24, 2024
@nelsonkopliku nelsonkopliku force-pushed the log-domain-events branch 4 times, most recently from 4f9501b to 13690ff Compare July 25, 2024 08:03
@nelsonkopliku nelsonkopliku marked this pull request as ready for review July 30, 2024 15:05
alias Trento.ActivityLog.ActivityLog
alias Trento.Infrastructure.Commanded.EventHandlers.ActivityLogEventHandler

test "should log a domain event" do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty useless test 🙈, as we don't have anything really interesting happening here, except from calling the ActivityLogger. I can get rid of this.

metadata: metadata
})

:ok
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A doubt I have here is: if for any reason logging a specific interesting domain event fails, we'd be continuing silently.

If we want to be more reliable and have the event handler retry, we might want to not always return :ok.
That means having ActivityLogger.log_activity possibly return an :ok/:error tuple.

I guess we can have this improvement as a followup.

lib/trento/activity_logging/activity_catalog.ex Outdated Show resolved Hide resolved
Comment on lines 85 to 92
|> Enum.filter(fn
["Trento", resource_type, "Events", _]
when resource_type in [
"Hosts",
"Clusters",
"SapSystems",
"Databases"
] ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expectation on the name structure of modules that contain events can be turned into a Credo check, for better enforcing consistency.

@nelsonkopliku nelsonkopliku force-pushed the log-domain-events branch 2 times, most recently from f50947e to a17a387 Compare July 31, 2024 20:00
@nelsonkopliku
Copy link
Member Author

Hey @gagandeepb @balanza the whole macros stuff have been revisited and eventually removed.
Take a look at your convenience.

Copy link
Contributor

@gagandeepb gagandeepb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good! Just one minor comment.

Comment on lines +227 to +231
for {event, expected_activity_type} <- events do
metadata =
event
|> Jason.encode!()
|> Jason.decode!()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you encode and decode again?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because what gets stored is an arrow map, while the event itself is an atom map

%{
  "foo" => "bar"
}
# vs
%{
  foo: "bar"
}

Any suggestion on different approaches?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something this should work/help:

Map.new(map_with_string_keys, fn {k, v} -> {String.to_exisiting_atom(k), v} end)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried, not working tho 😅

protocol Enumerable not implemented for %Trento.Hosts.Events.HostRegistered (and any other event struct I guess)

@nelsonkopliku nelsonkopliku merged commit 5cd0a17 into main Aug 2, 2024
26 checks passed
@nelsonkopliku nelsonkopliku deleted the log-domain-events branch August 2, 2024 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
elixir Pull requests that update Elixir code enhancement New feature or request env Create an ephimeral environment for the pr branch user story
Development

Successfully merging this pull request may close these issues.

3 participants