sensu / sensu-email-handler

Sensu Go Email Handler Plugin
https://sensu.io
MIT License
11 stars 17 forks source link

Templates should be capable of being assets #29

Closed asachs01 closed 4 years ago

asachs01 commented 5 years ago

This is probably a larger discussion that needs to happen in the Sensu Go project, but given that we have a template file that has to live on disk, it seems to run counter to the workflow established in Sensu Go, which is that everything is an api call/lives in the datastore. Ideally a template would be its own sort of primitive and could be reused across multiple handlers, but that may be a pipe dream. In the case of the email handler, and example would be:

type: TemplateConfig
api_version: core/v2
metadata:
  annotations: null
  labels: null
  name: email_template
  namespace: default
spec:
  subject: "Sensu Alert - {{.Entity.Name}}/{{.Check.Name}}: {{.Check.State}}"
  body: 
    '[
        "salutation": "Greetings"
        "contact": {{ Event.Contact }}
        "info": "The check {{ Check.Name }} is in a {{ Check.State }}"
        "occurrences": "{{ Event.Occurrences }}"
        "playbook": "http://URL/To/My/Playbook"
    ]'

The syntax here is probably not according to how things would actually work, but it gets the idea across.

nixwiz commented 4 years ago

Given the API changes necessary for this, I agree maybe a pipedream.

How about a body template being available in an annotation? It could get a little ugly since it would be a single line of go templating with embedded newlines or worse HTML. But that does give per check/entity templates.

Even crazier would be to allow a URL for the template file, that way it could be grabbed from a variety of sources (local file, artifactory, s3 bucket, git repo) instead of just a local file.

calebhailey commented 4 years ago

So... there's a PR floating around somewhere in one of our plugins which implements templates that are fed from annotation data. We had even started to implement it in the sensu-plugins-go-library project. The interface would look like this (for any plugin that would want to implement it):

---
type: Handler
api_version: core/v2
metadata:
  name: jira 
  annotations: 
    sensu.io/plugins/jira/config/project-key: "OPS" 
    sensu.io/plugins/jira/config/issue-type: "Task"
    sensu.io/plugins/jira/config/issue-description-template: | 
      Check: {{ .Check.Name }}
      Entity: {{ .Entity.Name }}

      Description: {{ .Check.Output }}

      Sensu URL: https://sensu.mycompany.com:3000/{{ .Check.Namespace }}/events/{{ .Entity.Name }}/{{ .Check.Name }}
spec:
  command: sensu-jira-handler --jira-url='https://sensu.atlassian.net' --jira-project-key='SEN' --jira-username='caleb@sensu.io' --jira-password='xxxxxxxxxx' --jira-issue-type='Bug'
  timeout: 10
  runtime_assets: 
  - sensu-jira-handler 
  filters:
  - is_incident 
echlebek commented 4 years ago

Given that we can already source templates from annotations, or any other data source, I'm going to close this. If someone is interested in adding pluggable templates to this library, please file a new issue.