<commit_before># coding=utf-8

"""
Send metrics to signalfx

#### Dependencies

 * signalfx (pip install signalfx)


#### Configuration
Enable this handler

 * handlers = diamond.handler.signalfx_handler.SignalfxHandler

 * auth_token = SIGNALFX_AUTH_TOKEN

 * batch_size = [optional | 300 ] will wait for this many requests before
     posting
"""
from Handler import Handler
import collections
import logging
import time

try:
    import signalfx
except ImportError:
    raise ImportError("Failed to load signalfx module. "
                      "Install signalfx module - `pip install signalfx`")

from diamond.util import get_diamond_version


class SignalfxHandler(Handler):

    # Inititalize Handler with url and batch size
    def __init__(self, config=None):
        super(SignalfxHandler, self).__init__(config)
        self.metrics = []
        auth_token = self.config['auth_token']
        url = self.config['url']
        self.batch_size = int(self.config['batch'])
        self.batch_max_interval = float(self.config['batch_max_interval'])
        self.resetBatchTimeout()
        self.signalfx = signalfx.SignalFx(auth_token, ingest_endpoint=url,
                                          batch_size=self.batch_size,
                                          user_agents=[self.user_agent()])

    def resetBatchTimeout(self):
        self.batch_max_timestamp = time.time() + self.batch_max_interval

    def get_default_config_help(self):
        """
        Returns the help text for the configuration options for this handler
        """
        config = super(SignalfxHandler, self).get_default_config_help()

        config.update({
            'url': 'Where to send metrics',
            'batch': 'How many to store before sending',
            'auth_token': 'Org API token to use when sending metrics',
        })

        return config

    def get_default_config(self):
        """
        Return the default config for the handler
        """
        config = super(SignalfxHandler, self).get_default_config()

        config.update({
            'url': 'https://ingest.signalfx.com/',
            'batch': 300,
            # Don't wait more than 10 sec between pushes
            'batch_max_interval': 10,
            'auth_token': '',
        })

        return config

    def process(self, metric):
        """
        Queue a metric.  Flushing queue if batch size reached
        """
        self.metrics.append(metric)
        if self.should_flush():
            self._send()
<commit_msg>Adding http timeout flag to handler<commit_after># coding=utf-8

"""
Send metrics to signalfx

#### Dependencies

 * signalfx (pip install signalfx)


#### Configuration
Enable this handler

 * handlers = diamond.handler.signalfx_handler.SignalfxHandler

 * auth_token = <PASSWORD>

 * batch_size = [optional | 300 ] will wait for this many requests before
     posting

 * batch_max_interval = [optional | 10 ] Max interval (secs) to wait before
     sending

 * timeout = [optional | 1 ] Timeout for the HTTP Post calls.
"""
from Handler import Handler
import collections
import logging
import time

try:
    import signalfx
except ImportError:
    raise ImportError("Failed to load signalfx module. "
                      "Install signalfx module - `pip install signalfx`")

from diamond.util import get_diamond_version


class SignalfxHandler(Handler):

    # Inititalize Handler with url and batch size
    def __init__(self, config=None):
        super(SignalfxHandler, self).__init__(config)
        self.metrics = []
        auth_token = self.config['auth_token']
        url = self.config['url']
        self.batch_size = int(self.config['batch'])
        self.batch_max_interval = float(self.config['batch_max_interval'])
        self.timeout = self.config['timeout']
        self.resetBatchTimeout()
        self.signalfx = signalfx.SignalFx(auth_token, ingest_endpoint=url,
                                          batch_size=self.batch_size,
                                          timeout=self.timeout,
                                          user_agents=[self.user_agent()])

    def resetBatchTimeout(self):
        self.batch_max_timestamp = time.time() + self.batch_max_interval

    def get_default_config_help(self):
        """
        Returns the help text for the configuration options for this handler
        """
        config = super(SignalfxHandler, self).get_default_config_help()

        config.update({
            'url': 'Where to send metrics',
            'batch': 'How many to store before sending',
            'batch_max_interval': 'Max interval (secs) to wait before sending',
            'timeout': 'Timeout for the HTTP Post calls.',
            'auth_token': 'Org API token to use when sending metrics',
        })

        return config

    def get_default_config(self):
        """
        Return the default config for the handler
        """
        config = super(SignalfxHandler, self).get_default_config()

        config.update({
            'url': 'https://ingest.signalfx.com/',
            'batch': 300,
            # Don't wait more than 10 sec between pushes
            'batch_max_interval': 10,
            'timeout': 1,
            'auth_token': '',
        })

        return config

    def process(self, metric):
        """
        Queue a metric.  Flushing queue if batch size reached
        """
        self.metrics.append(metric)
        if self.should_flush():
            self._send()

{'fraction_non_alphanumeric': 0.09454279978098193, 'fraction_numerical': 0.004745391494798321, 'mean_word_length': 2.2893157262905164, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1774047', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1431, 'n_words': 406}