Skip to content

Commit

Permalink
Merge pull request #52 from sensu/json-details-option
Browse files Browse the repository at this point in the history
Add --details-format option

Signed-off-by: Francis Guimond [email protected]
  • Loading branch information
fguimond authored Apr 4, 2023
2 parents 1896564 + 6298cea commit 2742bb2
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
uses: actions/checkout@v2
- name: Unshallow
run: git fetch --prune --unshallow
- name: Set up Go
- name: Set up Go 1.20
uses: actions/setup-go@v1
with:
go-version: 1.18.x
go-version: 1.20.x
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/static-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
- uses: actions/checkout@v1
with:
fetch-depth: 1
- uses: dominikh/staticcheck-action@v1.2.0
- uses: dominikh/staticcheck-action@v1.3.0
with:
version: "2022.1"
version: "2023.1.3"
env:
GO_VERSION: 1.18.1
GO_VERSION: 1.20.x
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Go 1.18
- name: Set up Go 1.20
uses: actions/setup-go@v1
with:
go-version: 1.18.x
go-version: 1.20.x
id: go
- name: Test
run: go test -v ./...
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

## 2.3.0 – 2022-05-06
## 2.4.0 - 2023-04-04

###
- Add `--details-format` option to be able to specify if the event details should be sent as a string or a JSON document

## 2.3.0 – 2022-05-06

### Changed

Expand Down
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Available Commands:
Flags:
--contact-routing Enable contact routing
-k, --dedup-key-template string The PagerDuty V2 API deduplication key template, can be set with PAGERDUTY_DEDUP_KEY_TEMPLATE (default "{{.Entity.Name}}-{{.Check.Name}}")
--details-format string The format of the details output ('string' or 'json'), can be set with PAGERDUTY_DETAILS_FORMAT (default "string")
-d, --details-template string The template for the alert details, can be set with PAGERDUTY_DETAILS_TEMPLATE (default full event JSON)
-h, --help help for sensu-pagerduty-handler
-s, --status-map string The status map used to translate a Sensu check status to a PagerDuty severity, can be set with PAGERDUTY_STATUS_MAP
Expand Down Expand Up @@ -132,6 +133,7 @@ spec:
--status-map "{\"info\":[0],\"warning\": [1],\"critical\": [2],\"error\": [3,127]}"
--summary-template "[{{.Entity.Namespace}}] {{.Entity.Name}}/{{.Check.Name}}: {{.Check.State}}"
--details-template "{{.Check.Output}}\n\n{{.Check}}"
--details-format string
timeout: 10
runtime_assets:
- sensu/sensu-pagerduty-handler
Expand All @@ -148,12 +150,16 @@ Most arguments for this handler are available to be set via environment
variables. However, any arguments specified directly on the command line
override the corresponding environment variable.
|Argument |Environment Variable |
|--------------------|----------------------------|
|--token |PAGERDUTY_TOKEN |
|--summary-template |PAGERDUTY_SUMMARY_TEMPLATE |
|--dedup-key-template|PAGERDUTY_DEDUP_KEY_TEMPLATE|
|--status-map |PAGERDUTY_STATUS_MAP |
| Argument | Environment Variable |
|----------------------|------------------------------|
| --dedup-key-template | PAGERDUTY_DEDUP_KEY_TEMPLATE |
| --details-template | PAGERDUTY_DETAILS_TEMPLATE |
| --details-format | PAGERDUTY_DETAILS_FORMAT |
| --status-map | PAGERDUTY_STATUS_MAP |
| --summary-template | PAGERDUTY_SUMMARY_TEMPLATE |
| --team | PAGERDUTY_TEAM |
| --team-suffix | PAGERDUTY_TEAM_SUFFIX |
| --token | PAGERDUTY_TOKEN |
**Security Note:** Care should be taken to not expose the auth token for this
handler by specifying it on the command line or by directly setting the
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/sensu/sensu-pagerduty-handler

go 1.18
go 1.20

require (
github.com/PagerDuty/go-pagerduty v1.3.0
Expand Down Expand Up @@ -40,9 +40,9 @@ require (
github.com/spf13/viper v1.7.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.0 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/net v0.0.0-20210610124326-52da8fb2a613 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.3.8 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,9 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210610124326-52da8fb2a613 h1:SqvqnUCcwFhyyRueFOEFTBaWeXYwK+CL/767809IlbQ=
golang.org/x/net v0.0.0-20210610124326-52da8fb2a613/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -380,15 +381,16 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
54 changes: 46 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,32 @@ type HandlerConfig struct {
teamName string
teamSuffix string
detailsTemplate string
detailsFormat string
contactRouting bool
contacts []string
}

type eventStatusMap map[string][]uint32

type detailsFormat string

const (
stringDetailsFormat detailsFormat = "string"
jsonDetailsFormat detailsFormat = "json"
)

func (df detailsFormat) IsValid() bool {
switch df {
case stringDetailsFormat, jsonDetailsFormat:
return true
}
return false
}

func (df detailsFormat) String() string {
return string(df)
}

var (
config = HandlerConfig{
PluginConfig: sensu.PluginConfig{
Expand Down Expand Up @@ -103,6 +123,15 @@ var (
Value: &config.detailsTemplate,
Default: "",
},
{
Path: "details-format",
Env: "PAGERDUTY_DETAILS_FORMAT",
Argument: "details-format",
Shorthand: "",
Usage: "The format of the details output ('string' or 'json'), can be set with PAGERDUTY_DETAILS_FORMAT",
Value: &config.detailsFormat,
Default: "string",
},
{
Path: "",
Env: "",
Expand Down Expand Up @@ -182,6 +211,10 @@ func checkArgs(event *corev2.Event) error {
}
}

if !detailsFormat(config.detailsFormat).IsValid() {
return fmt.Errorf("invalid details format: %s", config.detailsFormat)
}

return nil
}

Expand Down Expand Up @@ -395,7 +428,7 @@ func parseStatusMap(statusMapJSON string) (map[uint32]string, error) {
return nil, fmt.Errorf("invalid pagerduty severity: %s", severity)
}
for i := range statuses {
statusToSeverityMap[uint32(statuses[i])] = severity
statusToSeverityMap[statuses[i]] = severity
}
}

Expand All @@ -415,17 +448,22 @@ func getSummary(event *corev2.Event) (string, error) {
return summary, nil
}

func getDetails(event *corev2.Event) (interface{}, error) {
var (
details interface{}
err error
)

func getDetails(event *corev2.Event) (details interface{}, err error) {
if len(config.detailsTemplate) > 0 {
details, err = templates.EvalTemplate("details", config.detailsTemplate, event)
detailsStr, err := templates.EvalTemplate("details", config.detailsTemplate, event)
if err != nil {
return "", fmt.Errorf("failed to evaluate template %s: %v", config.detailsTemplate, err)
}

details = detailsStr
if config.detailsFormat == jsonDetailsFormat.String() {
var msgMap interface{}
err = json.Unmarshal([]byte(detailsStr), &msgMap)
if err != nil {
return "", fmt.Errorf("failed to unmarshal json details: %v", err)
}
details = msgMap
}
} else {
details = event
}
Expand Down
Loading

0 comments on commit 2742bb2

Please sign in to comment.