1 """The sentry integration."""
3 from __future__
import annotations
6 from types
import MappingProxyType
10 from sentry_sdk.integrations.aiohttp
import AioHttpIntegration
11 from sentry_sdk.integrations.logging
import LoggingIntegration
12 from sentry_sdk.integrations.sqlalchemy
import SqlalchemyIntegration
16 EVENT_HOMEASSISTANT_STARTED,
17 __version__
as current_version,
29 CONF_EVENT_CUSTOM_COMPONENTS,
31 CONF_EVENT_THIRD_PARTY_PACKAGES,
32 CONF_LOGGING_EVENT_LEVEL,
35 CONF_TRACING_SAMPLE_RATE,
36 DEFAULT_LOGGING_EVENT_LEVEL,
37 DEFAULT_LOGGING_LEVEL,
38 DEFAULT_TRACING_SAMPLE_RATE,
42 LOGGER_INFO_REGEX = re.compile(
r"^(\w+)\.?(\w+)?\.?(\w+)?\.?(\w+)?(?:\..*)?$")
46 """Set up Sentry from a config entry."""
50 CONF_ENVIRONMENT
not in entry.options
51 and CONF_ENVIRONMENT
in entry.data
52 and entry.data[CONF_ENVIRONMENT]
54 options = {**entry.options, CONF_ENVIRONMENT: entry.data[CONF_ENVIRONMENT]}
55 data = entry.data.copy()
56 data.pop(CONF_ENVIRONMENT)
57 hass.config_entries.async_update_entry(entry, data=data, options=options)
60 sentry_logging = LoggingIntegration(
61 level=entry.options.get(CONF_LOGGING_LEVEL, DEFAULT_LOGGING_LEVEL),
62 event_level=entry.options.get(
63 CONF_LOGGING_EVENT_LEVEL, DEFAULT_LOGGING_EVENT_LEVEL
69 huuid = await instance_id.async_get(hass)
74 if entry.options.get(CONF_TRACING):
76 "traces_sample_rate": entry.options.get(
77 CONF_TRACING_SAMPLE_RATE, DEFAULT_TRACING_SAMPLE_RATE
84 """Initialize the Sentry SDK."""
86 dsn=entry.data[CONF_DSN],
87 environment=entry.options.get(CONF_ENVIRONMENT),
91 SqlalchemyIntegration(),
93 release=current_version,
107 await hass.async_add_import_executor_job(_init_sdk)
109 async
def update_system_info(now):
116 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, update_system_info)
123 options: MappingProxyType[str, Any],
126 system_info: dict[str, bool | str],
127 custom_components: dict[str, Integration],
128 event: dict[str, Any],
129 hint: dict[str, Any],
131 """Process a Sentry event before sending it to Sentry."""
135 and event[
"tags"].
get(
"handled",
"no") ==
"yes"
136 and not options.get(CONF_EVENT_HANDLED)
143 "installation_type": system_info[
"installation_type"],
151 for integration
in hass.config.components
152 if integration !=
"auth" and "." not in integration
156 if (platform := entity_platform.current_platform.get())
is not None:
158 additional_tags[
"custom_component"] =
"no"
159 additional_tags[
"integration"] = platform.platform_name
160 additional_tags[
"platform"] = platform.domain
161 elif "logger" in event:
163 matches = LOGGER_INFO_REGEX.findall(event[
"logger"])
165 group1, group2, group3, group4 = matches[0]
167 if group1 ==
"homeassistant" and group2
and group3:
168 if group2 ==
"components":
170 additional_tags[
"custom_component"] =
"no"
171 additional_tags[
"integration"] = group3
172 if group4
and group4
in ENTITY_COMPONENTS:
173 additional_tags[
"platform"] = group4
176 additional_tags[group2] = group3
179 if not options.get(CONF_EVENT_THIRD_PARTY_PACKAGES):
181 additional_tags[
"package"] = group1
186 "integration" in additional_tags
187 and additional_tags[
"integration"]
in custom_components
189 if not options.get(CONF_EVENT_CUSTOM_COMPONENTS):
191 additional_tags[
"custom_component"] =
"yes"
194 event.setdefault(
"tags", {}).
update(additional_tags)
197 event.setdefault(
"user", {}).
update({
"id": huuid})
200 event.setdefault(
"contexts", {}).
update(
204 "custom_components":
"\n".join(sorted(custom_components)),
205 "integrations":
"\n".join(sorted(integrations)),
web.Response get(self, web.Request request, str config_key)
IssData update(pyiss.ISS iss)
def process_before_send(HomeAssistant hass, MappingProxyType[str, Any] options, str channel, str huuid, dict[str, bool|str] system_info, dict[str, Integration] custom_components, dict[str, Any] event, dict[str, Any] hint)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
ReleaseChannel get_release_channel()
CALLBACK_TYPE async_call_later(HomeAssistant hass, float|timedelta delay, HassJob[[datetime], Coroutine[Any, Any, None]|None]|Callable[[datetime], Coroutine[Any, Any, None]|None] action)
dict[str, Any] async_get_system_info(HomeAssistant hass)
dict[str, Integration] async_get_custom_components(HomeAssistant hass)
Generator[None] async_pause_setup(core.HomeAssistant hass, SetupPhases phase)