How to send metrics using Ruby

Installing the Ruby Gem

A Hosted Graphite Ruby Gem is available on github, and can be installed directly by typing:
gem install hosted_graphite

Using the Gem to send via TCP

HostedGraphite.protocol = HostedGraphite::TCP
HostedGraphite.send_metric('foo.tcp', 1.2)

Using the Gem to send via UDP

HostedGraphite.protocol = HostedGraphite::UDP
HostedGraphite.send_metric('foo.udp', 1.2)

Using the Gem to send via HTTP

HostedGraphite.protocol = HostedGraphite::HTTP
HostedGraphite.send_metric('foo.http', 1.2)

Using the Gem to send via StatsD

require 'hosted_graphite'
require 'statsd-ruby'
HostedGraphite.api_key = '<your-api-key>'
HostedGraphite.protocol = :statsd
HostedGraphite.<gauge-count-or-timing>('foo.statsd', 1.2)

Basic Ruby

Here are some ways to send via TCP, UDP and HTTP directly in your code.

Sending a metric via TCP

require 'socket'
conn = '', 2003
conn.puts " 1.2\n"

Sending a metric via UDP

require 'socket'
sock =
sock.send " 1.2\n", 0, "", 2003

Sending a metric via HTTP POST

require 'net/http'
uri = URI("")
api_key = "YOUR-API-KEY"
req =
req.basic_auth api_key, nil
req.body = "foo 1.2"
res = Net::HTTP.start(, uri.port) do |http|
Your API key can be found on your account home page.
Last modified 1mo ago