Skip to content
Open
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
8 changes: 5 additions & 3 deletions receivers/googlechat/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
const Version = schema.V1

type Config struct {
URL string `json:"url,omitempty" yaml:"url,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Message string `json:"message,omitempty" yaml:"message,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Message string `json:"message,omitempty" yaml:"message,omitempty"`
HideOpenButton bool `json:"hide_open_button,omitempty" yaml:"hide_open_button,omitempty"`
HideVersionInfo bool `json:"hide_version_info,omitempty" yaml:"hide_version_info,omitempty"`
}

func NewConfig(jsonData json.RawMessage, decryptFn receivers.DecryptFunc) (Config, error) {
Expand Down
40 changes: 25 additions & 15 deletions receivers/googlechat/v1/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ func TestNewConfig(t *testing.T) {
settings: `{ "url": "http://localhost" }`,
secrets: map[string][]byte{},
expectedConfig: Config{
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
URL: "http://localhost",
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
URL: "http://localhost",
HideOpenButton: false,
HideVersionInfo: false,
},
},
{
Expand All @@ -51,9 +53,11 @@ func TestNewConfig(t *testing.T) {
"url": []byte("http://localhost"),
},
expectedConfig: Config{
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
URL: "http://localhost",
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
URL: "http://localhost",
HideOpenButton: false,
HideVersionInfo: false,
},
},
{
Expand All @@ -63,28 +67,34 @@ func TestNewConfig(t *testing.T) {
"url": []byte("http://test"),
},
expectedConfig: Config{
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
URL: "http://test",
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
URL: "http://test",
HideOpenButton: false,
HideVersionInfo: false,
},
},
{
name: "All empty fields = minimal valid configuration",
settings: `{"url": "http://localhost", "title": "", "message": "", "avatar_url" : "", "use_discord_username": null}`,
expectedConfig: Config{
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
URL: "http://localhost",
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
URL: "http://localhost",
HideOpenButton: false,
HideVersionInfo: false,
},
},
{
name: "Extracts all fields + override from secrets",
settings: FullValidConfigForTesting,
secrets: receiversTesting.ReadSecretsJSONForTesting(FullValidSecretsForTesting),
expectedConfig: Config{
Title: "test-title",
Message: "test-message",
URL: "http://localhost/url-secret",
Title: "test-title",
Message: "test-message",
URL: "http://localhost/url-secret",
HideOpenButton: true,
HideVersionInfo: true,
},
},
}
Expand Down
44 changes: 24 additions & 20 deletions receivers/googlechat/v1/googlechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,37 @@ func (gcn *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, erro
tmplErr = nil
}

ruleURL := receivers.JoinURLPath(gcn.tmpl.ExternalURL.String(), "/alerting/list", l)
if gcn.isURLAbsolute(ruleURL, l) {
// Add a button widget (link to Grafana).
widgets = append(widgets, buttonWidget{
Buttons: []button{
{
TextButton: textButton{
Text: "OPEN IN GRAFANA",
OnClick: onClick{
OpenLink: openLink{
URL: ruleURL,
if !gcn.settings.HideOpenButton {
ruleURL := receivers.JoinURLPath(gcn.tmpl.ExternalURL.String(), "/alerting/list", l)
if gcn.isURLAbsolute(ruleURL, l) {
// Add a button widget (link to Grafana).
widgets = append(widgets, buttonWidget{
Buttons: []button{
{
TextButton: textButton{
Text: "OPEN IN GRAFANA",
OnClick: onClick{
OpenLink: openLink{
URL: ruleURL,
},
},
},
},
},
},
})
} else {
level.Warn(l).Log("msg", "Grafana external URL setting is missing or invalid. Skipping 'open in grafana' button to prevent Google from displaying empty alerts.", "ruleURL", ruleURL)
})
} else {
level.Warn(l).Log("msg", "Grafana external URL setting is missing or invalid. Skipping 'open in grafana' button to prevent Google from displaying empty alerts.", "ruleURL", ruleURL)
}
}

// Add text paragraph widget for the build version and timestamp.
widgets = append(widgets, textParagraphWidget{
Text: text{
Text: "Grafana v" + gcn.appVersion + " | " + (timeNow()).Format(time.RFC822),
},
})
if !gcn.settings.HideVersionInfo {
widgets = append(widgets, textParagraphWidget{
Text: text{
Text: "Grafana v" + gcn.appVersion + " | " + (timeNow()).Format(time.RFC822),
},
})
}

title := tmpl(gcn.settings.Title)
// Nest the required structs.
Expand Down
88 changes: 88 additions & 0 deletions receivers/googlechat/v1/googlechat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,94 @@ func TestNotify(t *testing.T) {
},
},
},
{
name: "Hide open button",
settings: Config{
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
URL: "http://localhost",
HideOpenButton: true,
}, // URL in settings = googlechat url
externalURL: "", // external URL = URL of grafana from configuration
alerts: []*types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"},
},
},
},
expMsg: &outerStruct{
PreviewText: "[FIRING:1] (val1)",
FallbackText: "[FIRING:1] (val1)",
Cards: []card{
{
Header: header{
Title: "[FIRING:1] (val1)",
},
Sections: []section{
{
Widgets: []widget{
textParagraphWidget{
Text: text{
Text: "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\n",
},
},

textParagraphWidget{
Text: text{
// RFC822 only has the minute, hence it works in most cases.
Text: "Grafana v" + appVersion + " | " + constNow.Format(time.RFC822),
},
},
},
},
},
},
},
},
},
{
name: "Hide version info",
settings: Config{
Title: templates.DefaultMessageTitleEmbed,
Message: templates.DefaultMessageEmbed,
URL: "http://localhost",
HideOpenButton: true,
HideVersionInfo: true,
}, // URL in settings = googlechat url
externalURL: "", // external URL = URL of grafana from configuration
alerts: []*types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
Annotations: model.LabelSet{"ann1": "annv1", "__dashboardUid__": "abcd", "__panelId__": "efgh"},
},
},
},
expMsg: &outerStruct{
PreviewText: "[FIRING:1] (val1)",
FallbackText: "[FIRING:1] (val1)",
Cards: []card{
{
Header: header{
Title: "[FIRING:1] (val1)",
},
Sections: []section{
{
Widgets: []widget{
textParagraphWidget{
Text: text{
Text: "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\n",
},
},
},
},
},
},
},
},
},
}

for _, c := range cases {
Expand Down
4 changes: 3 additions & 1 deletion receivers/googlechat/v1/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package v1
const FullValidConfigForTesting = `{
"url": "http://localhost",
"title": "test-title",
"message": "test-message"
"message": "test-message",
"hide_open_button": true,
"hide_version_info": true
}`

// FullValidSecretsForTesting is a string representation of JSON object that contains all fields that can be overridden from secrets.
Expand Down