Skip to content

Instantly share code, notes, and snippets.

@grosser
Last active December 15, 2015 22:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grosser/5331029 to your computer and use it in GitHub Desktop.
Save grosser/5331029 to your computer and use it in GitHub Desktop.
Non-blocking datadog metrics / reporting
require "dogapi"
require "celluloid/autostart"
require "singleton"
class LazyDog
include Celluloid
include Singleton
def emit_point(*args)
client.emit_point(*args)
rescue Exception => e
if is_a?(Celluloid::AsyncCall)
# exceptions in async code kill the worker
$stderr.puts "Swallowed exception #{e.inspect}"
else
raise
end
end
private
def client
@client ||= Dogapi::Client.new(API_KEY)
end
end
LazyDog.instance.emit_point('foo', 100) # blocking
=> ["202", {"status"=>"ok"}]
LazyDog.instance.async.emit_point('foo', 100) # instant
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment