Sentry integration enhancements (#38833)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
authored and GitHub committed 2020-08-20 11:37:27 +02:00
1 parent 4371068f6a
commit d3389fa22e
11 files changed
+780 -77

No files matched your search

+174 -32
View File
@@ -1,58 +1,200 @@
"""The sentry integration."""
import logging
import re
from typing import Dict, Union
import sentry_sdk
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
import voluptuous as vol
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import __version__
from homeassistant.const import __version__ as current_version
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.loader import Integration, async_get_custom_components
from .const import CONF_DSN, CONF_ENVIRONMENT, DOMAIN
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{vol.Required(CONF_DSN): cv.string, CONF_ENVIRONMENT: cv.string}
)
},
extra=vol.ALLOW_EXTRA,
from .const import (
CONF_DSN,
CONF_ENVIRONMENT,
CONF_EVENT_CUSTOM_COMPONENTS,
CONF_EVENT_HANDLED,
CONF_EVENT_THIRD_PARTY_PACKAGES,
CONF_LOGGING_EVENT_LEVEL,
CONF_LOGGING_LEVEL,
CONF_TRACING,
CONF_TRACING_SAMPLE_RATE,
DEFAULT_LOGGING_EVENT_LEVEL,
DEFAULT_LOGGING_LEVEL,
DEFAULT_TRACING_SAMPLE_RATE,
DOMAIN,
ENTITY_COMPONENTS,
)
CONFIG_SCHEMA = cv.deprecated(DOMAIN, invalidation_version="0.117")
async def async_setup(hass: HomeAssistant, config: dict):
LOGGER_INFO_REGEX = re.compile(r"^(\w+)\.?(\w+)?\.?(\w+)?\.?(\w+)?(?:\..*)?$")
async def async_setup(hass: HomeAssistant, config: dict) -> bool:
"""Set up the Sentry component."""
conf = config.get(DOMAIN)
if conf is not None:
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}
)
)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Sentry from a config entry."""
conf = entry.data
hass.data[DOMAIN] = conf
# Migrate environment from config entry data to config entry options
if (
CONF_ENVIRONMENT not in entry.options
and CONF_ENVIRONMENT in entry.data
and entry.data[CONF_ENVIRONMENT]
):
options = {**entry.options, CONF_ENVIRONMENT: entry.data[CONF_ENVIRONMENT]}
data = entry.data.copy()
data.pop(CONF_ENVIRONMENT)
hass.config_entries.async_update_entry(entry, data=data, options=options)
# https://docs.sentry.io/platforms/python/logging/
sentry_logging = LoggingIntegration(
level=logging.INFO, # Capture info and above as breadcrumbs
event_level=logging.ERROR, # Send errors as events
level=entry.options.get(CONF_LOGGING_LEVEL, DEFAULT_LOGGING_LEVEL),
event_level=entry.options.get(
CONF_LOGGING_EVENT_LEVEL, DEFAULT_LOGGING_EVENT_LEVEL
),
)
# Additional/extra data collection
channel = get_channel(current_version)
huuid = await hass.helpers.instance_id.async_get()
system_info = await hass.helpers.system_info.async_get_system_info()
custom_components = await async_get_custom_components(hass)
tracing = {}
if entry.options.get(CONF_TRACING):
tracing = {
"traceparent_v2": True,
"traces_sample_rate": entry.options.get(
CONF_TRACING_SAMPLE_RATE, DEFAULT_TRACING_SAMPLE_RATE
),
}
sentry_sdk.init(
dsn=conf.get(CONF_DSN),
environment=conf.get(CONF_ENVIRONMENT),
integrations=[sentry_logging],
release=f"homeassistant-{__version__}",
dsn=entry.data[CONF_DSN],
environment=entry.options.get(CONF_ENVIRONMENT),
integrations=[sentry_logging, AioHttpIntegration(), SqlalchemyIntegration()],
release=current_version,
before_send=lambda event, hint: process_before_send(
hass,
entry.options,
channel,
huuid,
system_info,
custom_components,
event,
hint,
),
**tracing,
)
return True
def get_channel(version: str) -> str:
"""Find channel based on version number."""
if "dev0" in version:
return "dev"
if "dev" in version:
return "nightly"
if "b" in version:
return "beta"
return "stable"
def process_before_send(
hass: HomeAssistant,
options,
channel: str,
huuid: str,
system_info: Dict[str, Union[bool, str]],
custom_components: Dict[str, Integration],
event,
hint,
):
"""Process a Sentry event before sending it to Sentry."""
# Filter out handled events by default
if (
"tags" in event
and event.tags.get("handled", "no") == "yes"
and not options.get(CONF_EVENT_HANDLED)
):
return None
# Additional tags to add to the event
additional_tags = {
"channel": channel,
"installation_type": system_info["installation_type"],
"uuid": huuid,
}
# Find out all integrations in use, filter "auth", because it
# triggers security rules, hiding all data.
integrations = [
integration
for integration in hass.config.components
if integration != "auth" and "." not in integration
]
# Add additional tags based on what caused the event.
platform = entity_platform.current_platform.get()
if platform is not None:
# This event happened in a platform
additional_tags["custom_component"] = "no"
additional_tags["integration"] = platform.platform_name
additional_tags["platform"] = platform.domain
elif "logger" in event:
# Logger event, try to get integration information from the logger name.
matches = LOGGER_INFO_REGEX.findall(event["logger"])
if matches:
group1, group2, group3, group4 = matches[0]
# Handle the "homeassistant." package differently
if group1 == "homeassistant" and group2 and group3:
if group2 == "components":
# This logger is from a component
additional_tags["custom_component"] = "no"
additional_tags["integration"] = group3
if group4 and group4 in ENTITY_COMPONENTS:
additional_tags["platform"] = group4
else:
# Not a component, could be helper, or something else.
additional_tags[group2] = group3
else:
# Not the "homeassistant" package, this third-party
if not options.get(CONF_EVENT_THIRD_PARTY_PACKAGES):
return None
additional_tags["package"] = group1
# If this event is caused by an integration, add a tag if this
# integration is custom or not.
if (
"integration" in additional_tags
and additional_tags["integration"] in custom_components
):
if not options.get(CONF_EVENT_CUSTOM_COMPONENTS):
return None
additional_tags["custom_component"] = "yes"
# Update event with the additional tags
event.setdefault("tags", {}).update(additional_tags)
# Update event data with Home Assistant Context
event.setdefault("contexts", {}).update(
{
"Home Assistant": {
"channel": channel,
"custom_components": "\n".join(sorted(custom_components)),
"integrations": "\n".join(sorted(integrations)),
**system_info,
},
}
)
return event
+103 -22
View File
@@ -1,56 +1,137 @@
"""Config flow for sentry integration."""
from __future__ import annotations
import logging
from typing import Any, Dict, Optional
from sentry_sdk.utils import BadDsn, Dsn
import voluptuous as vol
from homeassistant import config_entries, core
from homeassistant import config_entries
from homeassistant.core import callback
from .const import CONF_DSN, DOMAIN # pylint: disable=unused-import
from .const import ( # pylint: disable=unused-import
CONF_DSN,
CONF_ENVIRONMENT,
CONF_EVENT_CUSTOM_COMPONENTS,
CONF_EVENT_HANDLED,
CONF_EVENT_THIRD_PARTY_PACKAGES,
CONF_LOGGING_EVENT_LEVEL,
CONF_LOGGING_LEVEL,
CONF_TRACING,
CONF_TRACING_SAMPLE_RATE,
DEFAULT_LOGGING_EVENT_LEVEL,
DEFAULT_LOGGING_LEVEL,
DEFAULT_TRACING_SAMPLE_RATE,
DOMAIN,
LOGGING_LEVELS,
)
_LOGGER = logging.getLogger(__name__)
DATA_SCHEMA = vol.Schema({vol.Required(CONF_DSN): str})
async def validate_input(hass: core.HomeAssistant, data):
"""Validate the DSN input allows us to connect.
Data has the keys from DATA_SCHEMA with values provided by the user.
"""
# validate the dsn
Dsn(data["dsn"])
return {"title": "Sentry"}
class DomainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class SentryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a Sentry config flow."""
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
async def async_step_user(self, user_input=None):
@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> SentryOptionsFlow:
"""Get the options flow for this handler."""
return SentryOptionsFlow(config_entry)
async def async_step_user(
self, user_input: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""Handle a user config flow."""
if self._async_current_entries():
return self.async_abort(reason="already_configured")
return self.async_abort(reason="single_instance_allowed")
errors = {}
if user_input is not None:
try:
info = await validate_input(self.hass, user_input)
return self.async_create_entry(title=info["title"], data=user_input)
Dsn(user_input["dsn"])
except BadDsn:
errors["base"] = "bad_dsn"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
return self.async_create_entry(title="Sentry", data=user_input)
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)
async def async_step_import(self, import_config):
"""Import a config entry from configuration.yaml."""
return await self.async_step_user(import_config)
class SentryOptionsFlow(config_entries.OptionsFlow):
"""Handle Sentry options."""
def __init__(self, config_entry: config_entries.ConfigEntry):
"""Initialize Sentry options flow."""
self.config_entry = config_entry
async def async_step_init(
self, user_input: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""Manage Sentry options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Optional(
CONF_LOGGING_EVENT_LEVEL,
default=self.config_entry.options.get(
CONF_LOGGING_EVENT_LEVEL, DEFAULT_LOGGING_EVENT_LEVEL
),
): vol.In(LOGGING_LEVELS),
vol.Optional(
CONF_LOGGING_LEVEL,
default=self.config_entry.options.get(
CONF_LOGGING_LEVEL, DEFAULT_LOGGING_LEVEL
),
): vol.In(LOGGING_LEVELS),
vol.Optional(
CONF_ENVIRONMENT,
default=self.config_entry.options.get(CONF_ENVIRONMENT),
): str,
vol.Optional(
CONF_EVENT_HANDLED,
default=self.config_entry.options.get(
CONF_EVENT_HANDLED, False
),
): bool,
vol.Optional(
CONF_EVENT_CUSTOM_COMPONENTS,
default=self.config_entry.options.get(
CONF_EVENT_CUSTOM_COMPONENTS, False
),
): bool,
vol.Optional(
CONF_EVENT_THIRD_PARTY_PACKAGES,
default=self.config_entry.options.get(
CONF_EVENT_THIRD_PARTY_PACKAGES, False
),
): bool,
vol.Optional(
CONF_TRACING,
default=self.config_entry.options.get(CONF_TRACING, False),
): bool,
vol.Optional(
CONF_TRACING_SAMPLE_RATE,
default=self.config_entry.options.get(
CONF_TRACING_SAMPLE_RATE, DEFAULT_TRACING_SAMPLE_RATE
),
): vol.All(vol.Coerce(float), vol.Range(min=0.0, max=1.0)),
}
),
)
+46
View File
@@ -1,6 +1,52 @@
"""Constants for the sentry integration."""
import logging
DOMAIN = "sentry"
CONF_DSN = "dsn"
CONF_ENVIRONMENT = "environment"
CONF_EVENT_CUSTOM_COMPONENTS = "event_custom_components"
CONF_EVENT_HANDLED = "event_handled"
CONF_EVENT_THIRD_PARTY_PACKAGES = "event_third_party_packages"
CONF_LOGGING_EVENT_LEVEL = "logging_event_level"
CONF_LOGGING_LEVEL = "logging_level"
CONF_TRACING = "tracing"
CONF_TRACING_SAMPLE_RATE = "tracing_sample_rate"
DEFAULT_LOGGING_EVENT_LEVEL = logging.ERROR
DEFAULT_LOGGING_LEVEL = logging.WARNING
DEFAULT_TRACING_SAMPLE_RATE = 1.0
LOGGING_LEVELS = {
logging.DEBUG: "debug",
logging.INFO: "info",
logging.WARNING: "warning",
logging.ERROR: "error",
logging.CRITICAL: "critical",
}
ENTITY_COMPONENTS = [
"air_quality",
"alarm_control_panel",
"binary_sensor",
"calendar",
"camera",
"climate",
"cover",
"device_tracker",
"fan",
"geo_location",
"group",
"humidifier",
"light",
"lock",
"media_player",
"remote",
"scene",
"sensor",
"switch",
"vacuum",
"water_heater",
"weather",
]
@@ -3,6 +3,6 @@
"name": "Sentry",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/sentry",
"requirements": ["sentry-sdk==0.13.5"],
"codeowners": ["@dcramer"]
"requirements": ["sentry-sdk==0.16.5"],
"codeowners": ["@dcramer", "@frenck"]
}
+24 -2
View File
@@ -1,9 +1,31 @@
{
"config": {
"step": {
"user": { "title": "Sentry", "description": "Enter your Sentry DSN" }
"user": {
"title": "Sentry",
"description": "Enter your Sentry DSN",
"data": { "dsn": "DSN" }
}
},
"error": { "unknown": "Unexpected error", "bad_dsn": "Invalid DSN" },
"abort": { "already_configured": "Sentry is already configured" }
"abort": {
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]"
}
},
"options": {
"step": {
"init": {
"data": {
"environment": "Optional name of the environment.",
"event_custom_components": "Send events from custom components",
"event_handled": "Send handled events",
"event_third_party_packages": "Send events from third-party packages",
"logging_event_level": "The log level Sentry will register an event for",
"logging_level": "The log level Sentry will record logs as breadcrums for",
"tracing": "Enable performance tracing",
"tracing_sample_rate": "Tracing sample rate; between 0.0 and 1.0 (1.0 = 100%)"
}
}
}
}
}