Skip to content

Commit e6b389d

Browse files
authored
[collector] Only add service check fields if non-null (#3487)
The `api/v1/check_run` endpoint doesn't accept payloads with a `message` value of `null`, so avoid that. Do the same thing on the `tags` and `hostname` fields as it makes sense to be consistent.
1 parent 2ac2811 commit e6b389d

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

checks/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -963,12 +963,17 @@ def create_service_check(check_name, status, tags=None, timestamp=None,
963963
"""
964964
if check_run_id is None:
965965
check_run_id = get_next_id('service_check')
966-
return {
966+
service_check = {
967967
'id': check_run_id,
968968
'check': check_name,
969969
'status': status,
970-
'host_name': hostname,
971-
'tags': tags,
972970
'timestamp': float(timestamp or time.time()),
973-
'message': message
974971
}
972+
if hostname is not None:
973+
service_check['host_name'] = hostname
974+
if tags is not None:
975+
service_check['tags'] = tags
976+
if message is not None:
977+
service_check["message"] = message
978+
979+
return service_check

tests/core/test_common.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,24 @@ def test_service_check(self):
126126
timestamp = time.time()
127127

128128
check = AgentCheck('test', {}, {'checksd_hostname':'foo'})
129-
check.service_check(check_name, status, tags, timestamp, host_name)
129+
# No "message"/"tags" field
130+
check.service_check(check_name, status, timestamp=timestamp, hostname=host_name)
131+
self.assertEquals(len(check.service_checks), 1, check.service_checks)
132+
val = check.get_service_checks()
133+
self.assertEquals(len(val), 1)
134+
check_run_id = val[0].get('id', None)
135+
self.assertNotEquals(check_run_id, None)
136+
self.assertEquals([{
137+
'id': check_run_id,
138+
'check': check_name,
139+
'status': status,
140+
'host_name': host_name,
141+
'timestamp': timestamp,
142+
}], val)
143+
self.assertEquals(len(check.service_checks), 0, check.service_checks)
144+
145+
# With "message" field
146+
check.service_check(check_name, status, tags, timestamp, host_name, message='foomessage')
130147
self.assertEquals(len(check.service_checks), 1, check.service_checks)
131148
val = check.get_service_checks()
132149
self.assertEquals(len(val), 1)
@@ -139,10 +156,11 @@ def test_service_check(self):
139156
'host_name': host_name,
140157
'tags': tags,
141158
'timestamp': timestamp,
142-
'message': None,
159+
'message': 'foomessage',
143160
}], val)
144161
self.assertEquals(len(check.service_checks), 0, check.service_checks)
145162

163+
146164
def test_no_proxy(self):
147165
""" Starting with Agent 5.0.0, there should always be a local forwarder
148166
running and all payloads should go through it. So we should make sure

0 commit comments

Comments
 (0)