Skip to content

Commit

Permalink
feat: Add Alert Method types (#475)
Browse files Browse the repository at this point in the history
## Motivation

Similar to `Agent` or `Direct`, `AlertMethod` has different types based
on its `spec` contents.
Each type corresponds to the method's integration provider.
It would be beneficial to be able to quickly tell what type of
integration does given method support, just like `Agent.Spec.GetType`.

## Release Notes

Added `AlertMethod.Spec.GetType()` function and
`v1alpha.AlertMethodType` enum to help discern different types of alert
methods.
  • Loading branch information
nieomylnieja authored Jul 1, 2024
1 parent 47439ff commit 4bf666a
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ jobs:
${{ runner.os }}-yarn-
- name: Run spell and markdown checkers
run: make check/spell check/trailing check/markdown
- name: Check generated code
run: make check/generate
- name: Check formatting
run: make check/format
- name: Check generated code
run: make check/generate
- name: Run go vet
run: make check/vet
- name: Run golangci-lint
Expand Down
56 changes: 28 additions & 28 deletions manifest/v1alpha/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,61 +86,61 @@ type Status struct {
NewestBetaAgentVersion string `json:"newestBetaAgentVersion,omitempty"`
}

func (spec Spec) GetType() (v1alpha.DataSourceType, error) {
func (s Spec) GetType() (v1alpha.DataSourceType, error) {
switch {
case spec.Prometheus != nil:
case s.Prometheus != nil:
return v1alpha.Prometheus, nil
case spec.Datadog != nil:
case s.Datadog != nil:
return v1alpha.Datadog, nil
case spec.NewRelic != nil:
case s.NewRelic != nil:
return v1alpha.NewRelic, nil
case spec.AppDynamics != nil:
case s.AppDynamics != nil:
return v1alpha.AppDynamics, nil
case spec.Splunk != nil:
case s.Splunk != nil:
return v1alpha.Splunk, nil
case spec.Lightstep != nil:
case s.Lightstep != nil:
return v1alpha.Lightstep, nil
case spec.SplunkObservability != nil:
case s.SplunkObservability != nil:
return v1alpha.SplunkObservability, nil
case spec.Dynatrace != nil:
case s.Dynatrace != nil:
return v1alpha.Dynatrace, nil
case spec.Elasticsearch != nil:
case s.Elasticsearch != nil:
return v1alpha.Elasticsearch, nil
case spec.ThousandEyes != nil:
case s.ThousandEyes != nil:
return v1alpha.ThousandEyes, nil
case spec.Graphite != nil:
case s.Graphite != nil:
return v1alpha.Graphite, nil
case spec.BigQuery != nil:
case s.BigQuery != nil:
return v1alpha.BigQuery, nil
case spec.OpenTSDB != nil:
case s.OpenTSDB != nil:
return v1alpha.OpenTSDB, nil
case spec.GrafanaLoki != nil:
case s.GrafanaLoki != nil:
return v1alpha.GrafanaLoki, nil
case spec.CloudWatch != nil:
case s.CloudWatch != nil:
return v1alpha.CloudWatch, nil
case spec.Pingdom != nil:
case s.Pingdom != nil:
return v1alpha.Pingdom, nil
case spec.AmazonPrometheus != nil:
case s.AmazonPrometheus != nil:
return v1alpha.AmazonPrometheus, nil
case spec.Redshift != nil:
case s.Redshift != nil:
return v1alpha.Redshift, nil
case spec.SumoLogic != nil:
case s.SumoLogic != nil:
return v1alpha.SumoLogic, nil
case spec.Instana != nil:
case s.Instana != nil:
return v1alpha.Instana, nil
case spec.InfluxDB != nil:
case s.InfluxDB != nil:
return v1alpha.InfluxDB, nil
case spec.GCM != nil:
case s.GCM != nil:
return v1alpha.GCM, nil
case spec.AzureMonitor != nil:
case s.AzureMonitor != nil:
return v1alpha.AzureMonitor, nil
case spec.Generic != nil:
case s.Generic != nil:
return v1alpha.Generic, nil
case spec.Honeycomb != nil:
case s.Honeycomb != nil:
return v1alpha.Honeycomb, nil
case spec.LogicMonitor != nil:
case s.LogicMonitor != nil:
return v1alpha.LogicMonitor, nil
case spec.AzurePrometheus != nil:
case s.AzurePrometheus != nil:
return v1alpha.AzurePrometheus, nil
}
return 0, errors.New("unknown agent type")
Expand Down
18 changes: 18 additions & 0 deletions manifest/v1alpha/alert_methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v1alpha

//go:generate ../../bin/go-enum --values --noprefix

// DataSourceType represents the specific type of alert method.
//
/* ENUM(
Webhook = 1
PagerDuty
Slack
Discord
Opsgenie
ServiceNow
Jira
Teams
Email
)*/
type AlertMethodType int
100 changes: 100 additions & 0 deletions manifest/v1alpha/alert_methods_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions manifest/v1alpha/alertmethod/alert_method.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package alertmethod

import (
"github.com/pkg/errors"

"github.com/nobl9/nobl9-go/manifest"
"github.com/nobl9/nobl9-go/manifest/v1alpha"
)

//go:generate go run ../../../internal/cmd/objectimpl AlertMethod
Expand Down Expand Up @@ -56,6 +59,30 @@ type Spec struct {
Email *EmailAlertMethod `json:"email,omitempty"`
}

func (s Spec) GetType() (v1alpha.AlertMethodType, error) {
switch {
case s.Webhook != nil:
return v1alpha.Webhook, nil
case s.PagerDuty != nil:
return v1alpha.PagerDuty, nil
case s.Slack != nil:
return v1alpha.Slack, nil
case s.Discord != nil:
return v1alpha.Discord, nil
case s.Opsgenie != nil:
return v1alpha.Opsgenie, nil
case s.ServiceNow != nil:
return v1alpha.ServiceNow, nil
case s.Jira != nil:
return v1alpha.Jira, nil
case s.Teams != nil:
return v1alpha.Teams, nil
case s.Email != nil:
return v1alpha.Email, nil
}
return 0, errors.New("unknown alert method type")
}

type WebhookAlertMethod struct {
URL string `json:"url"` // Field required when AlertMethod is created.
Template *string `json:"template,omitempty"`
Expand Down
35 changes: 35 additions & 0 deletions manifest/v1alpha/alertmethod/alert_method_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package alertmethod

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/nobl9/nobl9-go/manifest/v1alpha"
)

func TestAlertMethod_Spec_GetType(t *testing.T) {
for _, method := range v1alpha.AlertMethodTypeValues() {
t.Run(method.String(), func(t *testing.T) {
spec := Spec{}
methodTypeStr := method.String()
setZeroValue(t, &spec, methodTypeStr)
typ, err := spec.GetType()
require.NoError(t, err)
assert.Equal(t, typ.String(), method.String())
})
}
}

// setZeroValue sets a zero value of a pointer field in a struct using reflection.
func setZeroValue(t *testing.T, obj interface{}, fieldName string) {
t.Helper()
objValue := reflect.ValueOf(obj).Elem()
fieldValue := objValue.FieldByName(fieldName)
if !fieldValue.IsValid() || !fieldValue.CanSet() {
t.Fatalf("cannot set value for field: %s", fieldName)
}
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
}

0 comments on commit 4bf666a

Please sign in to comment.