Skip to content

Commit

Permalink
feat(endpoint): change way to encode extra values
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed May 29, 2022
1 parent 02daaef commit af514c9
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 43 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
goftp.io/server v0.4.1
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a
golang.org/x/text v0.3.7
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down
21 changes: 0 additions & 21 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/jlaffaye/ftp v0.0.0-20190624084859-c1312a7102bf/go.mod h1:lli8NYPQOFy3O++YmYbqVgOcQ1JPCwdOy+5zSjKJ9qY=
github.com/jlaffaye/ftp v0.0.0-20211117213618-11820403398b h1:Ur6QAxsHCK99Quj9PaWafoV4unb0DO/HWiKExD+TN5g=
github.com/jlaffaye/ftp v0.0.0-20211117213618-11820403398b/go.mod h1:2lmrmq866uF2tnje75wQHzmPXhmSWUt7Gyx2vgK1RCU=
github.com/jlaffaye/ftp v0.0.0-20220310202011-d2c44e311e78 h1:urWv38lDLjDRk5fG9P8vvxlfpQXaKtRlZc+QLKk3FRA=
github.com/jlaffaye/ftp v0.0.0-20220310202011-d2c44e311e78/go.mod h1:oZaomI+9/et52UBjvNU9LCIqmgt816+7ljXCx0EIPzo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/macrat/go-parallel-pinger v1.1.2 h1:qw/qA+iHFUaXy2iM+fkQivll6YhCd+Jui6ceWxNNyOY=
github.com/macrat/go-parallel-pinger v1.1.2/go.mod h1:Sb734FetI568lFoNWSXiTTk8TUoShrh+1ISP9+vs+Gg=
github.com/macrat/go-parallel-pinger v1.1.3 h1:WpD2jHXoOn/A2Uy0CAvd85lpNHzX0jnO0hYSDcyEHA0=
github.com/macrat/go-parallel-pinger v1.1.3/go.mod h1:6Vkhm1prLe3/7Go6AUE6M9n0UjD94IwDK0iz/SystUc=
github.com/minio/minio-go/v6 v6.0.46/go.mod h1:qD0lajrGW49lKZLtXKtCB4X/qkMf0a5tBvN2PaZg7Gg=
Expand Down Expand Up @@ -50,36 +46,21 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f h1:hEYJvxw1lSnWIl8X9ofsYMklzaDs90JI2az5YMd4fPM=
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b h1:vI32FkLJNAWtGD4BwkThwEy6XS7ZLLMHkSkYfF8M0W0=
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220513224357-95641704303c h1:nF9mHSvoKBLkQNQhJZNsc66z2UzAMUbLGjC95CF3pU0=
golang.org/x/net v0.0.0-20220513224357-95641704303c/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220403205710-6acee93ad0eb h1:PVGECzEo9Y3uOidtkHGdd347NjLtITfJFO9BxFpmRoo=
golang.org/x/sys v0.0.0-20220403205710-6acee93ad0eb/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220405210540-1e041c57c461 h1:kHVeDEnfKn3T238CvrUcz6KeEsFHVaKh4kMTt6Wsysg=
golang.org/x/sys v0.0.0-20220405210540-1e041c57c461/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220406163625-3f8b81556e12 h1:QyVthZKMsyaQwBTJE04jdNN0Pp5Fn9Qga0mrgxyERQM=
golang.org/x/sys v0.0.0-20220406163625-3f8b81556e12/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a h1:N2T1jUrTQE9Re6TFF5PhvEHXHCguynGhKjWVsIUt5cY=
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand All @@ -90,5 +71,3 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 2 additions & 2 deletions internal/endpoint/templates/log.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<td class="status {{ .Status | to_lower }}">{{ .Status }}</td>
<td class="latency">{{ .Latency }}</td>
<td class="target">{{ .Target }}</td>
<td class="message">{{ .Message }}{{ range $k, $v := .Extra }} <span class="extra"><span class="extra-label">{{ $k }}</span> {{ $v }}</span>{{ end }}</td>
<td class="message">{{ .Message }}{{ range .ReadableExtra }} <span class="extra"><span class="extra-label">{{ .Key }}</span> {{ .Value }}</span>{{ end }}</td>
</tr>{{ end }}
</tbody>{{ if .Tail }}
<tbody><tr><td colspan="5" class="omit-indicator">{{ .Omitted }} records have omitted</td></tr></tbody>
Expand All @@ -115,7 +115,7 @@
<td class="status {{ .Status | to_lower }}">{{ .Status }}</td>
<td class="latency">{{ .Latency }}</td>
<td class="target">{{ .Target }}</td>
<td class="message">{{ .Message }}{{ range $k, $v := .Extra }} <span class="extra"><span class="extra-label">{{ $k }}</span> {{ $v }}</span>{{ end }}</td>
<td class="message">{{ .Message }}{{ range .ReadableExtra }} <span class="extra"><span class="extra-label">{{ .Key }}</span> {{ .Value }}</span>{{ end }}</td>
</tr>{{ end }}
</tbody>{{ end }}
</table>
Expand Down
2 changes: 1 addition & 1 deletion internal/endpoint/testdata/log.html
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@
<td class="status unknown">UNKNOWN</td>
<td class="latency">2.345ms</td>
<td class="target">http://c.example.com</td>
<td class="message">this is unknown <span class="extra"><span class="extra-label">hoge</span> fuga</span></td>
<td class="message">this is unknown <span class="extra"><span class="extra-label">extra</span> [1,2,3]</span> <span class="extra"><span class="extra-label">hoge</span> fuga</span></td>
</tr>
</tbody>
</table>
Expand Down
7 changes: 3 additions & 4 deletions internal/endpoint/testdata/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -402,17 +402,16 @@ <h1 aria-label="'http://c.example.com' is currently unknown">
this is aborted
---
hello: world
">
hello: world">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" aria-hidden="true"><use xlink:href="#aborted-icon" /></svg>
</span>
<span class="status-bit unknown" title="2021-01-02T15:04:09Z
unknown in 2.345ms
this is unknown
---
hoge: fuga
">
extra: [1,2,3]
hoge: fuga">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" aria-hidden="true"><use xlink:href="#unknown-icon" /></svg>
</span>

