trinodb / grafana-trino

The Trino datasource allows to query and visualize Trino data from within Grafana.
Apache License 2.0
35 stars 10 forks source link

Grafana dashboard Variables are not interpolated #256

Open kikillus opened 4 months ago

kikillus commented 4 months ago

Hi, we are using Trino 428 with the official Trino datasource provided by Grafana in version 1.0.6

We have set some Dashboard variables in Grafana, e.g. variable1. However, using them in a Query they do not seem to be replaced.

A sample Query like the following

SELECT
    *
FROM
    table
WHERE
    $__timefilter(datetime) AND
    column1 = $variable1

Fails with (line no. won't match, above example) error querying the database: trino: query failed (200 OK): "io.trino.sql.parser.ParsingException: line 11:19: mismatched input '$'. Expecting: 'ALL', 'ANY', 'SOME', <expression>"

The query actually sent to trino looks like the following.

SELECT
    *
FROM
    table
WHERE
    datetime BETWEEN TIMESTAMP '2024-02-01 13:00:00' AND TIMESTAMP  '2024-02-01 13:59:59'  AND
    column1 = $variable1

The timefilter Macro seems to be applied correctly, while the dashboard variable does not get replaced.

We have contacted the Support by Grafana, they said the problem was within the trino plugin as using other datasources the variables are interpolated correctly.

maczikasz commented 2 months ago

I just faced this bug. The reason for this is that the variables.ts does not properly resolve variables and the rawSql property is undefined. This causes the variable query to fail silently and this brings down the whole dashboard.

nineinchnick commented 2 months ago

@maczikasz could you open a pull request with a fix?

maczikasz commented 2 months ago

@nineinchnick Yes, I am actively working on it, it should come today or tomorrow. Do you reckon if we manage to fix it, you can release a new version of it soon-ish?

Just asking to know if we should also work on integrating the fix into our product, or rather wait for a release

nineinchnick commented 2 months ago

Yes, I can do a release quickly and it takes a day or two to get it approved by Grafana

maczikasz commented 2 months ago

Ok so I have done more digging into it and have concluded it's not a bug per se.

In our case, what happens is that, we use Athena and Trino datasources for different deployments, as a drop in replacement.

In the Athena datasource they implemented CustomVariableSupport with a generic type parameter of the same type that they use to represent queries for the panels.

The Trino datasource implements StandardVariableSupport that has a different structure to the class type that is used to represent queries in panels.

So in the Athena case we have a rawSQL property in the generated JSONModel and for the Trino case we need a query property. This causes the rawSQL to be replaced with the missing query property, that is empty, causing trino to execute an empty query and then replacing everything with ""

I suspect that the OP may have a similar situation, but without seeing their JSON model, I cannot confirm that.

This means there is no fix to the Trino plugin to be done, as it works as Grafana expects it to work, but maybe something to keep in mind if CustomVariableSupport would be implemented

nineinchnick commented 2 months ago

I'm not sure if I get the relationship with Athena. You're saying that the same dashboards cannot be reused with different data source plugins?

maczikasz commented 2 months ago

The relationship with Athena is that Athena uses Trino, and hence depending on whether we are running our product in AWS or not we use either or the other as a data source.

We can reuse most of the dashboards, except the one with the query variable for the reason mentioned above.

If you look into their code : https://github.com/grafana/athena-datasource/blob/main/src/variables.ts They just basically treat the variables as they treat any other query object.

Of course this is a speciality for our use case