Skip to content

Instantly share code, notes, and snippets.

@thefron
Last active February 15, 2017 03:06
Show Gist options
  • Save thefron/adbd1e207fad48130a22cb1eead6e336 to your computer and use it in GitHub Desktop.
Save thefron/adbd1e207fad48130a22cb1eead6e336 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'uri'
require 'json'
def put_metric(name, value, unit='Count')
metric_name = "ApiSidekiq#{name.split('_').collect(&:capitalize).join}"
`/usr/local/bin/aws cloudwatch put-metric-data --metric-name #{metric_name} --namespace Sidekiq --value #{value} --unit #{unit}`
end
uri = URI.parse("#{ENV['SIDEKIQ_WEB_HOST']}/stats")
req = Net::HTTP::Get.new(uri.request_uri)
req.basic_auth ENV['USERNAME'], ENV['PASSWORD']
resp = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(req)
end
if Integer(resp.code) == 200
data = JSON.parse(resp.body)
put_metric('BusyJobs', data['sidekiq']['busy'])
put_metric('EnqueuedJobs', data['sidekiq']['enqueued'])
put_metric('ScheduledJobs', data['sidekiq']['scheduled'])
put_metric('RetryJobs', data['sidekiq']['retries'])
put_metric('Processes', data['sidekiq']['processes'])
put_metric('RedisClients', data['redis']['connected_clients'])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment