Skip to content

Commit

Permalink
squash: added more tests... woof
Browse files Browse the repository at this point in the history
  • Loading branch information
arielvalentin committed Oct 9, 2023
1 parent 3a615ee commit e988093
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ def finish(_name, _id, payload)
tokens = otel&.fetch(:ctx_tokens)

exception = payload[:error] || payload[:exception_object]
if exception
on_exception(exception, span)
else
span&.status = OpenTelemetry::Trace::Status.ok
end
on_exception(exception, span) if exception
rescue StandardError => e
OpenTelemetry.handle_error(exception: e)
ensure
Expand All @@ -70,7 +66,7 @@ def finish(_name, _id, payload)
def finish_span(span, tokens)
# closes the span after all attributes have been finalized
begin
# Relies on span status being immutable
span&.status = OpenTelemetry::Trace::Status.ok if span&.status&.code == OpenTelemetry::Trace::Status::UNSET
span&.finish
rescue StandardError => e
OpenTelemetry.handle_error(exception: e)
Expand Down Expand Up @@ -144,9 +140,9 @@ def on_start(name, _id, payload)
# @return [Array] Context tokens that must be detached when finished
def attach_consumer_context(span)
consumer_context = OpenTelemetry::Trace.context_with_span(span)
aj_context = OpenTelemetry::Instrumentation::ActiveJob.context_with_span(span, parent_context: consumer_context)
internal_context = OpenTelemetry::Instrumentation::ActiveJob.context_with_span(span, parent_context: consumer_context)

[consumer_context, aj_context].map { |context| OpenTelemetry::Context.attach(context) }
[consumer_context, internal_context].map { |context| OpenTelemetry::Context.attach(context) }
end

# TODO: refactor into a strategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
let(:publish_span) { spans.find { |s| s.name == 'default publish' } }
let(:process_span) { spans.find { |s| s.name == 'default process' } }
let(:discard_span) { spans.find { |s| s.name == 'discard.active_job' } }
let(:retry_span) { spans.find { |s| s.name == 'retry_stopped.active_job' } }

before do
OpenTelemetry::Instrumentation::ActiveJob::Subscriber.uninstall
Expand Down Expand Up @@ -231,27 +232,32 @@

it 'tracks correctly for jobs that do retry in Rails 6 or earlier' do
skip "ActiveJob #{ActiveJob.version} starts at 0 in newer versions" if ActiveJob.version >= Gem::Version.new('7')
begin
RetryJob.perform_later
rescue StandardError
nil
end
_ { RetryJob.perform_later }.must_raise StandardError

executions = spans.filter { |s| s.kind == :consumer }.map { |s| s.attributes['rails.active_job.execution.counter'] }.compact.max
_(executions).must_equal(2) # total of 3 runs. The initial and 2 retries.
end

it 'tracks correctly for jobs that do retry in Rails 7 or later' do
skip "ActiveJob #{ActiveJob.version} starts at 1 in older versions" if ActiveJob.version < Gem::Version.new('7')
begin
RetryJob.perform_later
rescue StandardError
nil
end
_ { RetryJob.perform_later }.must_raise StandardError

executions = spans.filter { |s| s.kind == :consumer }.map { |s| s.attributes['rails.active_job.execution.counter'] }.compact.max
_(executions).must_equal(1)
end

it 'records retry errors' do
_ { RetryJob.perform_later }.must_raise StandardError

_(process_span.status.code).must_equal OpenTelemetry::Trace::Status::ERROR
_(process_span.status.description).must_equal 'from retry job'

_(retry_span.status.code).must_equal OpenTelemetry::Trace::Status::ERROR
_(retry_span.status.description).must_equal 'from retry job'
_(retry_span.events.first.name).must_equal 'exception'
_(retry_span.events.first.attributes['exception.type']).must_equal 'StandardError'
_(retry_span.events.first.attributes['exception.message']).must_equal 'from retry job'
end
end

describe 'active_job.provider_job_id' do
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/active_job/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RetryJob < ActiveJob::Base
retry_on StandardError, wait: 0, attempts: 2

def perform
raise StandardError
raise StandardError, 'from retry job'
end
end

Expand Down

0 comments on commit e988093

Please sign in to comment.