From 2569ec4ac5c11a9db980dfc31d569d2bb78e3d3a Mon Sep 17 00:00:00 2001 From: Klaas van Schelven Date: Tue, 19 Nov 2024 09:25:27 +0100 Subject: [PATCH] SDK instructions factored out to separate package --- bugsink/settings/default.py | 1 + .../templates/projects/project_sdk_setup.html | 70 +------------------ projects/views.py | 20 ++++++ requirements.txt | 1 + 4 files changed, 25 insertions(+), 67 deletions(-) diff --git a/bugsink/settings/default.py b/bugsink/settings/default.py index 21096d9..59ff7b8 100644 --- a/bugsink/settings/default.py +++ b/bugsink/settings/default.py @@ -56,6 +56,7 @@ INSTALLED_APPS = [ 'tailwind', # As currently set up, this is also needed in production (templatetags) 'admin_auto_filters', + 'sdkconf', ] BUGSINK_APPS = [ diff --git a/projects/templates/projects/project_sdk_setup.html b/projects/templates/projects/project_sdk_setup.html index 63af10e..fe02953 100644 --- a/projects/templates/projects/project_sdk_setup.html +++ b/projects/templates/projects/project_sdk_setup.html @@ -34,9 +34,7 @@ Install the SDK using pip: -
-
pip install sentry-sdk
-
+ {{ install }}

Step 2: Initialize the SDK

@@ -44,69 +42,8 @@ Initialize the SDK with your DSN: -{% comment %} add hoc construction of highlighted code: -from pygments import highlight -from pygments.lexers import PythonLexer -from pygments.formatters import HtmlFormatter -lexer = PythonLexer() -code = """import sentry_sdk -sentry_sdk.init( - "REPLACEME", + {{ conf }} - # The SDK's default is to not send PII; for a SaaS solution this is probably - # the right choice, but when you're using Bugsink (i.e. self-hosted), it's - # more likely that you want to send everything (and e.g. be able to see - # which user was affected by a certain event). Uncomment the following line - # to send PII. - # send_default_pii=True, - - # The SDK's default is to be quite conservative with the nr of local - # variables it sends per frame (the default is is 10). If you want to see - # more, you may either proceed with the monkey patching described in the - # issue below, or set max_request_body_size to "always" (which will send - # everything). Note that this may lead to very large messages, which may be - # dropped by the server (but the server is under your control, so you can - # change that). see https://github.com/getsentry/sentry-python/issues/377 - # max_request_body_size="always", - - # Setting up the release is highly recommended. The SDK will try to infer - # it, but explicitly setting it is more reliable. - # release=..., - - # Bugsink intentionally does not support traces. No need to send them then - traces_sample_rate=0, -)""" -print(highlight(code, lexer, HtmlFormatter()).replace("highlight", "p-4 mt-4 bg-slate-50 syntax-coloring").replace("REPLACEME", "{{ project.dsn }}")) -{% endcomment %} - -
import sentry_sdk
-sentry_sdk.init(
-    "{{ project.dsn }}",
-
-    # The SDK's default is to not send PII; for a SaaS solution this is probably
-    # the right choice, but when you're using Bugsink (i.e. self-hosted), it's
-    # more likely that you want to send everything (and e.g. be able to see
-    # which user was affected by a certain event). Uncomment the following line
-    # to send PII.
-    # send_default_pii=True,
-
-    # The SDK's default is to be quite conservative with the nr of local
-    # variables it sends per frame (the default is is 10). If you want to see
-    # more, you may either proceed with the monkey patching described in the
-    # issue below, or set max_request_body_size to "always" (which will send
-    # everything). Note that this may lead to very large messages, which may be
-    # dropped by the server (but the server is under your control, so you can
-    # change that). see https://github.com/getsentry/sentry-python/issues/377
-    # max_request_body_size="always",
-
-    # Setting up the release is highly recommended. The SDK will try to infer
-    # it, but explicitly setting it is more reliable.
-    # release=...,
-
-    # Bugsink intentionally does not support traces. No need to send them then.
-    traces_sample_rate=0,  
-)
-
For integration-specific (Django, Flask, etc) notes, see the Sentry documentation.
@@ -117,8 +54,7 @@ print(highlight(code, lexer, HtmlFormatter()).replace("highlight", "p-4 mt-4 bg- Verify the setup by sending an event: -
raise Exception("Raised Exception on purpose to send it to Bugsink")
-
+ {{ verify }}
Your event should now appear in the list of open issues. diff --git a/projects/views.py b/projects/views.py index a01b8dc..4c89753 100644 --- a/projects/views.py +++ b/projects/views.py @@ -1,5 +1,10 @@ from datetime import timedelta +from pygments import highlight +from pygments.lexers import PythonLexer, TextLexer +from pygments.formatters import HtmlFormatter + +from django.template.loader import get_template from django.shortcuts import render from django.db import models from django.shortcuts import redirect @@ -10,6 +15,7 @@ from django.contrib import messages from django.contrib.auth import logout from django.urls import reverse from django.utils import timezone +from django.utils.safestring import mark_safe from users.models import EmailVerification from teams.models import TeamMembership, Team, TeamRole @@ -379,6 +385,14 @@ def project_members_accept(request, project_pk): return render(request, "projects/project_members_accept.html", {"project": project, "membership": membership}) +def _sdk_code(part, context, lexer): + template = get_template("sdkconf/python/%s" % part) + code = template.render(context) + + return mark_safe( + highlight(code, lexer, HtmlFormatter()).replace("highlight", "p-4 mt-4 bg-slate-50 syntax-coloring")) + + @atomic_for_request_method def project_sdk_setup(request, project_pk): project = Project.objects.get(id=project_pk) @@ -387,6 +401,12 @@ def project_sdk_setup(request, project_pk): accepted=True).exists(): raise PermissionDenied("You are not a member of this project") + # NOTE about lexers:: I have bugsink/pyments_extensions; but the platforms mentioned there don't necessarily map to + # what I will make selectable here. "We'll see" whether yet another lookup dict will be needed. + return render(request, "projects/project_sdk_setup.html", { "project": project, + "install": _sdk_code("install", {}, TextLexer()), + "conf": _sdk_code("conf", {"dsn": project.dsn}, PythonLexer()), + "verify": _sdk_code("verify", {}, PythonLexer()), }) diff --git a/requirements.txt b/requirements.txt index 8ff556b..9c144c9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,3 +15,4 @@ monofy==1.1.* user_agents fastjsonschema verbose_csrf_middleware +sdkconf