Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ElasticsearchTarget helper class fails isinstance(Target) validation in AlertRules #553

Open
lockan opened this issue Nov 25, 2022 · 3 comments

Comments

@lockan
Copy link

lockan commented Nov 25, 2022

What you expected to happen?

Grafana 9.2.4
Grafanalib 0.7.0

When trying to define an AlertRulev9 object for use with an Elasticsearch data source the Elasticsearch helper classes should pass validation tests and correctly generate the JSON structure needed to provision a new working alert.

What happened?

Trying to the ElasticsearchTarget in the triggers block of the AlertRulev9 results in the error:

ValueError: triggers must either be a Target or AlertCondition

This appears to be because the validation check is looking for isinstance(Target) or ..., but the class ElasticsearchTarget inherits from Object, not Target.

I have also tried using the core Target class and passing the ElasticsearchTarget in to the target param.
e.g.

Target(
   ...
   target=ElasticsearchTarget( ... ),
   
)

If I do this my code will execute and I can provision the alert. However the resulting alert fails to parse correctly in Grafana with an error:

Failed to execute conditions: [plugin.downstreamError] failed to query data: type assertion to string failed.

How to reproduce it?

AlertRulev9(
    title="My provisioned Alert Rule",
    #condition="B",
    evaluateFor="5m",
    timeRangeFrom=300,
    timeRangeTo=0,
    noDataAlertState=ALERTRULE_STATE_DATA_OK,
    errorAlertState=ALERTRULE_STATE_DATA_ERROR,
    dashboard_uid="${REALLYREALDASBOARDUID}",
    triggers=[
        ElasticsearchTarget(
            refId="A",
            alias="",
            datasource="${VALID_OPENSEARCH_SRC_UID}",
            query = "userIdentity.type:Root"
            bucketAggs=[
                DateHistogramGroupBy(
                    field="@timestamp",
                    minDocCount=0,
                )
            ],
            metricAggs=[CountMetricAgg()],
        timeField="@timestamp",
        ).auto_bucket_agg_ids(),
        ElasticsearchAlertCondition(
            target=estarget,
            evaluator=GreaterThan(0),
            operator=OP_AND,
            reducerType=RTYPE_SUM,
        )
                
    ],
    annotations={
        "foor": "bar",
    },
    labels={
        "environment": "dev",
        "slack": "alerts",
    }
)
@mkotsalainen
Copy link

The same problem affects CloudwatchMetricsTarget.

@JamesGibo
Copy link
Collaborator

JamesGibo commented May 18, 2023

CloudwatchMetricsTarget fixed in #560

Similar fix could be used for Elasticsearch

@xFallingDuskx
Copy link

xFallingDuskx commented Jun 29, 2023

Hi! I noticed that the same issue is still happening. Has there been any progress on a fix? Also, I was wondering where do we specify the datasource UID when using ElasticsearchTarget?

Edit: I see that just changing the type of ElasticsearchTarget to be Target completely solves the issue and allows for the specification of a datasource. Maybe this change, along with the fix for CloudwatchMetricsTarget, can be pushed to the grafanalib package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants