Auditing userId and creation/modification date

Hello,
I’m thinking about audit attributes in our domain. What we basically need is to have informations about who and when created/modified entities.
I’ve read about auditing-interceptor but I’m not sure if I understand it correctly.

Let’s say I have command in which I don’t have any informations about user/date. So my best possibility is to add attributes (userId and currentTimestamp) to my command and fill those informations inside AuditingInterceptor (I’m using spring so I’d like to get informations from within spring beans)?
Then also extend my events with same attributes and implement MetaDataMutatingUnitOfWorkListenerAdapter?

Or is this approach wrong?

Thanks

Hi,

A better approach would be to add contextual data like that as metadata to the CommandMessage. Axon can then copy over the metadata to event messages that result from your command.

Use a CommandDispatchInterceptor to add the metadata to your command and an AuditDataProvider to copy the metadata to each event that gets published.

Best,
Rene

Hi Rene,
so basically I need to extend each event handler (where I need audit attributes) with MetaData attribute and get those required data from within it?

Thanks

Lukas

Can I also have MetaData in Sagas?

Hi Lukas,

Not sure I’m understanding correctly but if you’re asking how the metadata of an event can be read by an event handler then this is the way to go:

In the @EventHandler annotated method add a parameter annotated with @MetaData(key) to retrieve the metadata value mapped to the given key. Alternatively you can obtain the complete MetaData object by adding a MetaData parameter to your event handler method.

You can also do this for @SagaEventHandler annotated methods in sagas.

Rene

Hi Rene,
I understand that I need to add MetaData attribute to the @EventHandler ( or @EventSourcingHandler or @SagaEventHandler). This way I can get metadata inside this methods. If I don’t add this attribute are MetaData persisted anyway?

for example
MyEvent is published and catch inside @EventSourcingHandler, @SagaEventHandler and finally @EventHandler. I don’t need access to metadata inside @EventSourcingHandler and @SagaEventHandler but I will need it inside @EventHandler (to add attributes to query model).
Will be metadata available inside saga repository, in event sourcing repository and finally in eventhandler (in @EventHandler I will add
handle(MyEvent event, MetaData metadata)
)?

Yes, the MetaData always gets persisted in the domain event entry table. It does not matter if there are handlers for it or not.

OK. Thank you Rene.