Ema has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/354680 )

Change subject: Instrumentation fixes
......................................................................

Instrumentation fixes

- introduce a new global configuration option, 'instrumentation_ips', to
  specify the IPs on which to bind the instrumentation TCP port
- use Content-Type: text/plain by default, application/json when returning
  JSON content
- do not mention the URL in 404 response

Bug: T103882
Change-Id: Iedb821ef17234eb5825f6641d4eda0e0b5a41503
---
M pybal/instrumentation.py
M pybal/pybal.py
M pybal/test/test_instrumentation.py
3 files changed, 32 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/debs/pybal 
refs/changes/80/354680/1

diff --git a/pybal/instrumentation.py b/pybal/instrumentation.py
index a2a88c0..da04b11 100644
--- a/pybal/instrumentation.py
+++ b/pybal/instrumentation.py
@@ -20,9 +20,13 @@
 
 
 def wantJson(request):
-    return (request.requestHeaders.hasHeader('Accept')
-            and 'application/json' in
-            request.requestHeaders.getRawHeaders('Accept'))
+    if (request.requestHeaders.hasHeader('Accept')
+        and 'application/json' in 
request.requestHeaders.getRawHeaders('Accept')):
+        request.responseHeaders.addRawHeader(b"content-type", 
b"application/json")
+        return True
+    else:
+        request.responseHeaders.addRawHeader(b"content-type", b"text/plain")
+        return False
 
 
 class Resp404(Resource):
@@ -31,7 +35,7 @@
     def render_GET(self, request):
         request.setResponseCode(404)
         msg = {'error':
-               "The desired url {} was not found".format(request.uri)}
+               "The desired url was not found"}
         if wantJson(request):
             return json.dumps(msg)
         else:
diff --git a/pybal/pybal.py b/pybal/pybal.py
index 1521222..55f112e 100755
--- a/pybal/pybal.py
+++ b/pybal/pybal.py
@@ -625,9 +625,18 @@
         # Run the web server for instrumentation
         if configdict.getboolean('instrumentation', False):
             from twisted.web.server import Site
-            port = configdict.getint('instrumentation_port', 9090)
             factory = Site(instrumentation.ServerRoot())
-            reactor.listenTCP(port, factory)
+
+            port = configdict.getint('instrumentation_port', 9090)
+
+            # Bind on the IPs listed in 'instrumentation_ips'. Default to
+            # localhost v4 and v6 if no IPs have been specified in the
+            # configuration.
+            instrumentation_ips = eval(configdict.get(
+                'instrumentation_ips', '["127.0.0.1", "::1"]'))
+
+            for ipaddr in instrumentation_ips:
+                reactor.listenTCP(port, factory, interface=ipaddr)
 
         reactor.run()
     finally:
diff --git a/pybal/test/test_instrumentation.py 
b/pybal/test/test_instrumentation.py
index da03734..326b8d6 100644
--- a/pybal/test/test_instrumentation.py
+++ b/pybal/test/test_instrumentation.py
@@ -184,8 +184,9 @@
 
     def test_404(self):
         """Test case for an non-existent base url"""
-        hdr, _ = self._httpReq(uri='/test')
+        hdr, body = self._httpReq(uri='/test')
         self.assertTrue(hdr.startswith('HTTP/1.1 404 Not Found'))
+        self.assertEquals(body, 'The desired url was not found')
 
     def test_pool(self):
         """Test case for requesting a specific pool"""
@@ -210,5 +211,16 @@
         hdr, _ = self._httpReq(uri='/pools/test_pool0/mw1011')
         self.assertTrue(hdr.startswith('HTTP/1.1 404 Not Found'))
 
+    def test_content_type_text_plain(self):
+        hdr, _ = self._httpReq(uri='/pools/test_pool0')
+        self.assertTrue(hdr.startswith('HTTP/1.1 200 OK'))
+        self.assertTrue("Content-Type: text/plain" in hdr)
+
+    def test_content_type_application_json(self):
+        hdr, _ = self._httpReq(uri='/pools/test_pool0',
+                               headers={'Accept': 'application/json'})
+        self.assertTrue(hdr.startswith('HTTP/1.1 200 OK'))
+        self.assertTrue("Content-Type: application/json" in hdr)
+
     def tearDown(self):
         self.proto.connectionLost(Failure(TypeError("whatever")))

-- 
To view, visit https://gerrit.wikimedia.org/r/354680
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iedb821ef17234eb5825f6641d4eda0e0b5a41503
Gerrit-PatchSet: 1
Gerrit-Project: operations/debs/pybal
Gerrit-Branch: master
Gerrit-Owner: Ema <e...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to