grafana_alerts

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 7 Imported by: 5

README

Grafana Alert

To create a Grafana alert:

data := grafana_alerts.GrafanaAlertQuery{
    DatasourceUid: "__expr__",
    Model:         json.RawMessage(`{"conditions":[{"evaluator":{"params":[0,0],"type":"gt"},"operator":{"type":"and"},"query":{"params":[]},"reducer":{"params":[],"type":"avg"},"type":"query"}],"datasource":{"type":"__expr__","uid":"__expr__"},"expression":"1 == 1","hide":false,"intervalMs":1000,"maxDataPoints":43200,"refId":"A","type":"math"}`),
    RefId:         "A",
    RelativeTimeRange: grafana_alerts.RelativeTimeRangeObj{
    From: 0,
    To:   0,
    },
}

createGrafanaAlert := grafana_alerts.GrafanaAlertRule{
    Annotations:  map[string]string{"key_test": "value_test"},
    Condition:    "A",
    Data:         []*grafana_alerts.GrafanaAlertQuery{&data},
    FolderUID:    os.Getenv(envGrafanaFolderUid),
    NoDataState:  grafana_alerts.NoDataOk,
    ExecErrState: grafana_alerts.ErrOK,
    OrgID:        1,
    RuleGroup:    "rule_group_1",
    Title:        "test_alert",
    For:          int64(3),
}

client, err := grafana_alerts.New(apiToken, server.URL)
grafanaAlert, err := client.CreateGrafanaAlertRule(createGrafanaAlert)
function func name
create alert func (c *GrafanaAlertClient) CreateGrafanaAlertRule(payload GrafanaAlertRule) (*GrafanaAlertRule, error)
update alert func (c *GrafanaAlertClient) UpdateGrafanaAlertRule(payload GrafanaAlertRule) error
delete alert by uid func (c *GrafanaAlertClient) DeleteGrafanaAlertRule(uid string) error
get alert by uid func (c *GrafanaAlertClient) GetGrafanaAlertRule(uid string) (*GrafanaAlertRule, error)
list alerts func (c *GrafanaAlertClient) ListGrafanaAlertRules() ([]GrafanaAlertRule, error)

Documentation

Index

Constants

View Source
const (
	ErrOK          ExecErrState = "OK"
	ErrError       ExecErrState = "Error"
	ErrAlerting    ExecErrState = "Alerting"
	NoDataOk       NoDataState  = "OK"
	NoData         NoDataState  = "NoData"
	NoDataAlerting NoDataState  = "Alerting"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ExecErrState

type ExecErrState string

type GrafanaAlertClient

type GrafanaAlertClient struct {
	*client.Client
}

func New

func New(apiToken string, baseUrl string) (*GrafanaAlertClient, error)

func (*GrafanaAlertClient) CreateGrafanaAlertRule

func (c *GrafanaAlertClient) CreateGrafanaAlertRule(payload GrafanaAlertRule) (*GrafanaAlertRule, error)

func (*GrafanaAlertClient) DeleteGrafanaAlertRule

func (c *GrafanaAlertClient) DeleteGrafanaAlertRule(uid string) error

func (*GrafanaAlertClient) GetGrafanaAlertRule

func (c *GrafanaAlertClient) GetGrafanaAlertRule(uid string) (*GrafanaAlertRule, error)

func (*GrafanaAlertClient) ListGrafanaAlertRules

func (c *GrafanaAlertClient) ListGrafanaAlertRules() ([]GrafanaAlertRule, error)

func (*GrafanaAlertClient) UpdateGrafanaAlertRule

func (c *GrafanaAlertClient) UpdateGrafanaAlertRule(payload GrafanaAlertRule) error

type GrafanaAlertQuery

type GrafanaAlertQuery struct {
	DatasourceUid     string               `json:"datasourceUid"`
	Model             interface{}          `json:"model"`
	QueryType         string               `json:"queryType"`
	RefId             string               `json:"refId"`
	RelativeTimeRange RelativeTimeRangeObj `json:"relativeTimeRange"`
}

type GrafanaAlertRule

type GrafanaAlertRule struct {
	Annotations  map[string]string    `json:"annotations,omitempty"`
	Condition    string               `json:"condition"`    // Required
	Data         []*GrafanaAlertQuery `json:"data"`         // Required
	ExecErrState ExecErrState         `json:"execErrState"` // Required
	FolderUID    string               `json:"folderUID"`    // Required
	For          int64                `json:"for"`          // Required, representing nanoseconds
	Id           int64                `json:"id,omitempty"`
	Labels       map[string]string    `json:"labels,omitempty"`
	NoDataState  NoDataState          `json:"noDataState"` // Required
	OrgID        int64                `json:"orgID"`       // Required
	Provenance   string               `json:"provenance,omitempty"`
	RuleGroup    string               `json:"ruleGroup"` // Required
	Title        string               `json:"title"`     // Required
	Uid          string               `json:"uid,omitempty"`
	Updated      time.Time            `json:"updated"`
	IsPaused     bool                 `json:"isPaused"`
}

type NoDataState

type NoDataState string

type RelativeTimeRangeObj

type RelativeTimeRangeObj struct {
	From time.Duration `json:"from"`
	To   time.Duration `json:"to"`
}

Jump to

Keyboard shortcuts

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