Hosted Graphite Docs
Get StartedBook a Demo
  • Welcome to Hosted Graphite
  • Getting Started
  • HG-CLI
  • Sending Metrics
    • Supported Protocols
    • Graphite Tag Support
    • Metric Management
    • Metric APIs
  • Language Guide
    • Metric Libraries
    • .NET
    • Go
    • Java
    • Javascript
    • Node.js
    • PHP
    • Postman
    • Python 2.x
    • Python 3.x
    • Python Pickle
    • Ruby
    • Shell
    • TypeScript
  • Dashboard and Graphs
    • Primary Dashboards
    • Dashboard Library
    • Local Dashboard Integration
    • Worldmap Panel
    • Graphite Dashboard Guide
    • Graphite Graph Menu Reference
    • Other Dashboard Options
  • Alerting Guide
    • Alerting Overview
    • Alerts API
    • Notification Channels API
    • Scheduled Mutes API
    • Using Your Own Alerting
  • Agents Guide
    • The Hosted Graphite Agent
      • Base Metrics
      • System Layout
    • Telegraf
    • K8 Telegraf Daemon
    • OpenTelemetry
    • collectd Agent
    • StatsD Agent
    • Diamond
  • Add-Ons and Integrations Guide
    • AWS CloudWatch
    • Azure Monitor Metrics
    • GCP Metrics
    • Carbon-C-Relay
    • Circle CI
    • Cloudbees
    • Collectd Add Ons
    • GitHub
    • GitLab
    • Heroku
    • Hosted StatsD
    • New Relic
    • Papertrail
    • Pingdom
    • Sentry
    • Sitespeed
    • StatsD Add-on
    • Statuspage
  • Account Management
    • Access Keys
    • Account Diagnostics
    • Account Settings
    • Team Access: Limited Access Groups
    • SAML Authentication
    • Team Access
  • Additional Features
    • Aggregation Rules
    • Data Views
  • API Guides
    • Metrics API
    • Tag API
    • Graphite Render API
    • Render Variables API
    • Dashboard API
    • Annotations and Events API
    • Aggregation Rules API
    • Alerts APIs
  • FAQ
    • General
    • Business
    • Technical
    • Account Metrics and Limiting
    • Customization
    • Troubleshooting
    • Support
    • Changelog
Powered by GitBook
On this page
  • Installing the Ruby Gem
  • Using the Gem to send via TCP
  • Using the Gem to send via UDP
  • Using the Gem to send via HTTP
  • Using the Gem to send via StatsD
  • Basic Ruby
  • Sending a metric via TCP
  • Sending a metric via UDP
  • Sending a metric via HTTP POST
  • Sending a metric via StatsD

Was this helpful?

  1. Language Guide

Ruby

How to send metrics using Ruby

PreviousPython PickleNextShell

Last updated 1 year ago

Was this helpful?

Installing the Ruby Gem

A 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.rb-tcp', 1.2)

Using the Gem to send via UDP

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

Using the Gem to send via HTTP

HostedGraphite.protocol = HostedGraphite::HTTP
HostedGraphite.send_metric('foo.rb-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.rb-statsd', 1.2)

Basic Ruby

Here are some ways to send custom metric via TCP, UDP, HTTP, and StatsD directly from your code.

Sending a metric via TCP

require 'socket'

conn = TCPSocket.new 'YOUR-UID.carbon.hostedgraphite.com', 2003
conn.puts "YOUR-API-KEY.foo 1.2\n"
conn.close

Sending a metric via UDP

require 'socket'

sock = UDPSocket.new
sock.send "YOUR-API-KEY.foo 1.2\n", 0, "YOUR-UID.carbon.hostedgraphite.com", 2003

Sending a metric via HTTP POST

require 'net/http'

uri = URI("https://www.hostedgraphite.com/api/v1/sink")
api_key = "YOUR-API-KEY"

req = Net::HTTP::Post.new(uri.request_uri)
req.basic_auth api_key, nil
req.body = "foo 1.2"

res = Net::HTTP.start(uri.host, uri.port) do |http|
  http.request(req)
end

Sending a metric via StatsD

require 'socket'

statsd_host = 'statsd.hostedgraphite.com'
statsd_port = 8125
metric_key = 'YOUR-API-KEY.foo.ruby-statsd'
metric_value = 1.2

metric_message = "#{metric_key}:#{metric_value}|<c/g/ms>"

sock = UDPSocket.new
sock.send(metric_message, 0, statsd_host, statsd_port)
sock.close

NOTE: when sending standard ruby StatsD metrics, be sure to define counters (c), gauges (g), or timers (ms).

Your API key can be found on your page.

Hosted Graphite Ruby Gem
account home