Skip to content

Instantly share code, notes, and snippets.

@KushalP
Created January 23, 2014 19:51
Show Gist options
  • Save KushalP/8585572 to your computer and use it in GitHub Desktop.
Save KushalP/8585572 to your computer and use it in GitHub Desktop.
Example of a Heka Lua Sandbox decoder that will take JSON log lines (and some extra decoder config) and stream that information into the `Fields` variables of the Heka message.
require "cjson"
-- Generic decoder for JSON logs. This will extract all JSON
-- keys and add them to the `Fields` variable of the created
-- Heka message.
--
-- Example use:
--
-- [NginxJsonLogDecoder]
-- type = "SandboxDecoder"
-- script_type = "lua"
-- filename = "/location/of/json_log_decode.lua"
--
-- [NginxJsonLogDecoder.config]
-- app = "some application"
--
-- [some-nginx-json-log-file]
-- type = "LogfileInput"
-- logfile = "/some/location/of/an/nginx/log/file.json.log"
-- decoder = "NginxJsonLogDecoder"
function process_message()
local raw_message = read_message("Payload")
local app = read_config("app")
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
local message = {
Type = "MyGenericNamespace.JsonLog"
}
if app then
json["app"] = app
end
message.Payload = raw_message
message.Fields = json
inject_message(message)
return 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment