Skip to content

Instantly share code, notes, and snippets.

@mbeale
Last active December 4, 2017 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbeale/d28daba14e03ea263118dffad2500602 to your computer and use it in GitHub Desktop.
Save mbeale/d28daba14e03ea263118dffad2500602 to your computer and use it in GitHub Desktop.
Removing stale metrics from Librato
#!/usr/bin/env ruby
require 'librato/metrics'
c = Librato::Metrics
email = 'user@example.com'
token = 'abcd1234'
c.authenticate email, token
# Threshold to check for "last reported" time
days_ago = 30
# Calculate the timestamp from X days ago
time_cutoff = Time.now.to_i - (days_ago * 24 * 3600)
# Floor the time to the nearest hour
time_cutoff = time_cutoff - time_cutoff % 3600
results = []
# NOTE: if you have a lot of metrics, this could take quite a while to run!
# The metrics listing occurs in pages of 100 at a time, so if you have 20,000 metrics, that's
# 200 pages that get stored in memory
# Assume up to 500ms to get the last measurement as well.
name_filter = '' # Set this to limit the result set to a certain pattern, like 'abc' or 'abc*'
c.metrics(name: name_filter).each do |m|
next if m['type'] == 'composite'
metric_name = m['name']
if c.get_measurements(metric_name, start_time: time_cutoff,
resolution: 3600, count: 1).empty?
# Metric hasn't reported since X days ago
puts metric_name
results << metric_name
end
end
puts "Found #{results.size} metrics not reporting in #{days_ago} days (since #{Time.at(time_cutoff)}):"
# Delete 10 at a time, probably safe for up to 100
#results.each_slice(10) do |metric_names|
# puts "Deleting #{metric_names}..."
# Need to splat this so you get a list of args vs just the array
# Uncomment this to actually do the deletes
#c.delete_metrics(*metric_names)
#puts "done."
#end
@johnathanludwig
Copy link

Nice script! I was getting a bad request error on the get_measurements call for some metrics but fixed it by adding this right before it:

next if m['type'] == 'composite'

@mbeale
Copy link
Author

mbeale commented Jan 11, 2017

good catch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment