We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
psycopg2 cursor's __enter__ method is not patched to be traced
See behavior here:

```python
>>> import ddtrace
>>> ddtrace.patch_all()
>>> import psycopg2
>>> conn = psycopg2.connect('postgresql://localhost')
>>> print(type(conn.cursor()))
<class 'ddtrace.contrib.dbapi.TracedCursor'>
>>> with conn.cursor() as cur:
...     print(type(cur))
<type 'psycopg2.extensions.cursor'>
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `ddtrace/contrib/dbapi/__init__.py`
Content:
```
1 """
2 Generic dbapi tracing code.
3 """
4 
5 # stdlib
6 import logging
7 
8 # 3p
9 import wrapt
10 
11 # project
12 from ddtrace import Pin
13 from ddtrace.ext import sql
14 
15 
16 log = logging.getLogger(__name__)
17 
18 
19 class TracedCursor(wrapt.ObjectProxy):
20     """ TracedCursor wraps a psql cursor and traces it's queries. """
21 
22     _datadog_pin = None
23     _datadog_name = None
24 
25     def __init__(self, cursor, pin):
26         super(TracedCursor, self).__init__(cursor)
27         self._datadog_pin = pin
28         name = pin.app or 'sql'
29         self._datadog_name = '%s.query' % name
30 
31     def executemany(self, query, *args, **kwargs):
32         pin = self._datadog_pin
33         if not pin or not pin.enabled():
34             return self.__wrapped__.executemany(query, *args, **kwargs)
35         service = pin.service
36 
37         # FIXME[matt] properly handle kwargs here. arg names can be different
38         # with different libs.
39         with pin.tracer.trace(self._datadog_name, service=service, resource=query) as s:
40             s.span_type = sql.TYPE
41             s.set_tag(sql.QUERY, query)
42             s.set_tags(pin.tags)
43             s.set_tag("sql.executemany", "true")
44             try:
45                 return self.__wrapped__.executemany(query, *args, **kwargs)
46             finally:
47                 s.set_metric("db.rowcount", self.rowcount)
48 
49     def execute(self, query, *args, **kwargs):
50         pin = self._datadog_pin
51         if not pin or not pin.enabled():
52             return self.__wrapped__.execute(query, *args, **kwargs)
53 
54         service = pin.service
55         with pin.tracer.trace(self._datadog_name, service=service, resource=query) as s:
56             s.span_type = sql.TYPE
57             s.set_tag(sql.QUERY, query)
58             s.set_tags(pin.tags)
59             try:
60                 return self.__wrapped__.execute(query, *args, **kwargs)
61             finally:
62                 s.set_metric("db.rowcount", self.rowcount)
63 
64     def callproc(self, proc, args):
65         pin = self._datadog_pin
66         if not pin or not pin.enabled():
67             return self.__wrapped__.callproc(proc, args)
68 
69         with pin.tracer.trace(self._datadog_name, service=pin.service, resource=proc) as s:
70             s.span_type = sql.TYPE
71             s.set_tag(sql.QUERY, proc)
72             s.set_tags(pin.tags)
73             try:
74                 return self.__wrapped__.callproc(proc, args)
75             finally:
76                 s.set_metric("db.rowcount", self.rowcount)
77 
78 
79 class TracedConnection(wrapt.ObjectProxy):
80     """ TracedConnection wraps a Connection with tracing code. """
81 
82     _datadog_pin = None
83 
84     def __init__(self, conn):
85         super(TracedConnection, self).__init__(conn)
86         name = _get_vendor(conn)
87         Pin(service=name, app=name).onto(self)
88 
89     def cursor(self, *args, **kwargs):
90         cursor = self.__wrapped__.cursor(*args, **kwargs)
91         pin = self._datadog_pin
92         if not pin:
93             return cursor
94         return TracedCursor(cursor, pin)
95 
96 
97 def _get_vendor(conn):
98     """ Return the vendor (e.g postgres, mysql) of the given
99         database.
100     """
101     try:
102         name = _get_module_name(conn)
103     except Exception:
104         log.debug("couldnt parse module name", exc_info=True)
105         name = "sql"
106     return sql.normalize_vendor(name)
107 
108 def _get_module_name(conn):
109     return conn.__class__.__module__.split('.')[0]
110
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/ddtrace/contrib/dbapi/__init__.py b/ddtrace/contrib/dbapi/__init__.py
--- a/ddtrace/contrib/dbapi/__init__.py
+++ b/ddtrace/contrib/dbapi/__init__.py
@@ -75,6 +75,15 @@
             finally:
                 s.set_metric("db.rowcount", self.rowcount)
 
+    def __enter__(self):
+        # previous versions of the dbapi didn't support context managers. let's
+        # reference the func that would be called to ensure that errors
+        # messages will be the same.
+        self.__wrapped__.__enter__
+
+        # and finally, yield the traced cursor.
+        return self
+
 
 class TracedConnection(wrapt.ObjectProxy):
     """ TracedConnection wraps a Connection with tracing code. """

