Created
April 10, 2019 19:36
-
-
Save bytesandwich/38c2cf8609536ce6e0124445895022e1 to your computer and use it in GitHub Desktop.
Logstash ruby plugin file to collect spinnaker metrics in logstash
This file contains 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
require "json" | |
require 'socket' | |
@hostname = Socket.gethostname | |
def register(params) | |
end | |
# Turns one Netflix spectator application feed into many generic metrics | |
# suitable for ingestion in influxdb. | |
def filter(event) | |
applicationName = event.get("applicationName") | |
return [] if event.get("metrics").nil? | |
# event format: https://www.spinnaker.io/reference/monitoring/#metrics-overview | |
return event.get("metrics").map { |metricName,metricBody| | |
metricBody["values"].map { |instance| | |
result = { | |
"value" => instance["values"][0]["v"], | |
"@timestamp" => Time.at(instance["values"][0]["t"]/1000).utc.strftime("%Y-%m-%dT%H:%M:%SZ"), | |
"host" => @hostname, | |
"name" => "#{applicationName}.#{metricName}" | |
} | |
unless instance["tags"].nil? | |
result = instance["tags"].map{ |t| [t["key"], t["value"]] }.to_h.merge(result) | |
end | |
::LogStash::Event.new(result) | |
} | |
}.flatten | |
end | |
# Tests that run whenever logstash starts. | |
# To run tests without starting the rest of logstash: | |
# bin/logstash -e "filter { ruby { path => './parse_event.rb'} }" -t | |
test "It should emit multiple events from one spectator summary page." do | |
parameters do | |
{} | |
end | |
in_event { | |
test_file = "./spinnaker.log" | |
if File.file? test_file | |
file = File.read test_file | |
body = JSON.parse(file) | |
body.values.flatten | |
else | |
JSON.parse(`curl localhost:7002/spectator/metrics`) | |
end | |
} | |
expect("splits up the events") do |events| | |
puts "(FYI) example event:", events[0].to_hash | |
events.size > 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment