Skip to content

Commit

Permalink
Sentry integration (#41)
Browse files Browse the repository at this point in the history
* Sentry integration

* Linting
  • Loading branch information
elreplicante authored Dec 2, 2022
1 parent 66d3920 commit d386062
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from fastapi import FastAPI, HTTPException, File, UploadFile, BackgroundTasks
from fastapi.responses import FileResponse, JSONResponse
from fastapi.encoders import jsonable_encoder
from sentry_sdk.integrations.redis import RedisIntegration
from starlette.status import HTTP_202_ACCEPTED
from rsmq import RedisSMQ

from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
import sentry_sdk

from document_file import DocumentFile
Expand All @@ -32,11 +32,11 @@

try:
sentry_sdk.init(
os.environ.get("SENTRY_OCR_DSN"),
os.environ.get("SENTRY_CONVERT_TO_PDF_DSN"),
traces_sample_rate=0.1,
environment=os.environ.get("ENVIRONMENT", "development"),
integrations=[RedisIntegration()],
)
app.add_middleware(SentryAsgiMiddleware)
except Exception:
pass

Expand Down
82 changes: 42 additions & 40 deletions src/worker/queue_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pydantic import ValidationError
from rsmq.consumer import RedisSMQConsumer
from rsmq import RedisSMQ
from sentry_sdk import start_transaction
from sentry_sdk.integrations.redis import RedisIntegration
import sentry_sdk

Expand Down Expand Up @@ -38,55 +39,56 @@ def create_queues(self):
self.logger.exception("Error creating Redis queues")

def process(self, id, message, rc, ts):
try:
task = Task(**message)
except ValidationError:
self.logger.error(f"Not a valid message: {message}")
return True
with start_transaction(op="task", name='convert_to_pdf'):
try:
task = Task(**message)
except ValidationError:
self.logger.error(f"Not a valid message: {message}")
return True

self.logger.info(f"Valid message: {message}")
self.logger.info(f"Valid message: {message}")

try:
self.logger.info(f"Converting to PDF {task.params.filename}")
processed_pdf_filepath = convert_to_pdf(
task.params.filename, task.params.namespace
)
try:
self.logger.info(f"Converting to PDF {task.params.filename}")
processed_pdf_filepath = convert_to_pdf(
task.params.filename, task.params.namespace
)

self.logger.info(f"Converted to PDF {task.params.filename}")
self.logger.info(f"Converted to PDF {task.params.filename}")

if not processed_pdf_filepath:
message = Message(
namespace=task.params.namespace,
task=task.task,
params=task.params,
success=False,
error_message="Error during pdf convert",
)
self.logger.error(f"Error during pdf convert {task.params.filename}")

self.results_queue.sendMessage().message(message.dict()).execute()
self.logger.error(message.json())
return True

file_name = "".join(task.params.filename.split(".")[:-1])
processed_pdf_url = (
f"{SERVICE_URL}/processed_pdf/{task.params.namespace}/{file_name}.pdf"
)

if not processed_pdf_filepath:
message = Message(
namespace=task.params.namespace,
task=task.task,
params=task.params,
success=False,
error_message="Error during pdf convert",
success=True,
file_url=processed_pdf_url,
)
self.logger.error(f"Error during pdf convert {task.params.filename}")

self.results_queue.sendMessage().message(message.dict()).execute()
self.logger.error(message.json())
self.logger.info(message.json())
self.results_queue.sendMessage(delay=3).message(message.dict()).execute()
return True
except Exception as exception:
self.logger.exception(exception)
return True

file_name = "".join(task.params.filename.split(".")[:-1])
processed_pdf_url = (
f"{SERVICE_URL}/processed_pdf/{task.params.namespace}/{file_name}.pdf"
)

message = Message(
namespace=task.params.namespace,
task=task.task,
params=task.params,
success=True,
file_url=processed_pdf_url,
)

self.logger.info(message.json())
self.results_queue.sendMessage(delay=3).message(message.dict()).execute()
return True
except Exception as exception:
self.logger.exception(exception)
return True

def run(self):
try:
Expand All @@ -106,9 +108,9 @@ def run(self):
if __name__ == "__main__":
try:
sentry_sdk.init(
os.environ.get("SENTRY_PDF_CONVERT_DSN"),
os.environ.get("SENTRY_CONVERT_TO_PDF_DSN"),
traces_sample_rate=0.1,
environment=os.environ.get("ENVIRONMENT", "production"),
environment=os.environ.get("ENVIRONMENT", "development"),
integrations=[RedisIntegration()],
)
except Exception:
Expand Down

0 comments on commit d386062

Please sign in to comment.