Skip to content

Commit 2076f2a

Browse files
committed
Add after_create transaction hook
Add a hook that allows for blocks of arbitrary code to be applied to modify a transaction after it has been created.
1 parent c523050 commit 2076f2a

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

lib/appsignal/transaction.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ def create(namespace)
4444
end
4545
end
4646

47+
# @api private
48+
# @return [Array<Proc>]
49+
# Add a block, if given, to be executed after a transaction is created.
50+
# The block will be called with the transaction as an argument.
51+
# Returns the array of blocks that will be executed.
52+
def after_create(&block)
53+
@after_create ||= Set.new
54+
55+
return @after_create if block.nil?
56+
57+
@after_create << block
58+
end
59+
4760
# @api private
4861
def set_current_transaction(transaction)
4962
Thread.current[:appsignal_transaction] = transaction
@@ -137,6 +150,8 @@ def initialize(namespace, id: SecureRandom.uuid, ext: nil)
137150
@namespace,
138151
0
139152
) || Appsignal::Extension::MockTransaction.new
153+
154+
run_after_create_hooks
140155
end
141156

142157
# @api private
@@ -596,6 +611,12 @@ def to_h
596611

597612
attr_reader :breadcrumbs
598613

614+
def run_after_create_hooks
615+
self.class.after_create.each do |block|
616+
block.call(self)
617+
end
618+
end
619+
599620
def _set_error(error)
600621
backtrace = cleaned_backtrace(error.backtrace)
601622
@ext.set_error(

spec/lib/appsignal/transaction_spec.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,78 @@ def to_s
21452145
end
21462146
end
21472147

2148+
describe "#after_create" do
2149+
it "stores the given hook when passed as a block" do
2150+
transaction = new_transaction
2151+
2152+
expect(Appsignal::Transaction.after_create).to be_empty
2153+
Appsignal::Transaction.after_create do |t|
2154+
t.set_action("hook_action")
2155+
end
2156+
2157+
expect(Appsignal::Transaction.after_create).to_not be_empty
2158+
2159+
expect(transaction).to_not have_action("hook_action")
2160+
Appsignal::Transaction.after_create.first.call(transaction)
2161+
expect(transaction).to have_action("hook_action")
2162+
end
2163+
2164+
it "stores the given hook when using <<" do
2165+
expect(Appsignal::Transaction.after_create).to be_empty
2166+
proc = proc do |transaction|
2167+
transaction.set_action("hook_action")
2168+
end
2169+
2170+
Appsignal::Transaction.after_create << proc
2171+
2172+
expect(Appsignal::Transaction.after_create).to eq(Set.new([proc]))
2173+
end
2174+
2175+
it "only stores a hook once when added several times" do
2176+
expect(Appsignal::Transaction.after_create).to be_empty
2177+
proc = proc do |transaction|
2178+
transaction.set_action("hook_action")
2179+
end
2180+
2181+
Appsignal::Transaction.after_create(&proc)
2182+
Appsignal::Transaction.after_create << proc
2183+
2184+
expect(Appsignal::Transaction.after_create).to eq(Set.new([proc]))
2185+
end
2186+
2187+
it "calls the given hook when a transaction is created" do
2188+
block = proc do |transaction|
2189+
transaction.set_action("hook_action")
2190+
end
2191+
2192+
Appsignal::Transaction.after_create(&block)
2193+
2194+
expect(block).to(
2195+
receive(:call)
2196+
.with(kind_of(Appsignal::Transaction))
2197+
.and_call_original
2198+
)
2199+
2200+
expect(new_transaction).to have_action("hook_action")
2201+
end
2202+
2203+
it "calls all the hooks in order" do
2204+
Appsignal::Transaction.after_create do |transaction|
2205+
transaction.set_namespace("hook_namespace_1")
2206+
transaction.set_action("hook_action_1")
2207+
end
2208+
2209+
Appsignal::Transaction.after_create do |transaction|
2210+
transaction.set_action("hook_action_2")
2211+
end
2212+
2213+
transaction = new_transaction
2214+
2215+
expect(transaction).to have_namespace("hook_namespace_1")
2216+
expect(transaction).to have_action("hook_action_2")
2217+
end
2218+
end
2219+
21482220
describe "#start_event" do
21492221
let(:transaction) { new_transaction }
21502222

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def spec_system_tmp_dir
100100
Appsignal::Testing.clear!
101101
Appsignal::Loaders.clear!
102102
Appsignal::CheckIn.clear!
103+
Appsignal::Transaction.after_create.clear
103104

104105
clear_current_transaction!
105106
stop_minutely_probes

0 commit comments

Comments
 (0)