Skip to content

Introducing: Tagged Events!

Compare
Choose a tag to compare
@smudge smudge released this 12 Nov 18:19
· 9 commits to master since this release
2e0dedf
  • This adds a new tagged: true option to journal_attributes, and Journaled.tagged/Journaled.tag! helpers that allow events to be tagged with contextual metadata (useful for audit logs and tracing):

    class MyEvent
      include Journaled::Event
      
      journal_attributes :attr_1, :attr_2, tagged: true
    end
    
    Journaled.tagged(foo: 'bar') do
      # events emitted in this scope will have a `tags` attribute with contents `{ "foo": "bar" }`
    end
    
    # events emitted in this scope will have a `tags` attribute with contents `{}`

    All "tagged" events will be given a tags attribute, but it can be empty. Consuming apps with strict schema enforcement ("additionalProperties": false) will need to include the "tags" field in any relevant event schemas. While this feature is not a breaking change for existing events, we have incremented the gem by a full minor version (to 4.1.0).

  • Under the hood, this removes RequestStore in favor of ActiveSupport::CurrentAttributes. This is the new convention for Rails apps (available in 5.2+), so in theory consuming apps shouldn't be impacted by this, but it is another reason to increment a full minor version at the least.

  • The README now includes two standard usage examples:

class ApplicationController < ActionController::Base
  before_action do
    Journaled.tag!(request_id: request.request_id, current_user_id: current_user&.id)
  end
end

class ApplicationJob < ActiveJob::Base
  around_perform do |job, perform|
    Journaled.tagged(job_id: job.job_id, &perform)
  end
end