<commit_before># -*- coding: utf-8 -*-
'''
Export napalm-logs notifications as Prometheus metrics.
'''
from __future__ import absolute_import
from __future__ import unicode_literals

# Import stdlib
import logging

# Import third party libs
from prometheus_client import Counter, Gauge

# Import napalm-logs pkgs
import napalm_logs.utils
from napalm_logs.transport.base import TransportBase

log = logging.getLogger(__name__)


class PrometheusTransport(TransportBase):
    '''
    Prom transport class.
    '''
    def __init__(self, address, port, **kwargs):
        self.metrics = {}

    def __parse_without_details(self, msg):
        '''
        Helper to generate Counter metrics that only provide the host label
        from the structured message.
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host']
            )
        self.metrics[error].labels(host=msg['host']).inc()

    def __parse_user_action(self, msg):
        '''
        Helper to generate Counter metrics that provide the host label, together
        with the username under a YANG structure users > user > [USER].
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host', 'user']
            )
        self.metrics[error].labels(
            host=msg['host'],
            user=list(msg['yang_message']['users']['user'].keys())[0]
        ).inc()

    def __parse_interface_basic(self, msg):
        '''
        Helper to generate Counter metrics for interface notifications.
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host', 'interface']
            )
        if 'interface_state' not in self.metrics:
            self.metrics['interface_state'] = Gauge(
                'napalm_logs_interface_state',
                'State of this interface. 0=DOWN, 1=UP',
                ['host', 'interface']
            )
        labels = {
            'host': msg['host'],
            'interface': list(msg['yang_message']['interfaces']['interface'].keys())[0]
        }
        self.metrics[error].labels(**labels).inc()
        state = 1 if error == 'INTERFACE_UP' else 0
        self.metrics['interface_state'].labels(**labels).set(state)

    def __parse_bgp_basic(self, msg):
        '''
        Helper to generate Counter metrics for simple BGP notifications,
        providing the neighbor address and peer AS number.
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host', 'neighbor', 'peer_as']
            )
        neigh_dict = msg['yang_message']['bgp']['neighbors']['neighbor']
        neighbor = list(neigh_dict.keys())[0]
        self.metrics[error].labels(
            host=msg['host'],
            neighbor=neighbor,
            peer_as=neigh_dict[neighbor]['state']['peer_as']
        ).inc()

    def __parse_network_instance_bgp(self, msg):
        '''
        Helper to generate Counter metrics for simple BGP notifications, nested
        under the network-instance OpenConfig model, providing the neighbor
        address, instance name, and peer AS number.
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host', 'instance', 'neighbor', 'peer_as']
            )
        instance_name = list(msg['yang_message']['network-instances']['network-instance'].keys())[0]
        instance_dict = msg['yang_message']['network-instances']['network-instance'][instance_name]
        neigh_dict = instance_dict['protocols']['protocol']['bgp']['neighbors']['neighbor']
        neighbor = list(neigh_dict.keys())[0]
        self.metrics[error].labels(
            host=msg['host'],
            instance=instance_name,
            neighbor=neighbor,
            peer_as=neigh_dict[neighbor]['state']['peer_as']
        ).inc()

    def __parse_bgp_no_asn(self, msg):
        '''
        Helper to generate Counter metrics for simple BGP notifications - but
        without the AS number, so only the neighbor IP address.
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host', 'neighbor']
            )
        neigh_dict = msg['yang_message']['bgp']['neighbors']['neighbor']
        neighbor = list(neigh_dict.keys())[0]
        self.metrics[error].labels(
            host=msg['host'],
            neighbor=neighbor,
        ).inc()

    def __parse_ospf_neighbor(self, msg):
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),<commit_msg>Uninspired label, remaping to routing_instance<commit_after># -*- coding: utf-8 -*-
'''
Export napalm-logs notifications as Prometheus metrics.
'''
from __future__ import absolute_import
from __future__ import unicode_literals

# Import stdlib
import logging

