All Products
Search
Document Center

Simple Log Service:Create alert rules

Last Updated:Mar 18, 2025

You can call the CreateAlert operation to create alert rules.

Prerequisites

Parameters

def create_alert(self, project, detail):

Request parameters

Parameter

Type

Required

Description

Example

project

String

Yes

The name of the project.

demo-test-project

detail

Dictionary

Yes

The configuration that includes the settings to create an alert rule.

{
    "name": "alert-id-223456",
    "displayName": "Alert for testing",
    "type": "Alert",
    "status": "Enabled",
    "schedule": {
        "type": "FixedRate",
        "interval": "1m"
    },
    "configuration": {
        "version": "2.0",
        "type": "default",
        "dashboard": "internal-alert-analysis",
        "queryList": [{
            "storeType": "log",
            "region": "ap-southeast-1",
            "project": "demo-test-project",
            "store": "test-logstore",
            "query": "* | select count(*) cnt",
            "timeSpanType": "Truncated",
            "start": "-1m",
            "end": "absolute",
            "powerSqlMode": "auto"
        }],
        "groupConfiguration": {
            "type": "no_group",
            "fields": []
        },
        "joinConfigurations": [],
        "severityConfigurations": [{
            "severity": 6,
            "evalCondition": {
                "condition": "cnt > 0",
                "countCondition": ""
            }
        }],
        "labels": [{
            "key": "service",
            "value": "nginx"
        }],
        "annotations": [{
            "key": "title",
            "value": "Nginx Status Error"
        }, {
            "key": "desc",
            "value": "Nginx Status Error, count: ${cnt}"
        }],
        "autoAnnotation": True,
        "sendResolved": False,
        "threshold": 1,
        "noDataFire": False,
        "noDataSeverity": 6,
        "policyConfiguration": {
            "alertPolicyId": "sls.builtin.dynamic",
            "actionPolicyId": "test-action-policy",
            "repeatInterval": "1m",
            "useDefault": False
        }
    }
}

Description of the detail parameter

Parameter

Type

Required

Description

Example

name

String

Yes

The name of the alert rule. Make sure that the name is unique in a project.

alert-123456

displayName

String

Yes

The display name of the alert rule.

test-alert

type

String

Yes

The value is fixed as Alert.

status

String

No

The status of the alert monitoring rule. Valid values:

  • Enabled

  • Disabled (default)

description

String

No

The description of the alert rule.

An alert rule

schedule

Dictionary

Yes

The scheduling configuration of the alert rule.

{
    "type": "FixedRate",
    "interval": "1m"
}

configuration

Dictionary

Yes

The detailed configuration of the alert rule.

{
    "version": "2.0",
    "type": "default",
    "dashboard": "internal-alert-analysis",
    "queryList": [{
        "storeType": "log",
        "region": "ap-southeast-1",
        "project": "demo-test-project",
        "store": "test-logstore",
        "query": "* | select count(*) cnt",
        "timeSpanType": "Truncated",
        "start": "-1m",
        "end": "absolute",
        "powerSqlMode": "auto"
    }],
    "groupConfiguration": {
        "type": "no_group",
        "fields": []
    },
    "joinConfigurations": [],
    "severityConfigurations": [{
        "severity": 6,
        "evalCondition": {
            "condition": "cnt > 0",
            "countCondition": ""
        }
    }],
    "labels": [{
        "key": "service",
        "value": "nginx"
    }],
    "annotations": [{
        "key": "title",
        "value": "Nginx Status Error"
    }, {
        "key": "desc",
        "value": "Nginx Status Error, count: ${cnt}"
    }],
    "autoAnnotation": True,
    "sendResolved": False,
    "threshold": 1,
    "noDataFire": False,
    "noDataSeverity": 6,
    "policyConfiguration": {
        "alertPolicyId": "sls.builtin.dynamic",
        "actionPolicyId": "test-action-policy",
        "repeatInterval": "1m",
        "useDefault": False
    }
}

Description of the schedule parameter

Parameter

Type

Required

Description

Example

type

String

Yes

The type of the scheduling configuration for the alert rule. Valid values: FixedRate and Cron.

Cron

cronExpression

String

No

The cron expression. For more information, see Cron expression.

0/5 * * * *

runImmediately

bool

No

Specifies whether to immediately run the scheduled job.

False

timeZone

String

No

The time zone for the cron expression. This parameter is empty by default, which indicates that the time zone is UTC+8.

Null

delay

int

No

The number of seconds for which the scheduled job is delayed.

4

interval

String

No

The fixed interval.

1m

Description of the configuration parameter

Parameter

Type

Required

Description

Example

muteUntil

int

No

The time at which the alert rule is temporarily disabled.

1698907508

version

String

Yes

The version of the alert rule.

2.0

type

String

No

The type of the alert rule.

default

dashboard

String

No

The ID of the dashboard with which the alert rule is associated.

dasnboardExample

threshold

int

Yes

The maximum number of consecutive times that the trigger condition can be met.

1

noDataFire

bool

Yes

Specifies whether to enable the no-data alert feature.

False

noDataSeverity

int

No

The severity of the no-data alert.

6

sendResolved

bool

Yes

Specifies whether to send a recovery notification after an alert is cleared.

