Skip to content

Commit

Permalink
fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
BYK committed Nov 8, 2024
1 parent 6a6881e commit 17b87be
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions sentry_sdk/spotlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import urllib.request
import urllib.error
import urllib3
import sys

from itertools import chain, product

Expand All @@ -24,6 +25,9 @@
from sentry_sdk.envelope import Envelope


logger = logging.getLogger("spotlight")


DEFAULT_SPOTLIGHT_URL = "http://localhost:8969/stream"
DJANGO_SPOTLIGHT_MIDDLEWARE_PATH = "sentry_sdk.spotlight.SpotlightMiddleware"

Expand Down Expand Up @@ -112,21 +116,14 @@ def __init__(self, get_response):
spotlight_js_url,
method="HEAD",
)
status_code = urllib.request.urlopen(req).status
if status_code >= 200 and status_code < 400:
self._spotlight_script = SPOTLIGHT_JS_SNIPPET_PATTERN.format(
spotlight_js_url
)
else:
sentry_logger.debug(
"Could not get Spotlight JS from %s (status: %s), SpotlightMiddleware will not be useful.",
spotlight_js_url,
status_code,
)
self._spotlight_script = None
urllib.request.urlopen(req)
self._spotlight_script = SPOTLIGHT_JS_SNIPPET_PATTERN.format(
spotlight_js_url
)
except urllib.error.URLError as err:
sentry_logger.debug(
"Cannot get Spotlight JS to inject. SpotlightMiddleware will not be very useful.",
"Cannot get Spotlight JS to inject at %s. SpotlightMiddleware will not be very useful.",
spotlight_js_url,
exc_info=err,
)
self._spotlight_script = None
Expand Down Expand Up @@ -204,6 +201,10 @@ def process_exception(self, _request, exception):

def setup_spotlight(options):
# type: (Dict[str, Any]) -> Optional[SpotlightClient]
_handler = logging.StreamHandler(sys.stderr)
_handler.setFormatter(logging.Formatter(" [spotlight] %(levelname)s: %(message)s"))
logger.addHandler(_handler)
logger.setLevel(logging.INFO)

url = options.get("spotlight")

Expand All @@ -225,9 +226,9 @@ def setup_spotlight(options):
settings.MIDDLEWARE = type(middleware)(
chain(middleware, (DJANGO_SPOTLIGHT_MIDDLEWARE_PATH,))
)
logging.info("Enabled Spotlight integration for Django")
logger.info("Enabled Spotlight integration for Django")

client = SpotlightClient(url)
logging.info("Enabled Spotlight at %s", url)
logger.info("Enabled Spotlight using sidecar at %s", url)

return client

0 comments on commit 17b87be

Please # to comment.