question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Django integration: connect Javascript SDK

See original GitHub issue

I’m completely stuck with connecting the Python SDK (I’m using Django) and the Javascript SDK. I’ve installed sentry-python and it sends events to my onPrem Sentry installation. Now I want to connect it with the Frontend using Sentry SDK.

I followed the docs and I think I need to add the trace_id as a <meta> tag in my template. Either I’m too dumb or the docs could be improved, but I’ve spent 3+hrs trying to get it done with no success.

Environment

How do you use Sentry? Sentry onPrem, version 21.8.0, installed with the recommended Docker service using install.sh. Sentry seems to work, I can receive errors.

Which SDK and version? Backend: Python 3.8.5, Django 3.2.6 using sentry-sdk, version 1.3.1 Frontend: Basic HTML, Bootstrap, nothing fancy

Python virtual environment:

asgiref==3.4.1
certifi==2021.5.30
Django==3.2.6
django-appconf==1.0.4
django-compressor==2.4.1
django-debug-toolbar==3.2.2
django-extensions==3.1.3
django-honeypot==1.0.1
psycopg2-binary==2.9.1
pytz==2021.1
rcssmin==1.0.6
rjsmin==1.1.0
sentry-sdk==1.3.1
six==1.16.0
sqlparse==0.4.1
urllib3==1.26.6

Steps to Reproduce

  1. Set up the Django integration following the official docs. Unhandled errors in Django are working and being sent to my Sentry instance, so I guess this part is not in question.
  2. Following Connecting Services, I decided it would be easiest to add a <meta> tag in my Django template, so I followed this:
<meta name="sentry-trace" content="{{ span.toSentryTrace() }}" />

I understand that the docs are not Django-specific and I can’t call a function within a template in Django. So I’ve tried a lot of different things, but I don’t seem to have the trace_id available in my template, nor in the Python environment.

Do I need to add it using a custom template tag? Or is there a middleware I’m not aware of? I’ve googled for “Django Sentry trace_id” and a lot of other things, searched Github for examples, but didn’t find anything that worked.

How do I get that to work? I suspect other Django developers are struggling with that as well, and I hoped it was in the documentation, especially because Django is listed as an integration. I’m willing to contribute this to the docs if appreciated and someone can tell me a recommended way 😃

Expected Result

Docs including best-practice Instructions for connecting Django templates.

Actual Result

🙇‍♂️

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sl0thentr0pycommented, Jan 27, 2022

@tony they’re functionally equivalent but the first one (configure_scope) is more ‘canonical’ usage and is what we do internally in integrations as well. Hub.current always takes the current thread into account and is cloned per thread.

0reactions
tonycommented, Jan 22, 2022

@AbhiPrasad I think it’d valuable to have some documentation or examples with django. I’m equally curious.

Thank you for your example at https://github.com/getsentry/sentry-python/issues/1171#issuecomment-906687493

I am curious, what would be the difference between:

def some_view(request):
    with sentry_sdk.configure_scope() as scope:
        span = scope.span
        return render(request, 'template.html', {'sentry-trace': span.to_traceparent()})

and

def some_view(request):
    span = sentry_sdk.Hub.current.scope.span    
    return render(request, 'template.html', {'sentry-trace': span.to_traceparent()})

Are there any risks from grabbing it via sentry_sdk.Hub.current.scope.span? Could that potentially cause issues if it was running on a server with multiple threads/workers/etc?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Integrate Django to any Javascript Front End // Django ...
Learn the basic technique to Integrate Django to any Javascript Front End // Django to React // Django to AngularRelated repo: ...
Read more >
JavaScript - Django documentation
While most of Django core is Python, the admin and gis contrib apps contain JavaScript code. Please follow these coding standards when writing...
Read more >
Build a modern web app using Django and Javascript
Learn how to organize your codebase and rapidly create a backend API from scratch. Use Django templates, AlpineJS and TailwindCSS to build a ......
Read more >
How to pass data to javascript in Django Framework
1. Integrating Django with Reactjs using Django REST Framework ; 2. Rendering Data-Frame to html template in table view using Django Framework ;...
Read more >
How do I integrate this with Django - Sendbird Community
The core SDK documentation for Javascript is located here: Send your first message | Chat JavaScript SDK | Sendbird Docs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found