Created
July 21, 2015 18:19
-
-
Save tas50/cf671e3121efdc669578 to your computer and use it in GitHub Desktop.
Chef LWRP for graphing to Librato
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Support whyrun | |
def whyrun_supported? | |
true | |
end | |
action :graph do | |
if node.chef_environment == 'production' || node.chef_environment == 'staging' | |
converge_by("Graph deploy of #{@new_resource.name}") do | |
graph_deploy | |
end | |
else | |
Chef::Log.info "Not graphing #{@new_resource.name} as we're not in Staging/Prod." | |
end | |
end | |
def unique_annotation? | |
annotations = Librato::Metrics::Annotator.new | |
annotation_name = (node.chef_environment + '_' + @new_resource.name + '_deploys').to_sym | |
# fetch annotations for this app. rescue because 404 == unique (first ever) annotation | |
begin | |
host_annotations = annotations.fetch annotation_name, :start_time => (Time.now.to_i - 1800) | |
rescue | |
return true | |
end | |
# unassigned is our source. if its key is not there we got back an empty set and we're unique | |
return true unless host_annotations['events'].key?('unassigned') | |
# if any of the event titles match the desired title then we're not unique | |
message = "Graphing deploy of #{@new_resource.name} version #{@new_resource.version}" | |
host_annotations['events']['unassigned'].each do |a| | |
return false if a['title'] == message | |
end | |
true | |
end | |
# send the annotation to librato if | |
def graph_deploy | |
require 'librato/metrics' | |
annotation = (node.chef_environment + '_' + @new_resource.name + '_deploys').to_sym | |
creds = Chef::EncryptedDataBagItem.load('librato', 'keys')['chef_deploys'] | |
begin | |
Librato::Metrics.authenticate creds['username'], creds['token'] | |
# bail out if this deploy has already been graphed | |
return unless unique_annotation? | |
Librato::Metrics.annotate annotation, "Deployed #{@new_resource.name} version #{@new_resource.version}", :source => node.name | |
Chef::Log.info "Graphing deploy of #{@new_resource.name} version #{@new_resource.version}" | |
rescue | |
Chef::Log.error 'An error occurred when trying to graph the deploy with Librato' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment