<commit_before>"""
StatsD API
==========
The default statsd server that is used is localhost:8125. The ``STATSD``
environment variable can be used to set the statsd server connection
parameters. Note that the socket for communicating with statsd is
created once upon module import and will not change until the application is
restarted or the module is reloaded.

"""
import logging
import os
import socket
import time
import types
try:
    import urllib.parse as urlparse
except ImportError:
    import urlparse

__version__ = '1.2.0'

LOGGER = logging.getLogger(__name__)

STATSD_ADDR = None
SOCKET_ERROR = 'Error sending statsd metric'
STATSD_SOCKET = socket.socket(socket.AF_INET,
                              socket.SOCK_DGRAM,
                              socket.IPPROTO_UDP)
STATSD_PREFIX = None


def set_address():
    """Set the (host, port) to connect to from the environment.

    If the environment is updated, a call to this function will update the
    address this client connects to.

    This function will prefer to use the ``STATSD`` connection string
    environment variable, but will fall back to using the ``STATSD_HOST``
    and ``STATSD_PORT``.

    """
    global STATSD_ADDR
    connection_string = os.getenv('STATSD')
    if connection_string:
        url = urlparse.urlparse(connection_string)
        STATSD_ADDR = (url.hostname, url.port)
    else:
        STATSD_ADDR = (os.getenv('STATSD_HOST', 'localhost'),
                       int(os.getenv('STATSD_PORT', 8125)))


def set_prefix(prefix_value):
    """Set a statsd prefix that is included when using the statsd functions

    :param str prefix_value: prefix value

    """
    global STATSD_PREFIX
    STATSD_PREFIX = prefix_value


set_address()


def execution_timer(value):
    """The ``measure_duration`` decorator allows for easy instrumentation of
    the duration of function calls, using the method name in the key.

    The following example would add duration timing with the key ``my_function``

    .. code: python

        @statsd.measure_duration
        def my_function(foo):
            pass


    You can also have include a string argument value passed to your method as
    part of the key. Pass the index offset of the arguments to specify the
    argument number to use. In the following example, the key would be
    ``my_function.baz``:

    .. code:python

        @statsd.measure_duration(2)
        def my_function(foo, bar, 'baz'):
            pass

    """
    def _invoke(method, key_arg_position, *args, **kwargs):
        start_time = time.time()
        result = method(*args, **kwargs)
        duration = time.time() - start_time

        key = [method.func_name]
        if key_arg_position is not None:
            key.append(args[key_arg_position])
        add_timing('.'.join(key), value=duration)
        print('.'.join(key), duration)
        return result

    if type(value) is types.FunctionType:
        def wrapper(*args, **kwargs):
            return _invoke(value, None, *args, **kwargs)
        return wrapper
    else:
        def duration_decorator(func):
            def wrapper(*args, **kwargs):
                return _invoke(func, value, *args, **kwargs)
            return wrapper
        return duration_decorator


def add_timing(*args, **kwargs):<commit_msg>Add the statsd prefix env var<commit_after>"""
StatsD API
==========
The default statsd server that is used is localhost:8125. The ``STATSD``
environment variable can be used to set the statsd server connection
parameters. Note that the socket for communicating with statsd is
created once upon module import and will not change until the application is
restarted or the module is reloaded.

"""
import logging
import os
import socket
import time
import types
try:
    import urllib.parse as urlparse
except ImportError:
    import urlparse

__version__ = '1.2.0'

LOGGER = logging.getLogger(__name__)

STATSD_ADDR = None
SOCKET_ERROR = 'Error sending statsd metric'
STATSD_SOCKET = socket.socket(socket.AF_INET,
                              socket.SOCK_DGRAM,
                              socket.IPPROTO_UDP)
STATSD_PREFIX = os.getenv('STATSD_PREFIX', 'sprockets')


def set_address():
    """Set the (host, port) to connect to from the environment.

    If the environment is updated, a call to this function will update the
    address this client connects to.

    This function will prefer to use the ``STATSD`` connection string
    environment variable, but will fall back to using the ``STATSD_HOST``
    and ``STATSD_PORT``.

    """
    global STATSD_ADDR
    connection_string = os.getenv('STATSD')
    if connection_string:
        url = urlparse.urlparse(connection_string)
        STATSD_ADDR = (url.hostname, url.port)
    else:
        STATSD_ADDR = (os.getenv('STATSD_HOST', 'localhost'),
                       int(os.getenv('STATSD_PORT', 8125)))


def set_prefix(prefix_value):
    """Set a statsd prefix that is included when using the statsd functions

    :param str prefix_value: prefix value

    """
    global STATSD_PREFIX
    STATSD_PREFIX = prefix_value


set_address()


def execution_timer(value):
    """The ``measure_duration`` decorator allows for easy instrumentation of
    the duration of function calls, using the method name in the key.

    The following example would add duration timing with the key ``my_function``

    .. code: python

        @statsd.measure_duration
        def my_function(foo):
            pass


    You can also have include a string argument value passed to your method as
    part of the key. Pass the index offset of the arguments to specify the
    argument number to use. In the following example, the key would be
    ``my_function.baz``:

    .. code:python

        @statsd.measure_duration(2)
        def my_function(foo, bar, 'baz'):
            pass

    """
    def _invoke(method, key_arg_position, *args, **kwargs):
        start_time = time.time()
        result = method(*args, **kwargs)
        duration = time.time() - start_time

        key = [method.func_name]
        if key_arg_position is not None:
            key.append(args[key_arg_position])
        add_timing('.'.join(key), value=duration)
        return result

    if type(value) is types.FunctionType:
        def wrapper(*args, **kwargs):
            return _invoke(value, None, *args, **kwargs)
        return wrapper
    else:
        def duration_decorator(func):
            def wrapper(*args, **kwargs):
                return _invoke(func, value, *args, **kwargs)
            return wrapper
        return duration_decorator


def add_timing(*args, **kwargs):
{'fraction_non_alphanumeric': 0.08527014790220344, 'fraction_numerical': 0.003622094778146695, 'mean_word_length': 2.830635838150289, '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': '3612829', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1793, 'n_words': 661}