{"action":"created","issue":{"url":"https://api.github.com/repos/getsentry/sentry-python/issues/161","repository_url":"https://api.github.com/repos/getsentry/sentry-python","labels_url":"https://api.github.com/repos/getsentry/sentry-python/issues/161/labels{/name}","comments_url":"https://api.github.com/repos/getsentry/sentry-python/issues/161/comments","events_url":"https://api.github.com/repos/getsentry/sentry-python/issues/161/events","html_url":"https://github.com/getsentry/sentry-python/issues/161","id":376083557,"node_id":"MDU6SXNzdWUzNzYwODM1NTc=","number":161,"title":"How to set user context on request","user":{"login":"kendallroth","id":12562420,"node_id":"MDQ6VXNlcjEyNTYyNDIw","avatar_url":"https://avatars1.githubusercontent.com/u/12562420?v=4","gravatar_id":"","url":"https://api.github.com/users/kendallroth","html_url":"https://github.com/kendallroth","followers_url":"https://api.github.com/users/kendallroth/followers","following_url":"https://api.github.com/users/kendallroth/following{/other_user}","gists_url":"https://api.github.com/users/kendallroth/gists{/gist_id}","starred_url":"https://api.github.com/users/kendallroth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kendallroth/subscriptions","organizations_url":"https://api.github.com/users/kendallroth/orgs","repos_url":"https://api.github.com/users/kendallroth/repos","events_url":"https://api.github.com/users/kendallroth/events{/privacy}","received_events_url":"https://api.github.com/users/kendallroth/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-10-31T17:41:24Z","updated_at":"2018-11-01T19:37:09Z","closed_at":null,"author_association":"NONE","body":"I've been trying to get user context working for the past few days with no success unfortunately. I have created a Django middleware that does run with each received request and correctly gets the user from the request context, but the scope doesn't get updated properly. No matter what I try, the user data is never sent back to Sentry.io.\r\n\r\n**See the bottom code example**\r\n\r\n### Issue with `send_default_pii`\r\n\r\nAdditionally, I noticed that setting `send_default_pii=True` appears to actually send less tags to Sentry. For example, with `send_default_pii=False` I get these tags:\r\n\r\n![image](https://user-images.githubusercontent.com/12562420/47806466-3b5d8500-dd10-11e8-9288-2f4ee3e42b4d.png)\r\n\r\nHowever, with `send_default_pii=True` I only get these tags, which is backwards from what I would expect:\r\n\r\n![image](https://user-images.githubusercontent.com/12562420/47806684-b45cdc80-dd10-11e8-8bb6-54ce4d9e19f7.png)\r\n\r\nThe docs mention the following:\r\n> Personally identifiable information (such as user ids, usernames, cookies, authorization headers, ip addresses) is excluded unless send_default_pii is set to True.\r\n\r\nAlso from the docs (same page):\r\n> If you use django.contrib.auth and have set send_default_pii=True in your call to init, user data (current user id, email address, username) is attached to the event\r\n\r\n### Details\r\n\r\n- **Server**: Django with Graphene\r\n\r\n```python\r\n# settings.py\r\n\r\nimport sentry_sdk\r\nfrom sentry_sdk.integrations.django import DjangoIntegration\r\n\r\nMIDDLWARE = [\r\n    ..\r\n   'django.contrib.auth.middleware.AuthenticationMiddleware',\r\n    # Must be in both Django middleware and Graphene middleware (behaves as both)\r\n    #   Needs to be in middleware before sentry middleware to set user properly before trying to set Sentry context\r\n    'graphql_jwt.middleware.JSONWebTokenMiddleware',\r\n    ...\r\n    # Middleware to intercept requests and add Sentry user to context\r\n    'util.middleware.SentryMiddleware'\r\n]\r\n\r\nGRAPHENE = {\r\n    'SCHEMA': '....',\r\n    'MIDDLEWARE': [\r\n        ...\r\n        # Must be in both Django middleware and Graphene middleware (behaves as both)\r\n        #   Apparently meant to be Graphene middleware (as according to their docs)?\r\n        'graphql_jwt.middleware.JSONWebTokenMiddleware',\r\n    ]\r\n}\r\n\r\n...\r\n\r\n# Sentry error logging (at end of file)\r\nsentry_sdk.init(\r\n    dsn=os.getenv(\"API_SENTRY_DSN\"),\r\n    environment=os.getenv(\"ENV_NAME\"),\r\n    integrations=[DjangoIntegration()],\r\n    #  QUESTION: Setting to True appears to not send as much information to Sentry (tags)?\r\n    send_default_pii=True\r\n)\r\n```\r\n\r\nAm I initializing the SDK in the right place (docs mention the `settings.py` file)?\r\n\r\n```python\r\n# middleware.py\r\n\r\nfrom sentry_sdk import configure_scope\r\n\r\nclass SentryMiddleware:\r\n  def __init__(self, get_response):\r\n    \"\"\"Configuration and initialization.\"\"\"\r\n    self.get_response = get_response\r\n\r\n  def __call__(self, request):\r\n    \"\"\"Hook in each request before view is resolved.\"\"\"\r\n    # NOTE: User does exist here (if authenticated) and is set properly on the request\r\n    if request.user and request.user.is_authenticated:\r\n      user = {\r\n        \"email\": request.user.email,\r\n        \"id\": request.user.id,\r\n        \"name\": request.user.profile.full_name,\r\n        \"username\": request.user.username\r\n      }\r\n\r\n      # Does this actually modify the Sentry scope? It doesn't appear to send user details to Sentry...\r\n      with configure_scope() as scope:\r\n        scope.user = request.user\r\n\r\n    response = self.get_response(request)\r\n\r\n    return response\r\n```\r\n\r\nAm I using the middleware correctly? Any help would be appreciated\r\n"},"comment":{"url":"https://api.github.com/repos/getsentry/sentry-python/issues/comments/435160353","html_url":"https://github.com/getsentry/sentry-python/issues/161#issuecomment-435160353","issue_url":"https://api.github.com/repos/getsentry/sentry-python/issues/161","id":435160353,"node_id":"MDEyOklzc3VlQ29tbWVudDQzNTE2MDM1Mw==","user":{"login":"kendallroth","id":12562420,"node_id":"MDQ6VXNlcjEyNTYyNDIw","avatar_url":"https://avatars1.githubusercontent.com/u/12562420?v=4","gravatar_id":"","url":"https://api.github.com/users/kendallroth","html_url":"https://github.com/kendallroth","followers_url":"https://api.github.com/users/kendallroth/followers","following_url":"https://api.github.com/users/kendallroth/following{/other_user}","gists_url":"https://api.github.com/users/kendallroth/gists{/gist_id}","starred_url":"https://api.github.com/users/kendallroth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kendallroth/subscriptions","organizations_url":"https://api.github.com/users/kendallroth/orgs","repos_url":"https://api.github.com/users/kendallroth/repos","events_url":"https://api.github.com/users/kendallroth/events{/privacy}","received_events_url":"https://api.github.com/users/kendallroth/received_events","type":"User","site_admin":false},"created_at":"2018-11-01T19:37:09Z","updated_at":"2018-11-01T19:37:09Z","author_association":"NONE","body":"You are completely correct about `scope.user`, my apologies (didn't change that back when assembling example).\r\n\r\nI figured that was what the `DjangoIntegration` would do, but since it hadn't worked I thought I'd try it myself to see what was going wrong. I was able to verify that `request.user` was definitely set, but couldn't attach to the Sentry scope successfully.\r\n\r\nThe only difference between the two images (showing tag diff) was `send_default_pii`, which strikes me as very strange. I can't understand what may have caused that then, as we only use default other middleware.\r\n\r\nAs far as server, I'm not entirely sure what server we are using TBH, but our Docker file contains this:\r\n\r\n```bash\r\nEXPOSE ####\r\nCMD [\"./manage.py\", \"runserver\", \"0.0.0.0:####\"]\r\nENTRYPOINT [\"pipenv\", \"run\"]\r\n```\r\n\r\nOur `urls.py` file renders the Graphene \"view\":\r\n\r\n```python\r\nfrom django.urls import path\r\nfrom django.conf.urls import url\r\nfrom graphene_django.views import GraphQLView\r\n\r\nurlpatterns = [\r\n    path('admin/', admin.site.urls),\r\n    url(r'^graphql', csrf_exempt(\r\n        GraphQLView.as_view(graphiql=True, schema=schema)\r\n    ))\r\n]\r\n```\r\n\r\nAdditionally, we do have a file `wsgi.py` in the same directory as `settings.py`:\r\n\r\n```python\r\n\"\"\"\r\nWSGI config for [APP_NAME] project.\r\nIt exposes the WSGI callable as a module-level variable named ``application``.\r\nFor more information on this file, see\r\nhttps://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/\r\n\"\"\"\r\n\r\nimport os\r\nfrom django.core.wsgi import get_wsgi_application\r\n\r\nos.environ.setdefault(\"DJANGO_SETTINGS_MODULE\", \"[APP_NAME].settings\")\r\n\r\napplication = get_wsgi_application()\r\n```"}}
{"actor":{"display_login":"kendallroth"}}