Skip to content
This repository has been archived by the owner on Jun 11, 2018. It is now read-only.

Commit

Permalink
* fix for #95
Browse files Browse the repository at this point in the history
* added test for requests
  • Loading branch information
jalvz committed Feb 19, 2016
1 parent ff4f658 commit 118a438
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
15 changes: 8 additions & 7 deletions opbeat/instrumentation/packages/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ def call(self, module, method, wrapped, instance, args, kwargs):

signature = method.upper()

parsed_url = urlparse.urlparse(url)
host = parsed_url.hostname
signature += " " + host

if (parsed_url.port
and default_ports.get(parsed_url.scheme) != parsed_url.port):
signature += ":" + str(parsed_url.port)
if url:
parsed_url = urlparse.urlparse(url)
host = parsed_url.hostname or " "
signature += " " + host

if parsed_url.port and \
default_ports.get(parsed_url.scheme) != parsed_url.port:
signature += ":" + str(parsed_url.port)

with trace(signature, "ext.http.requests",
{'url': url}, leaf=True):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def build_extension(self, ext):
'webob',
'pytz',
'redis',
'requests',
'urllib3',
'jinja2',
'pytest-benchmark',
Expand Down
57 changes: 57 additions & 0 deletions tests/instrumentation/requests_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import requests
from requests.exceptions import InvalidURL, MissingSchema
from urllib3.exceptions import ConnectionError

import opbeat
import opbeat.instrumentation.control
from opbeat.traces import trace
from tests.helpers import get_tempstoreclient
from tests.utils.compat import TestCase


class InstrumentRequestsTest(TestCase):
def setUp(self):
self.client = get_tempstoreclient()
opbeat.instrumentation.control.instrument()

def test_requests_instrumentation(self):
self.client.begin_transaction("transaction.test")
with trace("test_pipeline", "test"):
try:
requests.get('http://example.com')
except ConnectionError:
pass
self.client.end_transaction("MyView")

_, traces = self.client.instrumentation_store.get_all()
self.assertIn('GET example.com', map(lambda x: x['signature'], traces))

def test_requests_instrumentation_via_session(self):
self.client.begin_transaction("transaction.test")
with trace("test_pipeline", "test"):
s = requests.Session()
try:
s.get('http://example.com')
except ConnectionError:
pass
self.client.end_transaction("MyView")

_, traces = self.client.instrumentation_store.get_all()
self.assertIn('GET example.com', map(lambda x: x['signature'], traces))

def test_requests_instrumentation_malformed_none(self):
self.client.begin_transaction("transaction.test")
with trace("test_pipeline", "test"):
self.assertRaises(MissingSchema, requests.get, None)

def test_requests_instrumentation_malformed_schema(self):
self.client.begin_transaction("transaction.test")
with trace("test_pipeline", "test"):
self.assertRaises(MissingSchema, requests.get, '')

def test_requests_instrumentation_malformed_path(self):
self.client.begin_transaction("transaction.test")
with trace("test_pipeline", "test"):
self.assertRaises(InvalidURL, requests.get, 'http://')


0 comments on commit 118a438

Please sign in to comment.