<commit_before>import diamond.collector
import subprocess

class PowerDNSCollector(diamond.collector.Collector):
    """
    Collects all metrics exported by the powerdns nameserver using the
    pdns_control binary.
    """ 
    def get_default_config(self): 
        """
        Returns the default collector settings
        """
        return { 
            'pdns_control': '/usr/bin/pdns_control', 
            'path': 'powerdns', 
        }

    def collect(self):
        sp = subprocess.Popen([self.config['pdns_control'], "list"], stdout=subprocess.PIPE)
        data = sp.communicate()[0]
        for metric in data.split(','):
            if not metric.strip():
                continue
            metric, value = metric.split('=')
            self.publish(metric, int(value))<commit_msg>Turn powerdns stats into derived where applicable<commit_after>import diamond.collector
import subprocess

class PowerDNSCollector(diamond.collector.Collector):
    """
    Collects all metrics exported by the powerdns nameserver using the
    pdns_control binary.
    """ 
    _GAUGE_KEYS = [
        'cache-bytes', 'cache-entries', 'chain-resends',
        'concurrent-queries', 'dlg-only-drops', 'dont-outqueries',
        'ipv6-outqueries', 'latency', 'max-mthread-stack', 'negcache-entries',
        'nsspeeds-entries',
        'packetcache-bytes', 'packetcache-entries', 'packetcache-size',
        'qa-latency', 'throttle-entries'
        ]
    def get_default_config(self): 
        """
        Returns the default collector settings
        """
        return { 
            'pdns_control': '/usr/bin/pdns_control', 
            'path': 'powerdns', 
        }

    def collect(self):
        sp = subprocess.Popen([self.config['pdns_control'], "list"], stdout=subprocess.PIPE)
        data = sp.communicate()[0]
        for metric in data.split(','):
            if not metric.strip():
                continue
            metric, value = metric.split('=')
            try:
                value = float(value)
            except:
                pass
            if metric not in self._GAUGE_KEYS:
                value = self.derivative(metric, value)
                if value < 0:
                    continue
            self.log.info("Metric %s has value %d", metric, int(value))
            self.publish(metric, value)
{'fraction_non_alphanumeric': 0.11873124732104587, 'fraction_numerical': 0.0017145306472353193, 'mean_word_length': 2.083223249669749, 'pattern_counts': {'":': 0, '<': 4, '<?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': '4039069', 'n_tokens_mistral': 683, 'n_tokens_neox': 652, 'n_words': 148}