diff --git a/.cursor/.gitignore b/.cursor/.gitignore new file mode 100644 index 0000000000..8bf7cc27a1 --- /dev/null +++ b/.cursor/.gitignore @@ -0,0 +1 @@ +plans/ diff --git a/.gitignore b/.gitignore index 1997d9b419..32635e3cdc 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /amtool /data/ /vendor +.idea diff --git a/notify/slack/slack.go b/notify/slack/slack.go index dbcea63dd7..f96efe61fa 100644 --- a/notify/slack/slack.go +++ b/notify/slack/slack.go @@ -36,17 +36,6 @@ import ( // https://api.slack.com/reference/messaging/attachments#legacy_fields - 1024, no units given, assuming runes or characters. const maxTitleLenRunes = 1024 -// Notifier implements a Notifier for Slack notifications. -type Notifier struct { - conf *config.SlackConfig - tmpl *template.Template - logger *slog.Logger - client *http.Client - retrier *notify.Retrier - - postJSONFunc func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error) -} - // New returns a new Slack notification handler. func New(c *config.SlackConfig, t *template.Template, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) { client, err := notify.NewClientWithTracing(*c.HTTPConfig, "slack", httpOpts...) @@ -64,43 +53,6 @@ func New(c *config.SlackConfig, t *template.Template, l *slog.Logger, httpOpts . }, nil } -// request is the request for sending a slack notification. -type request struct { - Channel string `json:"channel,omitempty"` - Timestamp string `json:"ts,omitempty"` - Username string `json:"username,omitempty"` - IconEmoji string `json:"icon_emoji,omitempty"` - IconURL string `json:"icon_url,omitempty"` - LinkNames bool `json:"link_names,omitempty"` - Text string `json:"text,omitempty"` - Attachments []attachment `json:"attachments"` -} - -// attachment is used to display a richly-formatted message block. -type attachment struct { - Title string `json:"title,omitempty"` - TitleLink string `json:"title_link,omitempty"` - Pretext string `json:"pretext,omitempty"` - Text string `json:"text"` - Fallback string `json:"fallback"` - CallbackID string `json:"callback_id"` - Fields []config.SlackField `json:"fields,omitempty"` - Actions []config.SlackAction `json:"actions,omitempty"` - ImageURL string `json:"image_url,omitempty"` - ThumbURL string `json:"thumb_url,omitempty"` - Footer string `json:"footer"` - Color string `json:"color,omitempty"` - MrkdwnIn []string `json:"mrkdwn_in,omitempty"` -} - -// slackResponse represents the response from Slack API. -type slackResponse struct { - OK bool `json:"ok"` - Error string `json:"error,omitempty"` - Channel string `json:"channel,omitempty"` - Timestamp string `json:"ts,omitempty"` -} - // Notify implements the Notifier interface. func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { var err error diff --git a/notify/slack/types.go b/notify/slack/types.go new file mode 100644 index 0000000000..edda2a7d67 --- /dev/null +++ b/notify/slack/types.go @@ -0,0 +1,73 @@ +// Copyright 2019 Prometheus Team +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package slack + +import ( + "context" + "io" + "log/slog" + "net/http" + + "github.com/prometheus/alertmanager/config" + "github.com/prometheus/alertmanager/notify" + "github.com/prometheus/alertmanager/template" +) + +// Notifier implements a Notifier for Slack notifications. +type Notifier struct { + conf *config.SlackConfig + tmpl *template.Template + logger *slog.Logger + client *http.Client + retrier *notify.Retrier + + postJSONFunc func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error) +} + +// request is the request for sending a Slack notification. +type request struct { + Channel string `json:"channel,omitempty"` + Timestamp string `json:"ts,omitempty"` + Username string `json:"username,omitempty"` + IconEmoji string `json:"icon_emoji,omitempty"` + IconURL string `json:"icon_url,omitempty"` + LinkNames bool `json:"link_names,omitempty"` + Text string `json:"text,omitempty"` + Attachments []attachment `json:"attachments"` +} + +// attachment is used to display a richly formatted message block. +type attachment struct { + Title string `json:"title,omitempty"` + TitleLink string `json:"title_link,omitempty"` + Pretext string `json:"pretext,omitempty"` + Text string `json:"text"` + Fallback string `json:"fallback"` + CallbackID string `json:"callback_id"` + Fields []config.SlackField `json:"fields,omitempty"` + Actions []config.SlackAction `json:"actions,omitempty"` + ImageURL string `json:"image_url,omitempty"` + ThumbURL string `json:"thumb_url,omitempty"` + Footer string `json:"footer"` + Color string `json:"color,omitempty"` + MrkdwnIn []string `json:"mrkdwn_in,omitempty"` +} + +// slackResponse represents the response from Slack API. +type slackResponse struct { + OK bool `json:"ok"` + Error string `json:"error,omitempty"` + Channel string `json:"channel,omitempty"` + Timestamp string `json:"ts,omitempty"` +}