Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add honeybadger alerts on exception #55

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $ aws sqs create-queue --queue-name sul-speech-to-text-todo-your-username
$ aws sqs create-queue --queue-name sul-speech-to-text-done-your-username
```

Create a bucket:
Create a bucket:

```shell
aws s3 mb s3://sul-speech-to-text-dev-your-username
Expand Down Expand Up @@ -238,3 +238,52 @@ pytest
```

Note: the tests use the [moto](https://docs.getmoto.org/en/latest/) library to mock out AWS resources. If you want to test live AWS you can follow the steps above to create a job, run, and then receive the done message.

### Linting and typechecking

This project uses [Ruff](https://docs.astral.sh/ruff/) for typechecking. If you've set up your virtual environment as described above, you can do:

```shell
ruff check
```

Which might get you output like
```
% ruff check
tests/test_speech_to_text.py:12:27: F401 [*] `unittest.mock.MagicMock` imported but unused
|
11 | import pytest
12 | from unittest.mock import MagicMock
| ^^^^^^^^^ F401
13 |
14 | BUCKET = "bucket"
|
= help: Remove unused import: `unittest.mock.MagicMock`

Found 1 error.
```

or (hopefully)

```
% ruff check
All checks passed!
```

And we use [mypy](https://www.mypy-lang.org/) for typechecking. You can run it like so:
```shell
mypy .
```

Which might get you something like
```
% mypy .
speech_to_text.py:89: error: Incompatible return value type (got "None", expected "dict[Any, Any]") [return-value]
Found 1 error in 1 file (checked 3 source files)
```

but hopefully gets you something like
```
% mypy .
Success: no issues found in 3 source files
```
2 changes: 2 additions & 0 deletions env-example
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ AWS_ROLE_ARN=arn:aws:iam::418214828013:role/DevelopersRole
SPEECH_TO_TEXT_S3_BUCKET=sul-speech-to-text-dev-your-username
SPEECH_TO_TEXT_TODO_SQS_QUEUE=sul-speech-to-text-todo-dev-your-username
SPEECH_TO_TEXT_DONE_SQS_QUEUE=sul-speech-to-text-done-dev-your-username
HONEYBADGER_API_KEY=CHANGE_ME
HONEYBADGER_ENV=stage
2 changes: 0 additions & 2 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
[mypy]

[mypy-whisper.*]
ignore_missing_imports = True
8 changes: 5 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
boto3
boto3-stubs[s3,sqs,sts]
ffprobe3
honeybadger
moto[s3,sqs,sts]
mypy
openai-whisper
python-dotenv
pytest
moto[s3,sqs,sts]
mypy
boto3-stubs[s3,sqs,sts]
ruff
9 changes: 9 additions & 0 deletions speech_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import shutil
import subprocess
import traceback
from honeybadger import honeybadger
from functools import cache
from pathlib import Path
from typing import Optional, Dict
Expand All @@ -22,6 +23,11 @@
from mypy_boto3_s3.service_resource import Bucket, S3ServiceResource
from mypy_boto3_sqs.service_resource import SQSServiceResource, Queue

honeybadger.configure(
api_key=os.environ.get("HONEYBADGER_API_KEY", ""),
environment=os.environ.get("HONEYBADGER_ENV", "stage"),
)


def main(daemon=True) -> None:
# loop forever looking for jobs unless daemon option says not to
Expand Down Expand Up @@ -277,6 +283,9 @@ def report_error(message: str, job: Optional[Dict], traceback: str) -> None:
"""
full_message = message + "\n" + traceback
logging.exception(full_message)
honeybadger.notify(
"Whisper AWS process \n" + message, context={"job": job, "traceback": traceback}
)

# it's possible that we are reporting an error without a job
# we can only send a message to the DONE queue if we have a job!
Expand Down
Loading