From 11b7cefa15062ef21769e373d28a156b861db9cc Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sat, 10 Jun 2017 23:35:23 +0200 Subject: Add password as a connection option --- prometheus_pgbouncer_exporter/cli.py | 13 ++++++++++--- prometheus_pgbouncer_exporter/utils.py | 9 ++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/prometheus_pgbouncer_exporter/cli.py b/prometheus_pgbouncer_exporter/cli.py index 4f55f1e..91bbd0a 100644 --- a/prometheus_pgbouncer_exporter/cli.py +++ b/prometheus_pgbouncer_exporter/cli.py @@ -74,6 +74,12 @@ def main(): help="User to connect to pgbouncer with", env_var='PGBOUNCER_USER', ) + p.add( + '--pgbouncer-password', + default=None, + help="Password to connect to pgbouncer with", + env_var='PGBOUNCER_PASSWORD', + ) p.add( '--pgbouncer-host', default=None, @@ -106,9 +112,10 @@ def main(): logging.info(p.format_values()) connection = get_connection( - options.pgbouncer_user, - options.pgbouncer_port, - options.pgbouncer_host, + user = options.pgbouncer_user, + port = options.pgbouncer_port, + host = options.pgbouncer_host, + password = options.pgbouncer_password, ) REGISTRY.register(StatsCollector( diff --git a/prometheus_pgbouncer_exporter/utils.py b/prometheus_pgbouncer_exporter/utils.py index 7f9aae0..3a99695 100644 --- a/prometheus_pgbouncer_exporter/utils.py +++ b/prometheus_pgbouncer_exporter/utils.py @@ -14,12 +14,11 @@ import psycopg2 -def get_connection(user, port, host): +def get_connection(user=None, port=None, host=None, dbname='pgbouncer', password=None): + kwargs = { 'user': user, 'port': port, 'host': host, 'dbname': dbname, 'password': password } + kwargs = dict([(k, v) for k, v in kwargs.iteritems() if v]) connection = psycopg2.connect( - database='pgbouncer', - user=user, - port=port, - host=host, + **kwargs ) # pgbouncer does not support transactions (as it does not make sense to), -- cgit v1.2.3 From 730f895edee9a6201741b4e511c2572fcb56649d Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sat, 10 Jun 2017 23:45:51 +0200 Subject: Add one test, at least --- prometheus_pgbouncer_exporter/utils.py | 2 +- tests/test_connection.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/test_connection.py diff --git a/prometheus_pgbouncer_exporter/utils.py b/prometheus_pgbouncer_exporter/utils.py index 3a99695..ea69f46 100644 --- a/prometheus_pgbouncer_exporter/utils.py +++ b/prometheus_pgbouncer_exporter/utils.py @@ -16,7 +16,7 @@ import psycopg2 def get_connection(user=None, port=None, host=None, dbname='pgbouncer', password=None): kwargs = { 'user': user, 'port': port, 'host': host, 'dbname': dbname, 'password': password } - kwargs = dict([(k, v) for k, v in kwargs.iteritems() if v]) + kwargs = dict([(k, v) for k, v in kwargs.items() if v]) connection = psycopg2.connect( **kwargs ) diff --git a/tests/test_connection.py b/tests/test_connection.py new file mode 100644 index 0000000..1c0f4ea --- /dev/null +++ b/tests/test_connection.py @@ -0,0 +1,12 @@ +import unittest + +from unittest.mock import patch, Mock + +from prometheus_pgbouncer_exporter import utils + +class ConnectionTest(unittest.TestCase): + @patch('prometheus_pgbouncer_exporter.utils.psycopg2.connect') + def test_get_connection_with_password_works(self, connect): + conn = utils.get_connection(host='/tmp/', dbname='template1') + connect.assert_called_once_with(host='/tmp/', dbname='template1') + connect.return_value.set_session.assert_called_once_with(autocommit=True) -- cgit v1.2.3 From 07481a34407d6ad98bebaa12127b86c352a04752 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sat, 10 Jun 2017 23:48:06 +0200 Subject: Add limitation to only install with python3 or up --- setup.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/setup.py b/setup.py index 3035917..c7971e5 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,9 @@ +import sys from setuptools import setup, find_packages +if sys.version_info.major < 3: + raise RuntimeError('Installing requires Python 3 or newer') + setup( name="prometheus-pgbouncer-exporter", packages=find_packages(), -- cgit v1.2.3 From 4884e2374cb1a5fa70c882cb985d2b53cb07e48e Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sat, 10 Jun 2017 23:50:59 +0200 Subject: Add gitlab-ci.yml --- .gitlab-ci.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..42ee908 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,10 @@ +image: python:3.3 + +before_script: + - python3 -v + - which python + - pip3 install psycopg2 + +unittest: + script: + - python3 -m unittest discover tests -- cgit v1.2.3 From b6ee32324517afe3f35e5c85b95b924718fff9af Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sat, 10 Jun 2017 23:56:09 +0200 Subject: Moar tests for get_connection --- tests/test_connection.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/test_connection.py b/tests/test_connection.py index 1c0f4ea..d4fb134 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -5,8 +5,14 @@ from unittest.mock import patch, Mock from prometheus_pgbouncer_exporter import utils class ConnectionTest(unittest.TestCase): + @patch('prometheus_pgbouncer_exporter.utils.psycopg2.connect') - def test_get_connection_with_password_works(self, connect): + def test_get_connection_with_host_and_dbname(self, connect): conn = utils.get_connection(host='/tmp/', dbname='template1') connect.assert_called_once_with(host='/tmp/', dbname='template1') connect.return_value.set_session.assert_called_once_with(autocommit=True) + + @patch('prometheus_pgbouncer_exporter.utils.psycopg2.connect') + def test_get_connection_with_old_args_plus_password_works(self, connect): + conn = utils.get_connection(user='pablo', port=5432, host='localhost', password='mypassword') + connect.assert_called_once_with(dbname='pgbouncer', user='pablo', port=5432, host='localhost', password='mypassword') -- cgit v1.2.3 From f78044bed6634ef016d6926d22c2395357fea01a Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sat, 10 Jun 2017 23:58:41 +0200 Subject: Add install stage to ci config --- .gitlab-ci.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 42ee908..f5f84d6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,6 +5,14 @@ before_script: - which python - pip3 install psycopg2 +stages: + - unittest + - install + unittest: script: - python3 -m unittest discover tests + +install: + script: + - python3 setup.py install -- cgit v1.2.3 From f4ff6f18d8eae8247969ddc4c0fc63733c2a8fe3 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sun, 11 Jun 2017 00:01:57 +0200 Subject: Lets try stages this way --- .gitlab-ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f5f84d6..9750b1e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,12 +7,14 @@ before_script: stages: - unittest - - install + - integration unittest: + stage: test script: - python3 -m unittest discover tests install: + stage: integration script: - python3 setup.py install -- cgit v1.2.3 From 97fec750571abbc22cf3682952b4d6ed748fbff1 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sun, 11 Jun 2017 00:03:04 +0200 Subject: Silly me, wrong stage defined --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9750b1e..2ccae85 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,7 +10,7 @@ stages: - integration unittest: - stage: test + stage: unittest script: - python3 -m unittest discover tests -- cgit v1.2.3 From 3f5936b94df8c8fd271b74c60a356491ab419c05 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sun, 11 Jun 2017 00:08:58 +0200 Subject: Add a check for integration to print the version --- .gitlab-ci.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2ccae85..e18739b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,16 +5,11 @@ before_script: - which python - pip3 install psycopg2 -stages: - - unittest - - integration - unittest: - stage: unittest script: - python3 -m unittest discover tests install: - stage: integration script: - python3 setup.py install + - promtheus-pgbouncer-exporter --version -- cgit v1.2.3 From 3226f78614f875b8cf8897024e9a657556575325 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sun, 11 Jun 2017 00:17:51 +0200 Subject: Use the right name for the executable --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e18739b..25eafaf 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,4 +12,4 @@ unittest: install: script: - python3 setup.py install - - promtheus-pgbouncer-exporter --version + - prometheus-pgbouncer-exporter --version -- cgit v1.2.3 From ee467de62f4dab8aa9804abaeb61db563aa4022b Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sun, 11 Jun 2017 00:18:37 +0200 Subject: Bump version up --- prometheus_pgbouncer_exporter/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus_pgbouncer_exporter/__init__.py b/prometheus_pgbouncer_exporter/__init__.py index 45b81bf..923225e 100644 --- a/prometheus_pgbouncer_exporter/__init__.py +++ b/prometheus_pgbouncer_exporter/__init__.py @@ -1,3 +1,3 @@ __ver_major__ = 1 -__ver_minor__ = 7 +__ver_minor__ = 8 __version__ = "%d.%d" % (__ver_major__, __ver_minor__) -- cgit v1.2.3 From bfd853d2d232445444fc8bf36bc407cda66cbec6 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sun, 11 Jun 2017 00:22:19 +0200 Subject: Add requirement to setup.py --- setup.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/setup.py b/setup.py index c7971e5..315b854 100644 --- a/setup.py +++ b/setup.py @@ -12,4 +12,8 @@ setup( 'prometheus-pgbouncer-exporter = prometheus_pgbouncer_exporter.cli:main', ], }, + install_requires=[ + 'psycopg2', + 'ConfigArgParse', + ], ) -- cgit v1.2.3 From 0a626be4cdc8eee651c008724559f338d47347c4 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Date: Sun, 11 Jun 2017 00:24:57 +0200 Subject: Finish adding installation dependencies --- .gitlab-ci.yml | 2 +- setup.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 25eafaf..8ec7a11 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,4 +12,4 @@ unittest: install: script: - python3 setup.py install - - prometheus-pgbouncer-exporter --version + - /usr/local/bin/prometheus-pgbouncer-exporter --version diff --git a/setup.py b/setup.py index 315b854..72e2f24 100644 --- a/setup.py +++ b/setup.py @@ -15,5 +15,6 @@ setup( install_requires=[ 'psycopg2', 'ConfigArgParse', + 'prometheus_client', ], ) -- cgit v1.2.3