HataluliRandima
Hi everyone
I have just published AuditTrailEx 0.1.0, an audit logging and change-history library for Ecto. I built it for my own Phoenix app and would love feedback before I go further.
What it does
It wraps your Repo writes and records a field-level diff of each change, in the same transaction as the change:
{:ok, user} =
AuditTrailEx.update(Repo, User.changeset(user, %{name: "Johnny"}),
actor: current_user,
metadata: %{request_id: "req_123"}
)
AuditTrailEx.history(Repo, User, user.id)
|> List.first()
|> AuditTrailEx.describe_text()
# Update MyApp.Accounts.User [42]
#
# Name changed
# From: "John"
# To: "Johnny"
It also works inside Ecto.Multi pipelines (AuditTrailEx.Multi.insert/update/delete). If the change or the audit write fails, the whole transaction rolls back.
Things I focused on
-
Safe defaults for secrets. Only persisted columns are recorded: virtual fields like
:passwordand association changesets are never stored. On top of that you can exclude or redact fields globally, per schema or per call. -
Field-level diffs, not full row snapshots, for updates.
-
Optional Postgres triggers. The wrapper approach misses
Repo.update_all, raw SQL and manual changes made inpsql. You can add a trigger per table, and those changes get recorded in the same table and format:# in a migration AuditTrailEx.Trigger.install() AuditTrailEx.Trigger.create(MyApp.Accounts.User)AuditTrailEx.with_context(Repo, [actor: current_user], fn -> Repo.update_all(User, set: [role: "member"]) end)Writes made through AuditTrailEx aren’t recorded twice.
-
Flexible actors via a protocol, so users, admins, API keys and background jobs can all be recorded.
-
Telemetry events that include field names but never field values.
-
Few dependencies:
ecto_sql,jasonandtelemetry. Phoenix and Plug are optional.
How it compares
There are good options already: PaperTrail and ExAudit wrap Repo calls, and Carbonite uses triggers. AuditTrailEx tries to offer both approaches writing to one table, with safe defaults for sensitive data. If one of those libraries already fits your needs, keep using it. I’m not trying to replace them.
Limitations (being upfront)
- It’s an early 0.x release, so the API may change before 1.0.
- It’s mainly tested on PostgreSQL, and the triggers need Postgres 13 or later.
- Exclusion and redaction depend on field names, so persisted secret columns (e.g.
hashed_password) must be listed in config. TRUNCATEisn’t captured.
Feedback wanted
- Revert and time travel is the next feature on my list:
AuditTrailEx.revert(Repo, event), and rebuilding a record as it was at a given time. Would you use it? How should it handle redacted fields, or records that changed after the event you’re reverting? - Is there anything in the API that feels awkward or un-Ecto-like?
- If you try it, bug reports and issues are very welcome.
- Hex: audit_trail_ex | Hex
- Docs: audit_trail_ex v0.1.0 — Documentation
- GitHub:
Thanks for reading!
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
rhcarvalho
Hi!
With the current approach used for auditing, I imagine transactionaly reverting a cascade of changes across tables would be challenging.
You mention an
eventin the API, but the update API takes a changeset. What event is this?Depending on the use case, you may want to query the state of the database at a certain point in time without necessarily reverting/changing the current state.
Look at native temporal features in Postgres (recently exposed in AshTemporal) and bitemporal databases like XTDB.
LostKobrakai
That native support in postgres, which AshTemporal was banking on, got removed from followup beta versions and won’t be in the next major version.
HataluliRandima
@rhcarvalho, good points:
event: it’s the%AuditTrailEx.Event{}row you get back fromAuditTrailEx.history/4or fromupdate(..., return_audit: true). The proposal didn’t make that clear, sorry.pg_current_xact_id()) on every event, from both the Elixir path and the triggers. That also answers “what did this request change?” without any revert.HataluliRandima
@LostKobrakai thanks for the heads-up. I won’t build anything on native temporal support then.
HataluliRandima
0.2.0 is out, following the feedback above:
AuditTrailEx.by_transaction(Repo, event)returns everything committed together.Upgrading: run
AuditTrailEx.Migration.add_transaction_id()in a migration before deploying. Existing events keepnil, and the table isn’t rewritten.Changelog: audit/CHANGELOG.md at main · HataluliRandima/audit · GitHub
Question for anyone interested: for reconstruction, how should fields that can’t be rebuilt be represented? Missing keys, a sentinel value, or a separate
unknown_fieldslist?