-
Notifications
You must be signed in to change notification settings - Fork 32
/
config.ru
155 lines (128 loc) · 3.69 KB
/
config.ru
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require 'oj'
Oj.default_options = { :mode => :strict }
opts = Oj.load( File.read('config.json') )
# prepare the logger
require 'logger'
log = Logger.new(STDERR)
log.progname = 'optica'
log.level = Logger::INFO unless opts['debug']
opts['log'] = log
# Override store mode from ENV
if ENV['OPTICA_SPLIT_MODE']
opts['split_mode'] = ENV['OPTICA_SPLIT_MODE'].downcase
end
# Enable GC stats
if opts['gc_stats']
if defined? GC::Profiler && GC::Profiler.respond_to?(:enable)
GC::Profiler.enable
elsif GC.respond_to?(:enable_stats)
GC.enable_stats
end
end
# Rack options
if opts['rack']
key_space_limit = opts['rack']['key_space_limit']
Rack::Utils.key_space_limit = key_space_limit if key_space_limit
end
# prepare statsd
require 'datadog/statsd'
STATSD = Datadog::Statsd.new(opts['statsd_host'], opts['statsd_port'])
# prepare to exit cleanly
$EXIT = false
# configure unicorn-worker-killer
if opts['worker_killer']
require 'unicorn/worker_killer'
wk_opts = opts['worker_killer']
if wk_opts['max_requests']
max_requests = wk_opts['max_requests']
# Max requests per worker
use Unicorn::WorkerKiller::MaxRequests, max_requests['min'], max_requests['max']
end
if wk_opts['mem_limit']
mem_limit = wk_opts['mem_limit']
# Max memory size (RSS) per worker
use Unicorn::WorkerKiller::Oom, mem_limit['min'], mem_limit['max']
end
end
# configure the store
require './store.rb'
store = Store.new(opts)
store.start
EVENTS_CLASSES = {
'rabbitmq' => {
'class_name' => 'EventsRabbitMQ',
'file_name' => './events_rmq.rb',
},
'sqs' => {
'class_name' => 'EventsSQS',
'file_name' => './events_sqs.rb'
},
}
events_classes = opts['events'] || ['rabbitmq']
# configure the event creator
events = events_classes.map do |name|
class_opts = EVENTS_CLASSES[name]
raise "unknown value '#{name}' for events option" unless class_opts
class_name = class_opts['class_name']
file_name = class_opts['file_name']
log.info "loading #{class_name} from #{file_name}"
require file_name
class_const = Object.const_get(class_name)
class_const.new(opts).tap do |obj|
obj.start
end
end
# set a signal handler
['INT', 'TERM', 'QUIT'].each do |signal|
trap(signal) do
$stderr.puts "Got signal #{signal} -- exit currently #{$EXIT}"
exit! if $EXIT
$EXIT = true
# stop the server
server = Rack::Handler.get(server) || Rack::Handler.default
server.shutdown if server.respond_to?(:shutdown)
# stop the components
store.stop()
events.stop()
exit!
end
end
# do we check the client IP?
ip_check = case opts['client_check']
when true, 'direct' then :direct
when 'forwarded_for' then :forwarded_for
when false, nil then false
else raise 'unknown value for ip_check option'
end
# load the app
require './optica.rb'
# configure tracing client
def datadog_config(log)
Datadog.configure do |c|
service = ENV.fetch('DD_SERVICE', 'optica')
c.use :sinatra, service_name: service
# Statsd instance used for sending runtime metrics
c.runtime_metrics.statsd = STATSD
end
# register tracer extension
Optica.register Datadog::Contrib::Sinatra::Tracer
# add correlation IDs to logger
log.formatter = proc do |severity, datetime, progname, msg|
correlation = Datadog.tracer.active_correlation rescue 'FAILED'
"[#{datetime}][#{progname}][#{severity}][#{correlation}] #{msg}\n"
end
end
begin
require 'ddtrace/auto_instrument'
datadog_config(log)
rescue LoadError
log.info "Datadog's tracing client not found, skipping..."
end
Optica.set :logger, log
Optica.set :store, store
Optica.set :events, events
Optica.set :ip_check, ip_check
Optica.set :split_mode, opts['split_mode']
# start the app
log.info "Starting sinatra server..."
run Optica