Alert evaluation interval Grafana API /JSON

Hello,

I am trying to deploy several alerts using the Grafana API, everything works fine expect for the evaluation interval parameter; In my JSON I tried “interval”, “IntervalSeconds” and “interval_seconds” as in terraform doc, but none worked.

Do you have any idea about whats happening?

Here is my JSON:

{
        'orgID': 1,
        'folderUID': 'ddebef16-293c-4c45-8925-dc442846d644',
        'ruleGroup': '$$',
        'interval': '3600s',
        'title': 'title',
        'condition': 'B',
        'data': [
            {
                'refId': 'A',
                'queryType': '',
                'relativeTimeRange': {
                    'from': 600,
                    'to': 0
                },
                'datasourceUid': 'debcd7b9-3788-44cf-a141-56194c8a707a',
                'model': {
                    'editorMode': 'code',
                    'format': 'table',
                    'hide': False,
                    'intervalMs': 1000,
                    'maxDataPoints': 43200,
                    'rawQuery': True,
                    'rawSql': f"select ... from ... where __ = {event}",
                    'refId': 'A',
                    'sql': {
                        'columns': [
                            {
                                'parameters': [],
                                'type': 'function'
                            }
                        ],
                        'groupBy': [
                            {
                                'property': {
                                    'type': 'string'
                                },
                                'type': 'groupBy'
                            }
                        ],
                        'limit': 50
                    }
                }
            },
            {
                'refId': 'B',
                'queryType': '',
                'relativeTimeRange': {
                    'from': 600,
                    'to': 0
                },
                'datasourceUid': '__expr__',
                'model': {
                    'conditions': [
                        {
                            'evaluator': {
                                'params': [0, 0],
                                'type': 'gt'
                            },
                            'operator': {
                                'type': 'and'
                            },
                            'query': {
                                'params': []
                            },
                            'reducer': {
                                'params': [],
                                'type': 'avg'
                            },
                            'type': 'query'
                        }
                    ],
                    'datasource': {
                        'name': 'Expression',
                        'type': '__expr__',
                        'uid': '__expr__'
                    },
                    'expression': '$A == 1',
                    'intervalMs': 1000,
                    'maxDataPoints': 43200,
                    'refId': 'B',
                    'type': 'math'
                }
            }
        ],
        'updated': '2023-07-17T09:42:35Z',
        'noDataState': 'NoData',
        'execErrState': 'Error',
        'for': '0s',
        'annotations': {
            'description': '$$: {{ $labels.$$ }}'
        },
        'labels': {
            '$$': '$$'
        },
        'isPaused': False
    }

One solution I found is creating an empty alert with evaluation group that has an interval set to 1h, and then create alerts with the same evaluation group.

Is there any other solution to specify the interval via the API?

1 Like

In the JSON you provided, the correct field to specify the evaluation interval is for, not interval. The for field represents the duration for which the condition must be true before the alert is triggered.

In your JSON, you have set "for": "0s", which means the condition must be true for 0 seconds before the alert is triggered. To set the evaluation interval to 1 hour (3600 seconds), you should change "for": "0s" to "for": "3600s".

In the appropriate API endpoint (you can see here) Alerting Provisioning HTTP API | Grafana documentation the API endpoint wants a ProvisionedAlertRule object. You’ll be able to see that there’s no interval key in that desired payload, but for is there.

Hope this helps!

Hello @davidallen5,

Thank you for the reply.

It is not exactly what I’m trying to do; What you said resulted in this:

image

But what I want is this:

image

Hello I am looking for an answer to the above question as well. @clevernessisamyth did you manage to find out how to do this?

Hello,

No, I didn’t find any solution.

I found a solution:

trigger a put request to the following API

PUT /api/v1/provisioning/folder/FOLDER_ID/rule-groups/RULE_GROUP

Payload
{“interval”:600}

The above will change all alerts in group to evaluate every 10 mins