From 096a9bf485397991f7386b0eabd1564aecad1091 Mon Sep 17 00:00:00 2001 From: mung9 Date: Tue, 24 Sep 2024 15:47:09 +0900 Subject: [PATCH 1/7] Upgrade slack-go/slack --- go.mod | 4 ++-- go.sum | 19 ++++--------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 4935b5c..843ebea 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.19 require ( github.com/containerd/cgroups v1.0.4 github.com/golang/mock v1.6.0 - github.com/slack-go/slack v0.11.3 + github.com/slack-go/slack v0.14.0 ) require ( @@ -14,7 +14,7 @@ require ( github.com/docker/go-units v0.4.0 // indirect github.com/godbus/dbus/v5 v5.0.4 // indirect github.com/gogo/protobuf v1.3.2 // indirect - github.com/gorilla/websocket v1.4.2 // indirect + github.com/gorilla/websocket v1.5.3 // indirect github.com/opencontainers/runtime-spec v1.0.2 // indirect github.com/sirupsen/logrus v1.8.1 // indirect golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect diff --git a/go.sum b/go.sum index df937ab..f0734ab 100644 --- a/go.sum +++ b/go.sum @@ -4,7 +4,6 @@ github.com/containerd/cgroups v1.0.4 h1:jN/mbWBEaz+T1pi5OFtnkQ+8qnmEbAr1Oo1FRm5B github.com/containerd/cgroups v1.0.4/go.mod h1:nLNQtsF7Sl2HxNebu77i1R0oDlhiTG+kO4JTrUzo6IA= github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= @@ -22,11 +21,11 @@ github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+Licev github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -38,25 +37,20 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/slack-go/slack v0.11.3 h1:GN7revxEMax4amCc3El9a+9SGnjmBvSUobs0QnO6ZO8= -github.com/slack-go/slack v0.11.3/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/slack-go/slack v0.14.0 h1:6c0UTfbRnvRssZUsZ2qe0Iu07VAMPjRqOa6oX8ewF4k= +github.com/slack-go/slack v0.14.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= -go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -79,18 +73,13 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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= From 321179bd89b3ca171738062fab23989042ef0278 Mon Sep 17 00:00:00 2001 From: mung9 Date: Tue, 24 Sep 2024 15:51:08 +0900 Subject: [PATCH 2/7] Change Reporter interface --- autopprof.go | 11 ++++--- autopprof_test.go | 70 +++++++++++++++++++++---------------------- report/report.go | 6 ++-- report/report_mock.go | 26 ++++++++-------- 4 files changed, 58 insertions(+), 55 deletions(-) diff --git a/autopprof.go b/autopprof.go index 9210d8b..02f20e9 100644 --- a/autopprof.go +++ b/autopprof.go @@ -7,10 +7,10 @@ import ( "bytes" "context" "fmt" - "github.com/daangn/autopprof/queryer" "log" "time" + "github.com/daangn/autopprof/queryer" "github.com/daangn/autopprof/report" ) @@ -245,7 +245,8 @@ func (ap *autoPprof) reportCPUProfile(cpuUsage float64) error { UsagePercentage: cpuUsage * 100, } bReader := bytes.NewReader(b) - if err := ap.reporter.ReportCPUProfile(ctx, bReader, ci); err != nil { + bSize := len(b) + if err := ap.reporter.ReportCPUProfile(ctx, bReader, bSize, ci); err != nil { return err } return nil @@ -331,7 +332,8 @@ func (ap *autoPprof) reportHeapProfile(memUsage float64) error { UsagePercentage: memUsage * 100, } bReader := bytes.NewReader(b) - if err := ap.reporter.ReportHeapProfile(ctx, bReader, mi); err != nil { + bSize := len(b) + if err := ap.reporter.ReportHeapProfile(ctx, bReader, bSize, mi); err != nil { return err } return nil @@ -419,7 +421,8 @@ func (ap *autoPprof) reportGoroutineProfile(goroutineCount int) error { Count: goroutineCount, } bReader := bytes.NewReader(b) - if err := ap.reporter.ReportGoroutineProfile(ctx, bReader, gi); err != nil { + bSize := len(b) + if err := ap.reporter.ReportGoroutineProfile(ctx, bReader, bSize, gi); err != nil { return err } return nil diff --git a/autopprof_test.go b/autopprof_test.go index 5d4dc90..dd92382 100644 --- a/autopprof_test.go +++ b/autopprof_test.go @@ -79,9 +79,9 @@ func TestStart(t *testing.T) { opt: Option{ Reporter: report.NewSlackReporter( &report.SlackReporterOption{ - App: "appname", - Token: "token", - Channel: "channel", + App: "appname", + Token: "token", + ChannelID: "channel_id", }, ), }, @@ -93,9 +93,9 @@ func TestStart(t *testing.T) { MemThreshold: 0.5, Reporter: report.NewSlackReporter( &report.SlackReporterOption{ - App: "appname", - Token: "token", - Channel: "channel", + App: "appname", + Token: "token", + ChannelID: "channel_id", }, ), }, @@ -144,9 +144,9 @@ func TestStop(t *testing.T) { MemThreshold: 0.5, Reporter: report.NewSlackReporter( &report.SlackReporterOption{ - App: "appname", - Token: "token", - Channel: "channel", + App: "appname", + Token: "token", + ChannelID: "channel_id", }, ), }) @@ -266,10 +266,10 @@ func TestAutoPprof_watchCPUUsage(t *testing.T) { mockReporter := report.NewMockReporter(ctrl) mockReporter.EXPECT(). - ReportCPUProfile(gomock.Any(), gomock.Any(), gomock.Any()). + ReportCPUProfile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). AnyTimes(). DoAndReturn( - func(_ context.Context, _ io.Reader, _ report.CPUInfo) error { + func(_ context.Context, _ io.Reader, _ int, _ report.CPUInfo) error { reported = true return nil }, @@ -329,10 +329,10 @@ func TestAutoPprof_watchCPUUsage_consecutive(t *testing.T) { mockReporter := report.NewMockReporter(ctrl) mockReporter.EXPECT(). - ReportCPUProfile(gomock.Any(), gomock.Any(), gomock.Any()). + ReportCPUProfile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). AnyTimes(). DoAndReturn( - func(_ context.Context, _ io.Reader, _ report.CPUInfo) error { + func(_ context.Context, _ io.Reader, _ int, _ report.CPUInfo) error { reportedCnt++ return nil }, @@ -426,7 +426,7 @@ func TestAutoPprof_watchCPUUsage_reportAll(t *testing.T) { Return([]byte("cpu_prof"), nil), mockReporter.EXPECT(). - ReportCPUProfile(gomock.Any(), gomock.Any(), report.CPUInfo{ + ReportCPUProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.CPUInfo{ ThresholdPercentage: 0.5 * 100, UsagePercentage: 0.6 * 100, }). @@ -444,7 +444,7 @@ func TestAutoPprof_watchCPUUsage_reportAll(t *testing.T) { Return([]byte("mem_prof"), nil), mockReporter.EXPECT(). - ReportHeapProfile(gomock.Any(), gomock.Any(), report.MemInfo{ + ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.MemInfo{ ThresholdPercentage: 0.5 * 100, UsagePercentage: 0.2 * 100, }). @@ -462,7 +462,7 @@ func TestAutoPprof_watchCPUUsage_reportAll(t *testing.T) { Return([]byte("goroutine_prof"), nil), mockReporter.EXPECT(). - ReportGoroutineProfile(gomock.Any(), gomock.Any(), report.GoroutineInfo{ + ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.GoroutineInfo{ ThresholdCount: 500, Count: 200, }). @@ -494,7 +494,7 @@ func TestAutoPprof_watchCPUUsage_reportAll(t *testing.T) { Return([]byte("cpu_prof"), nil), mockReporter.EXPECT(). - ReportCPUProfile(gomock.Any(), gomock.Any(), report.CPUInfo{ + ReportCPUProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.CPUInfo{ ThresholdPercentage: 0.5 * 100, UsagePercentage: 0.6 * 100, }). @@ -512,7 +512,7 @@ func TestAutoPprof_watchCPUUsage_reportAll(t *testing.T) { Return([]byte("goroutine_prof"), nil), mockReporter.EXPECT(). - ReportGoroutineProfile(gomock.Any(), gomock.Any(), report.GoroutineInfo{ + ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.GoroutineInfo{ ThresholdCount: 500, Count: 200, }). @@ -543,7 +543,7 @@ func TestAutoPprof_watchCPUUsage_reportAll(t *testing.T) { Return([]byte("cpu_prof"), nil), mockReporter.EXPECT(). - ReportCPUProfile(gomock.Any(), gomock.Any(), report.CPUInfo{ + ReportCPUProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.CPUInfo{ ThresholdPercentage: 0.5 * 100, UsagePercentage: 0.6 * 100, }). @@ -617,7 +617,7 @@ func TestAutoPprof_watchMemUsage(t *testing.T) { mockReporter := report.NewMockReporter(ctrl) mockReporter.EXPECT(). - ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any()). + ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). DoAndReturn( func(_ context.Context, _ io.Reader, _ report.MemInfo) error { reported = true @@ -679,7 +679,7 @@ func TestAutoPprof_watchMemUsage_consecutive(t *testing.T) { mockReporter := report.NewMockReporter(ctrl) mockReporter.EXPECT(). - ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any()). + ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). AnyTimes(). DoAndReturn( func(_ context.Context, _ io.Reader, _ report.MemInfo) error { @@ -776,7 +776,7 @@ func TestAutoPprof_watchMemUsage_reportAll(t *testing.T) { Return([]byte("mem_prof"), nil), mockReporter.EXPECT(). - ReportHeapProfile(gomock.Any(), gomock.Any(), report.MemInfo{ + ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.MemInfo{ ThresholdPercentage: 0.5 * 100, UsagePercentage: 0.6 * 100, }). @@ -794,7 +794,7 @@ func TestAutoPprof_watchMemUsage_reportAll(t *testing.T) { Return([]byte("cpu_prof"), nil), mockReporter.EXPECT(). - ReportCPUProfile(gomock.Any(), gomock.Any(), report.CPUInfo{ + ReportCPUProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.CPUInfo{ ThresholdPercentage: 0.5 * 100, UsagePercentage: 0.2 * 100, }). @@ -812,7 +812,7 @@ func TestAutoPprof_watchMemUsage_reportAll(t *testing.T) { Return([]byte("goroutine_prof"), nil), mockReporter.EXPECT(). - ReportGoroutineProfile(gomock.Any(), gomock.Any(), report.GoroutineInfo{ + ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.GoroutineInfo{ ThresholdCount: 500, Count: 200, }). @@ -844,7 +844,7 @@ func TestAutoPprof_watchMemUsage_reportAll(t *testing.T) { Return([]byte("mem_prof"), nil), mockReporter.EXPECT(). - ReportHeapProfile(gomock.Any(), gomock.Any(), report.MemInfo{ + ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.MemInfo{ ThresholdPercentage: 0.5 * 100, UsagePercentage: 0.6 * 100, }). @@ -862,7 +862,7 @@ func TestAutoPprof_watchMemUsage_reportAll(t *testing.T) { Return([]byte("goroutine_prof"), nil), mockReporter.EXPECT(). - ReportGoroutineProfile(gomock.Any(), gomock.Any(), report.GoroutineInfo{ + ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.GoroutineInfo{ ThresholdCount: 500, Count: 200, }). @@ -894,7 +894,7 @@ func TestAutoPprof_watchMemUsage_reportAll(t *testing.T) { Return([]byte("mem_prof"), nil), mockReporter.EXPECT(). - ReportHeapProfile(gomock.Any(), gomock.Any(), report.MemInfo{ + ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.MemInfo{ ThresholdPercentage: 0.5 * 100, UsagePercentage: 0.6 * 100, }). @@ -968,7 +968,7 @@ func TestAutoPprof_watchGoroutineCount(t *testing.T) { mockReporter := report.NewMockReporter(ctrl) mockReporter.EXPECT(). - ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any()). + ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). DoAndReturn( func(_ context.Context, _ io.Reader, _ report.GoroutineInfo) error { reported = true @@ -1031,7 +1031,7 @@ func TestAutoPprof_watchGoroutineCount_consecutive(t *testing.T) { mockReporter := report.NewMockReporter(ctrl) mockReporter.EXPECT(). - ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any()). + ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). AnyTimes(). DoAndReturn( func(_ context.Context, _ io.Reader, _ report.GoroutineInfo) error { @@ -1129,7 +1129,7 @@ func TestAutoPprof_watchGoroutineCount_reportAll(t *testing.T) { Return([]byte("goroutine_prof"), nil), mockReporter.EXPECT(). - ReportGoroutineProfile(gomock.Any(), gomock.Any(), report.GoroutineInfo{ + ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.GoroutineInfo{ ThresholdCount: 100, Count: 200, }). @@ -1147,7 +1147,7 @@ func TestAutoPprof_watchGoroutineCount_reportAll(t *testing.T) { Return([]byte("cpu_prof"), nil), mockReporter.EXPECT(). - ReportCPUProfile(gomock.Any(), gomock.Any(), report.CPUInfo{ + ReportCPUProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.CPUInfo{ ThresholdPercentage: 0.5 * 100, UsagePercentage: 0.2 * 100, }). @@ -1165,7 +1165,7 @@ func TestAutoPprof_watchGoroutineCount_reportAll(t *testing.T) { Return([]byte("mem_prof"), nil), mockReporter.EXPECT(). - ReportHeapProfile(gomock.Any(), gomock.Any(), report.MemInfo{ + ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.MemInfo{ ThresholdPercentage: 0.5 * 100, UsagePercentage: 0.2 * 100, }). @@ -1197,7 +1197,7 @@ func TestAutoPprof_watchGoroutineCount_reportAll(t *testing.T) { Return([]byte("goroutine_prof"), nil), mockReporter.EXPECT(). - ReportGoroutineProfile(gomock.Any(), gomock.Any(), report.GoroutineInfo{ + ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.GoroutineInfo{ ThresholdCount: 100, Count: 200, }). @@ -1215,7 +1215,7 @@ func TestAutoPprof_watchGoroutineCount_reportAll(t *testing.T) { Return([]byte("mem_prof"), nil), mockReporter.EXPECT(). - ReportHeapProfile(gomock.Any(), gomock.Any(), report.MemInfo{ + ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.MemInfo{ ThresholdPercentage: 0.5 * 100, UsagePercentage: 0.2 * 100, }). @@ -1247,7 +1247,7 @@ func TestAutoPprof_watchGoroutineCount_reportAll(t *testing.T) { Return([]byte("goroutine_prof"), nil), mockReporter.EXPECT(). - ReportGoroutineProfile(gomock.Any(), gomock.Any(), report.GoroutineInfo{ + ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any(), report.GoroutineInfo{ ThresholdCount: 100, Count: 200, }). diff --git a/report/report.go b/report/report.go index ee7513e..bec08c1 100644 --- a/report/report.go +++ b/report/report.go @@ -24,13 +24,13 @@ const ( // Reporter is responsible for reporting the profiling report to the destination. type Reporter interface { // ReportCPUProfile sends the CPU profiling data to the specific destination. - ReportCPUProfile(ctx context.Context, r io.Reader, ci CPUInfo) error + ReportCPUProfile(ctx context.Context, r io.Reader, size int, ci CPUInfo) error // ReportHeapProfile sends the heap profiling data to the specific destination. - ReportHeapProfile(ctx context.Context, r io.Reader, mi MemInfo) error + ReportHeapProfile(ctx context.Context, r io.Reader, size int, mi MemInfo) error // ReportGoroutineProfile sends the goroutine profiling data to the specific destination. - ReportGoroutineProfile(ctx context.Context, r io.Reader, gi GoroutineInfo) error + ReportGoroutineProfile(ctx context.Context, r io.Reader, size int, gi GoroutineInfo) error } // CPUInfo is the CPU usage information. diff --git a/report/report_mock.go b/report/report_mock.go index 56a4c62..7706cfc 100644 --- a/report/report_mock.go +++ b/report/report_mock.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: report/report.go +// Source: report.go // Package report is a generated GoMock package. package report @@ -36,43 +36,43 @@ func (m *MockReporter) EXPECT() *MockReporterMockRecorder { } // ReportCPUProfile mocks base method. -func (m *MockReporter) ReportCPUProfile(ctx context.Context, r io.Reader, ci CPUInfo) error { +func (m *MockReporter) ReportCPUProfile(ctx context.Context, r io.Reader, size int, ci CPUInfo) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ReportCPUProfile", ctx, r, ci) + ret := m.ctrl.Call(m, "ReportCPUProfile", ctx, r, size, ci) ret0, _ := ret[0].(error) return ret0 } // ReportCPUProfile indicates an expected call of ReportCPUProfile. -func (mr *MockReporterMockRecorder) ReportCPUProfile(ctx, r, ci interface{}) *gomock.Call { +func (mr *MockReporterMockRecorder) ReportCPUProfile(ctx, r, size, ci interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReportCPUProfile", reflect.TypeOf((*MockReporter)(nil).ReportCPUProfile), ctx, r, ci) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReportCPUProfile", reflect.TypeOf((*MockReporter)(nil).ReportCPUProfile), ctx, r, size, ci) } // ReportGoroutineProfile mocks base method. -func (m *MockReporter) ReportGoroutineProfile(ctx context.Context, r io.Reader, gi GoroutineInfo) error { +func (m *MockReporter) ReportGoroutineProfile(ctx context.Context, r io.Reader, size int, gi GoroutineInfo) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ReportGoroutineProfile", ctx, r, gi) + ret := m.ctrl.Call(m, "ReportGoroutineProfile", ctx, r, size, gi) ret0, _ := ret[0].(error) return ret0 } // ReportGoroutineProfile indicates an expected call of ReportGoroutineProfile. -func (mr *MockReporterMockRecorder) ReportGoroutineProfile(ctx, r, gi interface{}) *gomock.Call { +func (mr *MockReporterMockRecorder) ReportGoroutineProfile(ctx, r, size, gi interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReportGoroutineProfile", reflect.TypeOf((*MockReporter)(nil).ReportGoroutineProfile), ctx, r, gi) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReportGoroutineProfile", reflect.TypeOf((*MockReporter)(nil).ReportGoroutineProfile), ctx, r, size, gi) } // ReportHeapProfile mocks base method. -func (m *MockReporter) ReportHeapProfile(ctx context.Context, r io.Reader, mi MemInfo) error { +func (m *MockReporter) ReportHeapProfile(ctx context.Context, r io.Reader, size int, mi MemInfo) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ReportHeapProfile", ctx, r, mi) + ret := m.ctrl.Call(m, "ReportHeapProfile", ctx, r, size, mi) ret0, _ := ret[0].(error) return ret0 } // ReportHeapProfile indicates an expected call of ReportHeapProfile. -func (mr *MockReporterMockRecorder) ReportHeapProfile(ctx, r, mi interface{}) *gomock.Call { +func (mr *MockReporterMockRecorder) ReportHeapProfile(ctx, r, size, mi interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReportHeapProfile", reflect.TypeOf((*MockReporter)(nil).ReportHeapProfile), ctx, r, mi) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReportHeapProfile", reflect.TypeOf((*MockReporter)(nil).ReportHeapProfile), ctx, r, size, mi) } From cb01b81be943f9ebdea866ef12c5710e45574c25 Mon Sep 17 00:00:00 2001 From: mung9 Date: Tue, 24 Sep 2024 15:51:41 +0900 Subject: [PATCH 3/7] Change SlackReporter --- report/slack.go | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/report/slack.go b/report/slack.go index 27ab319..efc1783 100644 --- a/report/slack.go +++ b/report/slack.go @@ -21,31 +21,31 @@ const ( // SlackReporter is the reporter to send the profiling report to the // specific Slack channel. type SlackReporter struct { - app string - channel string + app string + channelID string client *slack.Client } // SlackReporterOption is the option for the Slack reporter. type SlackReporterOption struct { - App string - Token string - Channel string + App string + Token string + ChannelID string } // NewSlackReporter returns the new SlackReporter. func NewSlackReporter(opt *SlackReporterOption) *SlackReporter { return &SlackReporter{ - app: opt.App, - channel: opt.Channel, - client: slack.New(opt.Token), + app: opt.App, + channelID: opt.ChannelID, + client: slack.New(opt.Token), } } // ReportCPUProfile sends the CPU profiling data to the Slack. func (s *SlackReporter) ReportCPUProfile( - ctx context.Context, r io.Reader, ci CPUInfo, + ctx context.Context, r io.Reader, size int, ci CPUInfo, ) error { hostname, _ := os.Hostname() // Don't care about this error. var ( @@ -53,12 +53,13 @@ func (s *SlackReporter) ReportCPUProfile( filename = fmt.Sprintf(CPUProfileFilenameFmt, s.app, hostname, now) comment = fmt.Sprintf(cpuCommentFmt, ci.UsagePercentage, ci.ThresholdPercentage) ) - if _, err := s.client.UploadFileContext(ctx, slack.FileUploadParameters{ + if _, err := s.client.UploadFileV2Context(ctx, slack.UploadFileV2Parameters{ Reader: r, Filename: filename, + FileSize: size, Title: filename, InitialComment: comment, - Channels: []string{s.channel}, + Channel: s.channelID, }); err != nil { return fmt.Errorf("autopprof: failed to upload a file to Slack channel: %w", err) } @@ -67,7 +68,7 @@ func (s *SlackReporter) ReportCPUProfile( // ReportHeapProfile sends the heap profiling data to the Slack. func (s *SlackReporter) ReportHeapProfile( - ctx context.Context, r io.Reader, mi MemInfo, + ctx context.Context, r io.Reader, size int, mi MemInfo, ) error { hostname, _ := os.Hostname() // Don't care about this error. var ( @@ -75,12 +76,13 @@ func (s *SlackReporter) ReportHeapProfile( filename = fmt.Sprintf(HeapProfileFilenameFmt, s.app, hostname, now) comment = fmt.Sprintf(memCommentFmt, mi.UsagePercentage, mi.ThresholdPercentage) ) - if _, err := s.client.UploadFileContext(ctx, slack.FileUploadParameters{ + if _, err := s.client.UploadFileV2Context(ctx, slack.UploadFileV2Parameters{ Reader: r, Filename: filename, + FileSize: size, Title: filename, InitialComment: comment, - Channels: []string{s.channel}, + Channel: s.channelID, }); err != nil { return fmt.Errorf("autopprof: failed to upload a file to Slack channel: %w", err) } @@ -89,7 +91,7 @@ func (s *SlackReporter) ReportHeapProfile( // ReportGoroutineProfile sends the goroutine profiling data to the Slack. func (s *SlackReporter) ReportGoroutineProfile( - ctx context.Context, r io.Reader, gi GoroutineInfo, + ctx context.Context, r io.Reader, size int, gi GoroutineInfo, ) error { hostname, _ := os.Hostname() // Don't care about this error. var ( @@ -97,12 +99,13 @@ func (s *SlackReporter) ReportGoroutineProfile( filename = fmt.Sprintf(GoroutineProfileFilenameFmt, s.app, hostname, now) comment = fmt.Sprintf(goroutineCommentFmt, gi.Count, gi.ThresholdCount) ) - if _, err := s.client.UploadFileContext(ctx, slack.FileUploadParameters{ + if _, err := s.client.UploadFileV2Context(ctx, slack.UploadFileV2Parameters{ Reader: r, Filename: filename, + FileSize: size, Title: filename, InitialComment: comment, - Channels: []string{s.channel}, + Channel: s.channelID, }); err != nil { return fmt.Errorf("autopprof: failed to upload a file to Slack channel: %w", err) } From 87ba81e992cc343d2d6d2b71fe940ab44039ace9 Mon Sep 17 00:00:00 2001 From: mung9 Date: Tue, 24 Sep 2024 15:54:47 +0900 Subject: [PATCH 4/7] Update examples --- README.md | 6 +++--- examples/example.go | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 338dc6f..19af268 100644 --- a/README.md +++ b/README.md @@ -43,9 +43,9 @@ func main() { MemThreshold: 0.8, // Default: 0.75. Reporter: report.NewSlackReporter( &report.SlackReporterOption{ - App: "YOUR_APP_NAME", - Token: "YOUR_TOKEN_HERE", - Channel: "#REPORT_CHANNEL", + App: "YOUR_APP_NAME", + Token: "YOUR_TOKEN_HERE", + ChannelID: "REPORT_CHANNEL_ID", }, ), }) diff --git a/examples/example.go b/examples/example.go index 8c20a74..6963822 100644 --- a/examples/example.go +++ b/examples/example.go @@ -18,11 +18,13 @@ type mm struct { func main() { err := autopprof.Start(autopprof.Option{ + CPUThreshold: 0.8, // Default: 0.75. + MemThreshold: 0.8, // Default: 0.75. Reporter: report.NewSlackReporter( &report.SlackReporterOption{ - App: "YOUR_APP_NAME", - Token: "YOUR_TOKEN_HERE", - Channel: "#REPORT_CHANNEL", + App: "YOUR_APP_NAME", + Token: "YOUR_TOKEN_HERE", + ChannelID: "REPORT_CHANNEL_ID", }, ), }) From 2c2e9597e347825d993f67cb35d2d34b3f55899c Mon Sep 17 00:00:00 2001 From: mung9 Date: Tue, 24 Sep 2024 20:01:20 +0900 Subject: [PATCH 5/7] Fix tests --- autopprof_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/autopprof_test.go b/autopprof_test.go index dd92382..1f85dd9 100644 --- a/autopprof_test.go +++ b/autopprof_test.go @@ -619,7 +619,7 @@ func TestAutoPprof_watchMemUsage(t *testing.T) { mockReporter.EXPECT(). ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). DoAndReturn( - func(_ context.Context, _ io.Reader, _ report.MemInfo) error { + func(_ context.Context, _ io.Reader, _ int, _ report.MemInfo) error { reported = true return nil }, @@ -682,7 +682,7 @@ func TestAutoPprof_watchMemUsage_consecutive(t *testing.T) { ReportHeapProfile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). AnyTimes(). DoAndReturn( - func(_ context.Context, _ io.Reader, _ report.MemInfo) error { + func(_ context.Context, _ io.Reader, _ int, _ report.MemInfo) error { reportedCnt++ return nil }, @@ -970,7 +970,7 @@ func TestAutoPprof_watchGoroutineCount(t *testing.T) { mockReporter.EXPECT(). ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). DoAndReturn( - func(_ context.Context, _ io.Reader, _ report.GoroutineInfo) error { + func(_ context.Context, _ io.Reader, _ int, _ report.GoroutineInfo) error { reported = true return nil }, @@ -1034,7 +1034,7 @@ func TestAutoPprof_watchGoroutineCount_consecutive(t *testing.T) { ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). AnyTimes(). DoAndReturn( - func(_ context.Context, _ io.Reader, _ report.GoroutineInfo) error { + func(_ context.Context, _ io.Reader, _ int, _ report.GoroutineInfo) error { reportedCnt++ return nil }, From 74e1fff1c232da5be43b05baec251327c3113930 Mon Sep 17 00:00:00 2001 From: mung9 Date: Tue, 24 Sep 2024 20:36:23 +0900 Subject: [PATCH 6/7] Restore go.sum entries to support go 1.16 --- go.sum | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/go.sum b/go.sum index f0734ab..efe628a 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,7 @@ github.com/containerd/cgroups v1.0.4 h1:jN/mbWBEaz+T1pi5OFtnkQ+8qnmEbAr1Oo1FRm5B github.com/containerd/cgroups v1.0.4/go.mod h1:nLNQtsF7Sl2HxNebu77i1R0oDlhiTG+kO4JTrUzo6IA= github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= @@ -26,6 +27,7 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -41,16 +43,21 @@ github.com/slack-go/slack v0.14.0 h1:6c0UTfbRnvRssZUsZ2qe0Iu07VAMPjRqOa6oX8ewF4k github.com/slack-go/slack v0.14.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= +go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -73,13 +80,18 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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= From 00db77cef9aa51d786b6cf538afde8d6671210d8 Mon Sep 17 00:00:00 2001 From: mung9 Date: Thu, 26 Sep 2024 21:27:49 +0900 Subject: [PATCH 7/7] provide both the Channel and ChannelID parameters to avoid a breaking change --- report/slack.go | 61 ++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/report/slack.go b/report/slack.go index efc1783..d9c87f0 100644 --- a/report/slack.go +++ b/report/slack.go @@ -21,7 +21,9 @@ const ( // SlackReporter is the reporter to send the profiling report to the // specific Slack channel. type SlackReporter struct { - app string + app string + channel string + channelID string client *slack.Client @@ -29,8 +31,13 @@ type SlackReporter struct { // SlackReporterOption is the option for the Slack reporter. type SlackReporterOption struct { - App string - Token string + App string + Token string + // Deprecated: Use ChannelID instead. Reporting with a channel name is no longer supported because the latest Slack API for file uploads requires a channel ID instead of a channel name. + // For more details about the Slack API, refer to: https://api.slack.com/methods/files.completeUploadExternal + // + // For details about the file upload process: https://api.slack.com/messaging/files#uploading_files + Channel string ChannelID string } @@ -38,6 +45,7 @@ type SlackReporterOption struct { func NewSlackReporter(opt *SlackReporterOption) *SlackReporter { return &SlackReporter{ app: opt.App, + channel: opt.Channel, channelID: opt.ChannelID, client: slack.New(opt.Token), } @@ -53,14 +61,7 @@ func (s *SlackReporter) ReportCPUProfile( filename = fmt.Sprintf(CPUProfileFilenameFmt, s.app, hostname, now) comment = fmt.Sprintf(cpuCommentFmt, ci.UsagePercentage, ci.ThresholdPercentage) ) - if _, err := s.client.UploadFileV2Context(ctx, slack.UploadFileV2Parameters{ - Reader: r, - Filename: filename, - FileSize: size, - Title: filename, - InitialComment: comment, - Channel: s.channelID, - }); err != nil { + if err := s.reportProfile(ctx, r, size, filename, comment); err != nil { return fmt.Errorf("autopprof: failed to upload a file to Slack channel: %w", err) } return nil @@ -76,14 +77,7 @@ func (s *SlackReporter) ReportHeapProfile( filename = fmt.Sprintf(HeapProfileFilenameFmt, s.app, hostname, now) comment = fmt.Sprintf(memCommentFmt, mi.UsagePercentage, mi.ThresholdPercentage) ) - if _, err := s.client.UploadFileV2Context(ctx, slack.UploadFileV2Parameters{ - Reader: r, - Filename: filename, - FileSize: size, - Title: filename, - InitialComment: comment, - Channel: s.channelID, - }); err != nil { + if err := s.reportProfile(ctx, r, size, filename, comment); err != nil { return fmt.Errorf("autopprof: failed to upload a file to Slack channel: %w", err) } return nil @@ -99,15 +93,30 @@ func (s *SlackReporter) ReportGoroutineProfile( filename = fmt.Sprintf(GoroutineProfileFilenameFmt, s.app, hostname, now) comment = fmt.Sprintf(goroutineCommentFmt, gi.Count, gi.ThresholdCount) ) - if _, err := s.client.UploadFileV2Context(ctx, slack.UploadFileV2Parameters{ + if err := s.reportProfile(ctx, r, size, filename, comment); err != nil { + return fmt.Errorf("autopprof: failed to upload a file to Slack channel: %w", err) + } + return nil +} + +func (s *SlackReporter) reportProfile(ctx context.Context, r io.Reader, size int, filename, comment string) error { + if s.channelID != "" { + _, err := s.client.UploadFileV2Context(ctx, slack.UploadFileV2Parameters{ + Reader: r, + Filename: filename, + FileSize: size, + Title: filename, + InitialComment: comment, + Channel: s.channelID, + }) + return err + } + _, err := s.client.UploadFileContext(ctx, slack.FileUploadParameters{ Reader: r, Filename: filename, - FileSize: size, Title: filename, InitialComment: comment, - Channel: s.channelID, - }); err != nil { - return fmt.Errorf("autopprof: failed to upload a file to Slack channel: %w", err) - } - return nil + Channels: []string{s.channel}, + }) + return err }