notify: factorize code truncating strings (#1752)
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
parent
17e8cc04b8
commit
b10646f9ac
|
@ -1221,10 +1221,9 @@ func (n *OpsGenie) createRequest(ctx context.Context, as ...*types.Alert) (*http
|
||||||
apiURL.RawQuery = q.Encode()
|
apiURL.RawQuery = q.Encode()
|
||||||
msg = &opsGenieCloseMessage{Source: tmpl(n.conf.Source)}
|
msg = &opsGenieCloseMessage{Source: tmpl(n.conf.Source)}
|
||||||
default:
|
default:
|
||||||
message := tmpl(n.conf.Message)
|
message, truncated := truncate(tmpl(n.conf.Message), 130)
|
||||||
if len(message) > 130 {
|
if truncated {
|
||||||
message = message[:127] + "..."
|
level.Debug(n.logger).Log("msg", "truncated message due to OpsGenie message limit", "truncated_message", message, "incident", key)
|
||||||
level.Debug(n.logger).Log("msg", "Truncated message to %q due to OpsGenie message limit", "truncated_message", message, "incident", key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
apiURL.Path += "v2/alerts"
|
apiURL.Path += "v2/alerts"
|
||||||
|
@ -1362,9 +1361,9 @@ func (n *VictorOps) createVictorOpsPayload(ctx context.Context, as ...*types.Ale
|
||||||
messageType = victorOpsEventResolve
|
messageType = victorOpsEventResolve
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(stateMessage) > 20480 {
|
stateMessage, truncated := truncate(stateMessage, 20480)
|
||||||
stateMessage = stateMessage[0:20475] + "\n..."
|
if truncated {
|
||||||
level.Debug(n.logger).Log("msg", "Truncated stateMessage due to VictorOps stateMessage limit", "truncated_state_message", stateMessage, "incident", key)
|
level.Debug(n.logger).Log("msg", "truncated stateMessage due to VictorOps stateMessage limit", "truncated_state_message", stateMessage, "incident", key)
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := map[string]string{
|
msg := map[string]string{
|
||||||
|
@ -1439,9 +1438,8 @@ func (n *Pushover) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
|
||||||
parameters.Add("token", tmpl(string(n.conf.Token)))
|
parameters.Add("token", tmpl(string(n.conf.Token)))
|
||||||
parameters.Add("user", tmpl(string(n.conf.UserKey)))
|
parameters.Add("user", tmpl(string(n.conf.UserKey)))
|
||||||
|
|
||||||
title := tmpl(n.conf.Title)
|
title, truncated := truncate(tmpl(n.conf.Title), 250)
|
||||||
if len(title) > 250 {
|
if truncated {
|
||||||
title = title[:247] + "..."
|
|
||||||
level.Debug(n.logger).Log("msg", "Truncated title due to Pushover title limit", "truncated_title", title, "incident", key)
|
level.Debug(n.logger).Log("msg", "Truncated title due to Pushover title limit", "truncated_title", title, "incident", key)
|
||||||
}
|
}
|
||||||
parameters.Add("title", title)
|
parameters.Add("title", title)
|
||||||
|
@ -1453,8 +1451,8 @@ func (n *Pushover) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
|
||||||
message = tmpl(n.conf.Message)
|
message = tmpl(n.conf.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(message) > 1024 {
|
message, truncated = truncate(message, 1024)
|
||||||
message = message[:1021] + "..."
|
if truncated {
|
||||||
level.Debug(n.logger).Log("msg", "Truncated message due to Pushover message limit", "truncated_message", message, "incident", key)
|
level.Debug(n.logger).Log("msg", "Truncated message due to Pushover message limit", "truncated_message", message, "incident", key)
|
||||||
}
|
}
|
||||||
message = strings.TrimSpace(message)
|
message = strings.TrimSpace(message)
|
||||||
|
@ -1464,9 +1462,8 @@ func (n *Pushover) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
|
||||||
}
|
}
|
||||||
parameters.Add("message", message)
|
parameters.Add("message", message)
|
||||||
|
|
||||||
supplementaryURL := tmpl(n.conf.URL)
|
supplementaryURL, truncated := truncate(tmpl(n.conf.URL), 512)
|
||||||
if len(supplementaryURL) > 512 {
|
if truncated {
|
||||||
supplementaryURL = supplementaryURL[:509] + "..."
|
|
||||||
level.Debug(n.logger).Log("msg", "Truncated URL due to Pushover url limit", "truncated_url", supplementaryURL, "incident", key)
|
level.Debug(n.logger).Log("msg", "Truncated URL due to Pushover url limit", "truncated_url", supplementaryURL, "incident", key)
|
||||||
}
|
}
|
||||||
parameters.Add("url", supplementaryURL)
|
parameters.Add("url", supplementaryURL)
|
||||||
|
@ -1582,3 +1579,14 @@ func post(ctx context.Context, client *http.Client, url string, bodyType string,
|
||||||
req.Header.Set("Content-Type", bodyType)
|
req.Header.Set("Content-Type", bodyType)
|
||||||
return client.Do(req.WithContext(ctx))
|
return client.Do(req.WithContext(ctx))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func truncate(s string, n int) (string, bool) {
|
||||||
|
r := []rune(s)
|
||||||
|
if len(r) <= n {
|
||||||
|
return s, false
|
||||||
|
}
|
||||||
|
if n <= 3 {
|
||||||
|
return string(r[:n]), true
|
||||||
|
}
|
||||||
|
return string(r[:n-3]) + "...", true
|
||||||
|
}
|
||||||
|
|
|
@ -371,3 +371,64 @@ func TestVictorOpsCustomFields(t *testing.T) {
|
||||||
// Verify that a custom field was added to the payload and templatized.
|
// Verify that a custom field was added to the payload and templatized.
|
||||||
require.Equal(t, "message", m["Field_A"])
|
require.Equal(t, "message", m["Field_A"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTruncate(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
in string
|
||||||
|
n int
|
||||||
|
|
||||||
|
out string
|
||||||
|
trunc bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
in: "",
|
||||||
|
n: 5,
|
||||||
|
out: "",
|
||||||
|
trunc: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "abcde",
|
||||||
|
n: 2,
|
||||||
|
out: "ab",
|
||||||
|
trunc: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "abcde",
|
||||||
|
n: 4,
|
||||||
|
out: "a...",
|
||||||
|
trunc: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "abcde",
|
||||||
|
n: 5,
|
||||||
|
out: "abcde",
|
||||||
|
trunc: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "abcdefgh",
|
||||||
|
n: 5,
|
||||||
|
out: "ab...",
|
||||||
|
trunc: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "a⌘cde",
|
||||||
|
n: 5,
|
||||||
|
out: "a⌘cde",
|
||||||
|
trunc: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "a⌘cdef",
|
||||||
|
n: 5,
|
||||||
|
out: "a⌘...",
|
||||||
|
trunc: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(fmt.Sprintf("truncate(%s,%d)", tc.in, tc.n), func(t *testing.T) {
|
||||||
|
s, trunc := truncate(tc.in, tc.n)
|
||||||
|
require.Equal(t, tc.trunc, trunc)
|
||||||
|
require.Equal(t, tc.out, s)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue