Repository: getsentry/sentry

Issue:
sentry django send_fake_data errors out
## Important Details

How are you running Sentry?

* [ ] On-Premise docker [Version xyz]
* [ ] Saas (sentry.io)
* [x] Other [briefly describe your environment]
Development environment on macOS while working on sentry-plugins, with `sentry devserver`

## Description

When running `sentry django send_fake_data` a stacktrace is raised:

```
Traceback (most recent call last):
  File "MYPYTHONPATH/bin/sentry", line 11, in <module>
    load_entry_point('sentry', 'console_scripts', 'sentry')()
  File "MYPYTHONPATH/src/sentry/src/sentry/runner/__init__.py", line 161, in main
    cli(prog_name=get_prog(), obj={}, max_content_width=100)
  File "MYPYTHONPATH/lib/python2.7/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "MYPYTHONPATH/lib/python2.7/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "MYPYTHONPATH/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "MYPYTHONPATH/lib/python2.7/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "MYPYTHONPATH/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "MYPYTHONPATH/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "MYPYTHONPATH/src/sentry/src/sentry/runner/decorators.py", line 36, in inner
    return ctx.invoke(f, *args, **kwargs)
  File "MYPYTHONPATH/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "MYPYTHONPATH/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "MYPYTHONPATH/src/sentry/src/sentry/runner/commands/django.py", line 25, in django
    execute_from_command_line(argv=[ctx.command_path] + list(management_args))
  File "MYPYTHONPATH/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "MYPYTHONPATH/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "MYPYTHONPATH/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "MYPYTHONPATH/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "MYPYTHONPATH/src/sentry/src/sentry/management/commands/send_fake_data.py", line 75, in handle
    from raven.contrib.django.models import client
ImportError: No module named raven.contrib.django.models
```

## Steps to Reproduce

See above.

### What you expected to happen

Some fake data should magically appear!

### Possible Solution

Appears to be coming from this import:
https://github.com/getsentry/sentry/blob/317c65fbcd0f0b31a4e97c415bcf35256900a286/src/sentry/management/commands/send_fake_data.py#L75

Manually running `pip install raven` appears to solve it - no traceback appears, so I guess having raven installed, that solves the immediate issue.

However, I think that there's no events being sent anyhow - as perceived by this log:
```
[INFO] raven.contrib.django.client.DjangoClient: Raven is not configured (logging is disabled). Please see the documentation for more information. (message=u'Raven is not configured (logging is disabled). Please see the documentation for more information.')
Preparing to send events. Ctrl-C to exit.
```

I saw there's a push to go towards the unified python sdk - should this fix be overall done with that approach?



Discussion:
Hi, I guess this was an oversight of https://github.com/getsentry/sentry/pull/9949. `send_fake_data` should be ported to use `sentry_sdk` instead of raven.
I'm assigning myself but feel free to grab this issue if you get around to it

Resolving patch:
diff --git a/src/sentry/management/commands/send_fake_data.py b/src/sentry/management/commands/send_fake_data.py
--- a/src/sentry/management/commands/send_fake_data.py
+++ b/src/sentry/management/commands/send_fake_data.py
@@ -28,16 +28,14 @@ def exception(client):
         )
         try:
             raise next(exceptions)
-        except Exception:
+        except Exception as exc:
             email = next(emails)
-            return client.captureException(
-                data={
-                    "logger": next(loggers),
-                    "site": "web",
-                    "user": {"id": email, "email": email},
-                },
-                date=timestamp,
-            )
+            with client.configure_scope() as scope:
+                scope.user = {"id": email, "email": email}
+                scope.logger = next(loggers)
+                scope.site = "web"
+                scope.date = timestamp
+                return client.captureException(exc)
 
     return [exception]
 
@@ -53,7 +51,7 @@ def add_arguments(self, parser):
 
     def handle(self, **options):
         from django.conf import settings
-        from raven.contrib.django.models import client
+        from sentry.app import client
         from sentry.models import Project
 
         if not options["project"]:


Test patch: