HataluliRandima

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 :password and 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 in psql. 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, jason and telemetry. 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.
  • TRUNCATE isn’t captured.

Feedback wanted

  1. 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?
  2. Is there anything in the API that feels awkward or un-Ecto-like?
  3. If you try it, bug reports and issues are very welcome.

Thanks for reading!

Showing Posts 1 to 5

rhcarvalho

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 event in 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

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

HataluliRandima OP

@rhcarvalho, good points:

  • The event: it’s the %AuditTrailEx.Event{} row you get back from AuditTrailEx.history/4 or from update(..., return_audit: true). The proposal didn’t make that clear, sorry.
  • Changes across tables: agreed. Each event covers one row right now, and nothing groups the events from a single transaction, so a revert across tables isn’t realistic yet. I think the right first step is recording the transaction id (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.
  • Querying vs reverting: this changed my mind. I’m going to put revert on hold and look at read-only reconstruction first (the record as of time T). It would be clear about what it can’t rebuild: excluded and redacted fields, and rows that existed before auditing started.
HataluliRandima

HataluliRandima OP

@LostKobrakai thanks for the heads-up. I won’t build anything on native temporal support then.

HataluliRandima

HataluliRandima OP

0.2.0 is out, following the feedback above:

  • Every event records the Postgres transaction id, from both the Elixir path and the triggers, so AuditTrailEx.by_transaction(Repo, event) returns everything committed together.
  • Insert events store the full row (id, timestamps, defaults), as groundwork for read-only reconstruction.

Upgrading: run AuditTrailEx.Migration.add_transaction_id() in a migration before deploying. Existing events keep nil, 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_fields list?

— All posts loaded —

Where Next? Top

Trending in Announcing Top

achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
gtcode
Aloha gang, There’s a new class of AI model out from TypeSafe. Jev is their flagship model. You can learn about it here: ...
New
danschultzer
I’ve moved the strategies out of PowAssent and created a multi-provider framework for Elixir. This way the strategies can be used in any ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
AstonJ
With advancements in AI gathering pace and set to increasingly accelerate, perhaps it’s worth having an annual thread on the topic - feel...
New
bartblast
If you haven’t tried Hologram yet, what’s stopped you? Here’s why I’m asking. Building a framework turns out to be two jobs, the framewo...
New
mat-hek
Popcorn, the library that runs Elixir code in the browser, is now stable :popcorn: This is possible because it now builds on a very stab...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews