Skip to content

Commit

Permalink
[postgres] Fix unix socket connection with psycopg2 (#2968)
Browse files Browse the repository at this point in the history
psycopg2 can connect to a unix socket by passing the directory to the
socket to the `host` parameter of its `connect` function
  • Loading branch information
olivielpeau committed Oct 27, 2016
1 parent ca92d1a commit 5926c80
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 6 additions & 2 deletions checks.d/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
TABLE_COUNT_LIMIT = 200

def psycopg2_connect(*args, **kwargs):
del kwargs['ssl']
if 'ssl' in kwargs:
del kwargs['ssl']
if 'unix_sock' in kwargs:
kwargs['host'] = kwargs['unix_sock']
del kwargs['unix_sock']
return psycopg2.connect(*args, **kwargs)


Expand Down Expand Up @@ -605,7 +609,7 @@ def get_connection(self, key, host, port, user, password, dbname, ssl, connect_f
elif host.startswith('/'):
# If the hostname starts with /, it's probably a path
# to a UNIX socket. This is similar behaviour to psql
connection = pg.connect(unix_sock=host, user=user,
connection = connect_fct(unix_sock=host, user=user,
password=password, database=dbname)
else:
connection = connect_fct(host=host, user=user, password=password,
Expand Down
9 changes: 5 additions & 4 deletions conf.d/postgres.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ instances:
# - optional_tag2

# Connect using a UNIX socket (host must begin with a /)
# If `use_psycopg2` is enabled, use the directory of the UNIX socket (ex: `/run/postgresql/`)
# - host: /run/postgresql/.s.PGSQL.5433
# dbname: db_name

Expand All @@ -24,7 +25,7 @@ instances:
# - my_table
# - my_other_table
#
# By default all schemas are included. To track relations from specific schemas only,
# By default all schemas are included. To track relations from specific schemas only,
# use the following syntax:
#
# relations:
Expand All @@ -37,10 +38,10 @@ instances:

# Custom metrics
# Below are some examples of commonly used metrics, which are implemented as custom metrics.
# Uncomment them if you want to use them as is, or use as an example for creating your own custom metrics.
# Uncomment them if you want to use them as is, or use as an example for creating your own custom metrics.
# The format for describing custome metrics is identical with the one used for common metrics in postgres.py
# Be extra careful with ensuring proper custom metrics description format. If your custom metric does not work
# after an agent restart, look for errors in the output of "/etc/init.d/datadog-agent info" command, as well as
# Be extra careful with ensuring proper custom metrics description format. If your custom metric does not work
# after an agent restart, look for errors in the output of "/etc/init.d/datadog-agent info" command, as well as
# /var/log/datadog/collector.log file.
#
# custom_metrics:
Expand Down

0 comments on commit 5926c80

Please sign in to comment.