slack

package
v0.1.72 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAlertMethod added in v0.1.55

func NewAlertMethod(config *AlertMethodConfig) (alert.Method, error)

NewAlertMethod creates a new *AlertMethod or a non-nil error if there was an error.

Types

type AlertMethod added in v0.1.55

type AlertMethod struct {
	// contains filtered or unexported fields
}

AlertMethod implements the alert.AlertMethod interface for writing new alerts to Slack.

Example (BuildPayload)
records := []*alert.Record{
	{
		Filter:    "hits.hits._source",
		Text:      `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`,
		BodyField: true,
	},
	{
		Filter: "aggregation.hostname.buckets",
		Fields: []*alert.Field{
			{
				Key:   "foo",
				Count: 2,
			},
			{
				Key:   "bar",
				Count: 3,
			},
		},
	},
}

a, _ := NewAlertMethod(&AlertMethodConfig{
	WebhookURL: "https://hooks.slack.com/services/ABCDEFG",
	Channel:    "#alerts",
	Text:       "New alert!",
	Emoji:      ":robot",
	TextLimit:  200,
})

sm := a.(*AlertMethod)

payload := sm.buildPayload("Test rule", records)

// This loop is performed in order that tests will pass --
// it is not necessary to perform this
for i := range payload.Attachments {
	payload.Attachments[i].Timestamp = 1
}
data, err := json.MarshalIndent(payload, "", "    ")
if err != nil {
	log.Fatal(err)
}

fmt.Println(string(data))
Output:
{
    "channel": "#alerts",
    "text": "New alert!",
    "icon_emoji": ":robot",
    "attachments": [
        {
            "fallback": "",
            "color": "#ff0000",
            "title": "Test rule",
            "text": "hits.hits._source (1 of 3)\n```\n(part 1 of 3)\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut a\n\n(continued)\n```",
            "footer": "Go Elasticsearch Alerts",
            "footer_icon": "https://www.elastic.co/static/images/elastic-logo-200.png",
            "ts": 1,
            "mrkdwn_in": [
                "text"
            ]
        },
        {
            "fallback": "",
            "color": "#ff0000",
            "title": "Test rule",
            "text": "hits.hits._source (2 of 3)\n```\n(part 2 of 3)\n\nliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui\n\n(continued)\n```",
            "footer": "Go Elasticsearch Alerts",
            "footer_icon": "https://www.elastic.co/static/images/elastic-logo-200.png",
            "ts": 1,
            "mrkdwn_in": [
                "text"
            ]
        },
        {
            "fallback": "",
            "color": "#ff0000",
            "title": "Test rule",
            "text": "hits.hits._source (3 of 3)\n```\n(part 3 of 3)\n\n officia deserunt mollit anim id est laborum.\n```",
            "footer": "Go Elasticsearch Alerts",
            "footer_icon": "https://www.elastic.co/static/images/elastic-logo-200.png",
            "ts": 1,
            "mrkdwn_in": [
                "text"
            ]
        },
        {
            "fallback": "",
            "color": "#36a64f",
            "title": "Test rule",
            "fields": [
                {
                    "title": "foo",
                    "value": "2",
                    "short": true
                },
                {
                    "title": "bar",
                    "value": "3",
                    "short": true
                }
            ],
            "text": "aggregation.hostname.buckets",
            "footer": "Go Elasticsearch Alerts",
            "footer_icon": "https://www.elastic.co/static/images/elastic-logo-200.png",
            "ts": 1,
            "mrkdwn_in": [
                "text"
            ]
        }
    ]
}

func (*AlertMethod) Write added in v0.1.55

func (s *AlertMethod) Write(ctx context.Context, rule string, records []*alert.Record) error

Write creates a properly-formatted Slack message from the records and posts it to the webhook defined at the creation of the AlertMethod. If there was an error making the HTTP request, it returns a non-nil error.

type AlertMethodConfig added in v0.1.55

type AlertMethodConfig struct {
	WebhookURL  string `mapstructure:"webhook"`
	Channel     string `mapstructure:"channel"`
	Username    string `mapstructure:"username"`
	Text        string `mapstructure:"text"`
	Emoji       string `mapstructure:"emoji"`
	TextLimit   int    `mapstructure:"text_limit"`
	IncludeData bool   `mapstructure:"include_data"`
	Client      *http.Client
}

AlertMethodConfig configures where Slack alerts should be created and what they should look like.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL