Skip to content

Commit

Permalink
Adhere to code style (#3554)
Browse files Browse the repository at this point in the history
* Adhere to code style

* better
  • Loading branch information
ofek authored Apr 11, 2019
1 parent 3652e49 commit 8c0c536
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 107 deletions.
2 changes: 1 addition & 1 deletion pgbouncer/datadog_checks/pgbouncer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# (C) Datadog, Inc. 2018
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from .pgbouncer import PgBouncer
from .__about__ import __version__
from .pgbouncer import PgBouncer

__all__ = ['PgBouncer', '__version__']
126 changes: 58 additions & 68 deletions pgbouncer/datadog_checks/pgbouncer/pgbouncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,55 @@ class ShouldRestartException(Exception):
class PgBouncer(AgentCheck):
"""Collects metrics from pgbouncer
"""

RATE = AgentCheck.rate
GAUGE = AgentCheck.gauge
DB_NAME = 'pgbouncer'
SERVICE_CHECK_NAME = 'pgbouncer.can_connect'

STATS_METRICS = {
'descriptors': [
('database', 'db'),
],
'descriptors': [('database', 'db')],
'metrics': [
('total_requests', ('pgbouncer.stats.requests_per_second', RATE)), # < 1.8
('total_xact_count', ('pgbouncer.stats.transactions_per_second', RATE)), # >= 1.8
('total_query_count', ('pgbouncer.stats.queries_per_second', RATE)), # >= 1.8
('total_received', ('pgbouncer.stats.bytes_received_per_second', RATE)),
('total_sent', ('pgbouncer.stats.bytes_sent_per_second', RATE)),
('total_query_time', ('pgbouncer.stats.total_query_time', RATE)),
('total_xact_time', ('pgbouncer.stats.total_transaction_time', RATE)), # >= 1.8
('avg_req', ('pgbouncer.stats.avg_req', GAUGE)), # < 1.8
('avg_xact_count', ('pgbouncer.stats.avg_transaction_count', GAUGE)), # >= 1.8
('avg_query_count', ('pgbouncer.stats.avg_query_count', GAUGE)), # >= 1.8
('avg_recv', ('pgbouncer.stats.avg_recv', GAUGE)),
('avg_sent', ('pgbouncer.stats.avg_sent', GAUGE)),
('avg_query', ('pgbouncer.stats.avg_query', GAUGE)), # < 1.8
('avg_xact_time', ('pgbouncer.stats.avg_transaction_time', GAUGE)), # >= 1.8
('avg_query_time', ('pgbouncer.stats.avg_query_time', GAUGE)), # >= 1.8
('total_requests', ('pgbouncer.stats.requests_per_second', RATE)), # < 1.8
('total_xact_count', ('pgbouncer.stats.transactions_per_second', RATE)), # >= 1.8
('total_query_count', ('pgbouncer.stats.queries_per_second', RATE)), # >= 1.8
('total_received', ('pgbouncer.stats.bytes_received_per_second', RATE)),
('total_sent', ('pgbouncer.stats.bytes_sent_per_second', RATE)),
('total_query_time', ('pgbouncer.stats.total_query_time', RATE)),
('total_xact_time', ('pgbouncer.stats.total_transaction_time', RATE)), # >= 1.8
('avg_req', ('pgbouncer.stats.avg_req', GAUGE)), # < 1.8
('avg_xact_count', ('pgbouncer.stats.avg_transaction_count', GAUGE)), # >= 1.8
('avg_query_count', ('pgbouncer.stats.avg_query_count', GAUGE)), # >= 1.8
('avg_recv', ('pgbouncer.stats.avg_recv', GAUGE)),
('avg_sent', ('pgbouncer.stats.avg_sent', GAUGE)),
('avg_query', ('pgbouncer.stats.avg_query', GAUGE)), # < 1.8
('avg_xact_time', ('pgbouncer.stats.avg_transaction_time', GAUGE)), # >= 1.8
('avg_query_time', ('pgbouncer.stats.avg_query_time', GAUGE)), # >= 1.8
],
'query': """SHOW STATS""",
}

POOLS_METRICS = {
'descriptors': [
('database', 'db'),
('user', 'user'),
],
'descriptors': [('database', 'db'), ('user', 'user')],
'metrics': [
('cl_active', ('pgbouncer.pools.cl_active', GAUGE)),
('cl_waiting', ('pgbouncer.pools.cl_waiting', GAUGE)),
('sv_active', ('pgbouncer.pools.sv_active', GAUGE)),
('sv_idle', ('pgbouncer.pools.sv_idle', GAUGE)),
('sv_used', ('pgbouncer.pools.sv_used', GAUGE)),
('sv_tested', ('pgbouncer.pools.sv_tested', GAUGE)),
('sv_login', ('pgbouncer.pools.sv_login', GAUGE)),
('maxwait', ('pgbouncer.pools.maxwait', GAUGE)),
('cl_active', ('pgbouncer.pools.cl_active', GAUGE)),
('cl_waiting', ('pgbouncer.pools.cl_waiting', GAUGE)),
('sv_active', ('pgbouncer.pools.sv_active', GAUGE)),
('sv_idle', ('pgbouncer.pools.sv_idle', GAUGE)),
('sv_used', ('pgbouncer.pools.sv_used', GAUGE)),
('sv_tested', ('pgbouncer.pools.sv_tested', GAUGE)),
('sv_login', ('pgbouncer.pools.sv_login', GAUGE)),
('maxwait', ('pgbouncer.pools.maxwait', GAUGE)),
],
'query': """SHOW POOLS""",
}

DATABASES_METRICS = {
'descriptors': [
('database', 'db'),
],
'descriptors': [('database', 'db')],
'metrics': [
('pool_size', ('pgbouncer.databases.pool_size', GAUGE)),
('max_connections', ('pgbouncer.databases.max_connections', GAUGE)),
('current_connections', ('pgbouncer.databases.current_connections', GAUGE)),
('pool_size', ('pgbouncer.databases.pool_size', GAUGE)),
('max_connections', ('pgbouncer.databases.max_connections', GAUGE)),
('current_connections', ('pgbouncer.databases.current_connections', GAUGE)),
],
'query': """SHOW DATABASES""",
}
Expand All @@ -89,11 +83,7 @@ def _get_service_checks_tags(self, host, port, database_url, tags=None):
host = parsed_url.hostname
port = parsed_url.port

service_checks_tags = [
"host:%s" % host,
"port:%s" % port,
"db:%s" % self.DB_NAME
]
service_checks_tags = ["host:%s" % host, "port:%s" % port, "db:%s" % self.DB_NAME]
service_checks_tags.extend(tags)
service_checks_tags = list(set(service_checks_tags))

Expand Down Expand Up @@ -152,39 +142,31 @@ def _get_connect_kwargs(self, host, port, user, password, database_url):
return {'dsn': database_url}

if not host:
raise CheckException(
"Please specify a PgBouncer host to connect to.")
raise CheckException("Please specify a PgBouncer host to connect to.")

if not user:
raise CheckException(
"Please specify a user to connect to PgBouncer as.")
raise CheckException("Please specify a user to connect to PgBouncer as.")

if host in ('localhost', '127.0.0.1') and password == '':
return { # Use ident method
'dsn': "user={} dbname={}".format(user, self.DB_NAME)
}
# Use ident method
return {'dsn': "user={} dbname={}".format(user, self.DB_NAME)}

if port:
return {'host': host, 'user': user, 'password': password,
'database': self.DB_NAME, 'port': port}
return {'host': host, 'user': user, 'password': password, 'database': self.DB_NAME, 'port': port}

return {'host': host, 'user': user, 'password': password,
'database': self.DB_NAME}
return {'host': host, 'user': user, 'password': password, 'database': self.DB_NAME}

def _get_connection(self, key, host='', port='', user='',
password='', database_url='', tags=None, use_cached=True):
def _get_connection(self, key, host='', port='', user='', password='', database_url='', tags=None, use_cached=True):
"Get and memoize connections to instances"
if key in self.dbs and use_cached:
return self.dbs[key]
try:
connect_kwargs = self._get_connect_kwargs(
host=host, port=port, user=user,
password=password, database_url=database_url
host=host, port=port, user=user, password=password, database_url=database_url
)

connection = pg.connect(**connect_kwargs)
connection.set_isolation_level(
pg.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
connection.set_isolation_level(pg.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

# re-raise the CheckExceptions raised by _get_connect_kwargs()
except CheckException:
Expand All @@ -194,9 +176,12 @@ def _get_connection(self, key, host='', port='', user='',
redacted_url = self._get_redacted_dsn(host, port, user, database_url)
message = u'Cannot establish connection to {}'.format(redacted_url)

self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,
tags=self._get_service_checks_tags(host, port, database_url, tags),
message=message)
self.service_check(
self.SERVICE_CHECK_NAME,
AgentCheck.CRITICAL,
tags=self._get_service_checks_tags(host, port, database_url, tags),
message=message,
)
raise

self.dbs[key] = connection
Expand Down Expand Up @@ -231,17 +216,22 @@ def check(self, instance):
tags = list(set(tags))

try:
db = self._get_connection(key, host, port, user, password, tags=tags,
database_url=database_url, use_cached=use_cached)
db = self._get_connection(
key, host, port, user, password, tags=tags, database_url=database_url, use_cached=use_cached
)
self._collect_stats(db, tags)
except ShouldRestartException:
self.log.info("Resetting the connection")
db = self._get_connection(key, host, port, user, password, tags=tags,
database_url=database_url, use_cached=False)
db = self._get_connection(
key, host, port, user, password, tags=tags, database_url=database_url, use_cached=False
)
self._collect_stats(db, tags)

redacted_dsn = self._get_redacted_dsn(host, port, user, database_url)
message = u'Established connection to {}'.format(redacted_dsn)
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK,
tags=self._get_service_checks_tags(host, port, database_url, tags),
message=message)
self.service_check(
self.SERVICE_CHECK_NAME,
AgentCheck.OK,
tags=self._get_service_checks_tags(host, port, database_url, tags),
message=message,
)
3 changes: 2 additions & 1 deletion pgbouncer/setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from setuptools import setup
from codecs import open
from os import path

from setuptools import setup

HERE = path.abspath(path.dirname(__file__))

# Get version info
Expand Down
18 changes: 4 additions & 14 deletions pgbouncer/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,27 @@
# Licensed under Simplified BSD License (see LICENSE)
import re

from datadog_checks.utils.common import get_docker_hostname
import psycopg2

from datadog_checks.utils.common import get_docker_hostname

HOST = get_docker_hostname()
PORT = '6432'
USER = 'postgres'
PASS = 'datadog'
DB = 'datadog_test'

DEFAULT_INSTANCE = {
'host': HOST,
'port': PORT,
'username': USER,
'password': PASS,
'tags': ['optional:tag1']
}
DEFAULT_INSTANCE = {'host': HOST, 'port': PORT, 'username': USER, 'password': PASS, 'tags': ['optional:tag1']}

INSTANCE_URL = {
'database_url': 'postgresql://datadog:datadog@localhost:16432/datadog_test',
'tags': ['optional:tag1']
}
INSTANCE_URL = {'database_url': 'postgresql://datadog:datadog@localhost:16432/datadog_test', 'tags': ['optional:tag1']}


def get_version():
"""
Retrieve PgBouncer version
"""
regex = r'\d\.\d\.\d'
conn = psycopg2.connect(host=HOST, port=PORT, user=USER, password=PASS,
database='pgbouncer', connect_timeout=1)
conn = psycopg2.connect(host=HOST, port=PORT, user=USER, password=PASS, database='pgbouncer', connect_timeout=1)
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cur = conn.cursor()
cur.execute('SHOW VERSION;')
Expand Down
9 changes: 4 additions & 5 deletions pgbouncer/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import os
from copy import deepcopy

import pytest
import psycopg2
import pytest

from datadog_checks.dev import docker_run, WaitFor
from datadog_checks.dev import WaitFor, docker_run

from . import common

Expand All @@ -20,8 +20,7 @@ def container_up(service_name, port):
Try to connect to postgres/pgbouncer
"""
psycopg2.connect(
host=common.HOST, port=port, user=common.USER, password=common.PASS,
database=common.DB, connect_timeout=2,
host=common.HOST, port=port, user=common.USER, password=common.PASS, database=common.DB, connect_timeout=2
)


Expand All @@ -38,7 +37,7 @@ def dd_environment():
conditions=[
WaitFor(container_up, args=("Postgres", 5432)),
WaitFor(container_up, args=("PgBouncer", common.PORT)),
]
],
):

yield common.DEFAULT_INSTANCE
Expand Down
18 changes: 10 additions & 8 deletions pgbouncer/tests/test_pgbouncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import psycopg2

from datadog_checks.pgbouncer import PgBouncer

from . import common


def test_check(instance, aggregator):
# add some stats
connection = psycopg2.connect(host=common.HOST, port=common.PORT, user=common.USER, password=common.PASS,
database=common.DB, connect_timeout=1)
connection = psycopg2.connect(
host=common.HOST,
port=common.PORT,
user=common.USER,
password=common.PASS,
database=common.DB,
connect_timeout=1,
)
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cur = connection.cursor()
cur.execute('SELECT * FROM persons;')
Expand Down Expand Up @@ -57,10 +64,5 @@ def test_check(instance, aggregator):
aggregator.assert_metric('pgbouncer.databases.current_connections')

# Service checks
sc_tags = [
'host:{}'.format(common.HOST),
'port:{}'.format(common.PORT),
'db:pgbouncer',
'optional:tag1',
]
sc_tags = ['host:{}'.format(common.HOST), 'port:{}'.format(common.PORT), 'db:pgbouncer', 'optional:tag1']
aggregator.assert_service_check('pgbouncer.can_connect', status=PgBouncer.OK, tags=sc_tags)
11 changes: 1 addition & 10 deletions pgbouncer/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ minversion = 2.0
basepython = py37
envlist =
py{27,37}-{1.7,1.8}
flake8

[testenv]
dd_check_style = true
usedevelop = true
platform = linux|darwin|win32
passenv =
Expand All @@ -20,12 +20,3 @@ commands =
setenv =
1.7: PGBOUNCER_VERSION=1_7
1.8: PGBOUNCER_VERSION=1_8

[testenv:flake8]
skip_install = true
deps = flake8
commands = flake8 .

[flake8]
exclude = .eggs,.tox,build
max-line-length = 120

0 comments on commit 8c0c536

Please sign in to comment.