diff --git a/CHANGELOG.md b/CHANGELOG.md index c43a774..33abbaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- Add template function to allow expansion of event ID + +### Changed +- Updated included sample event to include event ID + ## [0.8.1] - 2020-10-22 ### Changed diff --git a/README.md b/README.md index a01ee0c..d20b7fa 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ - [Handler definition](#handler-definition) - [Annotations](#annotations) - [Templates](#templates) - - [Formatting Timestamps in Templates](#formatting-dates-in-templates) + - [Formatting Timestamps in Templates](#formatting-timestamps-in-templates) + - [Formatting the Event ID in Templates](#formatting-the-event-id-in-templates) - [Installing from source and contributing](#installing-from-source-and-contributing) ## Overview @@ -194,6 +195,20 @@ timestamps. [...] ``` +#### Formatting the Event ID in Templates + +Each Sensu Go event contains a unique event ID (UUID) that is presented to +this handler in a non-printable format. To properly print this value as +part of a template, the function UUIDFromBytes is provided. The example +below shows its use: + +``` +[...] +Event ID: {{UUIDFromBytes .ID}} +

Check Output Details

+Check Output: {{.Check.Output}} +``` + ## Installing from source and contributing Download the latest version of the sensu-email-handler from [releases][1], diff --git a/event.json b/event.json index 6cd2e24..0dd304e 100644 --- a/event.json +++ b/event.json @@ -1,4 +1,6 @@ { + "timestamp": 1598478983, + "id": "e60d1549-bd57-4281-8273-1a04409aa9fa", "entity": { "entity_class": "agent", "system": { @@ -118,5 +120,4 @@ "annotations": null } } - "timestamp": 1598478983 } diff --git a/main.go b/main.go index 5bae3e0..8bb64e6 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( ttemplate "text/template" "time" + "github.com/google/uuid" "github.com/sensu-community/sensu-plugin-sdk/sensu" corev2 "github.com/sensu/sensu-go/api/core/v2" ) @@ -364,12 +365,14 @@ func resolveTemplate(templateValue string, event *corev2.Event, contentType stri if contentType == ContentHTML { // parse using html/template tmpl, err = htemplate.New("test").Funcs(htemplate.FuncMap{ - "UnixTime": func(i int64) time.Time { return time.Unix(i, 0) }, + "UnixTime": func(i int64) time.Time { return time.Unix(i, 0) }, + "UUIDFromBytes": uuid.FromBytes, }).Parse(templateValue) } else { // default parse using text/template tmpl, err = ttemplate.New("test").Funcs(ttemplate.FuncMap{ - "UnixTime": func(i int64) time.Time { return time.Unix(i, 0) }, + "UnixTime": func(i int64) time.Time { return time.Unix(i, 0) }, + "UUIDFromBytes": uuid.FromBytes, }).Parse(templateValue) }