如何为 DataDog 配置此 PostgreSQL 检查?

How do I configure this PostgreSQL check for DataDog?

我尝试使用 DD 代理设置 postgres 检查,但我收到 postgres.py 脚本抛出的错误。正如您在屏幕截图中看到的,我正在使用这个简单的查询来获取与数据库的活动连接数。我把它放在 /etc/datadog-agent/conf.d/postgres.d/conf.yaml 中,像这样:

- metric_prefix: postgresql
     query: SELECT datname as db_name, count(pid) as active_connections FROM pg_stat_activity where state = 'active' group by db_name;
     columns:
       - name: active_connections
         type: gauge
       - name: db_name
         type: tag

我在 运行 配置检查时得到的错误如下:

[root@my_box postgres.d]# datadog-agent check postgres | grep -i -A 20 -B 20  active_connections
Error: postgres:953578488181a512 | (postgres.py:398) | non-numeric value `cldtx` for metric column `active_connections` of metric_prefix `postgresql`

如果我理解正确,conf.yaml 文件用于调用带有特定参数的 postgres.py 脚本。 postgres.py 脚本可以在这里找到: https://github.com/DataDog/integrations-core/blob/master/postgres/datadog_checks/postgres/postgres.py

在这些类型的检查中,响应顺序很重要,因为从数据库返回的列将被映射回 YAML 中指定的名称。

正在阅读错误信息:

Error: postgres:953578488181a512 | (postgres.py:398) | non-numeric value cldtx for metric column active_connections of metric_prefix postgresql

我们可以看到 cldtx 的值正在为 active_connections 列返回,在 YAML 中它被声明为一个 gauge,这是一个字符串。

修复应该很简单,通过重新排序 YAML,如下所示:

...
     columns:
       - name: db_name
         type: tag
       - name: active_connections
         type: gauge

或者,如果您想保持 YAML 有序,请将查询更改为:

...
     query: SELECT count(pid) as active_connections, datname as db_name FROM pg_stat_activity where state = 'active' group by db_name;
...