We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
"cannot decode v0.3 services payload" error when tracing aiopg
I have a Python application using aiopg and monitored by Datadog. When starting, it sends a bad frame to `trace-agent` and then everything goes fine.

Versions:
- datadog-agent: 6.2.1
- ddtrace-py: 0.12.1
- aiopg: 0.14.0

Here is a minimalist test case:

```python
import asyncio
import aiopg

from ddtrace import patch

patch(aiopg=True)


async def main():
    async with aiopg.connect(host=None) as db:
        pass

    while True:
        await asyncio.sleep(0.1)


loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```

This logs the following error:
```
failed_to_send services to Agent: HTTP error status 400, reason Bad Request, message Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Mon, 18 Jun 2018 15:25:18 GMT
Content-Length: 59
```

And then `trace-agent` reports:
```
trace-agent[4437]: 2018-06-18 15:31:16 ERROR (receiver.go:275) - cannot decode v0.3 services payload: msgp: attempted to decode type "nil" with method for "str"
```

I believe this is related to https://github.com/DataDog/datadog-trace-agent/issues/350.

--- 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/aiopg/connection.py`
Content:
```
1 import asyncio
2 import wrapt
3 
4 from aiopg.utils import _ContextManager
5 
6 from .. import dbapi
7 from ...ext import sql
8 from ...pin import Pin
9 
10 
11 class AIOTracedCursor(wrapt.ObjectProxy):
12     """ TracedCursor wraps a psql cursor and traces it's queries. """
13 
14     def __init__(self, cursor, pin):
15         super(AIOTracedCursor, self).__init__(cursor)
16         pin.onto(self)
17         name = pin.app or 'sql'
18         self._datadog_name = '%s.query' % name
19 
20     @asyncio.coroutine
21     def _trace_method(self, method, resource, extra_tags, *args, **kwargs):
22         pin = Pin.get_from(self)
23         if not pin or not pin.enabled():
24             result = yield from method(*args, **kwargs)  # noqa: E999
25             return result
26         service = pin.service
27 
28         with pin.tracer.trace(self._datadog_name, service=service,
29                               resource=resource) as s:
30             s.span_type = sql.TYPE
31             s.set_tag(sql.QUERY, resource)
32             s.set_tags(pin.tags)
33             s.set_tags(extra_tags)
34 
35             try:
36                 result = yield from method(*args, **kwargs)
37                 return result
38             finally:
39                 s.set_metric("db.rowcount", self.rowcount)
40 
41     @asyncio.coroutine
42     def executemany(self, query, *args, **kwargs):
43         # FIXME[matt] properly handle kwargs here. arg names can be different
44         # with different libs.
45         result = yield from self._trace_method(
46             self.__wrapped__.executemany, query, {'sql.executemany': 'true'},
47             query, *args, **kwargs)  # noqa: E999
48         return result
49 
50     @asyncio.coroutine
51     def execute(self, query, *args, **kwargs):
52         result = yield from self._trace_method(
53             self.__wrapped__.execute, query, {}, query, *args, **kwargs)
54         return result
55 
56     @asyncio.coroutine
57     def callproc(self, proc, args):
58         result = yield from self._trace_method(
59             self.__wrapped__.callproc, proc, {}, proc, args)  # noqa: E999
60         return result
61 
62 
63 class AIOTracedConnection(wrapt.ObjectProxy):
64     """ TracedConnection wraps a Connection with tracing code. """
65 
66     def __init__(self, conn):
67         super(AIOTracedConnection, self).__init__(conn)
68         name = dbapi._get_vendor(conn)
69         Pin(service=name, app=name).onto(self)
70 
71     def cursor(self, *args, **kwargs):
72         # unfortunately we also need to patch this method as otherwise "self"
73         # ends up being the aiopg connection object
74         coro = self._cursor(*args, **kwargs)
75         return _ContextManager(coro)
76 
77     @asyncio.coroutine
78     def _cursor(self, *args, **kwargs):
79         cursor = yield from self.__wrapped__._cursor(*args, **kwargs)  # noqa: E999
80         pin = Pin.get_from(self)
81         if not pin:
82             return cursor
83         return AIOTracedCursor(cursor, pin)
84
```
--- 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/aiopg/connection.py b/ddtrace/contrib/aiopg/connection.py
--- a/ddtrace/contrib/aiopg/connection.py
+++ b/ddtrace/contrib/aiopg/connection.py
@@ -4,8 +4,8 @@
 from aiopg.utils import _ContextManager
 
 from .. import dbapi
-from ...ext import sql
 from ...pin import Pin
+from ...ext import sql, AppTypes
 
 
 class AIOTracedCursor(wrapt.ObjectProxy):
@@ -63,10 +63,11 @@
 class AIOTracedConnection(wrapt.ObjectProxy):
     """ TracedConnection wraps a Connection with tracing code. """
 
-    def __init__(self, conn):
+    def __init__(self, conn, pin=None):
         super(AIOTracedConnection, self).__init__(conn)
         name = dbapi._get_vendor(conn)
-        Pin(service=name, app=name).onto(self)
+        db_pin = pin or Pin(service=name, app=name, app_type=AppTypes.db)
+        db_pin.onto(self)
 
     def cursor(self, *args, **kwargs):
         # unfortunately we also need to patch this method as otherwise "self"

{"golden_diff": "diff --git a/ddtrace/contrib/aiopg/connection.py b/ddtrace/contrib/aiopg/connection.py\n--- a/ddtrace/contrib/aiopg/connection.py\n+++ b/ddtrace/contrib/aiopg/connection.py\n@@ -4,8 +4,8 @@\n from aiopg.utils import _ContextManager\n \n from .. import dbapi\n-from ...ext import sql\n from ...pin import Pin\n+from ...ext import sql, AppTypes\n \n \n class AIOTracedCursor(wrapt.ObjectProxy):\n@@ -63,10 +63,11 @@\n class AIOTracedConnection(wrapt.ObjectProxy):\n     \"\"\" TracedConnection wraps a Connection with tracing code. \"\"\"\n \n-    def __init__(self, conn):\n+    def __init__(self, conn, pin=None):\n         super(AIOTracedConnection, self).__init__(conn)\n         name = dbapi._get_vendor(conn)\n-        Pin(service=name, app=name).onto(self)\n+        db_pin = pin or Pin(service=name, app=name, app_type=AppTypes.db)\n+        db_pin.onto(self)\n \n     def cursor(self, *args, **kwargs):\n         # unfortunately we also need to patch this method as otherwise \"self\"\n", "issue": "\"cannot decode v0.3 services payload\" error when tracing aiopg\nI have a Python application using aiopg and monitored by Datadog. When starting, it sends a bad frame to `trace-agent` and then everything goes fine.\r\n\r\nVersions:\r\n- datadog-agent: 6.2.1\r\n- ddtrace-py: 0.12.1\r\n- aiopg: 0.14.0\r\n\r\nHere is a minimalist test case:\r\n\r\n```python\r\nimport asyncio\r\nimport aiopg\r\n\r\nfrom ddtrace import patch\r\n\r\npatch(aiopg=True)\r\n\r\n\r\nasync def main():\r\n    async with aiopg.connect(host=None) as db:\r\n        pass\r\n\r\n    while True:\r\n        await asyncio.sleep(0.1)\r\n\r\n\r\nloop = asyncio.get_event_loop()\r\nloop.run_until_complete(main())\r\n```\r\n\r\nThis logs the following error:\r\n```\r\nfailed_to_send services to Agent: HTTP error status 400, reason Bad Request, message Content-Type: text/plain; charset=utf-8\r\nX-Content-Type-Options: nosniff\r\nDate: Mon, 18 Jun 2018 15:25:18 GMT\r\nContent-Length: 59\r\n```\r\n\r\nAnd then `trace-agent` reports:\r\n```\r\ntrace-agent[4437]: 2018-06-18 15:31:16 ERROR (receiver.go:275) - cannot decode v0.3 services payload: msgp: attempted to decode type \"nil\" with method for \"str\"\r\n```\r\n\r\nI believe this is related to https://github.com/DataDog/datadog-trace-agent/issues/350.\n", "before_files": [{"content": "import asyncio\nimport wrapt\n\nfrom aiopg.utils import _ContextManager\n\nfrom .. import dbapi\nfrom ...ext import sql\nfrom ...pin import Pin\n\n\nclass AIOTracedCursor(wrapt.ObjectProxy):\n    \"\"\" TracedCursor wraps a psql cursor and traces it's queries. \"\"\"\n\n    def __init__(self, cursor, pin):\n        super(AIOTracedCursor, self).__init__(cursor)\n        pin.onto(self)\n        name = pin.app or 'sql'\n        self._datadog_name = '%s.query' % name\n\n    @asyncio.coroutine\n    def _trace_method(self, method, resource, extra_tags, *args, **kwargs):\n        pin = Pin.get_from(self)\n        if not pin or not pin.enabled():\n            result = yield from method(*args, **kwargs)  # noqa: E999\n            return result\n        service = pin.service\n\n        with pin.tracer.trace(self._datadog_name, service=service,\n                              resource=resource) as s:\n            s.span_type = sql.TYPE\n            s.set_tag(sql.QUERY, resource)\n            s.set_tags(pin.tags)\n            s.set_tags(extra_tags)\n\n            try:\n                result = yield from method(*args, **kwargs)\n                return result\n            finally:\n                s.set_metric(\"db.rowcount\", self.rowcount)\n\n    @asyncio.coroutine\n    def executemany(self, query, *args, **kwargs):\n        # FIXME[matt] properly handle kwargs here. arg names can be different\n        # with different libs.\n        result = yield from self._trace_method(\n            self.__wrapped__.executemany, query, {'sql.executemany': 'true'},\n            query, *args, **kwargs)  # noqa: E999\n        return result\n\n    @asyncio.coroutine\n    def execute(self, query, *args, **kwargs):\n        result = yield from self._trace_method(\n            self.__wrapped__.execute, query, {}, query, *args, **kwargs)\n        return result\n\n    @asyncio.coroutine\n    def callproc(self, proc, args):\n        result = yield from self._trace_method(\n            self.__wrapped__.callproc, proc, {}, proc, args)  # noqa: E999\n        return result\n\n\nclass AIOTracedConnection(wrapt.ObjectProxy):\n    \"\"\" TracedConnection wraps a Connection with tracing code. \"\"\"\n\n    def __init__(self, conn):\n        super(AIOTracedConnection, self).__init__(conn)\n        name = dbapi._get_vendor(conn)\n        Pin(service=name, app=name).onto(self)\n\n    def cursor(self, *args, **kwargs):\n        # unfortunately we also need to patch this method as otherwise \"self\"\n        # ends up being the aiopg connection object\n        coro = self._cursor(*args, **kwargs)\n        return _ContextManager(coro)\n\n    @asyncio.coroutine\n    def _cursor(self, *args, **kwargs):\n        cursor = yield from self.__wrapped__._cursor(*args, **kwargs)  # noqa: E999\n        pin = Pin.get_from(self)\n        if not pin:\n            return cursor\n        return AIOTracedCursor(cursor, pin)\n", "path": "ddtrace/contrib/aiopg/connection.py"}], "after_files": [{"content": "import asyncio\nimport wrapt\n\nfrom aiopg.utils import _ContextManager\n\nfrom .. import dbapi\nfrom ...pin import Pin\nfrom ...ext import sql, AppTypes\n\n\nclass AIOTracedCursor(wrapt.ObjectProxy):\n    \"\"\" TracedCursor wraps a psql cursor and traces it's queries. \"\"\"\n\n    def __init__(self, cursor, pin):\n        super(AIOTracedCursor, self).__init__(cursor)\n        pin.onto(self)\n        name = pin.app or 'sql'\n        self._datadog_name = '%s.query' % name\n\n    @asyncio.coroutine\n    def _trace_method(self, method, resource, extra_tags, *args, **kwargs):\n        pin = Pin.get_from(self)\n        if not pin or not pin.enabled():\n            result = yield from method(*args, **kwargs)  # noqa: E999\n            return result\n        service = pin.service\n\n        with pin.tracer.trace(self._datadog_name, service=service,\n                              resource=resource) as s:\n            s.span_type = sql.TYPE\n            s.set_tag(sql.QUERY, resource)\n            s.set_tags(pin.tags)\n            s.set_tags(extra_tags)\n\n            try:\n                result = yield from method(*args, **kwargs)\n                return result\n            finally:\n                s.set_metric(\"db.rowcount\", self.rowcount)\n\n    @asyncio.coroutine\n    def executemany(self, query, *args, **kwargs):\n        # FIXME[matt] properly handle kwargs here. arg names can be different\n        # with different libs.\n        result = yield from self._trace_method(\n            self.__wrapped__.executemany, query, {'sql.executemany': 'true'},\n            query, *args, **kwargs)  # noqa: E999\n        return result\n\n    @asyncio.coroutine\n    def execute(self, query, *args, **kwargs):\n        result = yield from self._trace_method(\n            self.__wrapped__.execute, query, {}, query, *args, **kwargs)\n        return result\n\n    @asyncio.coroutine\n    def callproc(self, proc, args):\n        result = yield from self._trace_method(\n            self.__wrapped__.callproc, proc, {}, proc, args)  # noqa: E999\n        return result\n\n\nclass AIOTracedConnection(wrapt.ObjectProxy):\n    \"\"\" TracedConnection wraps a Connection with tracing code. \"\"\"\n\n    def __init__(self, conn, pin=None):\n        super(AIOTracedConnection, self).__init__(conn)\n        name = dbapi._get_vendor(conn)\n        db_pin = pin or Pin(service=name, app=name, app_type=AppTypes.db)\n        db_pin.onto(self)\n\n    def cursor(self, *args, **kwargs):\n        # unfortunately we also need to patch this method as otherwise \"self\"\n        # ends up being the aiopg connection object\n        coro = self._cursor(*args, **kwargs)\n        return _ContextManager(coro)\n\n    @asyncio.coroutine\n    def _cursor(self, *args, **kwargs):\n        cursor = yield from self.__wrapped__._cursor(*args, **kwargs)  # noqa: E999\n        pin = Pin.get_from(self)\n        if not pin:\n            return cursor\n        return AIOTracedCursor(cursor, pin)\n", "path": "ddtrace/contrib/aiopg/connection.py"}]}