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

DEV-46743: add support for ms teams workflows #50

Merged
merged 3 commits into from
Oct 31, 2024
Merged
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
2 changes: 1 addition & 1 deletion pkg/api/alerting.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (hs *HTTPServer) GetAlertNotifiers(ngalertEnabled bool) func(*models.ReqCon
availableNotifier := notifier.GetAvailableNotifiers()
allowedNotifiers := []alerting.NotifierPlugin{}
isAllowedNotifier := func(t string) bool {
allowedTypes := []string{"slack", "email", "opsgenie", "victorops", "teams", "webhook", "pagerduty", "logzio_opsgenie", "googlechat"} // LOGZ.IO GRAFANA CHANGE :: DEV-35483 - Allow type for Opsgenie Logzio intergration
allowedTypes := []string{"slack", "email", "opsgenie", "victorops", "teams", "webhook", "pagerduty", "logzio_opsgenie", "googlechat", "teams_workflows"} // LOGZ.IO GRAFANA CHANGE :: DEV-35483 - Allow type for Opsgenie Logzio intergration
isAllowedNotifier := false
for _, allowedType := range allowedTypes {
if allowedType == t {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ func (e *EmbeddedContactPoint) SecretKeys() ([]string, error) {
return []string{"url", "token"}, nil
case "teams":
return []string{}, nil
case "teams_workflows":
return []string{}, nil
case "telegram":
return []string{"bottoken"}, nil
case "threema":
Expand Down
22 changes: 22 additions & 0 deletions pkg/services/ngalert/notifier/available_channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,28 @@ func GetAvailableNotifiers() []*alerting.NotifierPlugin {
},
},
},
{
Type: "teams_workflows",
Name: "Microsoft Teams Workflows",
Description: "Sends notifications using Workflows to Microsoft Teams",
Heading: "Teams Workflows settings",
Options: []alerting.NotifierOption{
{
Label: "URL",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
Placeholder: "Teams Workflows incoming webhook url",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change this to workflows jargon

PropertyName: "url",
Required: true,
},
{ // New in 8.0.
Label: "Message",
Element: alerting.ElementTypeTextArea,
Placeholder: `{{ template "default.message" . }}`,
PropertyName: "message",
},
},
},
{
Type: "telegram",
Name: "Telegram",
Expand Down
7 changes: 4 additions & 3 deletions pkg/services/ngalert/notifier/channels/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewFactoryConfig(config *NotificationChannelConfig, notificationService not
}, nil
}

//LOGZ.IO GRAFANA CHANGE :: DEV-32721 - Remove upsupported contact points
// LOGZ.IO GRAFANA CHANGE :: DEV-32721 - Remove upsupported contact points
var receiverFactories = map[string]func(FactoryConfig) (NotificationChannel, error){
//"prometheus-alertmanager": AlertmanagerFactory,
//"dingding": DingDingFactory,
Expand All @@ -49,8 +49,9 @@ var receiverFactories = map[string]func(FactoryConfig) (NotificationChannel, err
"pagerduty": PagerdutyFactory,
//"pushover": PushoverFactory,
//"sensugo": SensuGoFactory,
"slack": SlackFactory,
"teams": TeamsFactory,
"slack": SlackFactory,
"teams": TeamsFactory,
"teams_workflows": TeamsWorkflowsFactory,
//"telegram": TelegramFactory,
//"threema": ThreemaFactory,
"victorops": VictorOpsFactory,
Expand Down
146 changes: 146 additions & 0 deletions pkg/services/ngalert/notifier/channels/teams_workflows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package channels

import (
"context"
"encoding/json"
"github.com/pkg/errors"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"

"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/notifications"
)

// TeamsWorkflowsNotifier is responsible for sending
// alert notifications to Microsoft teams with workflows.
type TeamsWorkflowsNotifier struct {
*Base
URL string
Message string
tmpl *template.Template
log log.Logger
ns notifications.WebhookSender
}

type TeamsWorkflowsConfig struct {
*NotificationChannelConfig
URL string
Message string
}

func TeamsWorkflowsFactory(fc FactoryConfig) (NotificationChannel, error) {
cfg, err := NewTeamsWorkflowsConfig(fc.Config)
if err != nil {
return nil, receiverInitError{
Reason: err.Error(),
Cfg: *fc.Config,
}
}
return NewTeamsWorkflowsNotifier(cfg, fc.NotificationService, fc.Template), nil
}

func NewTeamsWorkflowsConfig(config *NotificationChannelConfig) (*TeamsWorkflowsConfig, error) {
URL := config.Settings.Get("url").MustString()
if URL == "" {
return nil, errors.New("could not find url property in settings")
}
return &TeamsWorkflowsConfig{
NotificationChannelConfig: config,
URL: URL,
Message: config.Settings.Get("message").MustString(`{{ template "teams.default.message" .}}`),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ohadza do we want to decouple teams template from teams workflows template? (i.e. define teams_workflows.default.message).

}, nil
}

// NewTeamsWorkflowsNotifier is the constructor for TeamsWorkflows notifier.
func NewTeamsWorkflowsNotifier(config *TeamsWorkflowsConfig, ns notifications.WebhookSender, t *template.Template) *TeamsWorkflowsNotifier {
return &TeamsWorkflowsNotifier{
Base: NewBase(&models.AlertNotification{
Uid: config.UID,
Name: config.Name,
Type: config.Type,
DisableResolveMessage: config.DisableResolveMessage,
Settings: config.Settings,
}),
URL: config.URL,
Message: config.Message,
log: log.New("alerting.notifier.teams"),
ns: ns,
tmpl: t,
}
}

// Notify send an alert notification to Microsoft Teams.
func (tn *TeamsWorkflowsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
var tmplErr error
tmpl, _ := TmplText(ctx, tn.tmpl, as, tn.log, &tmplErr)

basePath := ToBasePathWithAccountRedirect(tn.tmpl.ExternalURL, types.Alerts(as...)) //LOGZ.IO GRAFANA CHANGE :: DEV-37746: Add switch to account query param
ruleURL := ToLogzioAppPath(joinUrlPath(basePath, "/alerting/list", tn.log)) // LOGZ.IO GRAFANA CHANGE :: DEV-31554 - Set APP url to logzio grafana for alert notification URLs

title := tmpl(DefaultMessageTitleEmbed)

body := map[string]interface{}{
"type": "message",
"attachments": []map[string]interface{}{
{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": map[string]interface{}{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.2",
"body": []map[string]interface{}{
{
"type": "TextBlock",
"text": title,
"weight": "bolder",
"size": "medium",
},
{
"type": "TextBlock",
"text": tmpl(tn.Message),
"wrap": true,
},
},
"actions": []map[string]interface{}{
{
"type": "Action.OpenUrl",
"title": "View Rule",
"url": ruleURL,
},
},
"backgroundImage": map[string]interface{}{
"color": getAlertStatusColor(types.Alerts(as...).Status()),
},
},
},
},
}

if tmplErr != nil {
tn.log.Warn("failed to template Teams message", "err", tmplErr.Error())
tmplErr = nil
}

u := tmpl(tn.URL)
if tmplErr != nil {
tn.log.Warn("failed to template Teams URL", "err", tmplErr.Error(), "fallback", tn.URL)
u = tn.URL
}

b, err := json.Marshal(&body)
if err != nil {
return false, errors.Wrap(err, "marshal json")
}
cmd := &models.SendWebhookSync{Url: u, Body: string(b)}

if err := tn.ns.SendWebhookSync(ctx, cmd); err != nil {
return false, errors.Wrap(err, "send notification to Teams")
}

return true, nil
}

func (tn *TeamsWorkflowsNotifier) SendResolved() bool {
return !tn.GetDisableResolveMessage()
}
Loading