-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathtest_pgbouncer_integration_e2e.py
197 lines (162 loc) · 6.91 KB
/
test_pgbouncer_integration_e2e.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import psycopg
import pytest
from packaging import version
from datadog_checks.pgbouncer import PgBouncer
from . import common
@pytest.mark.integration
@pytest.mark.usefixtures("dd_environment")
def test_check(instance, aggregator, datadog_agent, dd_run_check):
# add some stats
connection = psycopg.connect(
host=common.HOST,
port=common.PORT,
user=common.USER,
password=common.PASS,
dbname=common.DB,
connect_timeout=1,
)
cur = connection.cursor()
cur.execute('SELECT * FROM persons;')
# run the check
check = PgBouncer('pgbouncer', {}, [instance])
check.check_id = 'test:123'
dd_run_check(check)
env_version = common.get_version_from_env()
assert_metric_coverage(env_version, aggregator)
version_metadata = {
'version.raw': str(env_version),
'version.scheme': 'semver',
'version.major': str(env_version.major),
'version.minor': str(env_version.minor),
'version.patch': str(env_version.micro),
}
datadog_agent.assert_metadata('test:123', version_metadata)
@pytest.mark.integration
@pytest.mark.usefixtures("dd_environment")
def test_check_with_clients(instance, aggregator, datadog_agent, dd_run_check):
# add some stats
connection = psycopg.connect(
host=common.HOST,
port=common.PORT,
user=common.USER,
password=common.PASS,
dbname=common.DB,
connect_timeout=1,
)
cur = connection.cursor()
cur.execute('SELECT * FROM persons;')
instance.update(
{
'collect_per_client_metrics': True,
}
)
# run the check
check_with_clients = PgBouncer('pgbouncer', {}, [instance])
check_with_clients.check_id = 'test:123'
dd_run_check(check_with_clients)
env_version = common.get_version_from_env()
assert_metric_coverage(env_version, aggregator, include_clients=True)
@pytest.mark.integration
@pytest.mark.usefixtures("dd_environment")
def test_check_with_servers(instance, aggregator, datadog_agent, dd_run_check):
# add some stats
connection = psycopg.connect(
host=common.HOST,
port=common.PORT,
user=common.USER,
password=common.PASS,
dbname=common.DB,
connect_timeout=1,
)
cur = connection.cursor()
cur.execute('SELECT * FROM persons;')
instance.update(
{
'collect_per_server_metrics': True,
}
)
# run the check
check_with_servers = PgBouncer('pgbouncer', {}, [instance])
check_with_servers.check_id = 'test:123'
dd_run_check(check_with_servers)
env_version = common.get_version_from_env()
assert_metric_coverage(env_version, aggregator, include_servers=True)
@pytest.mark.integration
def test_critical_service_check(instance, aggregator, dd_run_check):
instance['port'] = '123' # Bad port
check = PgBouncer('pgbouncer', {}, [instance])
with pytest.raises(Exception):
dd_run_check(check)
aggregator.assert_service_check(PgBouncer.SERVICE_CHECK_NAME, status=PgBouncer.CRITICAL)
@pytest.mark.integration
@pytest.mark.usefixtures("dd_environment")
def test_check_with_url(instance_with_url, aggregator, datadog_agent, dd_run_check):
# run the check
check = PgBouncer('pgbouncer', {}, [instance_with_url])
check.check_id = 'test:123'
dd_run_check(check)
env_version = common.get_version_from_env()
assert_metric_coverage(env_version, aggregator)
version_metadata = {
'version.raw': str(env_version),
'version.scheme': 'semver',
'version.major': str(env_version.major),
'version.minor': str(env_version.minor),
'version.patch': str(env_version.micro),
}
datadog_agent.assert_metadata('test:123', version_metadata)
@pytest.mark.e2e
def test_check_e2e(dd_agent_check, instance):
# run the check
aggregator = dd_agent_check(instance, rate=True)
version = common.get_version_from_env()
assert_metric_coverage(version, aggregator)
def assert_metric_coverage(env_version, aggregator, include_clients=False, include_servers=False):
aggregator.assert_metric('pgbouncer.pools.cl_active')
aggregator.assert_metric('pgbouncer.pools.cl_waiting')
aggregator.assert_metric('pgbouncer.pools.sv_active')
aggregator.assert_metric('pgbouncer.pools.sv_idle')
aggregator.assert_metric('pgbouncer.pools.sv_used')
aggregator.assert_metric('pgbouncer.pools.sv_tested')
aggregator.assert_metric('pgbouncer.pools.sv_login')
aggregator.assert_metric('pgbouncer.pools.maxwait')
aggregator.assert_metric('pgbouncer.stats.avg_recv')
aggregator.assert_metric('pgbouncer.stats.avg_sent')
if env_version < version.parse('1.8.0'):
aggregator.assert_metric('pgbouncer.stats.avg_req')
aggregator.assert_metric('pgbouncer.stats.avg_query')
aggregator.assert_metric('pgbouncer.stats.requests_per_second')
else:
aggregator.assert_metric('pgbouncer.pools.maxwait_us')
aggregator.assert_metric('pgbouncer.stats.avg_transaction_time')
aggregator.assert_metric('pgbouncer.stats.avg_query_time')
aggregator.assert_metric('pgbouncer.stats.avg_transaction_count')
aggregator.assert_metric('pgbouncer.stats.avg_query_count')
aggregator.assert_metric('pgbouncer.stats.queries_per_second')
aggregator.assert_metric('pgbouncer.stats.transactions_per_second')
aggregator.assert_metric('pgbouncer.stats.total_transaction_time')
aggregator.assert_metric('pgbouncer.stats.total_wait_time')
aggregator.assert_metric('pgbouncer.stats.avg_wait_time')
aggregator.assert_metric('pgbouncer.stats.total_query_time')
aggregator.assert_metric('pgbouncer.stats.bytes_received_per_second')
aggregator.assert_metric('pgbouncer.stats.bytes_sent_per_second')
aggregator.assert_metric('pgbouncer.databases.pool_size', at_least=0)
aggregator.assert_metric('pgbouncer.databases.max_connections', at_least=0)
aggregator.assert_metric('pgbouncer.databases.current_connections', at_least=0)
aggregator.assert_metric('pgbouncer.max_client_conn')
if include_clients:
aggregator.assert_metric('pgbouncer.clients.connect_time')
aggregator.assert_metric('pgbouncer.clients.request_time')
if env_version >= version.parse('1.8.0'):
aggregator.assert_metric('pgbouncer.clients.wait')
aggregator.assert_metric('pgbouncer.clients.wait_us')
if include_servers:
aggregator.assert_metric('pgbouncer.servers.connect_time')
aggregator.assert_metric('pgbouncer.servers.request_time')
# Service checks
sc_tags = ['host:{}'.format(common.HOST), 'port:{}'.format(common.PORT), 'db:pgbouncer', 'optional:tag1']
aggregator.assert_service_check(PgBouncer.SERVICE_CHECK_NAME, status=PgBouncer.OK, tags=sc_tags)
aggregator.assert_all_metrics_covered()