<commit_before>import time

from heka.config import client_from_stream_config
from heka.holder import (
    CLIENT_HOLDER,
    get_client,
)
from pyramid.httpexceptions import (
    HTTPException,
    HTTPNotFound,
)

from ichnaea.exceptions import BaseJSONError


RAVEN_ERROR = 'Unhandled error occurred'


def get_heka_client():
    return get_client('ichnaea')


def set_heka_client(client):
    CLIENT_HOLDER.set_client('ichnaea', client)
    return get_heka_client()


def configure_heka(heka_config, _heka_client=None):
    if _heka_client is not None:
        return set_heka_client(_heka_client)

    client = get_heka_client()
    if heka_config:
        with open(heka_config, 'r') as fd:
            client = client_from_stream_config(fd, 'heka', client=client)
    return client


def heka_tween_factory(handler, registry):

    VALID_4xx_URLS = [
        '/v1/submit',
        '/v1/search',
        '/v1/geolocate',
        '/v1/geosubmit',
    ]

    def heka_tween(request):
        heka_client = registry.heka_client
        stats_client = registry.stats_client
        start = time.time()

        def timer_send():
            duration = time.time() - start
            path = request.path.replace('/', '.').lstrip('.')
            stats_client.timing('request.' + path, duration)

        def counter_send(status_code):
            path = request.path.replace('/', '.').lstrip('.')
            stats_client.incr('request.%s.%s' % (path, status_code))

        try:
            response = handler(request)
        except HTTPNotFound:
            # ignore 404's raised as exceptions
            raise
        except BaseJSONError:
            # don't capture client JSON exceptions
            timer_send()
            counter_send(400)<commit_msg>Fix unit of manual statds timer to be in ms instead of seconds.<commit_after>import time

from heka.config import client_from_stream_config
from heka.holder import (
    CLIENT_HOLDER,
    get_client,
)
from pyramid.httpexceptions import (
    HTTPException,
    HTTPNotFound,
)

from ichnaea.exceptions import BaseJSONError


RAVEN_ERROR = 'Unhandled error occurred'


def get_heka_client():
    return get_client('ichnaea')


def set_heka_client(client):
    CLIENT_HOLDER.set_client('ichnaea', client)
    return get_heka_client()


def configure_heka(heka_config, _heka_client=None):
    if _heka_client is not None:
        return set_heka_client(_heka_client)

    client = get_heka_client()
    if heka_config:
        with open(heka_config, 'r') as fd:
            client = client_from_stream_config(fd, 'heka', client=client)
    return client


def heka_tween_factory(handler, registry):

    VALID_4xx_URLS = [
        '/v1/submit',
        '/v1/search',
        '/v1/geolocate',
        '/v1/geosubmit',
    ]

    def heka_tween(request):
        heka_client = registry.heka_client
        stats_client = registry.stats_client
        start = time.time()

        def timer_send():
            duration = int(round((time.time() - start) * 1000))
            path = request.path.replace('/', '.').lstrip('.')
            stats_client.timing('request.' + path, duration)

        def counter_send(status_code):
            path = request.path.replace('/', '.').lstrip('.')
            stats_client.incr('request.%s.%s' % (path, status_code))

        try:
            response = handler(request)
        except HTTPNotFound:
            # ignore 404's raised as exceptions
            raise
        except BaseJSONError:
            # don't capture client JSON exceptions
            timer_send()
            counter_send(400)
{'fraction_non_alphanumeric': 0.10324729392173189, 'fraction_numerical': 0.0072162087149597555, 'mean_word_length': 2.547244094488189, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4297285', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1106, 'n_words': 240}