Skip to content

Commit

Permalink
Add BugnSnag to docs + some error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
battleoverflow committed Oct 3, 2023
1 parent abbc5d3 commit 0a5e362
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 30 deletions.
4 changes: 4 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ credentials:
key: MY_API_KEY
ssl: False

error_reporting:
- name: bugsnag
api_key: API_KEY

sources:
# This section defines each of the input sources for ThreatIngestor.
# Define as many as you want. ThreatIngestor maintains a "state" for each of
Expand Down
13 changes: 13 additions & 0 deletions docs/extras.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ Extras

There are a few extra tools included alongside ThreatIngestor, that didn't quite make sense as sources or operators.

BugSnag
-------

BugSnag monitoring is a valuable tool to have when running in daemon mode. Adding it is extremely simple and the only requirement is to have an API key.

Here's an example to include in your ``config.yml``:

.. code-block:: yaml
error_reporting:
- name: bugsnag
api_key: API_KEY
Quick Webapp
------------

Expand Down
54 changes: 44 additions & 10 deletions threatingestor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,29 @@
logger.info("Notifiers is not installed.")
notifiers = None

try:
import bugsnag
bugsnag_imported = True
except ImportError:
logger.info("BugSnag is not installed.")
bugsnag_imported = False

import threatingestor.config
import threatingestor.state
import threatingestor.exceptions
import threatingestor.whitelist

from threatingestor.bugsnagmonitor import configure_bugsnag, send_notification
BUGSNAG_ACTIVE = False

def bugsnag_notification(msg=None, metadata=None) -> None:
"""
Monitor your code with BugSnag
You can include a additional information with the `metadata` paramater.
"""

if bugsnag_imported:
bugsnag.notify(Exception(msg), metadata={"ThreatIngestor" : metadata})

class Ingestor:
"""ThreatIngestor main work logic.
Expand Down Expand Up @@ -63,12 +80,17 @@ def __init__(self, config_file):
except TypeError:
logger.exception("Couldn't initialize statsd client; bad config?")
sys.exit(1)

# Configure BugSnag
for service in self.config.error_reporting():
if service['name'] == "bugsnag":
configure_bugsnag(service['api_key'])
break
if bugsnag_imported:
for service in self.config.error_reporting():
if service['name'] == "bugsnag":
if service['api_key']:
bugsnag.configure(api_key=service['api_key'])
logger.debug("BugSnag configured")
BUGSNAG_ACTIVE = True

break

# Load state DB.
try:
Expand All @@ -77,7 +99,10 @@ def __init__(self, config_file):
except (OSError, IOError, threatingestor.exceptions.IngestorError):
# Error loading state DB.
logger.exception("Error reading state database")
send_notification("Error reading state database")

if BUGSNAG_ACTIVE:
bugsnag_notification("Error reading state database")

sys.exit(1)

# Instantiate plugins.
Expand All @@ -95,7 +120,10 @@ def __init__(self, config_file):

except (TypeError, ConnectionError, threatingestor.exceptions.PluginError):
logger.exception("Error initializing plugins")
send_notification("Error initializing plugins")

if BUGSNAG_ACTIVE:
bugsnag_notification("Error initializing plugins")

sys.exit(1)

def _is_whitelisted(self, artifact) -> bool:
Expand Down Expand Up @@ -132,7 +160,10 @@ def run_once(self):
except Exception:
self.statsd.incr(f'error.source.{source}')
logger.exception(f"Unknown error in source '{source}'")
send_notification(f"Unknown error in source '{source}'")

if BUGSNAG_ACTIVE:
bugsnag_notification(f"Unknown error in source '{source}'")

continue

# Save the source state.
Expand All @@ -155,7 +186,10 @@ def run_once(self):
except Exception:
self.statsd.incr(f'error.operator.{operator}')
logger.exception(f"Unknown error in operator '{operator}'")
send_notification(f"Unknown error in operator '{operator}'")

if BUGSNAG_ACTIVE:
bugsnag_notification(f"Unknown error in operator '{operator}'")

continue

# Record stats and update the summary.
Expand Down
20 changes: 0 additions & 20 deletions threatingestor/bugsnagmonitor.py

This file was deleted.

0 comments on commit 0a5e362

Please sign in to comment.