mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-10-30 07:14:20 +00:00
wip: ratdoni'
This commit is contained in:
6 files changed
+139
-9
No files matched your search
@@ -1,7 +1,15 @@
|
||||
import sentry_sdk
|
||||
|
||||
from config.env import env
|
||||
|
||||
_SENTRY_TAG_FIELDS = {
|
||||
"prowler_provider": "provider",
|
||||
"prowler_region": "region",
|
||||
"prowler_service": "service",
|
||||
"prowler_tenant_id": "tenant_id",
|
||||
"prowler_scan_id": "scan_id",
|
||||
"prowler_provider_uid": "provider_uid",
|
||||
}
|
||||
|
||||
IGNORED_EXCEPTIONS = [
|
||||
# Provider is not connected due to credentials errors
|
||||
"is not connected",
|
||||
@@ -81,7 +89,10 @@ IGNORED_EXCEPTIONS = [
|
||||
|
||||
def before_send(event, hint):
|
||||
"""
|
||||
before_send handles the Sentry events in order to send them or not
|
||||
before_send handles the Sentry events in order to send them or not.
|
||||
|
||||
It also promotes prowler context fields (injected by ProwlerContextFilter)
|
||||
from the LogRecord into Sentry event tags so they become searchable.
|
||||
"""
|
||||
# Ignore logs with the ignored_exceptions
|
||||
# https://docs.python.org/3/library/logging.html#logrecord-objects
|
||||
@@ -105,6 +116,16 @@ def before_send(event, hint):
|
||||
if log_lvl <= 40 and any(ignored in log_msg for ignored in IGNORED_EXCEPTIONS):
|
||||
return None # Explicitly return None to drop the event
|
||||
|
||||
# Promote prowler context fields to Sentry tags
|
||||
for record_attr, tag_name in _SENTRY_TAG_FIELDS.items():
|
||||
value = getattr(log_record, record_attr, None)
|
||||
if value:
|
||||
event.setdefault("tags", {})
|
||||
if isinstance(event["tags"], dict):
|
||||
event["tags"][tag_name] = str(value)
|
||||
elif isinstance(event["tags"], list):
|
||||
event["tags"].append([tag_name, str(value)])
|
||||
|
||||
# Ignore exceptions with the ignored_exceptions
|
||||
if "exc_info" in hint and hint["exc_info"]:
|
||||
exc_value = str(hint["exc_info"][1])
|
||||
|
||||
@@ -853,6 +853,22 @@ def perform_prowler_scan(
|
||||
scan_instance.started_at = datetime.now(tz=timezone.utc)
|
||||
scan_instance.save()
|
||||
|
||||
# Enrich Sentry context for all downstream errors (Layer 2: app-only tags)
|
||||
from prowler.lib.logger import (
|
||||
prowler_provider_uid_var,
|
||||
prowler_scan_id_var,
|
||||
prowler_tenant_id_var,
|
||||
)
|
||||
|
||||
prowler_tenant_id_var.set(str(tenant_id))
|
||||
prowler_scan_id_var.set(str(scan_id))
|
||||
prowler_provider_uid_var.set(str(provider_instance.uid))
|
||||
|
||||
sentry_sdk.set_tag("provider", str(provider_instance.provider))
|
||||
sentry_sdk.set_tag("tenant_id", str(tenant_id))
|
||||
sentry_sdk.set_tag("scan_id", str(scan_id))
|
||||
sentry_sdk.set_tag("provider_uid", str(provider_instance.uid))
|
||||
|
||||
# Find the mutelist processor if it exists
|
||||
with rls_transaction(tenant_id, using=READ_REPLICA_ALIAS):
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user