-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.2.0 | ||
3.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package alertmanager | ||
|
||
import "time" | ||
|
||
// See https://grafana.com/docs/grafana/latest/alerting/contact-points/notifiers/webhook-notifier/ | ||
|
||
type Message struct { | ||
Receiver string `json:"receiver"` | ||
Status string `json:"status"` | ||
OrgId int `json:"orgId"` | ||
Alerts []*Alert `json:"alerts"` | ||
ExternalURL string `json:"externalURL"` | ||
Version string `json:"version"` | ||
GroupKey string `json:"groupKey"` | ||
Title string `json:"title"` | ||
State string `json:"state"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type Alert struct { | ||
Status string `json:"status"` | ||
Url string `json:"url"` | ||
Labels map[string]string `json:"labels"` | ||
Annotations map[string]string `json:"annotations"` | ||
StartsAt time.Time `json:"startsAt"` | ||
EndsAt time.Time `json:"endsAt"` | ||
GeneratorURL string `json:"generatorURL"` | ||
SilenceURL string `json:"silenceURL"` | ||
DashboardURL string `json:"dashboardURL"` | ||
PanelURL string `json:"panelURL"` | ||
Fingerprint string `json:"fingerprint"` | ||
ValueString string `json:"valueString"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package alertmanager | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/muety/telepush/config" | ||
"github.com/muety/telepush/inlets" | ||
"github.com/muety/telepush/model" | ||
"github.com/muety/telepush/resolvers" | ||
"github.com/muety/telepush/util" | ||
) | ||
|
||
type GrafanaInlet struct{} | ||
|
||
func New() inlets.Inlet { | ||
return &GrafanaInlet{} | ||
} | ||
|
||
func (i *GrafanaInlet) Handler(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
var m Message | ||
|
||
dec := json.NewDecoder(r.Body) | ||
if err := dec.Decode(&m); err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
message := transformMessage(&m) | ||
|
||
ctx := r.Context() | ||
ctx = context.WithValue(ctx, config.KeyMessage, message) | ||
ctx = context.WithValue(ctx, config.KeyParams, &model.MessageParams{DisableLinkPreviews: true}) | ||
|
||
h.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} | ||
|
||
func transformMessage(in *Message) *model.DefaultMessage { | ||
var sb strings.Builder | ||
sb.WriteString("*Grafana* wrote:\n\n") | ||
|
||
for i, a := range in.Alerts { | ||
// Status | ||
var statusEmoji string | ||
switch a.Status { | ||
case "firing": | ||
statusEmoji = "❗️" | ||
break | ||
case "resolved": | ||
statusEmoji = "✅" | ||
} | ||
sb.WriteString(fmt.Sprintf("*⌛️ Status:* %s %s\n", a.Status, statusEmoji)) | ||
|
||
// Source URL | ||
sb.WriteString(fmt.Sprintf("*🔗 Source*: [Link](%s)\n", a.Url)) | ||
|
||
// Labels | ||
if len(a.Labels) > 0 { | ||
sb.WriteString(fmt.Sprintf("*🏷 Labels:*\n")) | ||
for k, v := range a.Labels { | ||
k = util.EscapeMarkdown(k) | ||
v = util.EscapeMarkdown(v) | ||
sb.WriteString(fmt.Sprintf("– `%s` = `%s`\n", k, v)) | ||
} | ||
} | ||
|
||
// Annotations | ||
if len(a.Annotations) > 0 { | ||
sb.WriteString(fmt.Sprintf("*📝 Annotations:*\n")) | ||
for k, v := range a.Annotations { | ||
k = util.EscapeMarkdown(k) | ||
v = util.EscapeMarkdown(v) | ||
sb.WriteString(fmt.Sprintf("– `%s` = `%s`\n", k, v)) | ||
} | ||
} | ||
|
||
if a.ValueString != "" { | ||
sb.WriteString(fmt.Sprintf("*📌 Value String:*\n")) | ||
sb.WriteString(fmt.Sprintf("`%s`\n", a.ValueString)) | ||
} | ||
|
||
if i < len(in.Alerts)-1 { | ||
sb.WriteString("---\n\n") | ||
} | ||
} | ||
|
||
return &model.DefaultMessage{ | ||
Text: sb.String(), | ||
Type: resolvers.TextType, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters