Repository navigation
Add support for Solid Queue #1623
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
446ce28
Add support for Solid Queue
lairtonmendes cc75634
Merge branch 'main' into feat/solid-queue-spy
smith 74fc56f
Merge branch 'main' into feat/solid-queue-spy
smith e837095
Merge branch 'main' into feat/solid-queue-spy
smith 4e0fdee
Changing the syntax to be compatible with an older version of Ruby.
lairtonmendes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,6 +171,7 @@ def available_instrumentations | |
| sinatra | ||
| sneakers | ||
| sns | ||
| solid_queue | ||
| sqs | ||
| sucker_punch | ||
| tilt | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Licensed to Elasticsearch B.V. under one or more contributor | ||
| # license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright | ||
| # ownership. Elasticsearch B.V. licenses this file to you under | ||
| # the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| # frozen_string_literal: true | ||
|
|
||
| module ElasticAPM | ||
| # @api private | ||
| module Spies | ||
| # @api private | ||
| class SolidQueueSpy | ||
| TYPE = 'SolidQueue' | ||
|
|
||
| # @api private | ||
| module Ext | ||
| def perform | ||
| name = job&.class_name | ||
| transaction = ElasticAPM.start_transaction(name, TYPE) | ||
| ElasticAPM.set_label(:queue, job.queue_name) if job&.queue_name | ||
|
|
||
| super | ||
|
|
||
| transaction&.done 'success' | ||
| transaction&.outcome = Transaction::Outcome::SUCCESS | ||
| rescue ::Exception => e | ||
| ElasticAPM.report(e, handled: false) | ||
| transaction&.done 'error' | ||
| transaction&.outcome = Transaction::Outcome::FAILURE | ||
| raise | ||
| ensure | ||
| ElasticAPM.end_transaction | ||
| end | ||
| end | ||
|
|
||
| def install | ||
| # +SolidQueue::ClaimedExecution+ lives under +app/models+ and is | ||
| # autoloaded via Zeitwerk by the Rails engine. | ||
| # | ||
| # Two hooks are needed: | ||
| # - +after_initialize+ fires after eager loading in production, which | ||
| # is when +ClaimedExecution+ is first defined. +to_prepare+ alone is | ||
| # not enough because Rails runs +to_prepare+ *before* eager loading. | ||
| # - +to_prepare+ handles code reloads in development so the patch | ||
| # survives class unloading between reloads. | ||
| # | ||
| # Fork mode (the default solid_queue supervisor) is handled by the agent | ||
| # itself via +Agent#detect_forking!+ on each +start_transaction+, so no | ||
| # extra lifecycle hook wiring is needed here. | ||
| if defined?(::Rails) && ::Rails.respond_to?(:application) && ::Rails.application | ||
| ::Rails.application.config.after_initialize { SolidQueueSpy.prepend_ext } | ||
| ::Rails.application.reloader.to_prepare { SolidQueueSpy.prepend_ext } | ||
| end | ||
|
|
||
| SolidQueueSpy.prepend_ext | ||
| end | ||
|
|
||
| def self.prepend_ext | ||
| return unless defined?(::SolidQueue::ClaimedExecution) | ||
| return if ::SolidQueue::ClaimedExecution.include?(Ext) | ||
|
|
||
| ::SolidQueue::ClaimedExecution.prepend(Ext) | ||
| end | ||
| end | ||
|
|
||
| register 'SolidQueue', 'solid_queue', SolidQueueSpy.new | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| # Licensed to Elasticsearch B.V. under one or more contributor | ||
| # license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright | ||
| # ownership. Elasticsearch B.V. licenses this file to you under | ||
| # the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
|
|
||
| # Solid Queue ships +SolidQueue::ClaimedExecution+ as an ActiveRecord model | ||
| # under +app/models+, loaded via the engine through Zeitwerk. To avoid | ||
| # requiring a full Rails boot here, the spec defines a stand-in constant with | ||
| # the same shape and lets the spy hook it. | ||
| module SolidQueue | ||
| class ClaimedExecution | ||
| attr_reader :job | ||
|
|
||
| def initialize(job) | ||
| @job = job | ||
| end | ||
|
|
||
| # Real solid_queue runs +ActiveJob::Base.execute+ here. For the spec we | ||
| # just dispatch to the test job stub so that +super+ inside the spy's | ||
| # +Ext+ module is exercised end-to-end. | ||
| def perform | ||
| job.run if job.respond_to?(:run) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| require 'elastic_apm/spies/solid_queue' | ||
|
|
||
| module ElasticAPM | ||
| RSpec.describe 'Spy: SolidQueue', :intercept do | ||
| class TestSolidQueueJob | ||
| attr_reader :queue_name | ||
|
|
||
| def initialize(queue_name: 'default') | ||
| @queue_name = queue_name | ||
| end | ||
|
|
||
| def class_name | ||
| self.class.name | ||
| end | ||
|
|
||
| def run | ||
| end | ||
| end | ||
|
|
||
| class ExplodingSolidQueueJob < TestSolidQueueJob | ||
| def run | ||
| raise ZeroDivisionError, 'boom' | ||
| end | ||
| end | ||
|
|
||
| it 'instruments successful job perform' do | ||
| with_agent do | ||
| ::SolidQueue::ClaimedExecution.new(TestSolidQueueJob.new).perform | ||
| end | ||
|
|
||
| transaction, = @intercepted.transactions | ||
| expect(transaction).to_not be_nil | ||
| expect(transaction.name).to eq 'ElasticAPM::TestSolidQueueJob' | ||
| expect(transaction.type).to eq 'SolidQueue' | ||
| expect(transaction.result).to eq 'success' | ||
| expect(transaction.outcome).to eq 'success' | ||
|
|
||
| labels = transaction.context.labels | ||
| expect(labels[:queue]).to eq 'default' | ||
| end | ||
|
|
||
| it 'reports errors and marks transaction as failure' do | ||
| expect do | ||
| with_agent do | ||
| ::SolidQueue::ClaimedExecution.new(ExplodingSolidQueueJob.new).perform | ||
| end | ||
| end.to raise_error(ZeroDivisionError) | ||
|
|
||
| transaction, = @intercepted.transactions | ||
| expect(transaction.name).to eq 'ElasticAPM::ExplodingSolidQueueJob' | ||
| expect(transaction.type).to eq 'SolidQueue' | ||
| expect(transaction.outcome).to eq 'failure' | ||
| expect(transaction.result).to eq 'error' | ||
|
|
||
| error, = @intercepted.errors | ||
| expect(error.exception.type).to eq 'ZeroDivisionError' | ||
| end | ||
|
|
||
| it 'captures the queue label from the job' do | ||
| with_agent do | ||
| job = TestSolidQueueJob.new(queue_name: 'critical') | ||
| ::SolidQueue::ClaimedExecution.new(job).perform | ||
| end | ||
|
|
||
| transaction, = @intercepted.transactions | ||
| expect(transaction.context.labels[:queue]).to eq 'critical' | ||
| end | ||
|
|
||
| it 'creates a transaction for each perform call' do | ||
| with_agent do | ||
| ::SolidQueue::ClaimedExecution.new(TestSolidQueueJob.new).perform | ||
| ::SolidQueue::ClaimedExecution.new(TestSolidQueueJob.new).perform | ||
| end | ||
|
|
||
| expect(@intercepted.transactions.size).to eq 2 | ||
| end | ||
|
|
||
| it 'prepends the Ext module onto SolidQueue::ClaimedExecution' do | ||
| expect(::SolidQueue::ClaimedExecution.ancestors) | ||
| .to include(Spies::SolidQueueSpy::Ext) | ||
| end | ||
|
|
||
| it "runs when the agent doesn't" do | ||
| expect do | ||
| ::SolidQueue::ClaimedExecution.new(TestSolidQueueJob.new).perform | ||
| end.to_not raise_error | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.