Skip to content

Commit

Permalink
Use type alias for Alertmanager template package (#71)
Browse files Browse the repository at this point in the history
* move type aliases to templates
* replace code to use the aliases
  • Loading branch information
yuri-tceretian authored Mar 29, 2023
1 parent c151a6d commit bd8ae70
Show file tree
Hide file tree
Showing 26 changed files with 136 additions and 142 deletions.
5 changes: 3 additions & 2 deletions notify/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/template"

"github.com/prometheus/alertmanager/types"

"github.com/grafana/alerting/images"
Expand All @@ -29,13 +29,14 @@ import (
"github.com/grafana/alerting/receivers/webex"
"github.com/grafana/alerting/receivers/webhook"
"github.com/grafana/alerting/receivers/wecom"
"github.com/grafana/alerting/templates"
)

// BuildReceiverIntegrations creates integrations for each configured notification channel in GrafanaReceiverConfig.
// It returns a slice of Integration objects, one for each notification channel, along with any errors that occurred.
func BuildReceiverIntegrations(
receiver GrafanaReceiverConfig,
tmpl *template.Template,
tmpl *templates.Template,
img images.ImageStore,
logger logging.LoggerFactory,
newWebhookSender func(n receivers.Metadata) (receivers.WebhookSender, error),
Expand Down
20 changes: 9 additions & 11 deletions notify/grafana_alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
"github.com/prometheus/alertmanager/provider/mem"
"github.com/prometheus/alertmanager/silence"
pb "github.com/prometheus/alertmanager/silence/silencepb"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/timeinterval"
"github.com/prometheus/alertmanager/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"

"github.com/grafana/alerting/models"
"github.com/grafana/alerting/templates"
)

const (
Expand Down Expand Up @@ -87,7 +87,7 @@ type GrafanaAlertmanager struct {
inhibitor *inhibit.Inhibitor
silencer *silence.Silencer
silences *silence.Silences
templates *Template
templates *templates.Template

// muteTimes is a map where the key is the name of the mute_time_interval
// and the value represents all configured time_interval(s)
Expand All @@ -100,7 +100,7 @@ type GrafanaAlertmanager struct {
configHash [16]byte
config []byte
receivers []*notify.Receiver
buildReceiverIntegrationFunc func(next *GrafanaReceiver, tmpl *Template) (Notifier, error)
buildReceiverIntegrationFunc func(next *GrafanaReceiver, tmpl *templates.Template) (Notifier, error)
}

// State represents any of the two 'states' of the alertmanager. Notification log or Silences.
Expand All @@ -123,9 +123,7 @@ type MaintenanceOptions interface {
}

var NewIntegration = notify.NewIntegration
var FromGlobs = template.FromGlobs

type Template = template.Template
type InhibitRule = config.InhibitRule
type MuteTimeInterval = config.MuteTimeInterval
type TimeInterval = timeinterval.TimeInterval
Expand All @@ -143,10 +141,10 @@ type Configuration interface {
InhibitRules() []InhibitRule
MuteTimeIntervals() []MuteTimeInterval
ReceiverIntegrations() (map[string][]*Integration, error)
BuildReceiverIntegrationsFunc() func(next *GrafanaReceiver, tmpl *Template) (Notifier, error)
BuildReceiverIntegrationsFunc() func(next *GrafanaReceiver, tmpl *templates.Template) (Notifier, error)

RoutingTree() *Route
Templates() *Template
Templates() *templates.Template

Hash() [16]byte
Raw() []byte
Expand Down Expand Up @@ -315,8 +313,8 @@ func (am *GrafanaAlertmanager) WithLock(fn func()) {
}

// TemplateFromPaths returns a set of *Templates based on the paths given.
func (am *GrafanaAlertmanager) TemplateFromPaths(u string, paths ...string) (*Template, error) {
tmpl, err := template.FromGlobs(paths)
func (am *GrafanaAlertmanager) TemplateFromPaths(u string, paths ...string) (*templates.Template, error) {
tmpl, err := templates.FromGlobs(paths)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -602,7 +600,7 @@ func (am *GrafanaAlertmanager) timeoutFunc(d time.Duration) time.Duration {
return d + am.waitFunc()
}

func (am *GrafanaAlertmanager) getTemplate() (*template.Template, error) {
func (am *GrafanaAlertmanager) getTemplate() (*templates.Template, error) {
am.reloadConfigMtx.RLock()
defer am.reloadConfigMtx.RUnlock()
if !am.ready() {
Expand All @@ -616,6 +614,6 @@ func (am *GrafanaAlertmanager) tenantString() string {
return fmt.Sprintf("%d", am.tenantID)
}

func (am *GrafanaAlertmanager) buildReceiverIntegration(next *GrafanaReceiver, tmpl *template.Template) (Notifier, error) {
func (am *GrafanaAlertmanager) buildReceiverIntegration(next *GrafanaReceiver, tmpl *templates.Template) (Notifier, error) {
return am.buildReceiverIntegrationFunc(next, tmpl)
}
3 changes: 2 additions & 1 deletion notify/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/prometheus/alertmanager/types"

receiversTesting "github.com/grafana/alerting/receivers/testing"
"github.com/grafana/alerting/templates"
)

func newFakeMaintanenceOptions(t *testing.T) *fakeMaintenanceOptions {
Expand Down Expand Up @@ -62,7 +63,7 @@ func (f *FakeConfig) RoutingTree() *Route {
panic("implement me")
}

func (f *FakeConfig) Templates() *Template {
func (f *FakeConfig) Templates() *templates.Template {
// TODO implement me
panic("implement me")
}
Expand Down
5 changes: 2 additions & 3 deletions receivers/dinding/dingding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net/url"

"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"

"github.com/grafana/alerting/logging"
Expand All @@ -19,11 +18,11 @@ type Notifier struct {
*receivers.Base
log logging.Logger
ns receivers.WebhookSender
tmpl *template.Template
tmpl *templates.Template
settings Config
}

func New(cfg Config, meta receivers.Metadata, template *template.Template, sender receivers.WebhookSender, logger logging.Logger) *Notifier {
func New(cfg Config, meta receivers.Metadata, template *templates.Template, sender receivers.WebhookSender, logger logging.Logger) *Notifier {
return &Notifier{
Base: receivers.NewBase(meta),
log: logger,
Expand Down
10 changes: 5 additions & 5 deletions receivers/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
"strings"

"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/template"

"github.com/prometheus/alertmanager/types"
"github.com/prometheus/common/model"

"github.com/grafana/alerting/images"
"github.com/grafana/alerting/logging"
"github.com/grafana/alerting/receivers"
template2 "github.com/grafana/alerting/templates"
"github.com/grafana/alerting/templates"
)

// Constants and models are set according to the official documentation https://discord.com/developers/docs/resources/webhook#execute-webhook-jsonform-params
Expand Down Expand Up @@ -69,7 +69,7 @@ type Notifier struct {
log logging.Logger
ns receivers.WebhookSender
images images.ImageStore
tmpl *template.Template
tmpl *templates.Template
settings Config
appVersion string
}
Expand All @@ -82,7 +82,7 @@ type discordAttachment struct {
state model.AlertStatus
}

func New(cfg Config, meta receivers.Metadata, template *template.Template, sender receivers.WebhookSender, images images.ImageStore, logger logging.Logger, appVersion string) *Notifier {
func New(cfg Config, meta receivers.Metadata, template *templates.Template, sender receivers.WebhookSender, images images.ImageStore, logger logging.Logger, appVersion string) *Notifier {
return &Notifier{
Base: receivers.NewBase(meta),
log: logger,
Expand All @@ -104,7 +104,7 @@ func (d Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}

var tmplErr error
tmpl, _ := template2.TmplText(ctx, d.tmpl, as, d.log, &tmplErr)
tmpl, _ := templates.TmplText(ctx, d.tmpl, as, d.log, &tmplErr)

msg.Content = tmpl(d.settings.Message)
if tmplErr != nil {
Expand Down
9 changes: 4 additions & 5 deletions receivers/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"path"
"path/filepath"

"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"

"github.com/grafana/alerting/images"
"github.com/grafana/alerting/logging"
"github.com/grafana/alerting/receivers"
template2 "github.com/grafana/alerting/templates"
"github.com/grafana/alerting/templates"
)

// Notifier is responsible for sending
Expand All @@ -23,11 +22,11 @@ type Notifier struct {
log logging.Logger
ns receivers.EmailSender
images images.ImageStore
tmpl *template.Template
tmpl *templates.Template
settings Config
}

func New(cfg Config, meta receivers.Metadata, template *template.Template, sender receivers.EmailSender, images images.ImageStore, logger logging.Logger) *Notifier {
func New(cfg Config, meta receivers.Metadata, template *templates.Template, sender receivers.EmailSender, images images.ImageStore, logger logging.Logger) *Notifier {
return &Notifier{
Base: receivers.NewBase(meta),
log: logger,
Expand All @@ -41,7 +40,7 @@ func New(cfg Config, meta receivers.Metadata, template *template.Template, sende
// Notify sends the alert notification.
func (en *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, error) {
var tmplErr error
tmpl, data := template2.TmplText(ctx, en.tmpl, alerts, en.log, &tmplErr)
tmpl, data := templates.TmplText(ctx, en.tmpl, alerts, en.log, &tmplErr)

subject := tmpl(en.settings.Subject)
alertPageURL := en.tmpl.ExternalURL.String()
Expand Down
11 changes: 5 additions & 6 deletions receivers/email/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/url"
"testing"

"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -82,17 +81,17 @@ func TestNotify(t *testing.T) {
"Alerts": templates.ExtendedAlerts{
templates.ExtendedAlert{
Status: "firing",
Labels: template.KV{"alertname": "AlwaysFiring", "severity": "warning"},
Annotations: template.KV{"runbook_url": "http://fix.me"},
Labels: templates.KV{"alertname": "AlwaysFiring", "severity": "warning"},
Annotations: templates.KV{"runbook_url": "http://fix.me"},
Fingerprint: "15a37193dce72bab",
SilenceURL: "http://localhost/base/alerting/silence/new?alertmanager=grafana&matcher=alertname%3DAlwaysFiring&matcher=severity%3Dwarning",
DashboardURL: "http://localhost/base/d/abc",
PanelURL: "http://localhost/base/d/abc?viewPanel=5",
},
},
"GroupLabels": template.KV{},
"CommonLabels": template.KV{"alertname": "AlwaysFiring", "severity": "warning"},
"CommonAnnotations": template.KV{"runbook_url": "http://fix.me"},
"GroupLabels": templates.KV{},
"CommonLabels": templates.KV{"alertname": "AlwaysFiring", "severity": "warning"},
"CommonAnnotations": templates.KV{"runbook_url": "http://fix.me"},
"ExternalURL": "http://localhost/base",
"RuleUrl": "http://localhost/base/alerting/list",
"AlertPageUrl": "http://localhost/base/alerting/list?alertState=firing&view=state",
Expand Down
9 changes: 4 additions & 5 deletions receivers/googlechat/googlechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"net/url"
"time"

"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"

"github.com/grafana/alerting/images"
"github.com/grafana/alerting/logging"
"github.com/grafana/alerting/receivers"
template2 "github.com/grafana/alerting/templates"
"github.com/grafana/alerting/templates"
)

// Notifier is responsible for sending
Expand All @@ -23,7 +22,7 @@ type Notifier struct {
log logging.Logger
ns receivers.WebhookSender
images images.ImageStore
tmpl *template.Template
tmpl *templates.Template
settings Config
appVersion string
}
Expand All @@ -33,7 +32,7 @@ var (
timeNow = time.Now
)

func New(cfg Config, meta receivers.Metadata, template *template.Template, sender receivers.WebhookSender, images images.ImageStore, logger logging.Logger, appVersion string) *Notifier {
func New(cfg Config, meta receivers.Metadata, template *templates.Template, sender receivers.WebhookSender, images images.ImageStore, logger logging.Logger, appVersion string) *Notifier {
return &Notifier{
Base: receivers.NewBase(meta),
log: logger,
Expand All @@ -50,7 +49,7 @@ func (gcn *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, erro
gcn.log.Debug("executing Google Chat notification")

var tmplErr error
tmpl, _ := template2.TmplText(ctx, gcn.tmpl, as, gcn.log, &tmplErr)
tmpl, _ := templates.TmplText(ctx, gcn.tmpl, as, gcn.log, &tmplErr)

var widgets []widget

Expand Down
12 changes: 6 additions & 6 deletions receivers/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"fmt"

"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/template"

"github.com/prometheus/alertmanager/types"
"github.com/prometheus/common/model"

"github.com/grafana/alerting/images"
"github.com/grafana/alerting/logging"
"github.com/grafana/alerting/receivers"
template2 "github.com/grafana/alerting/templates"
"github.com/grafana/alerting/templates"
)

type kafkaBody struct {
Expand Down Expand Up @@ -51,11 +51,11 @@ type Notifier struct {
log logging.Logger
images images.ImageStore
ns receivers.WebhookSender
tmpl *template.Template
tmpl *templates.Template
settings Config
}

func New(cfg Config, meta receivers.Metadata, template *template.Template, sender receivers.WebhookSender, images images.ImageStore, logger logging.Logger) *Notifier {
func New(cfg Config, meta receivers.Metadata, template *templates.Template, sender receivers.WebhookSender, images images.ImageStore, logger logging.Logger) *Notifier {
return &Notifier{
Base: receivers.NewBase(meta),
log: logger,
Expand All @@ -77,7 +77,7 @@ func (kn *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error
// Use the v2 API to send the alert notification.
func (kn *Notifier) notifyWithAPIV2(ctx context.Context, as ...*types.Alert) (bool, error) {
var tmplErr error
tmpl, _ := template2.TmplText(ctx, kn.tmpl, as, kn.log, &tmplErr)
tmpl, _ := templates.TmplText(ctx, kn.tmpl, as, kn.log, &tmplErr)

topicURL := kn.settings.Endpoint + "/topics/" + tmpl(kn.settings.Topic)
if tmplErr != nil {
Expand Down Expand Up @@ -114,7 +114,7 @@ func (kn *Notifier) notifyWithAPIV2(ctx context.Context, as ...*types.Alert) (bo
// Use the v3 API to send the alert notification.
func (kn *Notifier) notifyWithAPIV3(ctx context.Context, as ...*types.Alert) (bool, error) {
var tmplErr error
tmpl, _ := template2.TmplText(ctx, kn.tmpl, as, kn.log, &tmplErr)
tmpl, _ := templates.TmplText(ctx, kn.tmpl, as, kn.log, &tmplErr)

// For v3 the Produce URL is like this,
// <Endpoint>/v3/clusters/<KafkaClusterID>/topics/<Topic>/records
Expand Down
9 changes: 4 additions & 5 deletions receivers/line/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"net/url"
"path"

"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"

"github.com/grafana/alerting/logging"
"github.com/grafana/alerting/receivers"
template2 "github.com/grafana/alerting/templates"
"github.com/grafana/alerting/templates"
)

var (
Expand All @@ -25,11 +24,11 @@ type Notifier struct {
*receivers.Base
log logging.Logger
ns receivers.WebhookSender
tmpl *template.Template
tmpl *templates.Template
settings Config
}

func New(cfg Config, meta receivers.Metadata, template *template.Template, sender receivers.WebhookSender, logger logging.Logger) *Notifier {
func New(cfg Config, meta receivers.Metadata, template *templates.Template, sender receivers.WebhookSender, logger logging.Logger) *Notifier {
return &Notifier{
Base: receivers.NewBase(meta),
log: logger,
Expand Down Expand Up @@ -74,7 +73,7 @@ func (ln *Notifier) buildMessage(ctx context.Context, as ...*types.Alert) string
ruleURL := path.Join(ln.tmpl.ExternalURL.String(), "/alerting/list")

var tmplErr error
tmpl, _ := template2.TmplText(ctx, ln.tmpl, as, ln.log, &tmplErr)
tmpl, _ := templates.TmplText(ctx, ln.tmpl, as, ln.log, &tmplErr)

body := fmt.Sprintf(
"%s\n%s\n\n%s",
Expand Down
Loading

0 comments on commit bd8ae70

Please sign in to comment.