diff --git a/instrumentation/active_job/example/active_job.rb b/instrumentation/active_job/example/active_job.rb index 513c5fe8e..d23266986 100644 --- a/instrumentation/active_job/example/active_job.rb +++ b/instrumentation/active_job/example/active_job.rb @@ -4,6 +4,7 @@ # # SPDX-License-Identifier: Apache-2.0 +ENV['OTEL_SERVICE_NAME'] ||= 'otel-active-job-demo' require 'rubygems' require 'bundler/inline' @@ -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' @@ -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 @@ -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