Skip to content

Commit

Permalink
Merge pull request #134 from panawala/feature/update_alert_api
Browse files Browse the repository at this point in the history
add alert and resource api
  • Loading branch information
shabicheng authored Aug 30, 2021
2 parents f1670fc + 5792092 commit e3c4d4e
Show file tree
Hide file tree
Showing 12 changed files with 1,368 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
133 changes: 123 additions & 10 deletions client_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,38 @@ const (
NotificationTypeMessageCenter = "MessageCenter"
)

const (
CountConditionKey = "__count__"
)

type Severity int

const (
Report Severity = 2
Low Severity = 4
Medium Severity = 6
High Severity = 8
Critical Severity = 10
)

const (
JoinTypeCross = "cross_join"
JoinTypeInner = "inner_join"
JoinTypeLeft = "left_join"
JoinTypeRight = "right_join"
JoinTypeFull = "full_join"
JoinTypeLeftExclude = "left_exclude"
JoinTypeRightExclude = "right_exclude"
JoinTypeConcat = "concat"
JoinTypeNo = "no_join"
)

const (
GroupTypeNoGroup = "no_group"
GroupTypeLabelsAuto = "labels_auto"
GroupTypeCustom = "custom"
)

const (
ScheduleTypeFixedRate = "FixedRate"
ScheduleTypeHourly = "Hourly"
Expand All @@ -40,6 +72,63 @@ const (
ScheduleTypeResident = "Resident"
)

const (
StoreTypeLog = "log"
StoreTypeMetric = "metric"
StoreTypeMeta = "meta"
)

// SeverityConfiguration severity config by group
type SeverityConfiguration struct {
Severity Severity `json:"severity"`
EvalCondition ConditionConfiguration `json:"evalCondition"`
}

type ConditionConfiguration struct {
Condition string `json:"condition"`
CountCondition string `json:"countCondition"`
}

type JoinConfiguration struct {
Type string `json:"type"`
Condition string `json:"condition"`
}

type GroupConfiguration struct {
Type string `json:"type"`
Fields []string `json:"fields"`
}

type Tag struct {
Key string `json:"key"`
Value string `json:"value"`
}

type Token struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
Required bool `json:"required"`
Type string `json:"type"`
Default string `json:"default"`
Hide bool `json:"hide"`
}

type TemplateConfiguration struct {
Id string `json:"id"`
Type string `json:"type"`
Version string `json:"version"`
Lang string `json:"lang"`
Tokens map[string]string `json:"tokens"`
Annotations map[string]string `json:"annotations"`
}

type PolicyConfiguration struct {
UseDefault bool `json:"useDefault"`
RepeatInterval string `json:"repeatInterval"`
AlertPolicyId string `json:"alertPolicyId"`
ActionPolicyId string `json:"actionPolicyId"`
}

type Alert struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Expand All @@ -66,23 +155,20 @@ func (alert *Alert) MarshalJSON() ([]byte, error) {
return json.Marshal(body)
}

type AlertConfiguration struct {
Condition string `json:"condition"`
Dashboard string `json:"dashboard"`
QueryList []*AlertQuery `json:"queryList"`
MuteUntil int64 `json:"muteUntil"`
NotificationList []*Notification `json:"notificationList"`
NotifyThreshold int32 `json:"notifyThreshold"`
Throttling string `json:"throttling"`
}

type AlertQuery struct {
ChartTitle string `json:"chartTitle"`
LogStore string `json:"logStore"`
Query string `json:"query"`
TimeSpanType string `json:"timeSpanType"`
Start string `json:"start"`
End string `json:"end"`

StoreType string `json:"storeType"`
Project string `json:"project"`
Store string `json:"store"`
Region string `json:"region"`
RoleArn string `json:"roleArn"`
DashboardId string `json:"dashboardId"`
}

type Notification struct {
Expand All @@ -104,6 +190,33 @@ type Schedule struct {
Hour int32 `json:"hour"`
}

type AlertConfiguration struct {
Condition string `json:"condition"`
MuteUntil int64 `json:"muteUntil"`
NotificationList []*Notification `json:"notificationList"`
NotifyThreshold int32 `json:"notifyThreshold"`
Throttling string `json:"throttling"`

Version string `json:"version"`
Type string `json:"type"`
TemplateConfiguration *TemplateConfiguration `json:"templateConfiguration"`

Dashboard string `json:"dashboard"`
Threshold int `json:"threshold"`
NoDataFire bool `json:"noDataFire"`
NoDataSeverity Severity `json:"noDataSeverity"`
SendResolved bool `json:"sendResolved"`
QueryList []*AlertQuery `json:"queryList"`
Annotations []*Tag `json:"annotations"`
Labels []*Tag `json:"labels"`
SeverityConfigurations []*SeverityConfiguration `json:"severityConfigurations"`

JoinConfigurations []*JoinConfiguration `json:"joinConfigurations"`
GroupConfiguration GroupConfiguration `json:"groupConfiguration"`

PolicyConfiguration PolicyConfiguration `json:"policyConfiguration"`
}

func (c *Client) CreateSavedSearch(project string, savedSearch *SavedSearch) error {
body, err := json.Marshal(savedSearch)
if err != nil {
Expand Down
120 changes: 120 additions & 0 deletions client_alert_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package sls

import "encoding/json"

const (
ResourceNameAlertPolicy = "sls.alert.alert_policy"
ResourceNameActionPolicy = "sls.alert.action_policy"
ResourceNameUser = "sls.common.user"
ResourceNameUserGroup = "sls.common.user_group"
ResourceNameContentTemplate = "sls.alert.content_template"
ResourceNameGlobalConfig = "sls.alert.global_config"
ResourceNameWebhookIntegration = "sls.alert.webhook_application"
)

type (
// Notified users.
ResourceUser struct {
UserId string `json:"user_id"`
UserName string `json:"user_name"`
Enabled bool `json:"enabled"`
CountryCode string `json:"country_code"`
Phone string `json:"phone"`
Email []string `json:"email"`
SmsEnabled bool `json:"sms_enabled"`
VoiceEnabled bool `json:"voice_enabled"`
}

// ResourceUserGroup is a collection of users.
ResourceUserGroup struct {
Id string `json:"user_group_id"`
Name string `json:"user_group_name"`
Enabled bool `json:"enabled"`
Members []string `json:"members"`
}

// ResourceAlertPolicy defines how alerts should be grouped, inhibited and silenced.
ResourceAlertPolicy struct {
PolicyId string `json:"policy_id"`
PolicyName string `json:"policy_name"`
Parent string `json:"parent_id"`
IsDefault bool `json:"is_default"`
GroupPolicy string `json:"group_script"`
InhibitPolicy string `json:"inhibit_script"`
SilencePolicy string `json:"silence_script"`
}

// ResourceActionPolicy defines how to send alert notifications.
ResourceActionPolicy struct {
ActionPolicyId string `json:"action_policy_id"`
ActionPolicyName string `json:"action_policy_name"`
IsDefault bool `json:"is_default"`
PrimaryPolicyScript string `json:"primary_policy_script"`
SecondaryPolicyScript string `json:"secondary_policy_script"`
EscalationStartEnabled bool `json:"escalation_start_enabled"`
EscalationStartTimeout string `json:"escalation_start_timeout"`
EscalationInprogressEnabled bool `json:"escalation_inprogress_enabled"`
EscalationInprogressTimeout string `json:"escalation_inprogress_timeout"`
EscalationEnabled bool `json:"escalation_enabled"`
EscalationTimeout string `json:"escalation_timeout"`
Labels map[string]string `json:"labels"`
}

// ContentTemplate
ResourceTemplate struct {
Content string `json:"content"`
Locale string `json:"locale"`
Title string `json:"title"`
Subject string `json:"subject"`
SendType string `json:"send_type"`
Limit int `json:"limit"`
}

ResourceTemplates struct {
Sms ResourceTemplate `json:"sms"`
Voice ResourceTemplate `json:"voice"`
Email ResourceTemplate `json:"email"`
Dingtalk ResourceTemplate `json:"dingtalk"`
Webhook ResourceTemplate `json:"webhook"`
MessageCenter ResourceTemplate `json:"message_center"`
Wechat ResourceTemplate `json:"wechat"`
Lark ResourceTemplate `json:"lark"`
Slack ResourceTemplate `json:"slack"`
}
ResourceContentTemplate struct {
TemplateId string `json:"template_id"`
TemplateName string `json:"template_name"`
IsDefault bool `json:"is_default"`
Templates ResourceTemplates `json:"templates"`
}

// WebhookIntegration is a wrap of webhook notification config.
ResourceWebhookHeader struct {
Key string `json:"key"`
Value string `json:"value"`
}
WebhookIntegration struct {
Id string `json:"id"`
Name string `json:"name"`
Method string `json:"method"`
Url string `json:"url"`
Type string `json:"type"`
Headers []*ResourceWebhookHeader `json:"headers"`
}

// GlobalConfig is the global configuration for alerts.
GlobalConfig struct {
ConfigId string `json:"config_id"`
ConfigName string `json:"config_name"`
ConfigDetail struct {
AlertCenterLog struct {
Region string `json:"region"`
} `json:"alert_center_log"`
} `json:"config_detail"`
}
)

func JsonMarshal(v interface{}) string {
vBytes, _ := json.Marshal(v)
return string(vBytes)
}
Loading

0 comments on commit e3c4d4e

Please sign in to comment.