kit

package module
v0.0.0-...-28b8384 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: Apache-2.0 Imports: 16 Imported by: 2

README

alertmanager-kit

alertmanager-kit encapsulates the interface to alertmanager in a neat way.

compatibility

It currently only works on newer versions of alertmanager which provide v2 api.

usage

The following example demonstrates an usual usage. It fetches the Alertmanager status from the service, post/get alerts, post/get/delete silence.

// Init the config
//config := kit.ClientConfig{
//	URL: "http://139.198.112.79:59093",
//}
// Use this if running on kubernetes
config := kit.ClientConfig{
    Service: &kit.ServiceReference{
        Namespace: "monitoring",
        Name: "alertmanager",
    },
}

// Create client for alertmanager service
client, e := kit.NewClient(config)
if e != nil {
    panic(e)
}
ctx, _ := context.WithTimeout(context.Background(), time.Second*10)

// Get status
status, e := client.GetStatus(ctx)
prettyPrintlnOrPanic(status, e)

// Post alerts
e = client.PostAlerts(ctx, []*kit.RawAlert{{
    Labels:      map[string]string{
        "alertname": "test",
        "alerttype": "test",
        "namespace": "test",
        "pod": "test"},
    Annotations: map[string]string{"message": "test"},
}})
prettyPrintlnOrPanic(nil, e)

// Get alerts
alerts, e := client.GetAlerts(ctx,
    kit.NewAlertsFilter().WithFilter([]string{"alertname=\"test\""}))
prettyPrintlnOrPanic(alerts, e)

// Post silence
silenceId, e := client.PostSilence(ctx, &kit.RawSilence{
    StartsAt: strfmt.DateTime(time.Now()),
    EndsAt: strfmt.DateTime(time.Now().Add(time.Minute)),
    CreatedBy: "test",
    Matchers: []*kit.Matcher{{
        Name: "alertname",
        Value: "test",
    }},
})
prettyPrintlnOrPanic(silenceId, e)

// Get silence
silence, e := client.GetSilence(ctx, silenceId)
prettyPrintlnOrPanic(silence, e)

// Delete silence
e = client.DeleteSilence(ctx, silenceId)
prettyPrintlnOrPanic(nil, e)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alert

type Alert struct {
	RawAlert
	Fingerprint string       `json:"fingerprint"`
	Receivers   []*Receiver  `json:"receivers"`
	Status      *AlertStatus `json:"status"`
}

type AlertGroup

type AlertGroup struct {
	Alerts   []*Alert        `json:"alerts"`
	Labels   models.LabelSet `json:"labels"`
	Receiver *Receiver       `json:"receiver"`
}

type AlertStatus

type AlertStatus struct {
	InhibitedBy []string `json:"inhibitedBy"`
	SilencedBy  []string `json:"silencedBy"`
	State       string   `json:"state"`
}

type AlertmanagerClient

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

func NewClient

func NewClient(config ClientConfig) (*AlertmanagerClient, error)

func (*AlertmanagerClient) DeleteSilence

func (c *AlertmanagerClient) DeleteSilence(ctx context.Context, silId string) error

func (*AlertmanagerClient) GetAlertGroups

func (c *AlertmanagerClient) GetAlertGroups(ctx context.Context, af *AlertsFilter) ([]*AlertGroup, error)

GetAlerts gets alert groups by the filter.

func (*AlertmanagerClient) GetAlerts

func (c *AlertmanagerClient) GetAlerts(ctx context.Context, af *AlertsFilter) ([]*Alert, error)

GetAlerts gets alerts by the filter.

func (*AlertmanagerClient) GetReceivers

func (c *AlertmanagerClient) GetReceivers(ctx context.Context) ([]*Receiver, error)

func (*AlertmanagerClient) GetSilence

func (c *AlertmanagerClient) GetSilence(ctx context.Context, silenceId string) (*Silence, error)

GetSilence gets silence by silence id.

func (*AlertmanagerClient) GetSilences

func (c *AlertmanagerClient) GetSilences(ctx context.Context, filter []string) ([]*Silence, error)

GetSilences gets silences by filter filter supports a simplified prometheus query syntax, contains operators: =, !=, =~, !~ . This will be used to filter your query by value matching or regex matching, and their negative.

func (*AlertmanagerClient) GetStatus

func (*AlertmanagerClient) PostAlerts

func (c *AlertmanagerClient) PostAlerts(ctx context.Context, alerts []*RawAlert) error

PostAlerts posts alerts to alertmanager. Alerts will be posted to every instance behind alertmanager service.

func (*AlertmanagerClient) PostSilence

func (c *AlertmanagerClient) PostSilence(ctx context.Context, rsil *RawSilence) (string, error)

type AlertmanagerStatus

type AlertmanagerStatus models.AlertmanagerStatus

type AlertsFilter

type AlertsFilter struct {
	Active      bool `json:"active"`
	Inhibited   bool `json:"inhibited"`
	Silenced    bool `json:"silenced"`
	Unprocessed bool `json:"unprocessed"`
	// filter supports a simplified prometheus query syntax, contains operators: =, !=, =~, !~ .
	// This will be used to filter your query by value matching or regex matching, and their negative.
	Filter   []string `json:"filter"`
	Receiver string   `json:"receiver"`
}

func NewAlertsFilter

func NewAlertsFilter() *AlertsFilter

func (*AlertsFilter) WithActive

func (f *AlertsFilter) WithActive(active bool) *AlertsFilter

func (*AlertsFilter) WithFilter

func (f *AlertsFilter) WithFilter(filter []string) *AlertsFilter

func (*AlertsFilter) WithInhibited

func (f *AlertsFilter) WithInhibited(inhibited bool) *AlertsFilter

func (*AlertsFilter) WithReceiver

func (f *AlertsFilter) WithReceiver(receiver string) *AlertsFilter

func (*AlertsFilter) WithSilenced

func (f *AlertsFilter) WithSilenced(silenced bool) *AlertsFilter

func (*AlertsFilter) WithUnprocessed

func (f *AlertsFilter) WithUnprocessed(unprocessed bool) *AlertsFilter

type ClientConfig

type ClientConfig struct {
	URL     string            `json:"url,omitempty"`
	Service *ServiceReference `json:"service,omitemtpy"`
}

ClientConfig contains the information to make a connection with the alertmanger If you need to post alerts to alertmanager, and you have customized the target port of alertmanager service, please configure ClientConfig.Service.Target, regardless of whether you configure URL.

type Matcher

type Matcher struct {
	IsRegex bool   `json:"isRegex"`
	Name    string `json:"name"`
	Value   string `json:"value"`
}

type RawAlert

type RawAlert struct {
	Labels      models.LabelSet `json:"labels,omitempty"`
	Annotations models.LabelSet `json:"annotations,omitempty"`
	StartsAt    strfmt.DateTime `json:"startsAt,omitempty"`
	EndsAt      strfmt.DateTime `json:"endsAt,omitempty"`
}

type RawSilence

type RawSilence struct {
	ID        string          `json:"id"`
	StartsAt  strfmt.DateTime `json:"startsAt,omitempty"`
	EndsAt    strfmt.DateTime `json:"endsAt,omitempty"`
	Comment   string          `json:"comment"`
	CreatedBy string          `json:"createdBy"`
	Matchers  []*Matcher      `json:"matchers"`
}

type Receiver

type Receiver struct {
	Name string `json:"name"`
}

type ServiceReference

type ServiceReference struct {
	Namespace string `json:"namespace"`
	Name      string `json:"name"`
	// The port that will be exposed by this service. Defaults to 9093
	Port *int `json:"port,omitempty"`
	// TargetPort is the port to access on the backend instances targeted by the service.
	// If this is not specified, the value of the 'port' field is used.
	TargetPort *int `json:"targetPort,omitempty"`
}

type Silence

type Silence struct {
	RawSilence
	Status    *SilenceStatus  `json:"status"`
	UpdatedAt strfmt.DateTime `json:"updatedAt"`
}

type SilenceStatus

type SilenceStatus struct {
	State string `json:"state"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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