Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Appsignal monitoring #50

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.14.0 (2024-07-29)

- **[Breaking]** Adjust Appsignal middleware to use `Appsignal.monitor`.
To use the middleware the `appsignal` gem in version `>= 3.11.0` is required.
The configuration of the middleware changed and now only requires one option `class_name` and an optional `namespace`.

## 0.13.0 (2023-11-07)

- Allow adding multiple routing keys to the consumer configuration, configure method within consumer will only accept `routing_keys` array instead of `routing_key` string
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ears (0.13.0)
ears (0.14.0)
bunny (~> 2.22.0)
multi_json

Expand Down
14 changes: 6 additions & 8 deletions lib/ears/middlewares/appsignal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ module Middlewares
# A middleware that automatically wraps {Ears::Consumer#work} in an Appsignal transaction.
class Appsignal < Middleware
# @param [Hash] opts The options for the middleware.
# @option opts [String] :transaction_name The name of the Appsignal transaction.
# @option opts [String] :class_name The name of the class you want to monitor.
# @option opts [String] :namespace ('background') The namespace in which the action should appear.
def initialize(opts)
super()
@transaction_name = opts.fetch(:transaction_name)
@class_name = opts.fetch(:class_name)
@namespace = opts.fetch(:namespace, 'background')
end

def call(delivery_info, metadata, payload, app)
Expand All @@ -26,14 +26,12 @@ def call(delivery_info, metadata, payload, app)

private

attr_reader :transaction_name, :class_name
attr_reader :namespace, :class_name

def start_transaction(&block)
::Appsignal.monitor_transaction(
transaction_name,
class: class_name,
method: 'work',
queue_start: Time.now.utc,
::Appsignal.monitor(
namespace: namespace,
action: "#{class_name}#work",
&block
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ears/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ears
VERSION = '0.13.0'
VERSION = '0.14.0'
end
36 changes: 25 additions & 11 deletions spec/ears/middlewares/appsignal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,23 @@
let(:payload) { 'payload' }
let(:appsignal) { stub_const('Appsignal', Class.new) }
let(:middleware) do
Ears::Middlewares::Appsignal.new(
transaction_name: 'perform_job.test',
class_name: 'MyConsumer',
)
Ears::Middlewares::Appsignal.new(class_name: 'MyConsumer')
end
let(:now) { Time.utc(2020) }

before { allow(Time).to receive_message_chain(:now, :utc).and_return(now) }

it 'returns the result of the downstream middleware' do
expect(appsignal).to receive(:monitor_transaction).and_yield
expect(appsignal).to receive(:monitor).and_yield
expect(
middleware.call(delivery_info, metadata, payload, Proc.new { :moep }),
).to eq(:moep)
end

it 'starts an Appsignal transaction and calls the downstream middleware' do
expect(appsignal).to receive(:monitor_transaction).with(
'perform_job.test',
class: 'MyConsumer',
method: 'work',
queue_start: now,
expect(appsignal).to receive(:monitor).with(
namespace: 'background',
action: 'MyConsumer#work',
).and_yield
expect { |b|
middleware.call(delivery_info, metadata, payload, Proc.new(&b))
Expand All @@ -37,7 +32,7 @@

it 'calls set_error when an error is raised' do
error = RuntimeError.new('moep')
expect(appsignal).to receive(:monitor_transaction).and_yield
expect(appsignal).to receive(:monitor).and_yield
expect(appsignal).to receive(:set_error).with(error)

expect do
Expand All @@ -49,4 +44,23 @@
)
end.to raise_error(error)
end

context 'with namespace' do
let(:middleware) do
Ears::Middlewares::Appsignal.new(
namespace: 'cronjob',
class_name: 'MyConsumer',
)
end

it 'starts an Appsignal transaction with the given namespace and calls the downstream middleware' do
expect(appsignal).to receive(:monitor).with(
namespace: 'cronjob',
action: 'MyConsumer#work',
).and_yield
expect { |b|
middleware.call(delivery_info, metadata, payload, Proc.new(&b))
}.to yield_control
end
end
end