<commit_before># (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from __future__ import division

import re
import time
from copy import deepcopy
from distutils.version import LooseVersion

import pymongo
from six import PY3, iteritems, itervalues
from six.moves.urllib.parse import unquote_plus, urlsplit

from datadog_checks.base import AgentCheck, ConfigurationError, is_affirmative
from datadog_checks.base.utils.common import round_value

from . import metrics
from .utils import build_url

if PY3:
    long = int

DEFAULT_TIMEOUT = 30
ALLOWED_CUSTOM_METRICS_TYPES = ['gauge', 'rate', 'count', 'monotonic_count']
ALLOWED_CUSTOM_QUERIES_COMMANDS = ['aggregate', 'count', 'find']


class MongoDb(AgentCheck):
    """
    MongoDB agent check.

    # Metrics
    Metric available for collection are listed by topic as `MongoDb` class variables.

    Various metric topics are collected by default. Others require the
    corresponding option enabled in the check configuration file.

    ## Format
    Metrics are listed with the following format:
        ```
        metric_name -> metric_type
        ```
        or
        ```
        metric_name -> (metric_type, alias)*
        ```

    * `alias` parameter is optional, if unspecified, MongoDB metrics are reported
       with their original metric names.

    # Service checks
    Available service checks:
    * `mongodb.can_connect`
      Connectivity health to the instance.
    * `mongodb.replica_set_member_state`
      Disposition of the member replica set state.
    """

    # Source
    SOURCE_TYPE_NAME = 'mongodb'

    # Service check
    SERVICE_CHECK_NAME = 'mongodb.can_connect'

    # Replication states
    """
    MongoDB replica set states, as documented at
    https://docs.mongodb.org/manual/reference/replica-states/
    """
    REPLSET_MEMBER_STATES = {
        0: ('STARTUP', 'Starting Up'),
        1: ('PRIMARY', 'Primary'),
        2: ('SECONDARY', 'Secondary'),
        3: ('RECOVERING', 'Recovering'),
        4: ('Fatal', 'Fatal'),  # MongoDB docs don't list this state
        5: ('STARTUP2', 'Starting up (forking threads)'),
        6: ('UNKNOWN', 'Unknown to this replset member'),
        7: ('ARBITER', 'Arbiter'),
        8: ('DOWN', 'Down'),
        9: ('ROLLBACK', 'Rollback'),
        10: ('REMOVED', 'Removed'),
    }

    def __init__(self, name, init_config, instances=None):
        super(MongoDb, self).__init__(name, init_config, instances)

        # Members' last replica set states
        self._last_state_by_server = {}

        self.collection_metrics_names = (key.split('.')[1] for key in metrics.COLLECTION_METRICS)

        # x.509 authentication
        ssl_params = {
            'ssl': self.instance.get('ssl', None),
            'ssl_keyfile': self.instance.get('ssl_keyfile', None),
            'ssl_certfile': self.instance.get('ssl_certfile', None),
            'ssl_cert_reqs': self.instance.get('ssl_cert_reqs', None),
            'ssl_ca_certs': self.instance.get('ssl_ca_certs', None),
        }
        self.ssl_params = {key: value for key, value in iteritems(ssl_params) if value is not None}

        if 'server' in self.instance:
            self.warning('Option `server` is deprecated and will be removed in a future release. Use `hosts` instead.')
            self.server = self.instance['server']
        else:
            hosts = self.instance.get('hosts', [])
            if not hosts:<commit_msg>Avoid depleting collection_metric_names (#7393)<commit_after># (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from __future__ import division

import re
import time
from copy import deepcopy
from distutils.version import LooseVersion

import pymongo
from six import PY3, iteritems, itervalues
from six.moves.urllib.parse import unquote_plus, urlsplit

from datadog_checks.base import AgentCheck, ConfigurationError, is_affirmative
from datadog_checks.base.utils.common import round_value

from . import metrics
from .utils import build_url

if PY3:
    long = int

DEFAULT_TIMEOUT = 30
ALLOWED_CUSTOM_METRICS_TYPES = ['gauge', 'rate', 'count', 'monotonic_count']
ALLOWED_CUSTOM_QUERIES_COMMANDS = ['aggregate', 'count', 'find']


class MongoDb(AgentCheck):
    """
    MongoDB agent check.

    # Metrics
    Metric available for collection are listed by topic as `MongoDb` class variables.

    Various metric topics are collected by default. Others require the
    corresponding option enabled in the check configuration file.

    ## Format
    Metrics are listed with the following format:
        ```
        metric_name -> metric_type
        ```
        or
        ```
        metric_name -> (metric_type, alias)*
        ```

    * `alias` parameter is optional, if unspecified, MongoDB metrics are reported
       with their original metric names.

    # Service checks
    Available service checks:
    * `mongodb.can_connect`
      Connectivity health to the instance.
    * `mongodb.replica_set_member_state`
      Disposition of the member replica set state.
    """

    # Source
    SOURCE_TYPE_NAME = 'mongodb'

    # Service check
    SERVICE_CHECK_NAME = 'mongodb.can_connect'

    # Replication states
    """
    MongoDB replica set states, as documented at
    https://docs.mongodb.org/manual/reference/replica-states/
    """
    REPLSET_MEMBER_STATES = {
        0: ('STARTUP', 'Starting Up'),
        1: ('PRIMARY', 'Primary'),
        2: ('SECONDARY', 'Secondary'),
        3: ('RECOVERING', 'Recovering'),
        4: ('Fatal', 'Fatal'),  # MongoDB docs don't list this state
        5: ('STARTUP2', 'Starting up (forking threads)'),
        6: ('UNKNOWN', 'Unknown to this replset member'),
        7: ('ARBITER', 'Arbiter'),
        8: ('DOWN', 'Down'),
        9: ('ROLLBACK', 'Rollback'),
        10: ('REMOVED', 'Removed'),
    }

    def __init__(self, name, init_config, instances=None):
        super(MongoDb, self).__init__(name, init_config, instances)

        # Members' last replica set states
        self._last_state_by_server = {}

        self.collection_metrics_names = tuple(key.split('.')[1] for key in metrics.COLLECTION_METRICS)

        # x.509 authentication
        ssl_params = {
            'ssl': self.instance.get('ssl', None),
            'ssl_keyfile': self.instance.get('ssl_keyfile', None),
            'ssl_certfile': self.instance.get('ssl_certfile', None),
            'ssl_cert_reqs': self.instance.get('ssl_cert_reqs', None),
            'ssl_ca_certs': self.instance.get('ssl_ca_certs', None),
        }
        self.ssl_params = {key: value for key, value in iteritems(ssl_params) if value is not None}

        if 'server' in self.instance:
            self.warning('Option `server` is deprecated and will be removed in a future release. Use `hosts` instead.')
            self.server = self.instance['server']
        else:
            hosts = self.instance.get('hosts', [])
            if not hosts:
{'fraction_non_alphanumeric': 0.10943503628860111, 'fraction_numerical': 0.007969261420236231, 'mean_word_length': 2.9394618834080717, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1669216', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2069, 'n_words': 614}