Expand Down
2 changes: 1 addition & 1 deletion internal/testutil/testdata/test.log
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
{"time":"2021-01-02T15:04:06Z", "status":"HEALTHY", "latency":54.321, "target":"http://b.example.com", "message":"this is healthy"}
{"time":"2021-01-02T15:04:07Z", "status":"HEALTHY", "latency":345.678, "target":"http://a.example.com", "message":"hello world!!"}
{"time":"2021-01-02T15:04:08Z", "status":"ABORTED", "latency":1.234, "target":"http://c.example.com", "message":"this is aborted", "hello":"world"}
{"time":"2021-01-02T15:04:09Z", "status":"UNKNOWN", "latency":2.345, "target":"http://c.example.com", "message":"this is unknown", "hoge":"fuga"}
{"time":"2021-01-02T15:04:09Z", "status":"UNKNOWN", "latency":2.345, "target":"http://c.example.com", "message":"this is unknown", "hoge":"fuga", "extra":[1,2,3]}
62 changes: 54 additions & 8 deletions lib-ayd/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"

"github.com/macrat/ayd/internal/ayderr"
"gopkg.in/yaml.v3"
)

// Record is a record in Ayd log
Expand Down Expand Up @@ -36,20 +36,66 @@ func (r Record) String() string {
return string(b)
}

// ReadableExtra returns Extra map but encode each values as string.
func (r Record) ReadableExtra() []ExtraPair {
if len(r.Extra) == 0 {
return nil
}

xs := make([]ExtraPair, 0, len(r.Extra))
for k, v := range r.Extra {
s := ""
switch x := v.(type) {
case string:
s = x
case int, int32, int64, float32, float64:
s = fmt.Sprint(x)
default:
b, err := json.Marshal(x)
if err != nil {
s = fmt.Sprint(x)
} else {
s = string(b)
}
}
xs = append(xs, ExtraPair{k, s})
}

sort.Slice(xs, func(i, j int) bool {
return xs[i].Key < xs[j].Key
})

return xs
}

// ExtraPair is a pair of string, for Record.StringExtra..
type ExtraPair struct {
Key string
Value string
}

// ReadableMessage returns human readable message of the message field and extra fields.
func (r Record) ReadableMessage() string {
if len(r.Extra) == 0 {
return r.Message
}
extra, err := yaml.Marshal(r.Extra)
if err != nil {
return r.Message
var buf strings.Builder
buf.WriteString(r.Message)
if len(r.Message) > 0 && r.Message[len(r.Message)-1] != '\n' {
buf.WriteByte('\n')
}
msg := r.Message
if len(msg) > 0 && msg[len(msg)-1] != '\n' {
msg += "\n"
buf.WriteString("---")
for _, e := range r.ReadableExtra() {
if !strings.Contains(e.Value, "\n") {
fmt.Fprintf(&buf, "\n%s: %s", e.Key, e.Value)
} else {
fmt.Fprintf(&buf, "\n%s: |", e.Key)
for _, line := range strings.Split(e.Value, "\n") {
fmt.Fprintf(&buf, "\n %s", line)
}
}
}
return msg + "---\n" + string(extra)
return buf.String()
}

// UnmarshalJSON implements the json.Unmarshaler interface.
Expand Down
73 changes: 73 additions & 0 deletions lib-ayd/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ayd_test

import (
"encoding/json"
"fmt"
"net/url"
"strings"
"testing"
Expand Down Expand Up @@ -276,6 +277,78 @@ func TestRecord_json(t *testing.T) {
})
}

func TestRecord_ReadableMessage(t *testing.T) {
tests := []struct {
Message string
Extra map[string]interface{}
Output string
}{
{
"",
map[string]interface{}{
"array": []int{1, 2, 3},
"hello": "world",
"multi": "hello\nworld",
"num": 42,
"object": map[string]string{"key": "value"},
},
strings.Join([]string{
"---",
"array: [1,2,3]",
"hello: world",
"multi: |",
" hello",
" world",
"num: 42",
`object: {"key":"value"}`,
}, "\n"),
},
{
"hello\nworld",
map[string]interface{}{
"hello": "world",
},
strings.Join([]string{
"hello",
"world",
"---",
"hello: world",
}, "\n"),
},
{
"hello\nworld\n",
map[string]interface{}{
"hello": "world",
},
strings.Join([]string{
"hello",
"world",
"---",
"hello: world",
}, "\n"),
},
{
"hello world",
nil,
"hello world",
},
{
"",
nil,
"",
},
}

for i, tt := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
actual := ayd.Record{Message: tt.Message, Extra: tt.Extra}.ReadableMessage()
if diff := cmp.Diff(actual, tt.Output); diff != "" {
t.Errorf("unexpected output\n%s", diff)
}
})
}
}

func BenchmarkRecord_MarshalJSON(b *testing.B) {
record := ayd.Record{
CheckedAt: time.Date(2021, 1, 2, 15, 4, 5, 0, time.UTC),
Expand Down
7 changes: 3 additions & 4 deletions testdata/log.csv
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
2021-01-02T15:04:07Z,HEALTHY,345.678,http://a.example.com,hello world!!
2021-01-02T15:04:08Z,ABORTED,1.234,http://c.example.com,"this is aborted
---
hello: world
"
hello: world"
2021-01-02T15:04:09Z,UNKNOWN,2.345,http://c.example.com,"this is unknown
---
hoge: fuga
"
extra: [1,2,3]
hoge: fuga"
2 changes: 1 addition & 1 deletion testdata/log.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
{"time":"2021-01-02T15:04:06Z","status":"HEALTHY","latency":54.321,"target":"http://b.example.com","message":"this is healthy"},
{"time":"2021-01-02T15:04:07Z","status":"HEALTHY","latency":345.678,"target":"http://a.example.com","message":"hello world!!"},
{"time":"2021-01-02T15:04:08Z","status":"ABORTED","latency":1.234,"target":"http://c.example.com","message":"this is aborted","hello":"world"},
{"time":"2021-01-02T15:04:09Z","status":"UNKNOWN","latency":2.345,"target":"http://c.example.com","message":"this is unknown","hoge":"fuga"}
{"time":"2021-01-02T15:04:09Z","status":"UNKNOWN","latency":2.345,"target":"http://c.example.com","message":"this is unknown","extra":[1,2,3],"hoge":"fuga"}
]
7 changes: 7 additions & 0 deletions testdata/log.ltsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
time:2021-01-02T15:04:05Z status:HEALTHY latency:123.456 target:http://a.example.com message:hello world
time:2021-01-02T15:04:05Z status:FAILURE latency:12.345 target:http://b.example.com message:this is failure
time:2021-01-02T15:04:06Z status:HEALTHY latency:234.567 target:http://a.example.com message:hello world!
time:2021-01-02T15:04:06Z status:HEALTHY latency:54.321 target:http://b.example.com message:this is healthy
time:2021-01-02T15:04:07Z status:HEALTHY latency:345.678 target:http://a.example.com message:hello world!!
time:2021-01-02T15:04:08Z status:ABORTED latency:1.234 target:http://c.example.com message:this is aborted hello:world
time:2021-01-02T15:04:09Z status:UNKNOWN latency:2.345 target:http://c.example.com message:this is unknown extra:[1,2,3] hoge:fuga

0 comments on commit af514c9

Please sign in to comment.