{"golden_diff": "diff --git a/ddtrace/contrib/dbapi/__init__.py b/ddtrace/contrib/dbapi/__init__.py\n--- a/ddtrace/contrib/dbapi/__init__.py\n+++ b/ddtrace/contrib/dbapi/__init__.py\n@@ -75,6 +75,15 @@\n             finally:\n                 s.set_metric(\"db.rowcount\", self.rowcount)\n \n+    def __enter__(self):\n+        # previous versions of the dbapi didn't support context managers. let's\n+        # reference the func that would be called to ensure that errors\n+        # messages will be the same.\n+        self.__wrapped__.__enter__\n+\n+        # and finally, yield the traced cursor.\n+        return self\n+\n \n class TracedConnection(wrapt.ObjectProxy):\n     \"\"\" TracedConnection wraps a Connection with tracing code. \"\"\"\n", "issue": "psycopg2 cursor's __enter__ method is not patched to be traced\nSee behavior here:\r\n\r\n```python\r\n>>> import ddtrace\r\n>>> ddtrace.patch_all()\r\n>>> import psycopg2\r\n>>> conn = psycopg2.connect('postgresql://localhost')\r\n>>> print(type(conn.cursor()))\r\n<class 'ddtrace.contrib.dbapi.TracedCursor'>\r\n>>> with conn.cursor() as cur:\r\n...     print(type(cur))\r\n<type 'psycopg2.extensions.cursor'>\r\n```\n", "before_files": [{"content": "\"\"\"\nGeneric dbapi tracing code.\n\"\"\"\n\n# stdlib\nimport logging\n\n# 3p\nimport wrapt\n\n# project\nfrom ddtrace import Pin\nfrom ddtrace.ext import sql\n\n\nlog = logging.getLogger(__name__)\n\n\nclass TracedCursor(wrapt.ObjectProxy):\n    \"\"\" TracedCursor wraps a psql cursor and traces it's queries. \"\"\"\n\n    _datadog_pin = None\n    _datadog_name = None\n\n    def __init__(self, cursor, pin):\n        super(TracedCursor, self).__init__(cursor)\n        self._datadog_pin = pin\n        name = pin.app or 'sql'\n        self._datadog_name = '%s.query' % name\n\n    def executemany(self, query, *args, **kwargs):\n        pin = self._datadog_pin\n        if not pin or not pin.enabled():\n            return self.__wrapped__.executemany(query, *args, **kwargs)\n        service = pin.service\n\n        # FIXME[matt] properly handle kwargs here. arg names can be different\n        # with different libs.\n        with pin.tracer.trace(self._datadog_name, service=service, resource=query) as s:\n            s.span_type = sql.TYPE\n            s.set_tag(sql.QUERY, query)\n            s.set_tags(pin.tags)\n            s.set_tag(\"sql.executemany\", \"true\")\n            try:\n                return self.__wrapped__.executemany(query, *args, **kwargs)\n            finally:\n                s.set_metric(\"db.rowcount\", self.rowcount)\n\n    def execute(self, query, *args, **kwargs):\n        pin = self._datadog_pin\n        if not pin or not pin.enabled():\n            return self.__wrapped__.execute(query, *args, **kwargs)\n\n        service = pin.service\n        with pin.tracer.trace(self._datadog_name, service=service, resource=query) as s:\n            s.span_type = sql.TYPE\n            s.set_tag(sql.QUERY, query)\n            s.set_tags(pin.tags)\n            try:\n                return self.__wrapped__.execute(query, *args, **kwargs)\n            finally:\n                s.set_metric(\"db.rowcount\", self.rowcount)\n\n    def callproc(self, proc, args):\n        pin = self._datadog_pin\n        if not pin or not pin.enabled():\n            return self.__wrapped__.callproc(proc, args)\n\n        with pin.tracer.trace(self._datadog_name, service=pin.service, resource=proc) as s:\n            s.span_type = sql.TYPE\n            s.set_tag(sql.QUERY, proc)\n            s.set_tags(pin.tags)\n            try:\n                return self.__wrapped__.callproc(proc, args)\n            finally:\n                s.set_metric(\"db.rowcount\", self.rowcount)\n\n\nclass TracedConnection(wrapt.ObjectProxy):\n    \"\"\" TracedConnection wraps a Connection with tracing code. \"\"\"\n\n    _datadog_pin = None\n\n    def __init__(self, conn):\n        super(TracedConnection, self).__init__(conn)\n        name = _get_vendor(conn)\n        Pin(service=name, app=name).onto(self)\n\n    def cursor(self, *args, **kwargs):\n        cursor = self.__wrapped__.cursor(*args, **kwargs)\n        pin = self._datadog_pin\n        if not pin:\n            return cursor\n        return TracedCursor(cursor, pin)\n\n\ndef _get_vendor(conn):\n    \"\"\" Return the vendor (e.g postgres, mysql) of the given\n        database.\n    \"\"\"\n    try:\n        name = _get_module_name(conn)\n    except Exception:\n        log.debug(\"couldnt parse module name\", exc_info=True)\n        name = \"sql\"\n    return sql.normalize_vendor(name)\n\ndef _get_module_name(conn):\n    return conn.__class__.__module__.split('.')[0]\n", "path": "ddtrace/contrib/dbapi/__init__.py"}], "after_files": [{"content": "\"\"\"\nGeneric dbapi tracing code.\n\"\"\"\n\n# stdlib\nimport logging\n\n# 3p\nimport wrapt\n\n# project\nfrom ddtrace import Pin\nfrom ddtrace.ext import sql\n\n\nlog = logging.getLogger(__name__)\n\n\nclass TracedCursor(wrapt.ObjectProxy):\n    \"\"\" TracedCursor wraps a psql cursor and traces it's queries. \"\"\"\n\n    _datadog_pin = None\n    _datadog_name = None\n\n    def __init__(self, cursor, pin):\n        super(TracedCursor, self).__init__(cursor)\n        self._datadog_pin = pin\n        name = pin.app or 'sql'\n        self._datadog_name = '%s.query' % name\n\n    def executemany(self, query, *args, **kwargs):\n        pin = self._datadog_pin\n        if not pin or not pin.enabled():\n            return self.__wrapped__.executemany(query, *args, **kwargs)\n        service = pin.service\n\n        # FIXME[matt] properly handle kwargs here. arg names can be different\n        # with different libs.\n        with pin.tracer.trace(self._datadog_name, service=service, resource=query) as s:\n            s.span_type = sql.TYPE\n            s.set_tag(sql.QUERY, query)\n            s.set_tags(pin.tags)\n            s.set_tag(\"sql.executemany\", \"true\")\n            try:\n                return self.__wrapped__.executemany(query, *args, **kwargs)\n            finally:\n                s.set_metric(\"db.rowcount\", self.rowcount)\n\n    def execute(self, query, *args, **kwargs):\n        pin = self._datadog_pin\n        if not pin or not pin.enabled():\n            return self.__wrapped__.execute(query, *args, **kwargs)\n\n        service = pin.service\n        with pin.tracer.trace(self._datadog_name, service=service, resource=query) as s:\n            s.span_type = sql.TYPE\n            s.set_tag(sql.QUERY, query)\n            s.set_tags(pin.tags)\n            try:\n                return self.__wrapped__.execute(query, *args, **kwargs)\n            finally:\n                s.set_metric(\"db.rowcount\", self.rowcount)\n\n    def callproc(self, proc, args):\n        pin = self._datadog_pin\n        if not pin or not pin.enabled():\n            return self.__wrapped__.callproc(proc, args)\n\n        with pin.tracer.trace(self._datadog_name, service=pin.service, resource=proc) as s:\n            s.span_type = sql.TYPE\n            s.set_tag(sql.QUERY, proc)\n            s.set_tags(pin.tags)\n            try:\n                return self.__wrapped__.callproc(proc, args)\n            finally:\n                s.set_metric(\"db.rowcount\", self.rowcount)\n\n    def __enter__(self):\n        # previous versions of the dbapi didn't support context managers. let's\n        # reference the func that would be called to ensure that errors\n        # messages will be the same.\n        self.__wrapped__.__enter__\n\n        # and finally, yield the traced cursor.\n        return self\n\n\nclass TracedConnection(wrapt.ObjectProxy):\n    \"\"\" TracedConnection wraps a Connection with tracing code. \"\"\"\n\n    _datadog_pin = None\n\n    def __init__(self, conn):\n        super(TracedConnection, self).__init__(conn)\n        name = _get_vendor(conn)\n        Pin(service=name, app=name).onto(self)\n\n    def cursor(self, *args, **kwargs):\n        cursor = self.__wrapped__.cursor(*args, **kwargs)\n        pin = self._datadog_pin\n        if not pin:\n            return cursor\n        return TracedCursor(cursor, pin)\n\n\ndef _get_vendor(conn):\n    \"\"\" Return the vendor (e.g postgres, mysql) of the given\n        database.\n    \"\"\"\n    try:\n        name = _get_module_name(conn)\n    except Exception:\n        log.debug(\"couldnt parse module name\", exc_info=True)\n        name = \"sql\"\n    return sql.normalize_vendor(name)\n\ndef _get_module_name(conn):\n    return conn.__class__.__module__.split('.')[0]\n", "path": "ddtrace/contrib/dbapi/__init__.py"}]}