Ori.livneh has uploaded a new change for review. https://gerrit.wikimedia.org/r/232836
Change subject: Add statsvr, a reverse statsv ...................................................................... Add statsvr, a reverse statsv Whereas statsv converts URL-encoded metrics into statsd datagrams, statsvr does the reverse: it converts statsd datagrams into URLs, and fetches them from the canonical production instance. Bug: T109753 Change-Id: I99c3c0ebac31812e83cd067f64b1721cb0c5a57a --- A statsvr.py 1 file changed, 68 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/analytics/statsv refs/changes/36/232836/1 diff --git a/statsvr.py b/statsvr.py new file mode 100644 index 0000000..6975272 --- /dev/null +++ b/statsvr.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +""" + statsvr + ~~~~~~~ + A simple statsd -> web request gateway. + + Copyright 2015 Ori Livneh <[email protected]> + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +""" +import sys +reload(sys) +sys.setdefaultencoding('utf-8') + +import logging +import re +import socket +import urllib +import urllib2 + +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +sock.bind('', 8125) + +BUFFER_SIZE = 2 ** 16 +STATSV_URL = '//meta.wikimedia.org/beacon/statsv' + + +def sanitize_key(key): + key = key.replace('/', '-') + key = re.sub(r'\s+', '_', key) + key = re.sub(r'[^\w.-]', '', key) + return key + + +def url_encode_metric(key, value, type): + return '%s=%s%s' % (urllib.quote(key), value, type) + + +def dispatch(uri): + logging.info(uri) + urllib2.urlopen(uri) + + +while 1: + data, _ = sock.recvfrom(BUFFER_SIZE) + query = [] + for metric in data.rstrip('\n').split('\n'): + match = re.match('\A([^:]+):([^|]+)\|(.+)', metric) + if not match: + logging.error('Discarding invalid metric %s', metric) + continue + key, value, type = match.groups() + key = sanitize_key(key) + query.append(url_encode_metric(key, value, type)) + if len(query) >= 10: + dispatch('%s?%s' % (STATSV_URL, '&'.join(query))) + break -- To view, visit https://gerrit.wikimedia.org/r/232836 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I99c3c0ebac31812e83cd067f64b1721cb0c5a57a Gerrit-PatchSet: 1 Gerrit-Project: analytics/statsv Gerrit-Branch: master Gerrit-Owner: Ori.livneh <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
