mirror of
https://github.com/grafana/grafana.git
synced 2025-01-27 16:57:14 -06:00
Alerting: Append org ID to alert notification URLs (#57123)
This commit is contained in:
parent
bc280d0749
commit
db1fd10ff1
@ -87,6 +87,7 @@ const (
|
||||
// Annotations are actually a set of labels, so technically this is the label name of an annotation.
|
||||
DashboardUIDAnnotation = "__dashboardUid__"
|
||||
PanelIDAnnotation = "__panelId__"
|
||||
OrgIDAnnotation = "__orgId__"
|
||||
|
||||
// This isn't a hard-coded secret token, hence the nolint.
|
||||
//nolint:gosec
|
||||
|
@ -20,11 +20,11 @@ func TestDefaultTemplateString(t *testing.T) {
|
||||
Alert: model.Alert{
|
||||
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
|
||||
Annotations: model.LabelSet{
|
||||
"ann1": "annv1", "__dashboardUid__": "dbuid123", "__panelId__": "puid123", "__values__": "{\"A\": 1234}", "__value_string__": "1234",
|
||||
"ann1": "annv1", "__orgId__": "1", "__dashboardUid__": "dbuid123", "__panelId__": "puid123", "__values__": "{\"A\": 1234}", "__value_string__": "1234",
|
||||
},
|
||||
StartsAt: time.Now(),
|
||||
EndsAt: time.Now().Add(1 * time.Hour),
|
||||
GeneratorURL: "http://localhost/alert1",
|
||||
GeneratorURL: "http://localhost/alert1?orgId=1",
|
||||
},
|
||||
}, { // Firing without dashboard and panel ID.
|
||||
Alert: model.Alert{
|
||||
@ -38,7 +38,7 @@ func TestDefaultTemplateString(t *testing.T) {
|
||||
Alert: model.Alert{
|
||||
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val3"},
|
||||
Annotations: model.LabelSet{
|
||||
"ann1": "annv3", "__dashboardUid__": "dbuid456", "__panelId__": "puid456", "__values__": "{\"A\": 1234}", "__value_string__": "1234",
|
||||
"ann1": "annv3", "__orgId__": "1", "__dashboardUid__": "dbuid456", "__panelId__": "puid456", "__values__": "{\"A\": 1234}", "__value_string__": "1234",
|
||||
},
|
||||
StartsAt: time.Now().Add(-1 * time.Hour),
|
||||
EndsAt: time.Now().Add(-30 * time.Minute),
|
||||
@ -97,10 +97,10 @@ Labels:
|
||||
- lbl1 = val1
|
||||
Annotations:
|
||||
- ann1 = annv1
|
||||
Source: http://localhost/alert1
|
||||
Source: http://localhost/alert1?orgId=1
|
||||
Silence: http://localhost/grafana/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1
|
||||
Dashboard: http://localhost/grafana/d/dbuid123
|
||||
Panel: http://localhost/grafana/d/dbuid123?viewPanel=puid123
|
||||
Dashboard: http://localhost/grafana/d/dbuid123?orgId=1
|
||||
Panel: http://localhost/grafana/d/dbuid123?orgId=1&viewPanel=puid123
|
||||
|
||||
Value: A=1234
|
||||
Labels:
|
||||
@ -120,10 +120,10 @@ Labels:
|
||||
- lbl1 = val3
|
||||
Annotations:
|
||||
- ann1 = annv3
|
||||
Source: http://localhost/alert3
|
||||
Source: http://localhost/alert3?orgId=1
|
||||
Silence: http://localhost/grafana/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval3
|
||||
Dashboard: http://localhost/grafana/d/dbuid456
|
||||
Panel: http://localhost/grafana/d/dbuid456?viewPanel=puid456
|
||||
Dashboard: http://localhost/grafana/d/dbuid456?orgId=1
|
||||
Panel: http://localhost/grafana/d/dbuid456?orgId=1&viewPanel=puid456
|
||||
|
||||
Value: A=1234
|
||||
Labels:
|
||||
@ -147,13 +147,13 @@ Labels:
|
||||
Annotations:
|
||||
- ann1 = annv1
|
||||
|
||||
Source: [http://localhost/alert1](http://localhost/alert1)
|
||||
Source: [http://localhost/alert1?orgId=1](http://localhost/alert1?orgId=1)
|
||||
|
||||
Silence: [http://localhost/grafana/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1](http://localhost/grafana/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1)
|
||||
|
||||
Dashboard: [http://localhost/grafana/d/dbuid123](http://localhost/grafana/d/dbuid123)
|
||||
Dashboard: [http://localhost/grafana/d/dbuid123?orgId=1](http://localhost/grafana/d/dbuid123?orgId=1)
|
||||
|
||||
Panel: [http://localhost/grafana/d/dbuid123?viewPanel=puid123](http://localhost/grafana/d/dbuid123?viewPanel=puid123)
|
||||
Panel: [http://localhost/grafana/d/dbuid123?orgId=1&viewPanel=puid123](http://localhost/grafana/d/dbuid123?orgId=1&viewPanel=puid123)
|
||||
|
||||
|
||||
|
||||
@ -182,13 +182,13 @@ Labels:
|
||||
Annotations:
|
||||
- ann1 = annv3
|
||||
|
||||
Source: [http://localhost/alert3](http://localhost/alert3)
|
||||
Source: [http://localhost/alert3?orgId=1](http://localhost/alert3?orgId=1)
|
||||
|
||||
Silence: [http://localhost/grafana/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval3](http://localhost/grafana/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval3)
|
||||
|
||||
Dashboard: [http://localhost/grafana/d/dbuid456](http://localhost/grafana/d/dbuid456)
|
||||
Dashboard: [http://localhost/grafana/d/dbuid456?orgId=1](http://localhost/grafana/d/dbuid456?orgId=1)
|
||||
|
||||
Panel: [http://localhost/grafana/d/dbuid456?viewPanel=puid456](http://localhost/grafana/d/dbuid456?viewPanel=puid456)
|
||||
Panel: [http://localhost/grafana/d/dbuid456?orgId=1&viewPanel=puid456](http://localhost/grafana/d/dbuid456?orgId=1&viewPanel=puid456)
|
||||
|
||||
|
||||
|
||||
|
@ -89,6 +89,25 @@ func extendAlert(alert template.Alert, externalURL string, logger log.Logger) *E
|
||||
u.RawQuery = "viewPanel=" + panelId
|
||||
extended.PanelURL = u.String()
|
||||
}
|
||||
|
||||
generatorUrl, err := url.Parse(extended.GeneratorURL)
|
||||
if err != nil {
|
||||
logger.Debug("failed to parse generator URL while extending template data", "url", extended.GeneratorURL, "err", err.Error())
|
||||
return extended
|
||||
}
|
||||
|
||||
dashboardUrl, err := url.Parse(extended.DashboardURL)
|
||||
if err != nil {
|
||||
logger.Debug("failed to parse dashboard URL while extending template data", "url", extended.DashboardURL, "err", err.Error())
|
||||
return extended
|
||||
}
|
||||
|
||||
orgId := alert.Annotations[ngmodels.OrgIDAnnotation]
|
||||
if len(orgId) > 0 {
|
||||
extended.DashboardURL = setOrgIdQueryParam(dashboardUrl, orgId)
|
||||
extended.PanelURL = setOrgIdQueryParam(u, orgId)
|
||||
extended.GeneratorURL = setOrgIdQueryParam(generatorUrl, orgId)
|
||||
}
|
||||
}
|
||||
|
||||
if alert.Annotations != nil {
|
||||
@ -123,6 +142,14 @@ func extendAlert(alert template.Alert, externalURL string, logger log.Logger) *E
|
||||
return extended
|
||||
}
|
||||
|
||||
func setOrgIdQueryParam(url *url.URL, orgId string) string {
|
||||
q := url.Query()
|
||||
q.Set("orgId", orgId)
|
||||
url.RawQuery = q.Encode()
|
||||
|
||||
return url.String()
|
||||
}
|
||||
|
||||
func ExtendData(data *template.Data, logger log.Logger) *ExtendedData {
|
||||
alerts := []ExtendedAlert{}
|
||||
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/benbjohnson/clock"
|
||||
@ -55,6 +56,10 @@ func stateToPostableAlert(alertState *state.State, appURL *url.URL) *models.Post
|
||||
nA[ngModels.StateReasonAnnotation] = alertState.StateReason
|
||||
}
|
||||
|
||||
if alertState.OrgID != 0 {
|
||||
nA[ngModels.OrgIDAnnotation] = strconv.FormatInt(alertState.OrgID, 10)
|
||||
}
|
||||
|
||||
var urlStr string
|
||||
if uid := nL[ngModels.RuleUIDLabel]; len(uid) > 0 && appURL != nil {
|
||||
u := *appURL
|
||||
|
@ -2619,6 +2619,7 @@ var expNonEmailNotifications = map[string][]string{
|
||||
"grafana_folder": "default"
|
||||
},
|
||||
"annotations": {
|
||||
"__orgId__":"1",
|
||||
"__values__": "{\"A\":1}",
|
||||
"__value_string__": "[ var='A' labels={} value=1 ]"
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user