Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: datadog events #277

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions pkg/services/datadog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package services

import (
"bytes"
"fmt"
texttemplate "text/template"
)

type DatadogNotification struct {
AlertType string `json:"alertType,omitempty"`
Tags string `json:"tags,omitempty"`
}

func (n *DatadogNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) {
tags, err := texttemplate.New(name).Funcs(f).Parse(n.Tags)
if err != nil {
return nil, err
}

return func(notification *Notification, vars map[string]interface{}) error {
if notification.Datadog == nil {
notification.Datadog = &DatadogNotification{}
}

notification.Datadog.AlertType = n.AlertType

var tagsData bytes.Buffer
if err := tags.Execute(&tagsData, vars); err != nil {
return err
}
notification.Datadog.Tags = tagsData.String()

return nil
}, nil
}

type DatadogOptions struct {
DDApiKey string `json:"ddApiKey"`
DDAppKey string `json:"ddAppKey"`
}

type datadogService struct {
opts DatadogOptions
}

func NewDatadogService(opts DatadogOptions) NotificationService {
return &datadogService{opts: opts}
}

func (s *datadogService) Send(notification Notification, dest Destination) error {
// print notificaiton.Datadog
fmt.Printf("%+v", notification.Datadog)

return nil
}
74 changes: 74 additions & 0 deletions pkg/services/datadog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package services

import (
"testing"
"text/template"

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

func TestGetTemplater_Datadog(t *testing.T) {
n := Notification{
Datadog: &DatadogNotification{
AlertType: "info",
Tags: "{{.context.argocdUrl}}",
},
}
templater, err := n.GetTemplater("", template.FuncMap{})

if !assert.NoError(t, err) {
return
}

var notification Notification
err = templater(&notification, map[string]interface{}{
"context": map[string]interface{}{
"argocdUrl": "https://example.com",
"state": "success",
},
"app": map[string]interface{}{
"metadata": map[string]interface{}{
"name": "argocd-notifications",
},
"spec": map[string]interface{}{
"source": map[string]interface{}{
"repoURL": "https://github.com/argoproj-labs/argocd-notifications.git",
},
},
"status": map[string]interface{}{
"operationState": map[string]interface{}{
"syncResult": map[string]interface{}{
"revision": "0123456789",
},
},
},
},
})

if !assert.NoError(t, err) {
return
}

assert.Equal(t, "https://github.com/argoproj-labs/argocd-notifications.git", notification.GitHub.repoURL)
assert.Equal(t, "0123456789", notification.GitHub.revision)
assert.Equal(t, "success", notification.GitHub.Status.State)
assert.Equal(t, "continuous-delivery/argocd-notifications", notification.GitHub.Status.Label)
assert.Equal(t, "https://example.com/applications/argocd-notifications", notification.GitHub.Status.TargetURL)
}

/*
func TestSend_DataDogService_BadURL(t *testing.T) {
e := datadogService{}.Send(
Notification{
GitHub: &GitHubNotification{
repoURL: "hello",
},
},
Destination{
Service: "",
Recipient: "",
},
)
assert.ErrorContains(t, e, "does not have a `/`")
}
*/
10 changes: 10 additions & 0 deletions pkg/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Notification struct {
Pagerduty *PagerDutyNotification `json:"pagerduty,omitempty"`
PagerdutyV2 *PagerDutyV2Notification `json:"pagerdutyv2,omitempty"`
Newrelic *NewrelicNotification `json:"newrelic,omitempty"`
Datadog *DatadogNotification `json:"datadog,omitempty"`
}

// Destinations holds notification destinations group by trigger
Expand Down Expand Up @@ -103,6 +104,9 @@ func (n *Notification) GetTemplater(name string, f texttemplate.FuncMap) (Templa
if n.Newrelic != nil {
sources = append(sources, n.Newrelic)
}
if n.Datadog != nil {
sources = append(sources, n.Datadog)
}
return n.getTemplater(name, f, sources)
}

Expand Down Expand Up @@ -223,6 +227,12 @@ func NewService(serviceType string, optsData []byte) (NotificationService, error
return nil, err
}
return NewWebexService(opts), nil
case "datadpg":
var opts DatadogOptions
if err := yaml.Unmarshal(optsData, &opts); err != nil {
return nil, err
}
return NewDatadogService(opts), nil
default:
return nil, fmt.Errorf("service type '%s' is not supported", serviceType)
}
Expand Down
Loading