Skip to content

Commit

Permalink
Merge pull request #41 from sensu-community/feature/unixtime
Browse files Browse the repository at this point in the history
Add UnixTime func to template expansion
  • Loading branch information
echlebek authored Oct 7, 2020
2 parents aa10dd0 + d697f43 commit bbf3094
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

## [0.10.0] - 2020-10-07

### Added
Added UnixTime func to template expansion. See README for details.

## [0.9.0] - 2020-10-07

### Fixed
Expand Down
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,42 @@ To run the plugin independently of Sensu (ex. test/dev), you must set the env va

```
SENSU_LICENSE_FILE=$(sensuctl license info --format json)
```
```

## Templates

The templates package provides a wrapper to the [`text/template`][1] package
allowing for the use of templates to expand event attributes. An example of
this would be using the following as part of a handler:

```
--summary-template "{{.Entity.Name}}/{{.Check.Name}}"
```

Which, if given an event with an entity name of webserver01 and a check name of
check-nginx would yield `webserver01/check-nginx`.

### UnixTime template function

A Sensu Go event contains multiple timestamps (e.g. .Check.Issued,
.Check.Executed, .Check.LastOk) that are presented in UNIX timestamp format. A
function named UnixTime is provided to print these values in a customizable
human readable format as part of a template. To customize the output format of
the timestamp, use the same format as specified by Golang's [Time.Format][2].
Additional examples can be found [here][3].

**Note:** the predefined format constants are **not** available.

The example below demonstrates its use:

```
[...]
Service: {{.Entity.Name}}/{{.Check.Name}}
Executed: {{(UnixTime .Check.Executed).Format "2 Jan 2006 15:04:05"}}
Last OK: {{(UnixTime .Check.LastOK).Format "2 Jan 2006 15:04:05"}}
[...]
```

[1]: https://golang.org/pkg/text/template/
[2]: https://golang.org/pkg/time/#Time.Format
[3]: https://yourbasic.org/golang/format-parse-string-time-date-example/
5 changes: 4 additions & 1 deletion templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"text/template"
"time"
)

func EvalTemplate(templName, templStr string, templSrc interface{}) (string, error) {
Expand All @@ -14,7 +15,9 @@ func EvalTemplate(templName, templStr string, templSrc interface{}) (string, err
return "", fmt.Errorf("must pass in template")
}

templ, err := template.New(templName).Parse(templStr)
templ, err := template.New(templName).Funcs(template.FuncMap{
"UnixTime": func(i int64) time.Time { return time.Unix(i, 0) },
}).Parse(templStr)
if err != nil {
return "", fmt.Errorf("Error building template: %s", err)
}
Expand Down
13 changes: 13 additions & 0 deletions templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"github.com/sensu/sensu-go/types"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

var (
templateOk = "Check: {{ .Check.Name }} Entity: {{ .Entity.Name }} !"
templateOkUnixTime = "Check: {{ .Check.Name }} Entity: {{ .Entity.Name }} Executed: {{(UnixTime .Check.Executed).Format \"2 Jan 2006 15:04:05\"}} !"
templateVarNotFound = "Check: {{ .Check.NameZZZ }} Entity: {{ .Entity.Name }} !"
templateInvalid = "Check: {{ .Check.Name Entity: {{ .Entity.Name }} !"
)
Expand All @@ -23,6 +25,17 @@ func TestEvalTemplate_Valid(t *testing.T) {
assert.Equal(t, "Check: check-nginx Entity: webserver01 !", result)
}

// Valid test - Time Check
func TestEvalTemplateUnixTime_Valid(t *testing.T) {
event := &types.Event{}
_ = json.Unmarshal(testEventBytes, event)

executed := time.Unix(event.Check.Executed, 0).Format("2 Jan 2006 15:04:05")
result, err := EvalTemplate("templOk", templateOkUnixTime, event)
assert.Nil(t, err)
assert.Equal(t, "Check: check-nginx Entity: webserver01 Executed: " + executed + " !", result)
}

// Variable not found
func TestEvalTemplate_VarNotFound(t *testing.T) {
event := &types.Event{}
Expand Down

0 comments on commit bbf3094

Please sign in to comment.