meln5674 / grafana-mongodb-community-plugin

Open Source Grafana Plugin for querying MongoDB
GNU Affero General Public License v3.0
130 stars 17 forks source link

Grafana frontend error with aggregate operator `$toString` #15

Open yar2001 opened 1 year ago

yar2001 commented 1 year ago

I encountered an error when using some aggregate like this. The error occurred in Grafana frontend before sending any query to the backend.

{
    "$project": {
      "foo": {"$toString": "$_id"},
      "_id": 0
    }
}
PanelQueryRunner Error TypeError: Cannot read properties of undefined (reading 'value')
    at template_srv.ts:303:66
    at [Symbol.replace] (<anonymous>)
    at String.replace (<anonymous>)
    at Object.replace (template_srv.ts:278:19)
    at g.value (datasource.ts:25:52)
    at DataSourceWithBackend.ts:143:17
    at Array.map (<anonymous>)
    at g.query (DataSourceWithBackend.ts:120:29)
    at g.value (datasource.ts:33:7)
    at runRequest.ts:185:73

I’m using the v0.2.0+rc3 version and everything else works great except for this issue. It seems that the problem is related to the toString keyword. Whenever the field name contains the word toString, it will throw this error.

meln5674 commented 1 year ago

Sorry for the delayed response. This appears to be related to the variable replacement syntax, which, normally will leave "variables" alone if no variable with that name is defined, but given that this happens in Javascript, everything has a toString. I don't have a good solution right now, and I've not had a whole lot of time to look into it.

Unfortunately, it seems that grafana doesn't support any sort of escaping: https://github.com/grafana/grafana/issues/40603 . I'll likely need to add an option to disable variable replacements for these cases.

As crazy as it sounds, you could try actually making a variable named "toString", and setting it to "$toString" and see if the replacement works.

meln5674 commented 1 year ago

After spending some time investigating, this appears to be a bug within Grafana itself. I've submitted a bug which github has so helpfully linked above this post. Once that is closed, this issue should be resolved. I'll leave this open until then in case there are further developments.

mbaeuerle commented 1 year ago

One admittedly rather unconventional workaround is to create a constant dollar=$ which is then prepended to toString: ${dollar}toString. Source: https://community.grafana.com/t/escaping-a-dollar-sign-so-it-doesnt-get-treated-as-a-variable/67941/7

gmazoni commented 11 months ago

I am using https://www.mongodb.com/docs/manual/reference/operator/aggregation/convert/