sensu / sensu-plugin-sdk

A framework for creating Sensu plugins
MIT License
7 stars 8 forks source link

Feature Request: Impliment state file functionality in SDK #66

Closed jspaleta closed 1 year ago

jspaleta commented 2 years ago

Rationale

Some check patterns require a small amount of state for correct operation. Lets bake that functionality into the SDK.

Check State Use Cases

Implementation requirements derived from current sensu-log-check implementation

Operational requirements/assumptions

Basic functionality needed

  1. Function to read state filepath into corresponding golang struct pointer
    • return nil if state file is missing or empty
    • return error if read permission error or similar
  2. Function to write golang struct pointer into state file
    • silently create file if missing.
    • return error if write permission error or similar

      Advanced Functionality

      If possible, implement state file with client access locking aware so that check can hold a lock on the file while the check is operating preventing a second check from access the file. This will prevent operators from accidentally using the same state file for multiple independently running checks or check hooks.

Hypothetical plugin check operation with locking

  1. Pass state filepath as cmdline argument and save into plugin level attribute
  2. Establish state file session enabling file access lock automatically at start of check execution function, defer session close as part of check execution function as part of normal SDK operation.
  3. Read state filepath contents and hold in golang struct pointer as part of check execution business logic.
  4. Perform additional check operation business logic
  5. Write golang state struct pointer into state filepath as part of check execution business logic
  6. Perform remaining chec operation business logic
  7. run deferred close state file session dropping the client access lock at check execution end
calebhailey commented 2 years ago

This looks good, thanks @jspaleta 👍

Just a few comments:

jspaleta commented 2 years ago

I'm not sure I'm happy replacing sensu-check-log's state file with a sensu event and encoding all the file path names as arbitrarily long annotation keys and all the integer type seek offsets as strings values in the annotations. Having to encode everything as annotations means doing more work to type cast them back into the correct types.

For log check, being able to have an arbitrary golang struct instead of Sensu event is probably the better fit because the state needed there is never exposed in a sensu event.

So could you encode a sensu event as the state contnet in this implemtnation.. sure. Would it make sense to default to that in the template plugin repos that other plugins are derived from ? Sure. But I do not think Sensu event as structured right now is expressive enough for what log check functionality needs. If the SDK only implemented sensu event as state, I don't think check log would be able to use it.

echlebek commented 2 years ago

I think I have to -1 this one? This is scope overreach and won't save much code in SDK consumers, if they even want the semantics we prescribe.