Skip to content

Commit

Permalink
feat: add more details to demo
Browse files Browse the repository at this point in the history
  • Loading branch information
arielvalentin authored Sep 24, 2023
1 parent a813eb0 commit c2e8bfc
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions instrumentation/active_job/example/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# SPDX-License-Identifier: Apache-2.0

ENV['OTEL_SERVICE_NAME'] ||= 'otel-active-job-demo'
require 'rubygems'
require 'bundler/inline'

Expand All @@ -12,6 +13,7 @@
gem 'activejob', require: 'active_job'
# gem 'opentelemetry-instrumentation-active_job', path: '../'
gem 'opentelemetry-sdk'
gem 'opentelemetry-exporter-otlp'
end

require_relative '../lib/opentelemetry/instrumentation/active_job/subscriber'
Expand All @@ -22,6 +24,53 @@
at_exit { OpenTelemetry.tracer_provider.shutdown }
end

class FailingJob < ::ActiveJob::Base
queue_as :demo
def perform
raise 'this job failed'
end
end

class FailingRetryJob < ::ActiveJob::Base
queue_as :demo

retry_on StandardError, attempts: 2, wait: 0
def perform
raise 'this job failed'
end
end

class RetryJob < ::ActiveJob::Base
queue_as :demo

retry_on StandardError, attempts: 3, wait: 0
def perform
if executions < 3
raise 'this job failed'
else
puts <<~EOS
--------------------------------------------------
Done Retrying!
--------------------------------------------------
EOS
end
end
end

class DiscardJob < ::ActiveJob::Base
queue_as :demo

class DiscardError < StandardError; end

discard_on DiscardError

def perform
raise DiscardError, 'this job failed'
end
end

class TestJob < ::ActiveJob::Base
def perform
puts <<~EOS
Expand All @@ -34,7 +83,31 @@ def perform
end
end

class DoItNowJob < ::ActiveJob::Base
def perform
puts <<~EOS
--------------------------------------------------
Called with perform_now!
--------------------------------------------------
EOS
end
end

class BatchJob < ::ActiveJob::Base
def perform
TestJob.perform_later
FailingJob.perform_later
FailingRetryJob.perform_later
RetryJob.perform_later
DiscardJob.perform_later
end
end

::ActiveJob::Base.queue_adapter = :async

TestJob.perform_later
sleep 0.1 # allow the job to complete
DoItNowJob.perform_now
BatchJob.perform_later

sleep 1 # allow the job to complete

0 comments on commit c2e8bfc

Please sign in to comment.