Skip to content

Commit

Permalink
Merge branch 'feat/DEX-2166/query-logs-contexts' into 'master'
Browse files Browse the repository at this point in the history
[DEX-2166] feat: Add ContextItemProcessMiddleware for execution context tracking

Closes DEX-2166

See merge request nstmrt/rubygems/outbox!100
  • Loading branch information
bibendi committed Sep 9, 2024
2 parents c451529 + c09fb5b commit bb1660d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

## [6.8.0] - 2024-09-05

### Added

- Add outbox context to ActiveRecord query log tags

## [6.7.0] - 2024-08-29

### Added
Expand Down
10 changes: 10 additions & 0 deletions config/initializers/active_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

if ActiveRecord.version >= Gem::Version.new("7.0.0")
Rails.application.config.active_record.tap do |config|
config.query_log_tags << {
box_name: ->(context) { context[:box_item]&.class&.box_name },
box_item_id: ->(context) { context[:box_item]&.uuid }
}
end
end
5 changes: 5 additions & 0 deletions lib/sbmt/outbox/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class Engine < Rails::Engine
c.batch_process_middlewares.push("Sbmt::Outbox::Middleware::Sentry::TracingBatchProcessMiddleware")
c.item_process_middlewares.push("Sbmt::Outbox::Middleware::Sentry::TracingItemProcessMiddleware")
end

if defined?(ActiveSupport::ExecutionContext)
require_relative "middleware/execution_context/context_item_process_middleware"
c.item_process_middlewares.push("Sbmt::Outbox::Middleware::ExecutionContext::ContextItemProcessMiddleware")
end
end

rake_tasks do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "sbmt/outbox/middleware/execution_context/context_item_process_middleware"

module Sbmt
module Outbox
module Middleware
module ExecutionContext
class ContextItemProcessMiddleware
def call(item)
ActiveSupport::ExecutionContext[:box_item] = item

yield
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/sbmt/outbox/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Sbmt
module Outbox
VERSION = "6.7.0"
VERSION = "6.8.0"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require "sbmt/outbox/middleware/execution_context/context_item_process_middleware"

describe Sbmt::Outbox::Middleware::ExecutionContext::ContextItemProcessMiddleware do
let(:outbox_item) { create(:outbox_item) }

before do
skip("not supported in the current Rails version") unless defined?(ActiveSupport::ExecutionContext)
end

it "sets execution context" do
described_class.new.call(outbox_item) {}
expect(ActiveSupport::ExecutionContext.to_h[:box_item]).to eq outbox_item
end
end
7 changes: 6 additions & 1 deletion spec/lib/sbmt/outbox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

describe "item_process_middlewares" do
it "returns default middlewares" do
expect(described_class.item_process_middlewares).to eq([described_class::Middleware::Sentry::TracingItemProcessMiddleware])
expect(described_class.item_process_middlewares)
.to include described_class::Middleware::Sentry::TracingItemProcessMiddleware
if defined?(ActiveSupport::ExecutionContext)
expect(described_class.item_process_middlewares)
.to include described_class::Middleware::ExecutionContext::ContextItemProcessMiddleware
end
end
end
end

0 comments on commit bb1660d

Please sign in to comment.