Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
{
"server_zones": {
"hg.nginx.org": {
"processing": 2,
"requests": 0,
"responses": {"1xx": 0, "2xx": 0, "3xx": 0, "4xx": 0, "5xx": 0, "total": 0},
"discarded": 0,
"received": 0,
"sent": 0
}
}
},
1
))
context.plus_cache.put('test_status', (
{
"server_zones": {
"hg.nginx.org": {
"processing": 2,
"requests": 131714,
"responses": {"1xx": 0, "2xx": 96763, "3xx": 2343, "4xx": 627, "5xx": 17631, "total": 117364},
"discarded": 14348,
"received": 35855843,
"sent": 1542591786
}
}
},
2
))
status_zone_collector.collect()
"fails": 0,
"unavail": 0,
"health_checks": {"checks": 0, "fails": 0, "unhealthy": 0, "last_passed": True},
"downtime": 0,
"downstart": 0,
"selected": 1456184367000
}
]
}
}
}
},
1
))
context.plus_cache.put('test_api', (
{
"http" : {
"upstreams": {
"trac-backend": {
"peers": [
{
"id": 0,
"server": "10.0.0.1:8080",
"backup": False,
"weight": 1,
"state": "up",
"active": 0,
"requests": 50411,
"responses": {"1xx": 0, "2xx": 49034, "3xx": 507, "4xx": 864, "5xx": 6, "total": 50411},
"sent": 22594151,
"received": 2705341138,
def test_old_dummy_data(self):
plus_obj = PlusObject(local_name='test_obj', parent_local_id='nginx123', root_uuid='root123')
# Do a quick override of plus_status_internal_url_cache
plus_obj.plus_status_internal_url_cache = 'test_status'
# Insert some dummy data
context.plus_cache.put('test_status', ({'pluss': {'test_obj': {}}}, 0))
context.plus_cache.put('test_status', ({'pluss': {'test_obj': {'proper': 'data'}}}, 2))
status_collector = PlusStatusCollector(object=plus_obj)
status_collector.last_collect = 1 # Hard set timestamp
data = status_collector.gather_data()
assert_that(data, has_length(1))
assert_that(data, equal_to([({'proper': 'data'}, 2)]))
assert_that(status_collector.last_collect, equal_to(2))
def test_gather_data(self):
upstream = NginxApiHttpUpstreamObject(local_name='trac-backend', parent_local_id='nginx123', root_uuid='root123')
upstream.api_internal_url_cache = 'test_api'
# Get the upstream collector
upstream_collector = upstream.collectors[-1]
context.plus_cache.put('test_api', (
{
"http" : {
"upstreams": {
"trac-backend": {
"peers": [
{
"id": 0,
"server": "10.0.0.1:8080",
"backup": False,
"weight": 1,
"state": "up",
"active": 0,
"requests": 50411,
"responses": {"1xx": 0, "2xx": 49034, "3xx": 507, "4xx": 864, "5xx": 6, "total": 50411},
"sent": 22594151,
"received": 2705341138,
"responses": {"1xx": 100, "2xx": 200, "3xx": 300, "4xx": 400, "5xx": 500, "total": 1500},
"sent": 0,
"received": 0,
"fails": 0,
"unavail": 0,
"health_checks": {"checks": 0, "fails": 0, "unhealthy": 0, "last_passed": True},
"downtime": 0,
"downstart": 0,
"selected": 1456184367000
}
gauges = upstream.statsd.current['gauge']
# drop data with two different peer counts into the plus_cache, then collect the data
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 1}}}, 3))
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 2}}}, 14))
upstream_collector.collect()
assert_that(gauges['plus.upstream.peer.count'], equal_to([(14, 2)]))
# shows that the metric works even if the plus_cache data has been collected before
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 4}}}, 16))
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 2}}}, 20))
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 8}}}, 99))
upstream_collector.collect()
assert_that(gauges['plus.upstream.peer.count'], equal_to([(99, 8)]))
# shows that only peers with state == 'up' count towards upstream.peer.count
test_peer['state'] = 'down'
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 5}}}, 110))
upstream_collector.collect()
assert_that(gauges['plus.upstream.peer.count'], equal_to([(99, 8)])) # doesn't change because state is 'down'
# drop data with two different peer counts into the plus_cache, then collect the data
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 1}}}, 3))
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 2}}}, 14))
upstream_collector.collect()
assert_that(gauges['plus.upstream.peer.count'], equal_to([(14, 2)]))
# shows that the metric works even if the plus_cache data has been collected before
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 4}}}, 16))
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 2}}}, 20))
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 8}}}, 99))
upstream_collector.collect()
assert_that(gauges['plus.upstream.peer.count'], equal_to([(99, 8)]))
# shows that only peers with state == 'up' count towards upstream.peer.count
test_peer['state'] = 'down'
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 5}}}, 110))
upstream_collector.collect()
assert_that(gauges['plus.upstream.peer.count'], equal_to([(99, 8)])) # doesn't change because state is 'down'
test_peer['state'] = 'up'
context.plus_cache.put('test_status', ({"upstreams": {"trac-backend": {"peers": [test_peer] * 2}}}, 120))
upstream_collector.collect()
assert_that(gauges['plus.upstream.peer.count'], equal_to([(120, 2)]))
def test_gather_data(self):
upstream = NginxUpstreamObject(local_name='trac-backend', parent_local_id='nginx123', root_uuid='root123')
upstream.plus_status_internal_url_cache = 'test_status'
# Get the upstream collector
upstream_collector = upstream.collectors[-1]
context.plus_cache.put('test_status', (
{
"upstreams": {
"trac-backend": {
"peers": [
{
"id": 0,
"server": "10.0.0.1:8080",
"backup": False,
"weight": 1,
"state": "up",
"active": 0,
"requests": 50411,
"responses": {"1xx": 0, "2xx": 49034, "3xx": 507, "4xx": 864, "5xx": 6, "total": 50411},
"sent": 22594151,
"received": 2705341138,
"fails": 0,
also here we run plus metrics collection
"""
stamp = int(time.time())
# get plus status body
try:
status = context.http_client.get(self.object.plus_status_internal_url, timeout=1, log=False)
# modify status to move stream data up a level
if 'stream' in status:
status['streams'] = status['stream'].get('server_zones', {})
status['stream_upstreams'] = status['stream'].get('upstreams', {})
# Add the status payload to plus_cache so it can be parsed by other collectors (plus objects)
context.plus_cache.put(self.object.plus_status_internal_url, (status, stamp))
except GreenletExit:
raise
except:
context.log.error('failed to check plus_status url %s' % self.object.plus_status_internal_url)
context.log.debug('additional info', exc_info=True)
status = None
if not status:
return
connections = status.get('connections', {})
requests = status.get('requests', {})
ssl = status.get('ssl', {})
# gauges
self.object.statsd.gauge('nginx.http.conn.active', connections.get('active'))