fix(sentry): only import/run Sentry if enabled

This commit is contained in:
2024-11-24 10:05:22 +00:00
parent cf2d542e16
commit 2d4a1294cb
4 changed files with 121 additions and 82 deletions

View File

@ -2,11 +2,18 @@
import logging
import sentry_sdk
from webex_bot.models.command import Command
from webex_bot.models.response import Response, response_from_adaptive_card
from webexteamssdk.models.cards import (AdaptiveCard, Column, ColumnSet, Date,
FontSize, FontWeight, Text, TextBlock)
from webexteamssdk.models.cards import (
AdaptiveCard,
Column,
ColumnSet,
Date,
FontSize,
FontWeight,
Text,
TextBlock,
)
from webexteamssdk.models.cards.actions import Submit
from app.utils.config import config
@ -14,6 +21,9 @@ from app.utils.n8n import get_tasks, submit_task
log: logging.Logger = logging.getLogger(__name__)
if config.sentry_enabled:
import sentry_sdk
class SubmitTaskCommand(Command):
"""Submit task command."""
@ -115,8 +125,11 @@ class SubmitTaskCommand(Command):
Submit(title="Cancel", data={"command_keyword": "exit"}),
],
)
_result = response_from_adaptive_card(card)
if not config.sentry_enabled:
return _result
with sentry_sdk.start_transaction(name="submit_task_command"):
return response_from_adaptive_card(card)
return _result
class SubmitTaskCallback(Command):
@ -160,6 +173,8 @@ class SubmitTaskCallback(Command):
def execute(self, message, attachment_actions, activity) -> str:
"""Execute method."""
if not config.sentry_enabled:
return self.msg
with sentry_sdk.start_transaction(name="submit_task_callback"):
return self.msg
@ -177,14 +192,22 @@ class MyTasksCallback(Command):
def pre_execute(self, message, attachment_actions, activity) -> str:
"""Pre-execute method."""
_msg: str = "Getting your tasks..."
if not config.sentry_enabled:
return _msg
with sentry_sdk.start_transaction(name="my_tasks_preexec"):
return "Getting your tasks..."
return _msg
def execute(self, message, attachment_actions, activity) -> str | None:
"""Execute method."""
sender: str = attachment_actions.inputs.get("sender")
result: bool = get_tasks(requestor=sender)
_msg: str = "Failed to get tasks. Please try again."
if not config.sentry_enabled:
if not result:
return _msg
return None
with sentry_sdk.start_transaction(name="my_tasks_exec"):
if not result:
return "Failed to get tasks. Please try again."
return _msg
return None

View File

@ -2,7 +2,6 @@
import sys
import sentry_sdk
from sentry_sdk.integrations.stdlib import StdlibIntegration
from webex_bot.webex_bot import WebexBot
@ -11,6 +10,8 @@ from app.commands.submit_task import SubmitTaskCommand
from app.utils.config import config
if config.sentry_enabled:
import sentry_sdk
apm = sentry_sdk.init(
dsn=config.sentry_dsn,
enable_tracing=True,

View File

@ -1,10 +1,12 @@
"""N8N utils module."""
import requests
import sentry_sdk
from app.utils.config import config
if config.sentry_enabled:
import sentry_sdk
def __n8n_post(data: dict) -> bool:
"""Post data to N8N webhook URL.
@ -38,15 +40,18 @@ def submit_task(summary, description, completion_date, requestor) -> bool:
Returns:
bool: True if successful, else False.
"""
with sentry_sdk.start_transaction(name="submit_task"):
data: dict = {
"requestor": requestor,
"title": summary,
"description": description,
"completiondate": completion_date,
}
data: dict = {
"requestor": requestor,
"title": summary,
"description": description,
"completiondate": completion_date,
}
if not config.sentry_enabled:
_data = __n8n_post(data=data)
return _data
return _data
with sentry_sdk.start_transaction(name="submit_task"):
_data = __n8n_post(data=data)
return _data
def get_tasks(requestor) -> bool:
@ -58,8 +63,8 @@ def get_tasks(requestor) -> bool:
Returns:
bool: True if successful, else False.
"""
with sentry_sdk.start_transaction(name="get_tasks"):
headers: dict = {"Content-Type": "application/json"}
headers: dict = {"Content-Type": "application/json"}
if not config.sentry_enabled:
resp: requests.Response = requests.get(
url=config.n8n_webhook_url,
headers=headers,
@ -68,4 +73,14 @@ def get_tasks(requestor) -> bool:
params={"requestor": requestor},
)
_data = bool(resp.status_code == 200)
return _data
return _data
with sentry_sdk.start_transaction(name="get_tasks"):
resp: requests.Response = requests.get(
url=config.n8n_webhook_url,
headers=headers,
timeout=10,
verify=False,
params={"requestor": requestor},
)
_data = bool(resp.status_code == 200)
return _data