Skip to content

Commit

Permalink
Return JSON instead of Python objects from get_diagnoses()
Browse files Browse the repository at this point in the history
  • Loading branch information
alopezz committed Jul 21, 2023
1 parent 7e8bc03 commit f591625
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
6 changes: 3 additions & 3 deletions datadog_checks_base/datadog_checks/base/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,14 +1023,14 @@ def get_warnings(self):
return warnings

def get_diagnoses(self):
# type: () -> List[Diagnosis.Result]
# type: () -> str
"""
Return the list of diagnosis.
Return the list of diagnosis as a JSON encoded string.
The agent calls this method to retrieve diagnostics from integrations. This method
runs explicit diagnostics if available.
"""
return self.diagnosis.diagnoses + self.diagnosis.run_explicit()
return json.dumps([d._asdict() for d in (self.diagnosis.diagnoses + self.diagnosis.run_explicit())])

def _get_requests_proxy(self):
# type: () -> ProxySettings
Expand Down
54 changes: 37 additions & 17 deletions datadog_checks_base/tests/base/utils/test_diagnose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

import json

from datadog_checks.base import AgentCheck
from datadog_checks.base.utils.diagnose import Diagnosis

Expand Down Expand Up @@ -60,22 +62,22 @@ def explicit_diagnostic(self):
check = Foo("foo", {}, [{}])

# When the check hasn't yet run once, we should only see the explicit diagnostic results
assert check.get_diagnoses() == [
Diagnosis.Result(Diagnosis.DIAGNOSIS_FAIL, "foo check", "explicit diagnosis", None, None, None, None),
assert get_diagnoses(check) == [
diagnose_dict(Diagnosis.DIAGNOSIS_FAIL, "foo check", "explicit diagnosis"),
]

check.run()
assert check.get_diagnoses() == [
Diagnosis.Result(Diagnosis.DIAGNOSIS_SUCCESS, "foo check", "in-check diagnosis", None, None, None, None),
Diagnosis.Result(Diagnosis.DIAGNOSIS_FAIL, "foo check", "explicit diagnosis", None, None, None, None),
assert get_diagnoses(check) == [
diagnose_dict(Diagnosis.DIAGNOSIS_SUCCESS, "foo check", "in-check diagnosis", None, None, None, None),
diagnose_dict(Diagnosis.DIAGNOSIS_FAIL, "foo check", "explicit diagnosis", None, None, None, None),
]

# A second run should give us the same results, meaning we get a fresh set of diagnoses
# from the check run.
check.run()
assert check.get_diagnoses() == [
Diagnosis.Result(Diagnosis.DIAGNOSIS_SUCCESS, "foo check", "in-check diagnosis", None, None, None, None),
Diagnosis.Result(Diagnosis.DIAGNOSIS_FAIL, "foo check", "explicit diagnosis", None, None, None, None),
assert get_diagnoses(check) == [
diagnose_dict(Diagnosis.DIAGNOSIS_SUCCESS, "foo check", "in-check diagnosis", None, None, None, None),
diagnose_dict(Diagnosis.DIAGNOSIS_FAIL, "foo check", "explicit diagnosis", None, None, None, None),
]


Expand All @@ -98,10 +100,10 @@ def good_diagnostic(self):
check = Foo("foo", {}, [{}])
check.run()

assert check.get_diagnoses() == [
Diagnosis.Result(Diagnosis.DIAGNOSIS_SUCCESS, "foo check", "in-check diagnosis", None, None, None, None),
Diagnosis.Result(Diagnosis.DIAGNOSIS_UNEXPECTED_ERROR, "", "", None, None, None, "something went wrong"),
Diagnosis.Result(Diagnosis.DIAGNOSIS_SUCCESS, "foo check", "explicit diagnosis", None, None, None, None),
assert get_diagnoses(check) == [
diagnose_dict(Diagnosis.DIAGNOSIS_SUCCESS, "foo check", "in-check diagnosis", None, None, None, None),
diagnose_dict(Diagnosis.DIAGNOSIS_UNEXPECTED_ERROR, "", "", None, None, None, "something went wrong"),
diagnose_dict(Diagnosis.DIAGNOSIS_SUCCESS, "foo check", "explicit diagnosis", None, None, None, None),
]


Expand Down Expand Up @@ -136,11 +138,29 @@ def bad_diagnostic(self):
"raw_error": None,
}

assert check.get_diagnoses() == [
Diagnosis.Result(Diagnosis.DIAGNOSIS_SUCCESS, "foo check", "ok", **expected_fields),
Diagnosis.Result(Diagnosis.DIAGNOSIS_FAIL, "foo check", "fail", **expected_fields),
Diagnosis.Result(Diagnosis.DIAGNOSIS_WARNING, "foo check", "warn", **expected_fields),
Diagnosis.Result(
assert get_diagnoses(check) == [
diagnose_dict(Diagnosis.DIAGNOSIS_SUCCESS, "foo check", "ok", **expected_fields),
diagnose_dict(Diagnosis.DIAGNOSIS_FAIL, "foo check", "fail", **expected_fields),
diagnose_dict(Diagnosis.DIAGNOSIS_WARNING, "foo check", "warn", **expected_fields),
diagnose_dict(
Diagnosis.DIAGNOSIS_UNEXPECTED_ERROR, "", "", None, None, None, "something went wrong with ********"
),
]


def get_diagnoses(check):
"""Get diagnoses from a check as a list of dictionaries."""
return json.loads(check.get_diagnoses())


def diagnose_dict(result, name, diagnosis, category=None, description=None, remediation=None, raw_error=None):
"""Helper function to create diagnosis result dictionaries with defaults."""
return {
"result": result,
"name": name,
"diagnosis": diagnosis,
"category": category,
"description": description,
"remediation": remediation,
"raw_error": raw_error,
}

0 comments on commit f591625

Please sign in to comment.