Ema has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/365610 )
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
(cherry picked from commit 2bf2350d63874168e737987b616cc66f7630e791)
Change-Id: I55cb62b2ed91af3e61dec1ac91d579518ed48fc1
---
M pybal/instrumentation.py
M pybal/pybal.py
M pybal/test/test_instrumentation.py
3 files changed, 31 insertions(+), 7 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/operations/debs/pybal
refs/changes/10/365610/1
diff --git a/pybal/instrumentation.py b/pybal/instrumentation.py
index 56263d1..f8843e6 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 ca094fc..94aa627 100755
--- a/pybal/pybal.py
+++ b/pybal/pybal.py
@@ -631,9 +631,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 632c352..040227c 100644
--- a/pybal/test/test_instrumentation.py
+++ b/pybal/test/test_instrumentation.py
@@ -186,8 +186,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"""
@@ -212,6 +213,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/365610
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I55cb62b2ed91af3e61dec1ac91d579518ed48fc1
Gerrit-PatchSet: 1
Gerrit-Project: operations/debs/pybal
Gerrit-Branch: 1.13
Gerrit-Owner: Ema <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits