Skip to content

Commit

Permalink
squash: example
Browse files Browse the repository at this point in the history
  • Loading branch information
arielvalentin committed Oct 10, 2023
1 parent 4a24c08 commit ddb8906
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 13 deletions.
8 changes: 0 additions & 8 deletions instrumentation/active_job/example/Gemfile

This file was deleted.

93 changes: 88 additions & 5 deletions instrumentation/active_job/example/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,70 @@
#
# SPDX-License-Identifier: Apache-2.0

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

Bundler.require
gemfile do
source 'https://rubygems.org'
gem 'activejob', '~> 7.0.0', require: 'active_job'
gem 'opentelemetry-instrumentation-active_job', path: '../'
gem 'opentelemetry-sdk'
gem 'opentelemetry-exporter-otlp'
end

ENV['OTEL_LOG_LEVEL'] ||= 'fatal'
ENV['OTEL_TRACES_EXPORTER'] ||= 'console'
OpenTelemetry::SDK.configure do |c|
c.use 'OpenTelemetry::Instrumentation::ActiveJob'
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
Expand All @@ -27,7 +82,35 @@ 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 # To ensure we see both spans!
tracer = OpenTelemetry.tracer_provider.tracer('example', '0.1.0')

tracer.in_span('run-jobs') do
DoItNowJob.perform_now
BatchJob.perform_later
end

sleep 5 # allow the job to complete

0 comments on commit ddb8906

Please sign in to comment.