Skip to content

Instantly share code, notes, and snippets.

@nathwill
Created May 30, 2015 16:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathwill/d3f62d46d173b2456531 to your computer and use it in GitHub Desktop.
Save nathwill/d3f62d46d173b2456531 to your computer and use it in GitHub Desktop.
heka json decoder
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
--[[
Parses a payload containing JSON.
Config:
- type (string, optional, default nil):
Sets the message 'Type' header to the specified value
- payload_keep (bool, optional, default false)
Always preserve the original log line in the message payload.
.. code-block:: javascript
{
"msg": "Start Request",
"event": "artemis.web.ensure-running",
"extra": {
"workspace-id": "hpl4daza2f"
},
"time": "2015-05-06T20:40:05.509926234Z",
"severity": 1
}
*Example Heka Configuration*
.. code-block:: ini
[ArtemisLogInput]
type = "LogstreamerInput"
log_directory = "/srv/artemis/current/logs"
file_match = 'artemis\.log'
decoder = "JsonDecoder"
[JsonDecoder]
type = "SandboxDecoder"
filename = "lua_decoders/json_decoder.lua"
[JsonDecoder.config]
type = "artemis"
payload_keep = true
--]]
require "cjson"
local util = require("util")
local msg_type = read_config("type")
local payload_keep = read_config("payload_keep")
local msg = {
Type = msg_type,
Payload = nil,
Fields = nil,
Severity = nil
}
function process_message()
local ok, json = pcall(cjson.decode, read_message("Payload"))
if not ok then
return -1
end
if payload_keep then
msg.Payload = read_message("Payload")
end
msg.Severity = json["severity"]
local flat = {}
util.table_to_fields(json, flat, nil)
msg.Fields = flat
if not pcall(inject_message, msg) then return -1 end
return 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment