今年作った/メンテしたGoツールN選
golang.tokyo 11
Dec 11th, 2017
Profile
Mackerel
Mackerel
- はてな謹製のサーバー管理・監視SaaS
- 小規模から大規模システムまで
- ユーザー登録から3分でサーバー監視が可能
- 専属UI/UXデザイナーによる圧倒的な使い勝手
【宣伝】書籍発売中
【宣伝】みんなのGo言語
今年書いたりメンテナンスしたりしたGoプロダクト
- horenso
- go-httpdate
- ghg
- hakobe/paranoidhttp
- go-memcached-tool
- blogsync
- ghch
- axslogparser
- postailer
- go-ltsv
- timeout
- retry
go-httpdate (supported formats)
- "Wed, 09 Feb 1994 22:23:32 GMT" -- HTTP format
- "Thu Feb 3 17:03:55 GMT 1994" -- ctime(3) format
- "Thu Feb 3 00:00:00 1994", -- ANSI C asctime() format
- "Tuesday, 08-Feb-94 14:15:29 GMT" -- old rfc850 HTTP format
- "Tuesday, 08-Feb-1994 14:15:29 GMT" -- broken rfc850 HTTP format
- "03/Feb/1994:17:03:55 -0700" -- common logfile format
- "09 Feb 1994 22:23:32 GMT" -- HTTP format (no weekday)
- "08-Feb-94 14:15:29 GMT" -- rfc850 format (no weekday)
- "08-Feb-1994 14:15:29 GMT" -- broken rfc850 format (no weekday)
- "1994-02-03 14:15:29 -0100" -- ISO 8601 format
- "1994-02-03 14:15:29" -- zone is optional
- "1994-02-03" -- only date
- "1994-02-03T14:15:29" -- Use T as separator
- "19940203T141529Z" -- ISO 8601 compact format
- "19940203" -- only date
- "08-Feb-94" -- old rfc850 HTTP format (no weekday, no time)
- "08-Feb-1994" -- broken rfc850 HTTP format (no weekday, no time)
- "09 Feb 1994" -- proposed new HTTP format (no weekday, no time)
- "03/Feb/1994" -- common logfile format (no time, no offset)
- "Feb 3 1994" -- Unix 'ls -l' format
- "Feb 3 17:03" -- Unix 'ls -l' format
- "11-15-96 03:52PM" -- Windows 'dir' format
go-httpdate (synopsis)
// try to parse string and returns `time.Time` or error
t1, err := httpdate.Str2Time("Thu, 03 Feb 1994 12:33:44 GMT", time.UTC)
t2, err := httpdate.Str2Time("2017-11-11", time.UTC)
t3, err := httpdate.Str2Time("Thu Nov 9 18:20:31 GMT 2017", time.UTC)
t4, err := httpdate.Str2Time("08-Feb-94 14:15:29 GMT", time.UTC)
go-httpdate
go-httpdate (httpdate command available)
httpdateコマンドが付属。
% httpdate '2017-12-12'
1513004400
ghg (Why do not you use a go get
?)
- go get するにはGoの環境が必要
- 安定版ではなく、開発中の最新をビルドしてしまう
- ビルド情報などをバイナリに埋め込めない
ghg (synopsis)
- % ghg get tcnksm/ghr
- % $(ghg bin)/ghr # you can run the executable
- % ghg get -u Songmu/retry # upgrade and overwrite
- % ghg get motemen/ghq@v0.7.1 # specify the release version
ghg (back story)
これが、 mkr plugin install
の元になりました。
hakobe/paranoidhttp
- context対応
- Go1.7ではDialContextが増え、Go1.8では LookupIPAddr にcontextが差し込めるように
hakobe/paranoidhttp
- MackerelのURL外形監視で名前解決時間を省く実装を入れられるようになりました
- https://github.com/tcnksm/go-httpstat/pull/16
go-memcached-tool
- memcached-toolのGo移植
- これ自体はmemcachedの作者であり、今はGoのメインコミッターの一人であるBradfitz氏の書いたPerlスクリプト
- なのですが、これを僕がGoに移植したというのがちょっと面白ポイント
go-memcached-tool
Slabの状態を見る
% go-memcached-tool 127.0.0.1:11211
# Item_Size Max_age Pages Count Full? Evicted Evict_Time OOM
1 96B 17550349s 1 10922 yes 1121115 0 0
2 120B 17529950s 3 26214 yes 2693486 0 0
3 152B 17656340s 2 13793 yes 1262170 0 0
4 192B 17652024s 2 10917 yes 139583 0 0
...
go-memcached-tool
Dumpして、別のmemcachedに移す
% go-memcached-tool 127.0.0.1:11211 dump | nc cache2.example.com:11211
context対応したい
exec.CommandContext
の cancel
は SIGKILL
を送る
- timeoutは任意の停止シグナルを選べて便利
timeout
mackerel-agent内部で利用
tio := &timeout.Timeout{
Cmd: exec.Command("perl", "-E", "say 'Hello'"),
Duration: 10 * time.Second,
KillAfter: 5 * time.Second,
}
exitStatus, stdout, stderr, err := tio.Run()
retry (synopsis)
mackerel-agent内部で利用
err := retry.Retry(3, 1*time.Second, func() error {
// return error once in a while
})
if err != nil {
// error handling
}