meln5674 / grafana-mongodb-community-plugin

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

How to provision datasource? #26

Closed Urhengulas closed 1 year ago

Urhengulas commented 1 year ago

Hi @meln5674, thank you for the great plugin!

I am using it with docker-compose as you have it in the examples. Now I want to provision the datasource definition so I do not have to configure it manually when I setup the service. Provisioning works by passing grafana a yaml file with the parameters of the datasource. Docs are here: https://grafana.com/docs/grafana/latest/administration/provisioning/.

Unfortunately I cannot get it to work completely. It does register the datasource, but I get a parsing error.

Setup

You can find my code at https://github.com/Urhengulas/emission-meter/tree/4a74cf9a89ae6d9174d0e8adf2f52ebe9ca7270a.

Initially I configured the datasource manually in the UI by setting the URL to mongodb://mongo_db:27017. This works and I am able to connect to and query the database.

Now I am configuring the datasource like this:

# ./grafana/provisioning/datasources/datasource.yml
datasources:
  - name: mongodb-community
    type: meln5674-mongodb-community
    access: proxy
    url: "mongodb://mongo_db:27017"
    isDefault: true
    editable: true

and pass the file to the container through a volume:

# compose.yml
grafana:
  image: grafana/grafana-oss:10.1.5
  environment:
    - "GF_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS=meln5674-mongodb-community"
    - "GF_INSTALL_PLUGINS=https://github.com/meln5674/grafana-mongodb-community-plugin/releases/download/v0.2.0%2Brc4/meln5674-mongodb-community.zip;meln5674-mongodb-community"
  ports:
    - "3000:3000"
  volumes:
    - ./grafana/provisioning:/etc/grafana/provisioning

It successfully registers the datasource and shows up like that:

Screenshot from 2023-10-20 11-24-08

(Note the mongodb://mongo_db:27017)

Error

When I click on the datasource and click "Save & test" I get following error in the UI:

Error while connecting to MongoDB: error parsing uri: scheme must be "mongodb" or "mongodb+srv"

In the docker-compose logs I get following two log lines:

emission-meter-grafana-1 | logger=plugin.meln5674-mongodb-community t=2023-10-20T09:24:48.465383567Z level=info msg="CheckHealth called" request="map[Headers:map[http_X-Datasource-Uid:PD3083DE512941242 http_X-Grafana-Org-Id:1] PluginContext:map[AppInstanceSettings: DataSourceInstanceSettings:map[BasicAuthEnabled:false BasicAuthUser: Database: DecryptedSecureJSONData: ID:1 JSONData:map[] Name:mongodb-community Type:meln5674-mongodb-community UID:PD3083DE512941242 URL:mongodb://mongo_db:27017 Updated:2023-10-20T09:24:48Z User:] OrgID:1 PluginID:meln5674-mongodb-community User:map[Email:admin@localhost Login:admin Name: Role:Admin]]]"

emission-meter-grafana-1 | logger=context userId=1 orgId=1 uname=admin t=2023-10-20T09:24:48.465552069Z level=info msg="Request Completed" method=GET path=/api/datasources/uid/PD3083DE512941242/health status=400 remote_addr=172.22.0.1 time_ms=1 duration=1.555448ms size=130 referer=http://localhost:3000/connections/datasources/edit/PD3083DE512941242 handler=/api/datasources/uid/:uid/health

While it says "Request Completed", which sounds rather positive, the status code is 400.


How to make it work? 😁

Urhengulas commented 1 year ago

Ha, I just checked how the docker-compose log looks like when I configure it manually and set the URL. And it does look a bit different!

emission-meter-grafana-1 | logger=plugin.meln5674-mongodb-community t=2023-10-20T09:50:33.780475076Z level=info msg="CheckHealth called" request="map[Headers:map[http_X-Datasource-Uid:PD3083DE512941242 http_X-Grafana-Org-Id:1] PluginContext:map[AppInstanceSettings: DataSourceInstanceSettings:map[BasicAuthEnabled:false BasicAuthUser: Database: DecryptedSecureJSONData: ID:1 JSONData:map[url:mongodb://mongo_db:27017] Name:mongodb-community Type:meln5674-mongodb-community UID:PD3083DE512941242 URL:mongodb://mongo_db:27017 Updated:2023-10-20T09:50:33Z User:] OrgID:1 PluginID:meln5674-mongodb-community User:map[Email:admin@localhost Login:admin Name: Role:Admin]]]"

Above the log contained JSONData:map[], now it contains JSONData:map[url:mongodb://mongo_db:27017]. So I guess your plugin does not use the URL paramter, but a separate one?!

Urhengulas commented 1 year ago

Following provisioning configuration works!!

apiVersion: 1

datasources:
  - name: mongodb-community
    type: meln5674-mongodb-community
    access: proxy
    # url: "mongodb://mongo_db:27017"
    isDefault: true
    editable: true
    jsonData:
      url: "mongodb://mongo_db:27017"

My problem is therefore solved 😁

On a sidenote: Maybe it makes sense for you to switch to the top-level url parameter? I don't know if there is any advantage, but it seems like the default way to go.