Skip to content

Commit

Permalink
provide both the Channel and ChannelID parameters to avoid a breaking…
Browse files Browse the repository at this point in the history
… change
  • Loading branch information
mung9 committed Sep 26, 2024
1 parent 74e1fff commit 00db77c
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions report/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,31 @@ 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
}

// 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
}

// NewSlackReporter returns the new SlackReporter.
func NewSlackReporter(opt *SlackReporterOption) *SlackReporter {
return &SlackReporter{
app: opt.App,
channel: opt.Channel,
channelID: opt.ChannelID,
client: slack.New(opt.Token),
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 00db77c

Please sign in to comment.