-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sensu Handler shim for Sensu 2.0 event data. #190
Sensu Handler shim for Sensu 2.0 event data. #190
Conversation
…e Handler class. New option makes it possible to use sensu-plugin based handlers with Sensu 2.0 events until handlers provide native 2.0 event support.
i need to clean things up for rubocop. I just wanted to get the PR out for people to look at asap |
lib/sensu-plugin/utils.rb
Outdated
# until they natively support 2.0 | ||
## | ||
def event_2to1 | ||
if @event.key?('entity') && @event['client'].empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be @event['client'].nil?
? Does empty cover nil
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the normal use case it definitely needs to be empty... as event_read will set client to {} if client doesn't exist. I'll update to check for nil? || empty? to cover all cases.
I don't want to do this as part of event_read, as I want to control when this gets called.. for now just for handlers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
empty?
does not cover .nil?
@jspaleta Could the 2to1 method be refactored and/or moved to its own gem so that it can be used for https://github.com/sensu/sensu-1.x-filter-wrapper ? |
okay refactoring this now as a utility function. I'm sure it makes sense as a separate gem right now. It definitely feels like something in the scope of sensu-plugin which provides nice base classes for mutators,checks and handlers. |
sync with upstream repo.
I agree with @jspaleta that this makes sense to be in the base plugin class as a util method, any GRPC service can depend on the base plugin class. |
@Cyphus irb toy example: |
lib/sensu-plugin/utils.rb
Outdated
# will be set to true as a hint to indicate this is a mapped event object. | ||
# | ||
## | ||
def event_2to1(orig_event = nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personally I'd opt for a longer more descriptive name, I also find that saying 2to1
aloud is confusing. It's unclear if its 221
, two to one
, or if its combining two events into a single event. Maybe adding the word shim
would give it more context which would make it less confusing. I can't say I am coming up with a great method name either and so far only end up with a very verbose name such as shim_v2_event_data_into_v1
, event_data_shim_v2
, sensu2_event_compatibility
, mutate_sensu2_event_data_to_v1
, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attempt to refactor function name into something more descriptive in later commits.
lib/sensu-mutator.rb
Outdated
TRUTHY_VALUES = %w[1 t true yes y].freeze | ||
automap = ENV['SENSU_MAP_V2_EVENT_INTO_V1'].to_s.downcase | ||
|
||
if mutator.config[:map_v2_event_into_v1,] || TRUTHY_VALUES.include?(automap) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
,
looks like a mistake
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting that this is allowed syntax by ruby. Fixed in newer commits.
lib/sensu-plugin/utils.rb
Outdated
# Deep copy of orig_event | ||
event = Marshal.load(Marshal.dump(orig_event)) | ||
|
||
# Trigger mapping code iff enity exists and client does not |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like an extra f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
lib/sensu-plugin/utils.rb
Outdated
# action used in logs and fluentd plugins handlers | ||
## | ||
state = event['check']['state'] || 'unknown::2.0_event' | ||
event['action'] ||= 'flapping' if state.casecmp('flapping').zero? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this could be much simpler and less error prone with a case statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactored using a hash mapping, since the states to action are 1 to 1.
lib/sensu-plugin/utils.rb
Outdated
if event['check']['history'] | ||
legacy_history = [] | ||
event['check']['history'].each do |h| | ||
legacy_history << h['status'].to_s || '3' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm if we are not sure what the history is I feel like we should use nil
(though that might cause issues?) as not knowing how to translate is not quite the same as the check itself returning an unknown
state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also is history really stored as the string when really they are integers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the 1.4 events I have access to, the are stored as strings.
there is at least one mutator in the sensu-plugins collection that makes use of the history, and in fact makes a comment that the history is saved as an array strings.
sensu-plugins-pagerduty/bin/mutator-pagerduty-priority-override.rb
Returning nil instead of '3' may cause weird issues nil.to_i gets translated to 0 which is the 'ok' status. I think if we don't know how to translate the status in the history its better to use the 3 aka 'unknown' to make sure we flag it as a problem that needs to be investigated.
Or it may be better to throw an error here instead of providing a fallback if the status is unmappable to a string.
…llback if state is not in understood mapping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM @portertech anything else you catch I missed?
@@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. | |||
This project adheres to [Semantic Versioning](http://semver.org/). | |||
|
|||
## [Unreleased] | |||
### Added | |||
- Added map_v2_event_into_v1 method to Utils for all plugin classes to use. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add attribution for you post acceptance or you can do it prior, either way works.
Description
Added event_2to1 method to Utils and --enable-2.0-event option to base Handler class. New option makes it possible to use sensu-plugin based handlers with Sensu 2.0 events until handlers provide native 2.0 event support.
Motivation and Context
the event data model changed in Sensu 2.0, so to make it possible to continue to use the community managed handlers a quick mapping back into Sensu 1.4 event JSON representation can be performed.
How Has This Been Tested?
Spot checked with local rebuild of sensu-plugin-logs handler so far.
Types of changes
Checklist: