How to determine if sysdig field exists or handle error if it doesn't

90 views Asked by At

I'm using Sysdig to capture some events and have a small chisel (LUA script) to capture and format the events as necessary. On the on_init() I'm requesting fields like so :

f_field = chisel.request_field("<field>")

My question is how can I check if a field exists before requesting it? I'm going to use a new field only just released on 0.24.1 but ideally I'd like my chisel to continue to work on older versions of sysdig without this field. I've tried wrapping the call to chisel.request_field in a pcall() like so :

ok, f_field = pcall(chisel.request_field("<field>"))

and even implementing my own "get_field" function :

function get_field(field)
  ok, f = pcall(chisel.request_field(field))
  if ok then return f else return nil end
end

f_field = get_field("<field>")
if f_field ~= nil then
  -- do something
end

but the error ("chisel requesting nonexistent field <field>") persists.

I can't see a way to check if a field exists but I can't seem to handle the error either. I really don't want multiple versions of my scripts if possible.

Thanks Steve H

1

There are 1 answers

1
user10485017 On BEST ANSWER

You're almost there. Your issue is in how you're using pcall. Pcall takes a function value and any arguments you wish to call that function with. In your example you're passing the result of the request_field function call to pcall. Try this instead..

ok, f = pcall(chisel.request_field, "field")

pcall will call the chisel method with your args in a protected mode and catch any subsequent errors.