0

I'm using a grafana docker container, and a Postgresql docker container. I think I manage to add a data source to Grafana, however, the Plugin health check fails as in the picture below. Also, when I try to query I get a connection error...

[![Bookmark Bar Switcher here.][1]][1]

I've already checked, and the user I created in the sql db is a superuser, so it has the permission to query the table. In my python script, I'm using the grafana http api to add the datasource.

datasource = {
            "name": "postgres",
            "type": "postgres",
            "host": f"http://{database.config.host}",
            "database": database.config.database,
            "user": database.config.user,
            "password": database.config.password,
            "access": "proxy",
            "port": database.config.port,
            "sslmode": "disable",
        }
        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.api_key}",
        }
        response = requests.post(
            self.grafana_url + self.API_DATASOURCES,
            json=datasource,
            headers=headers,
            timeout=2,
        )

My logs, state:

logger=context t=2023-05-24T23:47:11.909546523Z level=warn msg="failed to look up session from cookie" error="user token not found"
logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:11.909856964Z level=warn msg=Unauthorized error="user token not found" remote_addr=192.168.16.1 traceID=
logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:11.909908982Z level=info msg="Request Completed" method=GET path=/api/live/ws status=401 remote_addr=192.168.16.1 time_ms=0 duration=790.303µs size=40 referer= handler=/api/live/ws
logger=context t=2023-05-24T23:47:24.906185083Z level=warn msg="failed to look up session from cookie" error="user token not found"
logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:24.906382401Z level=warn msg=Unauthorized error="user token not found" remote_addr=192.168.16.1 traceID=
logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:24.906419291Z level=info msg="Request Completed" method=GET path=/api/live/ws status=401 remote_addr=192.168.16.1 time_ms=0 duration=416.706µs size=40 referer= handler=/api/live/ws
logger=context t=2023-05-24T23:47:26.901251349Z level=warn msg="failed to look up session from cookie" error="user token not found"
logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:26.901763659Z level=warn msg=Unauthorized error="user token not found" remote_addr=192.168.16.1 traceID=
logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:26.901862131Z level=info msg="Request Completed" method=GET path=/api/live/ws status=401 remote_addr=192.168.16.1 time_ms=1 duration=1.014872ms size=40 referer= handler=/api/live/ws
logger=context t=2023-05-24T23:47:33.749570154Z level=warn msg="failed to look up session from cookie" error="user token not found"
logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:33.749850255Z level=info msg="Request Completed" method=GET path=/connections/your-connections/datasources/edit/edec663f-a7cc-4316-9aa1-d672dee7d717 status=302 remote_addr=192.168.16.1 time_ms=0 duration=523.514µs size=29 referer=http://localhost:3000/login handler=/connections/your-connections/datasources/edit/*
logger=context t=2023-05-24T23:47:33.751853587Z level=warn msg="failed to look up session from cookie" error="user token not found"
logger=context t=2023-05-24T23:47:36.899061961Z level=warn msg="failed to look up session from cookie" error="user token not found"
logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:36.899246767Z level=warn msg=Unauthorized error="user token not found" remote_addr=192.168.16.1 traceID=
logger=context userId=0 orgId=0 uname= t=2023-05-24T23:47:36.899285702Z level=info msg="Request Completed" method=GET path=/api/live/ws status=401 remote_addr=192.168.16.1 time_ms=0 duration=439.616µs size=40 referer= handler=/api/live/ws
logger=context t=2023-05-24T23:47:42.741638708Z level=warn msg="failed to look up session from cookie" error="user token not found"
logger=http.server t=2023-05-24T23:47:42.75135848Z level=info msg="Successful Login" User=admin@localhost
logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:47:42.894368364Z level=info msg="Request Completed" method=GET path=/api/live/ws status=-1 remote_addr=192.168.16.1 time_ms=0 duration=841.72µs size=0 referer= handler=/api/live/ws
logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:47:42.912548509Z level=info msg="Request Completed" method=GET path=/api/datasources/uid/edec663f-a7cc-4316-9aa1-d672dee7d717 status=404 remote_addr=192.168.16.1 time_ms=0 duration=461.639µs size=35 referer=http://localhost:3000/connections/your-connections/datasources/edit/edec663f-a7cc-4316-9aa1-d672dee7d717 handler=/api/datasources/uid/:uid
logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:47:42.946075661Z level=info msg="Request Completed" method=GET path=/api/datasources/edec663f-a7cc-4316-9aa1-d672dee7d717 status=400 remote_addr=192.168.16.1 time_ms=0 duration=341.626µs size=27 referer=http://localhost:3000/connections/your-connections/datasources/edit/edec663f-a7cc-4316-9aa1-d672dee7d717 handler=/api/datasources/:id
logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:47:56.90833634Z level=info msg="Request Completed" method=GET path=/api/live/ws status=-1 remote_addr=192.168.16.1 time_ms=1 duration=1.658ms size=0 referer= handler=/api/live/ws
logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:48:00.997970403Z level=error msg="Plugin health check failed" error="failed to check plugin health: health check failed" remote_addr=192.168.16.1 traceID=
logger=context userId=1 orgId=1 uname=newuser t=2023-05-24T23:48:00.998018152Z level=error msg="Request Completed" method=GET path=/api/datasources/uid/fb4800be-7c42-40fb-be33-f3e0d87ebaab/health status=500 remote_addr=192.168.16.1 time_ms=0 duration=623.119µs size=53 referer=http://localhost:3000/connections/your-connections/datasources/edit/fb4800be-7c42-40fb-be33-f3e0d87ebaab handler=/api/datasources/uid/:uid/health
An old man in the sea.
  • 1,169
  • 1
  • 13
  • 30
  • 1
    @DaleK because it's 1am, where I live... :D – An old man in the sea. May 25 '23 at 00:05
  • Most likely your plugin doesn't connect to db. Recheck configuration of db connection: quite possibly something wrong there. One of more popular error: [use of localhost](https://stackoverflow.com/q/24319662/21363224) to connect two containers. – markalex May 25 '23 at 04:31
  • I'm adding some more information in this github discussion. https://github.com/grafana/grafana/discussions/69047 Not sure if I should add it here as well... @markalex thanks for the comment, I think what you said is part of the problem, yes. ;) – An old man in the sea. May 25 '23 at 08:44

1 Answers1

0

For future reference, The http payload:

{"name": "PostgreSQLPython", "type": "postgres", "url": "local_postgres:5432", "user": "postgres_user_fake", "database": "postgres_db_name_fake", "basicAuth": false, "access": "proxy", "withCredentials": false, "isDefault": true, "jsonData": {"tlsAuth": false, "connMaxLifetime": 14400, "database": "postgres_db_name_fake", "maxIdleConns": 100, "maxIdleConnsAuto": true, "maxOpenConns": 100, "sslmode": "disable", "postgresVersion": 1500}, "secureJsonData": {"password": "postgres_password_fake"}}

Notice the url doesn't have http://... in some forums they recommended it, but it seems that for Grafana 9.3 and above it's no longer needed.

headers = { 'Accept': 'application/json', "Content-Type": "application/json", "Authorization": f"Bearer {self.api_key}", }

AND...

For docker containers we need to use the alias. Thanks to the comment above.

After reading the comment above, I edited the

And then we reach Elysium... And then we reach Elysium...

An old man in the sea.
  • 1,169
  • 1
  • 13
  • 30