Sentry DSN parsing

This commit is contained in:
Klaas van Schelven
2023-11-10 17:50:53 +01:00
parent 66b30bb792
commit bb8de31717
4 changed files with 66 additions and 0 deletions

View File

@@ -35,6 +35,7 @@ INSTALLED_APPS = [
'tailwind',
'theme',
'compat',
'projects',
'ingest',
'issues',

0
compat/__init__.py Normal file
View File

36
compat/dsn.py Normal file
View File

@@ -0,0 +1,36 @@
import urllib.parse
def _get_url(sentry_dsn, ingest_method):
parts = urllib.parse.urlsplit(sentry_dsn)
# note: we don't replicate Sentry's requirement that a project id must be an int
path_before_api, project_id = parts.path.rsplit("/", 1)
return (
parts.scheme + "://" + parts.hostname + (":" + str(parts.port) if parts.port else "") +
path_before_api + "/api/" + project_id + "/" + ingest_method + "/")
def get_store_url(sentry_dsn):
# In sentry's-lingo 'store' just means 'ingest events one by one'
return _get_url(sentry_dsn, "store")
def get_envelope_url(sentry_dsn):
return _get_url(sentry_dsn, "envelope")
def get_header_value(sentry_dsn):
parts = urllib.parse.urlsplit(sentry_dsn)
return "Sentry " + ", ".join("%s=%s" % (key, value) for key, value in {
"sentry_key": parts.username,
# this refers to the Sentry Protocol Version. It's hard to find documentation about this, but the current (late
# 2023) value is 7, and this has been the sentry-python's version since its Initial commit in June 2018.
"sentry_version": "7",
# sentry_secret is deprecated, as mentioned elsewhere in the code.
# sentry_client ... may be useful, let's figure that out later TODO
}.items())

29
compat/tests.py Normal file
View File

@@ -0,0 +1,29 @@
from unittest import TestCase
from .dsn import get_store_url, get_envelope_url, get_header_value
class DsnTestCase(TestCase):
def test_get_store_url(self):
self.assertEquals(
"https://hosted.bugsink/api/1/store/",
get_store_url("https://public_key@hosted.bugsink/1"))
self.assertEquals(
"https://hosted.bugsink/some/path/api/1/store/",
get_store_url("https://public_key@hosted.bugsink/some/path/1"))
def test_get_store_url_non_default_port(self):
self.assertEquals(
"http://hosted.bugsink:8000/api/1/store/",
get_store_url("http://public_key@hosted.bugsink:8000/1"))
def test_get_envelope_url(self):
self.assertEquals(
"https://hosted.bugsink/api/1/envelope/",
get_envelope_url("https://public_key@hosted.bugsink/1"))
def test_get_header_value(self):
self.assertEquals(
"Sentry sentry_key=public_key, sentry_version=7",
get_header_value("https://public_key@hosted.bugsink/1"))