Class: Store::Graphite

Inherits:
Store
  • Object
show all
Defined in:
lib/store/graphite.rb

Overview

Class for extracting data from Graphite (github.com/graphite-project/graphite-web)

Instance Method Summary collapse

Constructor Details

#initialize(origin, settings) ⇒ Graphite


6
7
8
9
# File 'lib/store/graphite.rb', line 6

def initialize origin,settings
  super
  @base_url = mandatory_param :url, "store_settings"
end

Instance Method Details

#get_metric(metric, start = nil, stop = nil, step = nil) ⇒ Object

Retrieve data for a graphite metric

Raises:


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/store/graphite.rb', line 32

def get_metric metric, start=nil, stop=nil, step=nil
  url = get_metric_url metric,start,stop,step
  m = metric.metric_id

  # Graphite errors are python stack dumps, so let's parse out what we need
  parse = lambda{|v| v.split("\n").select{|i| i[/^Exception/]}.join("\n").gsub("&#3  9;","'")}

  stats = json_metrics url, {error_parse: parse}

  raise Store::Error, "No data available for metric #{m}" if stats == []

  metric = []
  stats[0][:datapoints].each do |node|
    #because Graphite does [value,epoch], instead of the more sane [epoch,value]
    metric << {x: node[1], y: node[0] } 
  end

  # Ensure a hard limit on the size of the array before returning
  points = (stop - start)/step
  return metric.take(points)
end

#get_metric_url(metric, start = nil, stop = nil, step = nil) ⇒ Object

For a given metric, return a Graphite json feed URL


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/store/graphite.rb', line 17

def get_metric_url metric, start=nil, stop=nil, step=nil
  m = metric.metric_id
  url =  @base_url + 
    "/render?target=" + m + 
    "&from=#{start}" +
    "&until=#{stop}" +
    "&format=json"

  # Graphite requires summation -- TODO differenciate between sums and counters?
  url.gsub!(m, "summarize(#{m},'#{step}sec','avg')") if step

  return url
end

#get_metrics_listObject

Return Graphite's internal metrics list


12
13
14
# File 'lib/store/graphite.rb', line 12

def get_metrics_list
  json_metrics_list "#{@base_url}/metrics/index.json"
end