From 67e136be0de81215593b9765a9de1660fa72420b Mon Sep 17 00:00:00 2001 From: Matt Sanders Date: Tue, 4 Nov 2014 17:47:19 -0800 Subject: [PATCH 1/2] Add #get_composite for fetching composite measurements --- lib/librato/metrics.rb | 2 +- lib/librato/metrics/client.rb | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/librato/metrics.rb b/lib/librato/metrics.rb index 76c8f0e..c8d85b1 100644 --- a/lib/librato/metrics.rb +++ b/lib/librato/metrics.rb @@ -76,7 +76,7 @@ module Metrics :api_endpoint=, :authenticate, :connection, :faraday_adapter, :faraday_adapter=, :persistence, :persistence=, :persister, - :get_metric, :get_measurements, :metrics, + :get_composite, :get_metric, :get_measurements, :metrics, :delete_metrics, :update_metric, :update_metrics, :submit, :sources, :get_source, :update_source, diff --git a/lib/librato/metrics/client.rb b/lib/librato/metrics/client.rb index f81226c..e4bcc39 100644 --- a/lib/librato/metrics/client.rb +++ b/lib/librato/metrics/client.rb @@ -163,6 +163,30 @@ def fetch(metric, options={}) options.empty? ? metric : metric["measurements"] end + # Retrieve measurements for a given composite metric definition. + # :start_time and :resolution are required options, :end_time is + # optional. + # + # @example Get 5m moving average of 'foo' + # measurements = Librato::Metrics.get_composite + # 'moving_average(mean(series("foo", "*"), {size: "5"}))', + # :start_time => Time.now.to_i - 60*60, :resolution => 300 + # + # @param [String] definition Composite definition + # @param [hash] options Query options + def get_composite(definition, options={}) + unless options[:start_time] && options[:resolution] + raise "You must provide a :start_time and :resolution" + end + query = options.dup + query[:compose] = definition + url = connection.build_url("metrics", query) + response = connection.get(url) + parsed = SmartJSON.read(response.body) + # TODO: pagination support + parsed + end + # Retrieve a specific metric by name, optionally including data points # # @example Get attributes for a metric From 3f5a8a411214588af26e7ab8fcbccecf69ebd619 Mon Sep 17 00:00:00 2001 From: Matt Sanders Date: Tue, 4 Nov 2014 17:53:07 -0800 Subject: [PATCH 2/2] Add example for fetching composites to README --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c59612..8341de5 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,11 @@ Get the 20 most recent 15 minute data point rollups for `temperature`: data = Librato::Metrics.get_measurements :temperature, :count => 20, :resolution => 900 -There are many more options supported for querying, take a look at the [REST API docs](http://dev.librato.com/v1/get/metrics/:name) or the [`get_metric`](http://rubydoc.info/github/librato/librato-metrics/master/Librato/Metrics/Client#get_metric-instance_method)/[`get_measurements`](http://rubydoc.info/github/librato/librato-metrics/master/Librato/Metrics/Client#get_measurements-instance_method) documentation for more details. +Get the 5 minute moving average for `temperature` for the last hour, assuming temperature is submitted once per minute: + + data = Librato::Metrics.get_composite 'moving_average(mean(series("temperature", "*"), {size: "5"}))', :start_time => Time.now.to_i - 60*60, :resolution => 300 + +There are many more options supported for querying, take a look at the [REST API docs](http://dev.librato.com/v1/get/metrics/:name) or the individual method documentation for more details. ## Setting Metric Properties