Created
May 8, 2014 23:33
-
-
Save asenchi/fa0dc7bd8f78d2ceb9cc to your computer and use it in GitHub Desktop.
canary.io -> Heka lua decoder
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 "cjson" | |
-- Generic decoder for Canary JSON data. This will extract all JSON | |
-- keys and add them to the `Fields` variable of the created | |
-- Heka message. | |
-- | |
-- Example use: | |
-- | |
-- [CanaryDecoder] | |
-- type = "SandboxDecoder" | |
-- script_type = "lua" | |
-- filename = "/usr/share/heka/lua_decoders/canary.lua" | |
-- module_directory = "/usr/share/lua" | |
-- | |
-- [HttpInput] | |
-- url = "https://api.canary.io/checks/https-github.com/measurements?range=10" | |
-- ticker_interval = 1 | |
-- decoder = "CanaryDecoder" | |
-- | |
-- [fileoutput] | |
-- type = "FileOutput" | |
-- message_matcher = "Fields[location] == 'do-ny2'" | |
-- path = "/var/log/heka/canary-output.log" | |
-- prefix_ts = true | |
-- perm = "666" | |
function process_message() | |
local raw_message = read_message("Payload") | |
local json = cjson.decode(raw_message) | |
if not json then | |
-- When plain text is found, ship it in it's raw form. | |
inject_message(raw_message) | |
return 0 | |
end | |
for element = 1, #json do | |
local cj2 = cjson.new() | |
local fields = json[element] | |
if fields.check then | |
fields.check_id = fields.check.id | |
fields.check_url = fields.check.url | |
fields.check = nil | |
end | |
local message = { | |
Type = "Canary", | |
Payload = cj2.encode(json[element]), | |
Fields = fields | |
} | |
inject_message(message) | |
end | |
return 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Resulting log from the example above: