vbezgachev / maxctrl_exporter

MaxScale metrics exporter for Prometheus
Apache License 2.0
12 stars 11 forks source link

MAXSCALE_URL not taken from the EnvironmentFile #13

Open taabest6 opened 2 years ago

taabest6 commented 2 years ago

Hi there

We are trying to setup multiple maxctrl_exporters on one host using systemd template unit files.

Therefore we are using environmentfiles like this:

[user@hostname ~]# cat /etc/.maxctrl_exporter.d/maxctrl-instance1
MAXSCALE_EXPORTER_PORT=10503
MAXSCALE_URL=127.0.0.1:8990
# also tried with: "http://localhost:8990" and many others
MAXSCALE_USERNAME=maxctrl_exporter
MAXSCALE_PASSWORD=MySecurePwd

Actual behavior (after systemctl restart): Exporter is scraping MaxScale JSON API at the default port: 127.0.0.1:8989

Log:

2022/08/18 14:27:17 Starting MaxScale exporter
2022/08/18 14:27:17 Scraping MaxScale JSON API at: 127.0.0.1:8989
2022/08/18 14:27:17 Started MaxScale exporter, listening on port: 10503

Expected behavior: MaxScale JSON API should be scraping the MaxScale JSON API at 127.0.0.1:8990.

Interestingly enough:

  1. MAXSCALE_EXPORTER_PORT is taken from the EnvironmentFile without issue. We can change 10503 as we want, and it's taken after the systemctl restart. The fact, that this is working, is leading us to the conclusion, that our general config must be fine.
  2. When we configure the service with ENVIRONMENT directly in the systemd service file, it's working as expected:
# /usr/lib/systemd/system/maxctrl_exporter_cusmsp_dev1.service
Environment=MAXSCALE_URL=127.0.0.1:8994 MAXSCALE_EXPORTER_PORT=10416 MAXSCALE_USERNAME=maxctrl_exporter MAXSCALE_PASSWORD=MySecurePW
ExecStart=/usr/bin/sh -c 'exec /opt/prometheus/maxctrl_exporter/maxctrl_exporter >>

This issue is reffering to line 321: https://github.com/vbezgachev/maxctrl_exporter/blob/dad90f843a17796bac10370fa1ffcdfafc9a096f/maxctrl_exporter.go#L321

ktugan commented 2 years ago

It would be helpful if you could show how you load the environment variables before starting the exporter. An educated guess of mine is that you probably didnt load the environment variables in the correct way.

The behavior of the exporter is working perfectly fine, as you have demonstrated yourself with the systemd file. The line of code is also the canon way to retrieve environment variables: https://pkg.go.dev/os#Getenv. So its highly unlikely that something is wrong in the code and my suspicion stands that the handover/loading of the environment variables is incorrect.

Here a few tips to help you debug/fix it:

  1. Use inline syntax to start the exporter.
    MAXSCALE_USERNAME=maxctrl_exporter MAXSCALE_PASSWORD=MySecurePwd /opt/prometheus/maxctrl_exporter/maxctrl_exporter
  2. Use export other wise. If not inline
    export MAXSCALE_USERNAME=maxctrl_exporter
    export MAXSCALE_PASSWORD=MySecurePwd
    /opt/prometheus/maxctrl_exporter/maxctrl_exporter
  3. Write a 1 liner golang with os.Getenv and print it out. Then try to see which method of env variable setting works for it.