Grafana requires time range for alert rule creation with instant PromQL quieriy

Hi, I’m playing with new provisioning API for alerts and I’m getting error when I’m trying to create rule with instant PromQL query and “empty” time range:

ERRO[2022-08-22T16:12:02.643+00:00] RPC /ia.v1beta1.Rules/CreateAlertRule done in 24.130287ms with unexpected error: status: 500, body: {"message":"invalid alert query A: invalid relative time range: {From:0s To:0s}","traceID":"00000000000000000000000000000000"}  request=287c2b10-2235-11ed-b296-0242ac150002

Same error I’m getting when I’m trying to create same rule with Grafana UI and time range from: now to: now. However action Run queries works fine and I’m getting correct query results.

It’s not clear what time range should I specify for the instant query? It’s just has no sense.

1 Like

welcome to the :grafana: community @artemgavrilov!

We have new API documentation for the provisioning APIs in the next version. The AlertQuery type has a parameter for QueryType, which is a string. Have you tried providing the string instant?

I did some testing using developer tools in my browser to see what the AlertQuery type looks like when I create an instant query alert and saw that it used the QueryType of “instant” in the request. The copy as curl option in developer tools is a really useful tool when you’re trying to see what some of these requests look like.

Here’s a link to the docs.

Hi i also have the same error, this is the data block that i have added, the requirement is to evaluate the instant query given and check if it passes the threshold. What is the changes that i have to make.

data:
- refId: A
datasourceUid: ‘P4169E866C3094E38’
model:
conditions:
- evaluator:
params:
- 10
type: gt
operator:
type: and
query:
params:
- A
reducer:
type: last
type: query
datasource:
type: expr
uid: ‘P4169E866C3094E38’
expression: (sum(rate(istio_requests_total{reporter=“destination”, response_code=~“5.*”, destination_workload_namespace=~“vn-dev”}[2m])) by (destination_workload) / sum(rate(istio_requests_total{reporter=“destination”, destination_workload_namespace=~“vn-dev”}[2m])) by (destination_workload))
intervalMs: 1000
maxDataPoints: 43200
queryType: ‘instant’
refId: A
type: math

The above issue is sorted, broke it into two different blocks,

data:
- refId: A
queryType: ‘’
datasourceUid: ‘P067175A64D1E8E5E’
relativeTimeRange: {“from”: 600,“to”: 0}
model:
expression: ‘(sum(rate(istio_requests_total{reporter=“destination”, response_code=~“5.*”, destination_workload_namespace=~“vn-dev”}[2m])) by (destination_workload) / sum(rate(istio_requests_total{reporter=“destination”, destination_workload_namespace=~“vn-dev”}[2m])) by (destination_workload))’
intervalMs: 1000
maxDataPoints: 43200
refId: A
- refId: B
datasourceUid: ‘-100’
model:
conditions:
- evaluator:
params:
- 0.1
type: gt
operator:
type: and
query:
params:
- A
reducer:
type: last
type: query
datasource:
type: expr
uid: ‘-100’
expression: A
intervalMs: 1000
maxDataPoints: 43200
refId: B
type: classic_conditions

Is there any update on this issue? the answer above referring to the docs, shows that there was not any attempt made even to investigate the issue. The docs mention nothing about the whole yaml structure.

Furthermore, the docs Create and manage alerting resources using file provisioning | Grafana documentation mention that one could also use a json file. Which does not seem to be picked up by grafana even if you attempt to reload the resources via the corresponding API.

So how should the relativeTimeRange be defined within the yaml file? Where is that documented?

1 Like

Finally, managed to get my alerting rule yaml to work . This is the format of the file:

apiVersion: 1

groups:

  • orgId: 1
    name: network
    folder: alerts
    interval: 60s
    rules:
    • uid: 2
      title: My rule
      condition: B
      data:
      • refId: A
        queryType: ‘’
        relativeTimeRange:
        from: 600
        to: 0
        datasourceUid: 123456
        model:
        editorMode: code
        expression: ‘some expression’
        intervalMs: 1000
        maxDataPoints: 43200
        legendFormat: __auto
        range: true
        refId: A
      • refId: B
        queryType: ‘’
        relativeTimeRange:
        from: 0
        to: 0
        datasourceUid: -100
        model:
        conditions:
        - evaluator:
        params:
        - 2000
        type: gt
        operator:
        type: and
        query:
        params:
        - A
        reducer:
        params:
        type: last
        type: query
        datasource:
        type: expr
        uid: “-100”
        expression: A
        intervalMs: 1000
        maxDataPoints: 43200
        refId: B
        type: classic_conditions
        noDataState: NoData
        for: 3m
        annotations:
        summary: Test
        labels:
        env: testnet