Skip to content

Commit

Permalink
WIP: Datadog Events
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Marcelo-Tanner <[email protected]>
  • Loading branch information
kzap committed Mar 10, 2024
1 parent d4eaae8 commit 9c4926d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
96 changes: 96 additions & 0 deletions pkg/services/datadog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package services

import (
"bytes"
"context"
texttemplate "text/template"

slackutil "github.com/argoproj/notifications-engine/pkg/util/slack"
)

type DatadogNotification struct {
Attachments string `json:"attachments,omitempty"`
Blocks string `json:"blocks,omitempty"`
GroupingKey string `json:"groupingKey"`
NotifyBroadcast bool `json:"notifyBroadcast"`
DeliveryPolicy slackutil.DeliveryPolicy `json:"deliveryPolicy"`
}

func (n *DatadogNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) {
slackAttachments, err := texttemplate.New(name).Funcs(f).Parse(n.Attachments)
if err != nil {
return nil, err
}
slackBlocks, err := texttemplate.New(name).Funcs(f).Parse(n.Blocks)
if err != nil {
return nil, err
}
groupingKey, err := texttemplate.New(name).Funcs(f).Parse(n.GroupingKey)
if err != nil {
return nil, err
}

return func(notification *Notification, vars map[string]interface{}) error {
if notification.Slack == nil {
notification.Slack = &SlackNotification{}
}
var slackAttachmentsData bytes.Buffer
if err := slackAttachments.Execute(&slackAttachmentsData, vars); err != nil {
return err
}
notification.Slack.Attachments = slackAttachmentsData.String()

var slackBlocksData bytes.Buffer
if err := slackBlocks.Execute(&slackBlocksData, vars); err != nil {
return err
}
notification.Slack.Blocks = slackBlocksData.String()

var groupingKeyData bytes.Buffer
if err := groupingKey.Execute(&groupingKeyData, vars); err != nil {
return err
}
notification.Slack.GroupingKey = groupingKeyData.String()

notification.Slack.NotifyBroadcast = n.NotifyBroadcast
notification.Slack.DeliveryPolicy = n.DeliveryPolicy
return nil
}, nil
}

type DatadogOptions struct {
Username string `json:"username"`
Icon string `json:"icon"`
Token string `json:"token"`
SigningSecret string `json:"signingSecret"`
Channels []string `json:"channels"`
InsecureSkipVerify bool `json:"insecureSkipVerify"`
ApiURL string `json:"apiURL"`
DisableUnfurl bool `json:"disableUnfurl"`
}

type datadogService struct {
opts DatadogOptions
}

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

func (s *datadogService) Send(notification Notification, dest Destination) error {
slackNotification, msgOptions, err := buildMessageOptions(notification, dest, s.opts)
if err != nil {
return err
}
return slackutil.NewThreadedClient(
newSlackClient(s.opts),
slackState,
).SendMessage(
context.TODO(),
dest.Recipient,
slackNotification.GroupingKey,
slackNotification.NotifyBroadcast,
slackNotification.DeliveryPolicy,
msgOptions,
)
}
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

0 comments on commit 9c4926d

Please sign in to comment.