Skip to content

Instantly share code, notes, and snippets.

@mtowers
Last active February 26, 2020 16:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mtowers/8324021 to your computer and use it in GitHub Desktop.
Save mtowers/8324021 to your computer and use it in GitHub Desktop.
A Dashing dashboard widget for displaying a graph of metrics data from Librato

Description

A Dashing widget for displaying a graph of metrics data from Librato

Dependencies

httparty

Add it to dashing's gemfile:

gem 'httparty'

and run bundle install.

Usage

To use this widget, you'll first need to set up an API token for your Librato account and add it to your job script

1. Create a Librato API token for the widget

  1. Go to https://metrics.librato.com/account#api_tokens
  2. Click the '+' icon to create a new API token.
  3. Enter a name for the token (e.g. 'Dashing Dashboard Widget')
  4. Click the 'view only' option
  5. Click 'Create'

2. Start coding

  1. Copy the librato.rb file in to your dashing jobs/ folder. (or use dashing install 8324021 to install directly from this gist)
  2. Update the api_user, api_token and metric_name variables in jobs/librato.rb
    api_user = '[YOUR LIBRATO ACCOUNT EMAIL]'
    api_token = '[YOUR LIBRATO API TOKEN]' 
    
    ...
    
    metric_name = '[YOUR LIBRATO METRIC NAME]'
  1. Add the widget HTML to your dashboard

NOTE: The data-id attribute should based on the name of your metric (e.g. 'librato.my-metric-name')

    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
      <div data-id="librato.my-librato-metric" data-view="Graph" data-title="My Metric" data-moreinfo="powered by librato"></div>
    </li>
require 'httparty'
api_endpoint = 'https://metrics-api.librato.com/v1/metrics'
api_user = '[YOUR LIBRATO EMAIL ADDRESS]'
api_token = '[YOUR LIBRATO API TOKEN]'
auth_credentials = { :username => api_user, :password => api_token }
request_headers = { "Accept" => "application/json" }
# the interval in seconds on which you want to query the librato API
# this should match the reporting interval for the metric you wish to query
# (see: http://dev.librato.com/v1/time-intervals)
query_interval = 60
# the name of the librato metric you want to query
metric_name = '[YOUR LIBRATO METRIC NAME]'
# query url
# (see: http://dev.librato.com/v1/get/metrics/:name)
query_url = "#{api_endpoint}/#{metric_name}?count=#{query_interval}&resolution=#{query_interval}&summarize_sources=true&breakout_sources=false"
# creates the points array from the measurements in the response
def interpolate_points(measurements)
points = []
measurements.each_index do |i|
point = { x: i * 60, y: measurements[i]['count'] } # use the 'count' metric
# point = { x: i, y: measurements[i]['value'] } # use the 'value' metric
points << point
end
points
end
SCHEDULER.every "#{query_interval}s", :first_in => 0 do
response = HTTParty.get(query_url, :headers => request_headers, :basic_auth => auth_credentials )
response_body = JSON.parse(response.body)
points = interpolate_points(response_body['measurements']['all'])
send_event("librato.#{metric_name}", points: points)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment