Commit d8220247 authored by ale's avatar ale
Browse files

modify the format of the exported instrumentation variables

parent 154aaf22
......@@ -3,6 +3,7 @@ import logging
import os
import time
import traceback
import urlparse
from nospam import dbshelve
from nospam import plugin_base
from nospam import urls
......@@ -11,6 +12,12 @@ from nospam import instrumentation
log = logging.getLogger(__name__)
def _extract_domain(url):
if url.startswith('http'):
return urlparse.urlsplit(url).netloc
return url
class NoSpam(object):
def __init__(self, config):
......@@ -58,11 +65,11 @@ class NoSpam(object):
return all_urls
def _incr_counter(self, kind, site=None):
all_key = '%s:@all' % kind
all_key = 'comments{kind=%s,site=@all}' % kind
self._counters[all_key] = self._counters.get(all_key, 0) + 1
instrumentation.incr_counter(all_key)
if site:
site_key = '%s:%s' % (kind, site)
site_key = 'comments{kind=%s,site=%s}' % (kind, site)
instrumentation.incr_counter(site_key)
self._counters[site_key] = self._counters.get(site_key, 0) + 1
......@@ -86,7 +93,7 @@ class NoSpam(object):
result = plugin.testComment(comment)
# Keep track of the match counts for each plugin.
if result[0] > 0:
instrumentation.incr_counter('match/%s' % (
instrumentation.incr_counter('plugin_match{plugin=%s}' % (
plugin.__class__.__name__,))
results.append(result)
tot_score = sum(map(lambda x: x[0], results))
......@@ -95,14 +102,15 @@ class NoSpam(object):
comment['train'] = 'spam'
self.classify(comment)
msg = ', '.join([x[1] for x in results if x[0] > 0])
log.info('spam: site=%s score=%g %s' % (comment['site'], tot_score, msg))
self._incr_counter('spam', comment['site'])
site = _extract_domain(comment['site'])
log.info('spam: site=%s score=%g %s' % (site, tot_score, msg))
self._incr_counter('spam', site)
return (True, tot_score, msg)
self._incr_counter('ham', comment['site'])
self._incr_counter('ham', site)
return (False, 0, None)
def classify(self, comment):
instrumentation.incr_counter('classify/%s' % comment.get('train', 'err'))
instrumentation.incr_counter('classify{train=%s}' % comment.get('train', 'err'))
comment['_urls'] = self._extract_urls(comment)
for plugin in self._plugins:
plugin.classifyComment(comment)
......@@ -120,7 +128,7 @@ class BlogSpamCompatibleAPI(object):
self._ns = ns
def testComment(self, comment):
instrumentation.incr_counter('rpc/testComment')
instrumentation.incr_counter('rpc_count{type=testComment}')
try:
for attr in ('comment', 'site'):
if attr not in comment:
......@@ -135,7 +143,7 @@ class BlogSpamCompatibleAPI(object):
return 'ERROR:%s' % str(e)
def classifyComment(self, comment):
instrumentation.incr_counter('rpc/classifyComment')
instrumentation.incr_counter('rpc_count{type=classifyComment}')
try:
for attr in ('comment', 'site', 'train'):
if attr not in comment:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment