Skip to content

Commit

Permalink
Merge pull request #203 from mackerelio/improve-mkr-wrap-message-trun…
Browse files Browse the repository at this point in the history
…cation

[wrap] Improve message truncation algorithm
  • Loading branch information
itchyny committed Apr 17, 2019
2 parents cf4a786 + c294c49 commit d9bec76
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 6 deletions.
26 changes: 20 additions & 6 deletions wrap/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,25 @@ func (re *result) buildMsg(detail bool) string {
}
buf := &bytes.Buffer{}
template.Must(msgTpl.Clone()).Execute(buf, s)
msg := buf.String()
const messageLengthLimit = 1024
runes := []rune(msg)
if len(runes) > messageLengthLimit {
msg = string(runes[0:messageLengthLimit])
const messageLengthLimit = 1024 // https://mackerel.io/api-docs/entry/check-monitoring#post
return truncate(buf.String(), messageLengthLimit, "\n...\n")
}

func truncate(src string, limit int, sep string) string {
rs := []rune(src)
if len(rs) <= limit {
return src
}
seprs := []rune(sep)
var i int
rest := limit - len(seprs)
if 0 < rest {
i += rest / 2
rest -= rest / 2
}
i += copy(rs[i:], seprs)
if 0 < rest {
copy(rs[i:], rs[len(rs)-rest:])
}
return msg
return string(rs[:limit])
}
13 changes: 13 additions & 0 deletions wrap/testdata/long.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"
"os"
)

func main() {
for i := 0; i < 100; i++ {
fmt.Println("Hello world!")
}
os.Exit(1)
}
106 changes: 106 additions & 0 deletions wrap/wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"

mackerel "github.com/mackerelio/mackerel-client-go"
Expand Down Expand Up @@ -73,6 +74,30 @@ Note: This is note
% go run testdata/stub.go
Hello.
exit status 1
`,
NotificationInterval: 0,
},
ExitCode: 1,
},
{
Name: "long output",
Args: []string{
"-name=test-check",
"-detail",
"-note", "This is note",
"--",
"go", "run", "testdata/long.go",
},
Result: testResult{
Name: "test-check",
Status: mackerel.CheckStatusCritical,
Message: `command exited with code: 1
Note: This is note
% go run testdata/long.go
` + strings.Repeat("Hello world!\n", 33) + `Hello w
...
!
` + strings.Repeat("Hello world!\n", 38) + `exit status 1
`,
NotificationInterval: 0,
},
Expand Down Expand Up @@ -183,3 +208,84 @@ func TestCommand_Action_withoutConf(t *testing.T) {
err, expect)
}
}

func Test_truncate(t *testing.T) {
testCases := []struct {
src string
limit int
sep string
expected string
}{
{
src: "",
limit: 0,
sep: "",
expected: "",
},
{
src: "",
limit: 10,
sep: " ... ",
expected: "",
},
{
src: "Hello, world!",
limit: 100,
sep: " ... ",
expected: "Hello, world!",
},
{
src: "Hello, world!",
limit: 0,
sep: " ... ",
expected: "",
},
{
src: "Hello, world!",
limit: 3,
sep: " ... ",
expected: " ..",
},
{
src: "Hello, world!",
limit: 5,
sep: " ... ",
expected: " ... ",
},
{
src: "Hello, world!",
limit: 10,
sep: " ... ",
expected: "He ... ld!",
},
{
src: "Hello, world!",
limit: 15,
sep: " ... ",
expected: "Hello, world!",
},
{
src: "こんにちは、世界",
limit: 6,
sep: "..",
expected: "こん..世界",
},
{
src: strings.Repeat("abcde", 10),
limit: 30,
sep: " ... ",
expected: "abcdeabcdeab ... cdeabcdeabcde",
},
}
for _, tc := range testCases {
got := truncate(tc.src, tc.limit, tc.sep)
if got != tc.expected {
t.Errorf("truncate(%q, %d, %q) should be %q but got: %q",
tc.src, tc.limit, tc.sep, tc.expected, got)
}
if len([]rune(got)) > tc.limit {
t.Errorf("length of truncate(%q, %d, %q) should not exceed %d but got: %d",
tc.src, tc.limit, tc.sep, tc.limit, len([]rune(got)))
}
}
}

0 comments on commit d9bec76

Please sign in to comment.