False

autoAnnotation

bool

Yes

Specifies whether to allow the system to automatically add annotations.

False

queryList

List

Yes

The query statements. For more information about the variables in query statements, see AlertQuery.

[{
    "storeType": "log",
    "region": "ap-southeast-1",
    "project": "demo-test-project",
    "store": "test-logstore",
    "query": "* | select count(*) cnt",
    "timeSpanType": "Truncated",
    "start": "-1m",
    "end": "absolute",
    "powerSqlMode": "auto"
}]

annotations

List

No

The annotations of the alert. For more information about the variables in annotations, see AlertTag.

[{
    "key": "title",
    "value": "Nginx Status Error"
}, {
    "key": "desc",
    "value": "Nginx Status Error, count: ${cnt}"
}]

labels

List

No

The labels of the alert. For more information about the variables in labels, see AlertTag.

[{
    "key": "service",
    "value": "nginx"
}]

templateConfiguration

Dictionary

No

The configuration of the alert template. For more information, see TemplateConfiguration.

{
    "id": "sls.app.ack.ip.not_enough",
    "type": "sys",
    "lang": "cn"
}

conditionConfiguration

Dictionary

No

The condition that is used to trigger alerts. For more information, see ConditionConfiguration.

{
    "condition": "cnt > 100",
    "countCondition": ""
}

severityConfigurations

List

Yes

The configurations of the alert severity. For more information about the variables in configurations, see SeverityConfiguration.

[{
    "severity": 6,
    "evalCondition": {
        "condition": "cnt > 0",
        "countCondition": ""
    }
}]

joinConfigurations

List

No

The set operation configurations for the query statements of the alert rule. For more information about the variables in configurations, see JoinConfiguration.

[{
    "type": "left_join",
    "condition": "$0.id == $1.id"
}]

groupConfiguration

Dictionary

Yes

The configuration of group evaluation. For more information, see GroupConfiguration.

{
    "type": "no_group",
    "fields": []
}

policyConfiguration

Dictionary

No

The configuration for sending alerts to Simple Log Service Notification. For more information, see PolicyConfiguration.

{
    "alertPolicyId": "sls.builtin.dynamic",
    "actionPolicyId": "test-action-policy",
    "repeatInterval": "1m",
    "useDefault": False
}

sinkEventStore

Dictionary

No

The configuration for sending alerts to Eventstore. For more information, see SinkEventStoreConfiguration.

{
    "enabled": TRUE,
    "endpoint": "ap-southeast-1-intranet.log.aliyuncs.com",
    "project": "exampleProject",
    "eventStore": "exampleStore",
    "roleArn": "acs:ram::123456789:role/aliyunlogetlrole"
}

sinkCms

Dictionary

No

The configuration for sending alerts to CloudMonitor Event Center. For more information, see SinkCmsConfiguration.

{
    "enabled": True
}

sinkAlerthub

Dictionary

No

The configuration for sending alerts to the notification system of Simple Log Service. For more information, see SinkAlerthubConfiguration.

{
    "enabled": True
}

tags

List

No

The custom tags of the alert.

["host"]

Response parameters

For more information about response parameters, see CreateAlert.

Sample code

import os
from aliyun.log import LogClient

alert = {
    "name": "alert-id-1234",
    "displayName": "Alert for testing",
    "type": "Alert",
    "status": "Enabled",
    "schedule": {
        "type": "FixedRate",
        "interval": "1m"
    },
    "configuration": {
        "version": "2.0",
        "type": "default",
        "dashboard": "internal-alert-analysis",
        "queryList": [{
            "storeType": "log",
            "region": "ap-southeast-1",
            "project": "demo-test-project",
            "store": "test-logstore",
            "query": "* | select count(*) cnt",
            "timeSpanType": "Truncated",
            "start": "-1m",
            "end": "absolute",
            "powerSqlMode": "auto"
        }],
        "groupConfiguration": {
            "type": "no_group",
            "fields": []
        },
        "joinConfigurations": [],
        "severityConfigurations": [{
            "severity": 6,
            "evalCondition": {
                "condition": "cnt > 0",
                "countCondition": ""
            }
        }],
        "labels": [{
            "key": "service",
            "value": "nginx"
        }],
        "annotations": [{
            "key": "title",
            "value": "Nginx Status Error"
        }, {
            "key": "desc",
            "value": "Nginx Status Error, count: ${cnt}"
        }],
        "autoAnnotation": True,
        "sendResolved": False,
        "threshold": 1,
        "noDataFire": False,
        "noDataSeverity": 6,
        "policyConfiguration": {
            "alertPolicyId": "sls.builtin.dynamic",
            "actionPolicyId": "test-action-policy",
            "repeatInterval": "1m",
            "useDefault": False
        }
    }
}


def main():
    # In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
    access_key_id = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID", "")
    access_key_secret = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "")

    # Specify a Simple Log Service endpoint.
    endpoint = "ap-southeast-1.log.aliyuncs.com"

    client = LogClient(endpoint, access_key_id, access_key_secret)

    # The name of the project.
    project = "demo-test-project"

    res = client.create_alert(project, alert)
    res.log_print()


if __name__ == "__main__":
    main()

References