# Import third party libs
from prometheus_client import Counter, Gauge

# Import napalm-logs pkgs
import napalm_logs.utils
from napalm_logs.transport.base import TransportBase

log = logging.getLogger(__name__)


class PrometheusTransport(TransportBase):
    '''
    Prom transport class.
    '''
    def __init__(self, address, port, **kwargs):
        self.metrics = {}

    def __parse_without_details(self, msg):
        '''
        Helper to generate Counter metrics that only provide the host label
        from the structured message.
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host']
            )
        self.metrics[error].labels(host=msg['host']).inc()

    def __parse_user_action(self, msg):
        '''
        Helper to generate Counter metrics that provide the host label, together
        with the username under a YANG structure users > user > [USER].
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host', 'user']
            )
        self.metrics[error].labels(
            host=msg['host'],
            user=list(msg['yang_message']['users']['user'].keys())[0]
        ).inc()

    def __parse_interface_basic(self, msg):
        '''
        Helper to generate Counter metrics for interface notifications.
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host', 'interface']
            )
        if 'interface_state' not in self.metrics:
            self.metrics['interface_state'] = Gauge(
                'napalm_logs_interface_state',
                'State of this interface. 0=DOWN, 1=UP',
                ['host', 'interface']
            )
        labels = {
            'host': msg['host'],
            'interface': list(msg['yang_message']['interfaces']['interface'].keys())[0]
        }
        self.metrics[error].labels(**labels).inc()
        state = 1 if error == 'INTERFACE_UP' else 0
        self.metrics['interface_state'].labels(**labels).set(state)

    def __parse_bgp_basic(self, msg):
        '''
        Helper to generate Counter metrics for simple BGP notifications,
        providing the neighbor address and peer AS number.
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host', 'neighbor', 'peer_as']
            )
        neigh_dict = msg['yang_message']['bgp']['neighbors']['neighbor']
        neighbor = list(neigh_dict.keys())[0]
        self.metrics[error].labels(
            host=msg['host'],
            neighbor=neighbor,
            peer_as=neigh_dict[neighbor]['state']['peer_as']
        ).inc()

    def __parse_network_instance_bgp(self, msg):
        '''
        Helper to generate Counter metrics for simple BGP notifications, nested
        under the network-instance OpenConfig model, providing the neighbor
        address, instance name, and peer AS number.
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host', 'routing_instance', 'neighbor', 'peer_as']
            )
        instance_name = list(msg['yang_message']['network-instances']['network-instance'].keys())[0]
        instance_dict = msg['yang_message']['network-instances']['network-instance'][instance_name]
        neigh_dict = instance_dict['protocols']['protocol']['bgp']['neighbors']['neighbor']
        neighbor = list(neigh_dict.keys())[0]
        self.metrics[error].labels(
            host=msg['host'],
            routing_instance=instance_name,
            neighbor=neighbor,
            peer_as=neigh_dict[neighbor]['state']['peer_as']
        ).inc()

    def __parse_bgp_no_asn(self, msg):
        '''
        Helper to generate Counter metrics for simple BGP notifications - but
        without the AS number, so only the neighbor IP address.
        '''
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
                'Counter for {error} notifications'.format(error=error),
                ['host', 'neighbor']
            )
        neigh_dict = msg['yang_message']['bgp']['neighbors']['neighbor']
        neighbor = list(neigh_dict.keys())[0]
        self.metrics[error].labels(
            host=msg['host'],
            neighbor=neighbor,
        ).inc()

    def __parse_ospf_neighbor(self, msg):
        error = msg['error']
        if error not in self.metrics:
            self.metrics[error] = Counter(
                'napalm_logs_{error}'.format(error=error.lower()),
{'fraction_non_alphanumeric': 0.1296794927473291, 'fraction_numerical': 0.0019108833492573613, 'mean_word_length': 2.294420600858369, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2997298', 'n_tokens_mistral': 3220, 'n_tokens_neox': 3037, 'n_words': 709}