Skip to content

Instantly share code, notes, and snippets.

@robbkidd
Last active June 15, 2023 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbkidd/eb2a5e57a6b6680bddbfa13d9a4b2990 to your computer and use it in GitHub Desktop.
Save robbkidd/eb2a5e57a6b6680bddbfa13d9a4b2990 to your computer and use it in GitHub Desktop.
A More Courteous Unicorn Worker Assassin
require 'sinatra'
require_relative 'otel.conf.rb'
require 'rack-timeout'
use Rack::Timeout,
service_timeout: 15, # shorter than unicorn's timeout!
term_on_timeout: false # TERM prevents at_exit()
require './unreliable_app'
run UnreliableApp
source 'https://rubygems.org'
gem 'unicorn'
gem 'rack-timeout'
gem 'sinatra'
gem 'opentelemetry-sdk'
gem 'opentelemetry-exporter-otlp'
gem 'opentelemetry-instrumentation-all'
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
require 'opentelemetry/instrumentation/sinatra'
##
# A module to represent how an organization might wrap and customize
# observability configuration.
#
module O11yWrapper
##
# Standard minimal OTel SDK configuration.
#
OpenTelemetry::SDK.configure do |c|
c.service_name = 'unreliable_app'
c.use_all() # enables all instrumentation!
end
##
# flag to mark that we've been told to put the chairs up and turn out the lights
#
@@shutting_down = false
##
# Call to make a best effort at shipping closed and queued telemetry data out of
# the process and off to backends.
#
def self.export_hail_mary
unless @@shutting_down
@@shutting_down = true
case OpenTelemetry.tracer_provider.shutdown(timeout: 5)
when OpenTelemetry::SDK::Trace::Export::SUCCESS ; puts "🔭 Span export successful. 🚀"
when OpenTelemetry::SDK::Trace::Export::FAILURE ; puts "🔭 Span export failed. 🤷"
when OpenTelemetry::SDK::Trace::Export::TIMEOUT ; puts "🔭 Span export timed out. ⌛️"
end
puts "🦄💬 'I did what I could.' 🫡"
end
end
end
##
# Gentler process exits will be handled with an at_exit.
#
at_exit do
puts "🔭 Exiting. 🧹 Cleaning up."
O11yWrapper.export_hail_mary
end
local_resource('unreliable app',
serve_cmd='unicorn --config-file unicorn.conf.rb',
deps=[
'Gemfile.lock',
'unicorn.conf.rb',
'otel.conf.rb',
'config.ru',
'unreliable_app.rb',
],
links=[
link('http://localhost:8080/', 'GET'),
link('http://localhost:8080/sleepy?nap_seconds=10', 'SLEEPY GET'),
]
)
# go install github.com/CtrlSpice/otel-desktop-viewer@latest
# it's pretty dope!
local_resource('trace viewer',
serve_cmd='$(go env GOPATH)/bin/otel-desktop-viewer',
links=[link('http://localhost:8000/', 'View Traces')]
)
local_resource('poke',
cmd='curl localhost:8080',
auto_init=False
)
local_resource('sleepy poke',
cmd='curl localhost:8080/sleepy?nap_seconds=5',
auto_init=False
)
local_resource('too sleepy poke',
cmd='curl localhost:8080/sleepy?nap_seconds=20',
auto_init=False
)
local_resource('update gems',
cmd='bundle install',
auto_init=False,
deps=['Gemfile']
)
worker_processes 2
timeout 20 # longer than rack-timeout!
require 'sinatra/base'
require 'opentelemetry/sdk'
class UnreliableApp < Sinatra::Base
AppTracer = OpenTelemetry.tracer_provider.tracer('unreliable')
def compute_dynamism
AppTracer.in_span('compute_dynamism') do
rand(36**6).to_s(36)
end
end
get '/' do
dynamism = compute_dynamism
"<p>Randomness: #{dynamism}"
end
get '/sleepy' do
dynamism = compute_dynamism
puts "🥱 Maybe I could use a nap ..."
nap_seconds = params['nap_seconds'] || 1
AppTracer.in_span('napping', attributes: {"nap_seconds" => nap_seconds}) do
sleep nap_seconds.to_i
end
"<p>I had to sleep for a bit (#{nap_seconds} seconds). Randomness: #{dynamism}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment