Skip to content

Commit

Permalink
feat(api): more verbose (#2695)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahargl authored Nov 28, 2024
1 parent 613effa commit b92b470
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
2 changes: 2 additions & 0 deletions keep/api/core/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ async def extract_generic_body(request: Request) -> dict | bytes | FormData:
return await request.form()
else:
try:
logger.debug("Parsing body as json")
body = await request.json()
logger.debug("Parsed body as json")
return body
except Exception:
logger.debug("Failed to parse body as json, returning raw body")
Expand Down
17 changes: 16 additions & 1 deletion keep/api/routes/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import logging
import os
import time
from typing import List, Optional

import celpy
Expand Down Expand Up @@ -368,7 +369,16 @@ async def receive_event(

provider_class = None
try:
t = time.time()
logger.debug(f"Getting provider class for {provider_type}")
provider_class = ProvidersFactory.get_provider_class(provider_type)
logger.debug(
"Got provider class",
extra={
"provider_type": provider_type,
"time": time.time() - t,
},
)
except ModuleNotFoundError:
raise HTTPException(
status_code=400, detail=f"Provider {provider_type} not found"
Expand All @@ -379,8 +389,10 @@ async def receive_event(
)

# Parse the raw body
t = time.time()
logger.debug("Parsing event raw body")
event = provider_class.parse_event_raw_body(event)

logger.debug("Parsed event raw body", extra={"time": time.time() - t})
if REDIS:
redis: ArqRedis = await get_pool()
job = await redis.enqueue_job(
Expand All @@ -403,6 +415,8 @@ async def receive_event(
},
)
else:
t = time.time()
logger.debug("Adding task to process event")
bg_tasks.add_task(
process_event,
{},
Expand All @@ -414,6 +428,7 @@ async def receive_event(
trace_id,
event,
)
logger.debug("Added task to process event", extra={"time": time.time() - t})
return Response(status_code=202)


Expand Down
44 changes: 23 additions & 21 deletions keep/api/tasks/process_event_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

TIMES_TO_RETRY_JOB = 5 # the number of times to retry the job in case of failure
KEEP_STORE_RAW_ALERTS = os.environ.get("KEEP_STORE_RAW_ALERTS", "false") == "true"
KEEP_CORRELATION_ENABLED = os.environ.get("KEEP_CORRELATION_ENABLED", "true") == "true"

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -388,28 +389,29 @@ def __handle_formatted_events(

incidents = []
# Now we need to run the rules engine
try:
rules_engine = RulesEngine(tenant_id=tenant_id)
incidents: List[IncidentDto] = rules_engine.run_rules(
enriched_formatted_events, session=session
)
if KEEP_CORRELATION_ENABLED:
try:
rules_engine = RulesEngine(tenant_id=tenant_id)
incidents: List[IncidentDto] = rules_engine.run_rules(
enriched_formatted_events, session=session
)

# TODO: Replace with incidents workflow triggers. Ticket: https://github.com/keephq/keep/issues/1527
# if new grouped incidents were created, we need to push them to the client
# if incidents:
# logger.info("Adding group alerts to the workflow manager queue")
# workflow_manager.insert_events(tenant_id, grouped_alerts)
# logger.info("Added group alerts to the workflow manager queue")
except Exception:
logger.exception(
"Failed to run rules engine",
extra={
"provider_type": provider_type,
"num_of_alerts": len(formatted_events),
"provider_id": provider_id,
"tenant_id": tenant_id,
},
)
# TODO: Replace with incidents workflow triggers. Ticket: https://github.com/keephq/keep/issues/1527
# if new grouped incidents were created, we need to push them to the client
# if incidents:
# logger.info("Adding group alerts to the workflow manager queue")
# workflow_manager.insert_events(tenant_id, grouped_alerts)
# logger.info("Added group alerts to the workflow manager queue")
except Exception:
logger.exception(
"Failed to run rules engine",
extra={
"provider_type": provider_type,
"num_of_alerts": len(formatted_events),
"provider_id": provider_id,
"tenant_id": tenant_id,
},
)

pusher_client = get_pusher_client() if notify_client else None
# Get the notification cache
Expand Down
14 changes: 13 additions & 1 deletion keep/identitymanager/identitymanagerfactory.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import enum
import importlib
import logging
import os
import time
from typing import Type

from keep.api.core.config import config
from keep.contextmanager.contextmanager import ContextManager
from keep.identitymanager.authverifierbase import AuthVerifierBase
from keep.identitymanager.identitymanager import BaseIdentityManager

logger = logging.getLogger(__name__)


class IdentityManagerTypes(enum.Enum):
"""
Expand Down Expand Up @@ -94,6 +98,8 @@ def _load_manager(manager_type: str, manager_class: str, *args, **kwargs):
NotImplementedError: If the specified manager type or class is not implemented.
"""
try:
t = time.time()
logger.debug(f"Loading {manager_class} for {manager_type}")
manager_type = (
IdentityManagerFactory._backward_compatible_get_identity_manager(
manager_type
Expand All @@ -113,13 +119,19 @@ def _load_manager(manager_type: str, manager_class: str, *args, **kwargs):
raise NotImplementedError(
f"{manager_class} for {manager_type} not implemented"
)

logger.debug(
f"Loaded {manager_class} for {manager_type} in {time.time() - t} seconds"
)
# look for the class that contains the manager_class in its name
for _attr in dir(module):
if manager_class in _attr.lower() and "base" not in _attr.lower():
class_name = _attr
break
manager_class: Type = getattr(module, class_name)
return manager_class(*args, **kwargs)
resp = manager_class(*args, **kwargs)
logger.debug(f"Found class {class_name} in {time.time() - t} seconds")
return resp
except (ImportError, AttributeError):
raise NotImplementedError(
f"{manager_class} for {manager_type} not implemented"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "keep"
version = "0.30.5"
version = "0.30.6"
description = "Alerting. for developers, by developers."
authors = ["Keep Alerting LTD"]
packages = [{include = "keep"}]
Expand Down

0 comments on commit b92b470

Please sign in to comment.