Filter AddonConfigurationError from sentry (#1912)

This commit is contained in:
Joakim Sørensen 2020-08-14 14:06:46 +02:00 committed by GitHub
parent c9585033cb
commit 84755836c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 12 deletions

View File

@ -43,6 +43,7 @@ from ..coresys import CoreSys
from ..docker.addon import DockerAddon
from ..docker.stats import DockerStats
from ..exceptions import (
AddonConfigurationError,
AddonsError,
AddonsNotSupportedError,
DockerAPIError,
@ -375,7 +376,7 @@ class Addon(AddonModel):
_LOGGER.debug("Add-on %s write options: %s", self.slug, options)
return
raise AddonsError()
raise AddonConfigurationError()
async def remove_data(self) -> None:
"""Remove add-on data."""

View File

@ -282,8 +282,8 @@ def supervisor_debugger(coresys: CoreSys) -> None:
def setup_diagnostics(coresys: CoreSys) -> None:
"""Sentry diagnostic backend."""
def filter_event(event, _hint):
return filter_data(coresys, event)
def filter_event(event, hint):
return filter_data(coresys, event, hint)
# Set log level
sentry_logging = LoggingIntegration(

View File

@ -105,6 +105,10 @@ class AddonsError(HassioError):
"""Addons exception."""
class AddonConfigurationError(AddonsError):
"""Error with add-on configuration."""
class AddonsNotSupportedError(HassioNotSupportedError):
"""Addons don't support a function."""

View File

@ -6,6 +6,7 @@ from aiohttp import hdrs
from ..const import ENV_SUPERVISOR_DEV, HEADER_TOKEN_OLD, CoreStates
from ..coresys import CoreSys
from ..exceptions import AddonConfigurationError
RE_URL: re.Pattern = re.compile(r"(\w+:\/\/)(.*\.\w+)(.*)")
@ -19,10 +20,16 @@ def sanitize_url(url: str) -> str:
return re.sub(RE_URL, r"\1example.com\3", url)
def filter_data(coresys: CoreSys, event: dict) -> dict:
def filter_data(coresys: CoreSys, event: dict, hint: dict) -> dict:
"""Filter event data before sending to sentry."""
dev_env: bool = bool(os.environ.get(ENV_SUPERVISOR_DEV, 0))
# Ignore some exceptions
if "exc_info" in hint:
_, exc_value, _ = hint["exc_info"]
if isinstance(exc_value, (AddonConfigurationError)):
return None
# Ignore issue if system is not supported or diagnostics is disabled
if not coresys.config.diagnostics or not coresys.supported or dev_env:
return None
@ -48,6 +55,7 @@ def filter_data(coresys: CoreSys, event: dict) -> dict:
"dns": coresys.plugins.dns.version,
"multicast": coresys.plugins.multicast.version,
"cli": coresys.plugins.cli.version,
"disk_free_space": coresys.host.info.free_space,
}
}
)
@ -78,5 +86,4 @@ def filter_data(coresys: CoreSys, event: dict) -> dict:
if key in [hdrs.HOST, hdrs.X_FORWARDED_HOST]:
event["request"]["headers"][i] = [key, "example.com"]
print(event)
return event

View File

@ -2,23 +2,30 @@
from unittest.mock import patch
from supervisor.const import CoreStates
from supervisor.exceptions import AddonConfigurationError
from supervisor.misc.filter import filter_data
SAMPLE_EVENT = {"sample": "event"}
def test_ignored_exception(coresys):
"""Test ignored exceptions."""
hint = {"exc_info": (None, AddonConfigurationError(), None)}
assert filter_data(coresys, SAMPLE_EVENT, hint) is None
def test_diagnostics_disabled(coresys):
"""Test if diagnostics is disabled."""
coresys.config.diagnostics = False
coresys.supported = True
assert filter_data(coresys, SAMPLE_EVENT) is None
assert filter_data(coresys, SAMPLE_EVENT, {}) is None
def test_not_supported(coresys):
"""Test if not supported."""
coresys.config.diagnostics = True
coresys.supported = False
assert filter_data(coresys, SAMPLE_EVENT) is None
assert filter_data(coresys, SAMPLE_EVENT, {}) is None
def test_is_dev(coresys):
@ -26,7 +33,7 @@ def test_is_dev(coresys):
coresys.config.diagnostics = True
coresys.supported = True
with patch("os.environ", return_value=[("ENV_SUPERVISOR_DEV", "1")]):
assert filter_data(coresys, SAMPLE_EVENT) is None
assert filter_data(coresys, SAMPLE_EVENT, {}) is None
def test_not_started(coresys):
@ -35,10 +42,10 @@ def test_not_started(coresys):
coresys.supported = True
coresys.core.state = CoreStates.INITIALIZE
assert filter_data(coresys, SAMPLE_EVENT) == SAMPLE_EVENT
assert filter_data(coresys, SAMPLE_EVENT, {}) == SAMPLE_EVENT
coresys.core.state = CoreStates.SETUP
assert filter_data(coresys, SAMPLE_EVENT) == SAMPLE_EVENT
assert filter_data(coresys, SAMPLE_EVENT, {}) == SAMPLE_EVENT
def test_defaults(coresys):
@ -47,7 +54,8 @@ def test_defaults(coresys):
coresys.supported = True
coresys.core.state = CoreStates.RUNNING
filtered = filter_data(coresys, SAMPLE_EVENT)
with patch("shutil.disk_usage", return_value=(42, 42, 2 * (1024.0 ** 3))):
filtered = filter_data(coresys, SAMPLE_EVENT, {})
assert ["installation_type", "supervised"] in filtered["tags"]
assert filtered["extra"]["supervisor"]["arch"] == "amd64"
@ -71,7 +79,8 @@ def test_sanitize(coresys):
coresys.supported = True
coresys.core.state = CoreStates.RUNNING
filtered = filter_data(coresys, event)
with patch("shutil.disk_usage", return_value=(42, 42, 2 * (1024.0 ** 3))):
filtered = filter_data(coresys, event, {})
assert ["url", "https://example.com"] in filtered["tags"]