|  | 
| 1 | 1 | package notify | 
| 2 | 2 | 
 | 
| 3 | 3 | import ( | 
|  | 4 | +	"context" | 
|  | 5 | +	"fmt" | 
|  | 6 | +	"sync" | 
|  | 7 | + | 
| 4 | 8 | 	"github.com/go-kit/log" | 
|  | 9 | +	"github.com/go-kit/log/level" | 
|  | 10 | +	"github.com/prometheus/alertmanager/config" | 
| 5 | 11 | 	"github.com/prometheus/alertmanager/notify" | 
|  | 12 | +	"github.com/prometheus/alertmanager/types" | 
|  | 13 | +	commoncfg "github.com/prometheus/common/config" | 
|  | 14 | + | 
|  | 15 | +	promDiscord "github.com/prometheus/alertmanager/notify/discord" | 
|  | 16 | +	promEmail "github.com/prometheus/alertmanager/notify/email" | 
|  | 17 | +	promMsteams "github.com/prometheus/alertmanager/notify/msteams" | 
|  | 18 | +	promOpsgenie "github.com/prometheus/alertmanager/notify/opsgenie" | 
|  | 19 | +	promPagerduty "github.com/prometheus/alertmanager/notify/pagerduty" | 
|  | 20 | +	promPushover "github.com/prometheus/alertmanager/notify/pushover" | 
|  | 21 | +	promSlack "github.com/prometheus/alertmanager/notify/slack" | 
|  | 22 | +	promSns "github.com/prometheus/alertmanager/notify/sns" | 
|  | 23 | +	promTelegram "github.com/prometheus/alertmanager/notify/telegram" | 
|  | 24 | +	promVictorops "github.com/prometheus/alertmanager/notify/victorops" | 
|  | 25 | +	promWebex "github.com/prometheus/alertmanager/notify/webex" | 
|  | 26 | +	promWebhook "github.com/prometheus/alertmanager/notify/webhook" | 
|  | 27 | +	promWechat "github.com/prometheus/alertmanager/notify/wechat" | 
|  | 28 | +	"github.com/prometheus/alertmanager/template" | 
| 6 | 29 | 
 | 
| 7 | 30 | 	"github.com/grafana/alerting/http" | 
| 8 | 31 | 	"github.com/grafana/alerting/images" | 
|  | 32 | +	"github.com/grafana/alerting/notify/nfstatus" | 
| 9 | 33 | 	"github.com/grafana/alerting/receivers" | 
| 10 | 34 | 	"github.com/grafana/alerting/receivers/alertmanager" | 
| 11 | 35 | 	"github.com/grafana/alerting/receivers/dinding" | 
| @@ -35,9 +59,11 @@ import ( | 
| 35 | 59 | 
 | 
| 36 | 60 | type WrapNotifierFunc func(integrationName string, notifier notify.Notifier) notify.Notifier | 
| 37 | 61 | 
 | 
| 38 |  | -// BuildReceiverIntegrations creates integrations for each configured notification channel in GrafanaReceiverConfig. | 
|  | 62 | +var NoWrap WrapNotifierFunc = func(_ string, notifier notify.Notifier) notify.Notifier { return notifier } | 
|  | 63 | + | 
|  | 64 | +// BuildGrafanaReceiverIntegrations creates integrations for each configured notification channel in GrafanaReceiverConfig. | 
| 39 | 65 | // It returns a slice of Integration objects, one for each notification channel, along with any errors that occurred. | 
| 40 |  | -func BuildReceiverIntegrations( | 
|  | 66 | +func BuildGrafanaReceiverIntegrations( | 
| 41 | 67 | 	receiver GrafanaReceiverConfig, | 
| 42 | 68 | 	tmpl *templates.Template, | 
| 43 | 69 | 	img images.Provider, | 
| @@ -133,3 +159,170 @@ func BuildReceiverIntegrations( | 
| 133 | 159 | 	} | 
| 134 | 160 | 	return integrations | 
| 135 | 161 | } | 
|  | 162 | + | 
|  | 163 | +// BuildPrometheusReceiverIntegrations builds a list of integration notifiers off of a receiver config. | 
|  | 164 | +// Taken from https://github.com/grafana/mimir/blob/fa489e696481fe0b7b97598077565dc5027afa84/pkg/alertmanager/alertmanager.go#L754 | 
|  | 165 | +// which is taken from https://github.com/prometheus/alertmanager/blob/94d875f1227b29abece661db1a68c001122d1da5/cmd/alertmanager/main.go#L112-L159. | 
|  | 166 | +func BuildPrometheusReceiverIntegrations( | 
|  | 167 | +	nc config.Receiver, | 
|  | 168 | +	tmplProvider TemplatesProvider, | 
|  | 169 | +	httpClientOptions []http.ClientOption, | 
|  | 170 | +	logger log.Logger, | 
|  | 171 | +	wrapper WrapNotifierFunc, | 
|  | 172 | +) ([]*nfstatus.Integration, error) { | 
|  | 173 | +	var ( | 
|  | 174 | +		errs         types.MultiError | 
|  | 175 | +		integrations []*nfstatus.Integration | 
|  | 176 | +		tmpl         *template.Template | 
|  | 177 | +		httpOps      []commoncfg.HTTPClientOption | 
|  | 178 | +		initOnce     = sync.OnceFunc(func() { // lazy evaluate template so we do not create one if we don't need it | 
|  | 179 | +			httpOps = http.ToHTTPClientOption(httpClientOptions...) | 
|  | 180 | +			t, err := tmplProvider.GetTemplate(templates.MimirKind) | 
|  | 181 | +			if err != nil { | 
|  | 182 | +				errs.Add(err) | 
|  | 183 | +				return | 
|  | 184 | +			} | 
|  | 185 | +			tmpl = t | 
|  | 186 | +		}) | 
|  | 187 | +		add = func(name string, i int, rs notify.ResolvedSender, f func(l log.Logger) (notify.Notifier, error)) { | 
|  | 188 | +			initOnce() | 
|  | 189 | +			n, err := f(log.With(logger, "integration", name)) | 
|  | 190 | +			if err != nil { | 
|  | 191 | +				errs.Add(err) | 
|  | 192 | +				return | 
|  | 193 | +			} | 
|  | 194 | +			if wrapper != nil { | 
|  | 195 | +				n = wrapper(name, n) | 
|  | 196 | +			} | 
|  | 197 | +			integrations = append(integrations, nfstatus.NewIntegration(n, rs, name, i, nc.Name)) | 
|  | 198 | +		} | 
|  | 199 | +	) | 
|  | 200 | + | 
|  | 201 | +	for i, c := range nc.WebhookConfigs { | 
|  | 202 | +		add("webhook", i, c, func(l log.Logger) (notify.Notifier, error) { return promWebhook.New(c, tmpl, l, httpOps...) }) | 
|  | 203 | +	} | 
|  | 204 | +	for i, c := range nc.EmailConfigs { | 
|  | 205 | +		add("email", i, c, func(l log.Logger) (notify.Notifier, error) { return promEmail.New(c, tmpl, l), nil }) | 
|  | 206 | +	} | 
|  | 207 | +	for i, c := range nc.PagerdutyConfigs { | 
|  | 208 | +		add("pagerduty", i, c, func(l log.Logger) (notify.Notifier, error) { return promPagerduty.New(c, tmpl, l, httpOps...) }) | 
|  | 209 | +	} | 
|  | 210 | +	for i, c := range nc.OpsGenieConfigs { | 
|  | 211 | +		add("opsgenie", i, c, func(l log.Logger) (notify.Notifier, error) { return promOpsgenie.New(c, tmpl, l, httpOps...) }) | 
|  | 212 | +	} | 
|  | 213 | +	for i, c := range nc.WechatConfigs { | 
|  | 214 | +		add("wechat", i, c, func(l log.Logger) (notify.Notifier, error) { return promWechat.New(c, tmpl, l, httpOps...) }) | 
|  | 215 | +	} | 
|  | 216 | +	for i, c := range nc.SlackConfigs { | 
|  | 217 | +		add("slack", i, c, func(l log.Logger) (notify.Notifier, error) { return promSlack.New(c, tmpl, l, httpOps...) }) | 
|  | 218 | +	} | 
|  | 219 | +	for i, c := range nc.VictorOpsConfigs { | 
|  | 220 | +		add("victorops", i, c, func(l log.Logger) (notify.Notifier, error) { return promVictorops.New(c, tmpl, l, httpOps...) }) | 
|  | 221 | +	} | 
|  | 222 | +	for i, c := range nc.PushoverConfigs { | 
|  | 223 | +		add("pushover", i, c, func(l log.Logger) (notify.Notifier, error) { return promPushover.New(c, tmpl, l, httpOps...) }) | 
|  | 224 | +	} | 
|  | 225 | +	for i, c := range nc.SNSConfigs { | 
|  | 226 | +		add("sns", i, c, func(l log.Logger) (notify.Notifier, error) { return promSns.New(c, tmpl, l, httpOps...) }) | 
|  | 227 | +	} | 
|  | 228 | +	for i, c := range nc.TelegramConfigs { | 
|  | 229 | +		add("telegram", i, c, func(l log.Logger) (notify.Notifier, error) { return promTelegram.New(c, tmpl, l, httpOps...) }) | 
|  | 230 | +	} | 
|  | 231 | +	for i, c := range nc.DiscordConfigs { | 
|  | 232 | +		add("discord", i, c, func(l log.Logger) (notify.Notifier, error) { return promDiscord.New(c, tmpl, l, httpOps...) }) | 
|  | 233 | +	} | 
|  | 234 | +	for i, c := range nc.WebexConfigs { | 
|  | 235 | +		add("webex", i, c, func(l log.Logger) (notify.Notifier, error) { return promWebex.New(c, tmpl, l, httpOps...) }) | 
|  | 236 | +	} | 
|  | 237 | +	for i, c := range nc.MSTeamsConfigs { | 
|  | 238 | +		add("msteams", i, c, func(l log.Logger) (notify.Notifier, error) { return promMsteams.New(c, tmpl, l, httpOps...) }) | 
|  | 239 | +	} | 
|  | 240 | +	// If we add support for more integrations, we need to add them to validation as well. See validation.allowedIntegrationNames field. | 
|  | 241 | +	if errs.Len() > 0 { | 
|  | 242 | +		return nil, &errs | 
|  | 243 | +	} | 
|  | 244 | +	return integrations, nil | 
|  | 245 | +} | 
|  | 246 | + | 
|  | 247 | +// BuildReceiversIntegrations builds integrations for the provided API receivers and returns them mapped by receiver name. | 
|  | 248 | +// It ensures uniqueness of receivers by the name, overwriting duplicates and logs warnings. | 
|  | 249 | +// Returns an error if any integration fails during its construction. | 
|  | 250 | +func BuildReceiversIntegrations( | 
|  | 251 | +	tenantID int64, | 
|  | 252 | +	apiReceivers []*APIReceiver, | 
|  | 253 | +	templ TemplatesProvider, | 
|  | 254 | +	images images.Provider, | 
|  | 255 | +	decryptFn GetDecryptedValueFn, | 
|  | 256 | +	emailSender receivers.EmailSender, | 
|  | 257 | +	httpClientOptions []http.ClientOption, | 
|  | 258 | +	notifierFunc WrapNotifierFunc, | 
|  | 259 | +	version string, | 
|  | 260 | +	logger log.Logger, | 
|  | 261 | +) (map[string][]*Integration, error) { | 
|  | 262 | +	nameToReceiver := make(map[string]*APIReceiver, len(apiReceivers)) | 
|  | 263 | +	for _, receiver := range apiReceivers { | 
|  | 264 | +		if existing, ok := nameToReceiver[receiver.Name]; ok { | 
|  | 265 | +			itypes := make([]string, 0, len(existing.GrafanaIntegrations.Integrations)) | 
|  | 266 | +			for _, i := range existing.GrafanaIntegrations.Integrations { | 
|  | 267 | +				itypes = append(itypes, i.Type) | 
|  | 268 | +			} | 
|  | 269 | +			level.Warn(logger).Log("msg", "receiver with same name is defined multiple times. Only the last one will be used", "receiver_name", receiver.Name, "overwritten_integrations", itypes) | 
|  | 270 | +		} | 
|  | 271 | +		nameToReceiver[receiver.Name] = receiver | 
|  | 272 | +	} | 
|  | 273 | + | 
|  | 274 | +	integrationsMap := make(map[string][]*Integration, len(apiReceivers)) | 
|  | 275 | +	for name, apiReceiver := range nameToReceiver { | 
|  | 276 | +		integrations, err := BuildReceiverIntegrations(tenantID, apiReceiver, templ, images, decryptFn, emailSender, httpClientOptions, notifierFunc, version, logger) | 
|  | 277 | +		if err != nil { | 
|  | 278 | +			return nil, fmt.Errorf("failed to build receiver %s: %w", name, err) | 
|  | 279 | +		} | 
|  | 280 | +		integrationsMap[name] = integrations | 
|  | 281 | +	} | 
|  | 282 | +	return integrationsMap, nil | 
|  | 283 | +} | 
|  | 284 | + | 
|  | 285 | +// BuildReceiverIntegrations builds integrations for the provided API receiver and returns them. | 
|  | 286 | +// It supports both Prometheus and Grafana integrations and ensures that both of them use only templates dedicated for the kind. | 
|  | 287 | +func BuildReceiverIntegrations( | 
|  | 288 | +	tenantID int64, | 
|  | 289 | +	receiver *APIReceiver, | 
|  | 290 | +	tmpls TemplatesProvider, | 
|  | 291 | +	images images.Provider, | 
|  | 292 | +	decryptFn GetDecryptedValueFn, | 
|  | 293 | +	emailSender receivers.EmailSender, | 
|  | 294 | +	httpClientOptions []http.ClientOption, | 
|  | 295 | +	wrapNotifierFunc WrapNotifierFunc, | 
|  | 296 | +	version string, | 
|  | 297 | +	logger log.Logger, | 
|  | 298 | +) ([]*Integration, error) { | 
|  | 299 | +	var integrations []*Integration | 
|  | 300 | +	if len(receiver.Integrations) > 0 { | 
|  | 301 | +		receiverCfg, err := BuildReceiverConfiguration(context.Background(), receiver, DecodeSecretsFromBase64, decryptFn) | 
|  | 302 | +		if err != nil { | 
|  | 303 | +			return nil, err | 
|  | 304 | +		} | 
|  | 305 | +		tmpl, err := tmpls.GetTemplate(templates.GrafanaKind) | 
|  | 306 | +		if err != nil { | 
|  | 307 | +			return nil, err | 
|  | 308 | +		} | 
|  | 309 | +		integrations = BuildGrafanaReceiverIntegrations( | 
|  | 310 | +			receiverCfg, | 
|  | 311 | +			tmpl, | 
|  | 312 | +			images, | 
|  | 313 | +			logger, | 
|  | 314 | +			emailSender, | 
|  | 315 | +			wrapNotifierFunc, | 
|  | 316 | +			tenantID, | 
|  | 317 | +			version, | 
|  | 318 | +			httpClientOptions..., | 
|  | 319 | +		) | 
|  | 320 | +	} | 
|  | 321 | +	mimir, err := BuildPrometheusReceiverIntegrations(receiver.ConfigReceiver, tmpls, httpClientOptions, logger, wrapNotifierFunc) | 
|  | 322 | +	if err != nil { | 
|  | 323 | +		return nil, err | 
|  | 324 | +	} | 
|  | 325 | +	integrations = append(integrations, mimir...) | 
|  | 326 | + | 
|  | 327 | +	return integrations, nil | 
|  | 328 | +} | 
0 commit comments