percona / mongodb_exporter

A Prometheus exporter for MongoDB including sharding, replication and storage engines
Apache License 2.0
1.18k stars 423 forks source link

Error: context deadline exceeded #767

Closed olegkron closed 10 months ago

olegkron commented 10 months ago

Hi everyone!

Getting context deadline exceeded when trying to connect from mongodb_exporter to my Mongodb docker container.

services: mongodb: image: mongo:7.0 container_name: mongodb restart: unless-stopped ports:

Mongodb root user permissions (accessed in mongodb container with mongosh)

admin> db.auth("root","abcd")
{ ok: 1 }
admin> db.getUsers()
{
  users: [
    {
      _id: 'admin.root',
      userId: UUID('dbb05153-b47e-4743-a01e-d5d8fb4de3f5'),
      user: 'root',
      db: 'admin',
      roles: [ { role: 'root', db: 'admin' } ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    }
  ],
  ok: 1
}

My docker network (Output of docker network inspect abcd_network):

[
    {
        "Name": "abcd-network",
        "Id": "2700cf8f7cf4b508aaa96f0adfe5cce79364feaa072db6478a91d820bd6c2655",
        "Created": "2023-12-20T22:58:42.905836216Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "83d77673c602c276fd4fad665ad80c5a71faaf6b3de30f5b7ec5947b658e6988": {
                "Name": "mongodb-exporter",
                "EndpointID": "6779d819ba900ead7ca11715c859053b06e71129ddd28ef3959b438854e47b63",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "de7ef6dd3c2ca6b64815d9c9544d5b6b50b61849ba1d2a62ed7a07986ca86a47": {
                "Name": "mongodb",
                "EndpointID": "2cdef5a91334bba45beb96b12ebbbecc3771dbf0631b0ce185ef60d44cf111c0",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

mongodb logs (Getting 'connection ended' from mongodb-exporter internal IP)

{"t":{"$date":"2023-12-21T20:16:45.782+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn4692","msg":"Connection ended","attr":{"remote":"172.18.0.3:33600","uuid":{"uuid":{"$uuid":"e10155f8-a85a-4b33-aa85-b2ed9b2e96b8"}},"connectionId":4692,"connectionCount":9}}
{"t":{"$date":"2023-12-21T20:16:45.783+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn4691","msg":"Connection ended","attr":{"remote":"172.18.0.3:33592","uuid":{"uuid":{"$uuid":"7e0da3a8-8d9e-4341-bc2c-1188a8ce2c27"}},"connectionId":4691,"connectionCount":8}}
{"t":{"$date":"2023-12-21T20:16:45.783+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn4693","msg":"Connection ended","attr":{"remote":"172.18.0.3:33610","uuid":{"uuid":{"$uuid":"f5173e84-fb04-43f7-a773-2072a12de7ba"}},"connectionId":4693,"connectionCount":7}}

mongodb-exporter logs, where I get the context deadline exceeded error:

abcd@abcd-ubuntu:~/abcd-backend/mongodb$ docker logs mongodb-exporter
time="2023-12-21T18:06:52Z" level=debug msg="Compatible mode: false"
time="2023-12-21T18:06:52Z" level=debug msg="Connection URI: mongodb://root:abcd@mongodb:27017/admin"
level=info ts=2023-12-21T18:06:52.313Z caller=tls_config.go:274 msg="Listening on" address=[::]:9216
level=info ts=2023-12-21T18:06:52.314Z caller=tls_config.go:277 msg="TLS is disabled." http2=false address=[::]:9216
time="2023-12-21T18:06:59Z" level=error msg="Cannot connect to MongoDB: cannot connect to MongoDB: server selection error: context deadline exceeded, current topology: { Type: Single, Servers: [{ Addr: mongodb:27017, Type: Unknown, Last error: dial tcp 172.18.0.2:27017: connect: connection refused }, ] }"

Thank you in advance for helping me out!

olegkron commented 10 months ago

Just realised that the error was related to the deadline I have set in docker-compose: - '--mongodb.connect-timeout-ms=20000'

After removing the line everything works!