summaryrefslogtreecommitdiffstats
path: root/fluent-bit/scripts/test.lua
blob: 91ff2f1a93cff0026b9755cf4fb089f4d356cae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
--[[

   This Lua script provides 3 interfaces or callbacks for filter_lua:

   - cb_print   => Print records to the standard output
   - cb_drop    => Drop the record
   - cb_replace => Replace record content with a new table

   The key inside each function is to do a proper handling of the
   return values. Each function must return 3 values:

      return code, timestamp, record

   where:

   - code     : -1 record must be deleted
                 0 record not modified, keep the original
                 1 record was modified, replace timestamp and record.
                 2 record was modified, replace record and keep timestamp.
   - timestamp: Unix timestamp with precision (double)
   - record   : Table with multiple key/val

   Uppon return if code == 1 (modified), then filter_lua plugin
   will replace the original timestamp and record with the returned
   values. If code == 0 the original record is kept otherwise if
   code == -1, the original record will be deleted.
]]

-- Print record to the standard output
function cb_print(tag, timestamp, record)
   output = tag .. ":  [" .. string.format("%f", timestamp) .. ", { "

   for key, val in pairs(record) do
      output = output .. string.format(" %s => %s,", key, val)
   end
   
   output = string.sub(output,1,-2) .. " }]"
   print(output)

   -- Record not modified so 'code' return value is 0 (first parameter)
   return 0, 0, 0
end

-- Drop the record
function cb_drop(tag, timestamp, record)
   return -1, 0, 0
end

-- Compose a new JSON map and report it
function cb_replace(tag, timestamp, record)
   -- Record modified, so 'code' return value (first parameter) is 1
   new_record = {}
   new_record["new"] = 12345
   new_record["old"] = record
   return 1, timestamp, new_record
end