Last active
June 15, 2023 22:52
-
-
Save robbkidd/eb2a5e57a6b6680bddbfa13d9a4b2990 to your computer and use it in GitHub Desktop.
A More Courteous Unicorn Worker Assassin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'unicorn' | |
gem 'rack-timeout' | |
gem 'sinatra' | |
gem 'opentelemetry-sdk' | |
gem 'opentelemetry-exporter-otlp' | |
gem 'opentelemetry-instrumentation-all' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
worker_processes 2 | |
timeout 20 # longer than rack-timeout! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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