Esempio n. 1
0
def test_from_instance_invalid():
    # missing url
    with pytest.raises(ConfigurationError):
        from_instance({})
    # empty url
    with pytest.raises(ConfigurationError):
        from_instance({'url': ''})
def test_from_instance():
    instance = {
        "username": "******",
        "password": "******",
        "is_external": "yes",
        "url": "http://foo.bar",
        "tags": ["a", "b:c"],
    }
    c = from_instance(instance)
    assert c.username == "user"
    assert c.password == "pass"
    assert c.admin_forwarder is False
    assert c.cluster_stats is True
    assert c.url == "http://foo.bar"
    assert c.tags == ["url:http://foo.bar", "a", "b:c"]
    assert c.timeout == DEFAULT_TIMEOUT
    assert c.service_check_tags == ["host:foo.bar", "port:None", "a", "b:c"]

    instance = {"url": "http://192.168.42.42:12999", "timeout": 15}
    c = from_instance(instance)
    assert c.username is None
    assert c.password is None
    assert c.cluster_stats is False
    assert c.url == "http://192.168.42.42:12999"
    assert c.tags == ["url:http://192.168.42.42:12999"]
    assert c.timeout == 15
    assert c.service_check_tags == ["host:192.168.42.42", "port:12999"]

    instance = {
        "username": "******",
        "password": "******",
        "url": "https://foo.bar:9200",
        "ssl_verify": "true",
        "ssl_cert": "/path/to/cert.pem",
        "ssl_key": "/path/to/cert.key",
        "admin_forwarder": "1"
    }
    c = from_instance(instance)
    assert c.username == "user"
    assert c.password == "pass"
    assert c.admin_forwarder is True
    assert c.cluster_stats is False
    assert c.url == "https://foo.bar:9200"
    assert c.tags == ["url:https://foo.bar:9200"]
    assert c.timeout == DEFAULT_TIMEOUT
    assert c.service_check_tags == ["host:foo.bar", "port:9200"]
    assert c.ssl_verify == "true"
    assert c.ssl_cert == "/path/to/cert.pem"
    assert c.ssl_key == "/path/to/cert.key"
Esempio n. 3
0
def _test_check(elastic_check, instance, aggregator, cluster_tags, node_tags):
    config = from_instance(instance)
    es_version = elastic_check._get_es_version()

    # node stats, blacklist metrics that can't be tested in a small, single node instance
    blacklist = [
        'elasticsearch.indices.segments.index_writer_max_memory_in_bytes'
    ]
    blacklist.extend(ADDITIONAL_METRICS_1_x)
    for m_name in stats_for_version(es_version):
        if m_name in blacklist:
            continue
        aggregator.assert_metric(m_name, at_least=1, tags=node_tags)

    # cluster stats
    expected_metrics = health_stats_for_version(es_version)
    expected_metrics.update(CLUSTER_PENDING_TASKS)
    for m_name in expected_metrics:
        aggregator.assert_metric(m_name, at_least=1, tags=cluster_tags)

    aggregator.assert_service_check('elasticsearch.can_connect',
                                    status=ESCheck.OK,
                                    tags=config.service_check_tags)

    # Assert service metadata
    # self.assertServiceMetadata(['version'], count=3)
    # FIXME: 0.90.13 returns randomly a red status instead of yellow,
    # so we don't do a coverage test for it
    # Remove me when we stop supporting 0.90.x (not supported anymore by ES)
    if es_version != [0, 90, 13]:
        # Warning because elasticsearch status should be yellow, according to
        # http://chrissimpson.co.uk/elasticsearch-yellow-cluster-status-explained.html
        aggregator.assert_service_check('elasticsearch.cluster_health')
Esempio n. 4
0
def test_index_metrics(dd_environment, aggregator, elastic_check, instance, cluster_tags):
    instance['index_stats'] = True
    config = from_instance(instance)
    es_version = elastic_check._get_es_version(config)
    if es_version < [1, 0, 0]:
        pytest.skip("Index metrics are only tested in version 1.0.0+")

    elastic_check.check(instance)
    for m_name in index_stats_for_version(es_version):
        aggregator.assert_metric(m_name, tags=cluster_tags + ['index_name:testindex'])
Esempio n. 5
0
def test_from_instance_defaults():
    c = from_instance({'url': 'http://example.com'})

    assert c.admin_forwarder is False
    assert c.pshard_stats is False
    assert c.pshard_graceful_to is False
    assert c.cluster_stats is False
    assert c.index_stats is False
    assert c.service_check_tags == ['host:example.com', 'port:None']
    assert c.tags == ['url:http://example.com']
    assert c.url == 'http://example.com'
    assert c.pending_task_stats is True
Esempio n. 6
0
def test_pshard_metrics(elastic_cluster, elastic_check, aggregator):
    instance = {'url': URL, 'pshard_stats': True, 'username': USER, 'password': PASSWORD}
    config = from_instance(instance)
    es_version = elastic_check._get_es_version(config)

    elastic_check.check(instance)

    pshard_stats_metrics = pshard_stats_for_version(es_version)
    for m_name, desc in pshard_stats_metrics.iteritems():
        if desc[0] == 'gauge':
            aggregator.assert_metric(m_name)

    # Our pshard metrics are getting sent, let's check that they're accurate
    # Note: please make sure you don't install Maven on the CI for future
    # elastic search CI integrations. It would make the line below fail :/
    aggregator.assert_metric('elasticsearch.primaries.docs.count')
Esempio n. 7
0
def test_health_event(dd_environment, aggregator, elastic_check):
    dummy_tags = ['elastique:recherche']
    instance = {'url': URL, 'username': USER, 'password': PASSWORD, 'tags': dummy_tags}
    config = from_instance(instance)
    es_version = elastic_check._get_es_version(config)

    # Should be yellow at first
    requests.put(URL + '/_settings', data='{"index": {"number_of_replicas": 100}')

    elastic_check.check(instance)

    if es_version < [2, 0, 0]:
        assert len(aggregator.events) == 1
        assert sorted(aggregator.events[0]['tags']) == sorted(set(['url:{}'.format(URL)] + dummy_tags + CLUSTER_TAG))
    else:
        aggregator.assert_service_check('elasticsearch.cluster_health')
def test_from_instance_defaults():
    c = from_instance({'url': 'http://example.com'})

    assert c.admin_forwarder is False
    assert c.pshard_stats is False
    assert c.pshard_graceful_to is False
    assert c.cluster_stats is False
    assert c.index_stats is False
    assert c.password is None
    assert c.service_check_tags == ['host:example.com', 'port:None']
    assert c.health_tags == []
    assert c.ssl_cert is None
    assert c.ssl_key is None
    assert c.ssl_verify is None
    assert c.tags == ['url:http://example.com']
    assert c.timeout == DEFAULT_TIMEOUT
    assert c.url == 'http://example.com'
    assert c.username is None
    assert c.pending_task_stats is True
Esempio n. 9
0
def test_from_instance_cluster_stats():
    c = from_instance({'url': 'http://example.com', 'is_external': True})
    assert c.cluster_stats is True
Esempio n. 10
0
def test_check(elastic_cluster, elastic_check, aggregator):
    default_tags = ["url:http://{}:{}".format(HOST, PORT)]

    for _ in xrange(2):
        try:
            elastic_check.check(CONFIG)
        except Exception:
            time.sleep(1)

    elastic_check.check(CONFIG)

    expected_metrics = dict(ESCheck.STATS_METRICS)
    ESCheck.CLUSTER_HEALTH_METRICS.update(ESCheck.CLUSTER_PENDING_TASKS)
    expected_metrics.update(ESCheck.CLUSTER_HEALTH_METRICS)

    config = from_instance(CONFIG)
    es_version = elastic_check._get_es_version(config)

    assert es_version == get_es_version()

    if es_version >= [6, 3, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_6_3)
    else:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_PRE_6_3)

    if es_version < [5, 0, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_PRE_5_0_0)

    if es_version >= [0, 90, 5]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_0_90_5)
        if es_version >= [0, 90, 10]:
            expected_metrics.update(ESCheck.JVM_METRICS_POST_0_90_10)
        else:
            expected_metrics.update(ESCheck.JVM_METRICS_PRE_0_90_10)
    else:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_PRE_0_90_5)
        expected_metrics.update(ESCheck.JVM_METRICS_PRE_0_90_10)

    if es_version >= [1, 0, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_1_0_0)

    if es_version < [2, 0, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_PRE_2_0)
        if es_version >= [0, 90, 5]:
            expected_metrics.update(
                ESCheck.ADDITIONAL_METRICS_POST_0_90_5_PRE_2_0)
        # Currently has issues in test framework
        # if es_version >= [1, 0, 0]:
        # expected_metrics.update(ESCheck.ADDITIONAL_METRICS_1_x)

    if es_version >= [1, 3, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_1_3_0)

    if es_version >= [1, 4, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_1_4_0)

    if es_version >= [1, 5, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_1_5_0)

    if es_version >= [1, 6, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_1_6_0)

    if es_version >= [2, 0, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_2_0)

    if es_version >= [2, 1, 0]:
        expected_metrics.update(ESCheck.ADDITIONAL_METRICS_POST_2_1)

    if os.environ.get("DD_ELASTIC_LOCAL_HOSTNAME"):
        local_hostname = os.environ.get("DD_ELASTIC_LOCAL_HOSTNAME")
    elif es_version < [2, 0, 0]:
        local_hostname = socket.gethostname()
    else:
        local_hostname = '127.0.0.1'

    contexts = [(CONF_HOSTNAME, default_tags + TAGS),
                (local_hostname, default_tags + TAGS)]

    stats_keys = (set(expected_metrics.keys()) -
                  set(ESCheck.CLUSTER_HEALTH_METRICS.keys()) -
                  set(ESCheck.CLUSTER_PENDING_TASKS.keys()))

    # index_writer_max_memory metric was removed in v5
    deprecated_metrics = [
        'elasticsearch.indices.segments.index_writer_max_memory_in_bytes'
    ]
    for m_name, desc in set(expected_metrics.iteritems()):
        for hostname, m_tags in contexts:
            m_tags = m_tags + CLUSTER_TAG
            if (m_name in ESCheck.CLUSTER_HEALTH_METRICS
                    and hostname == local_hostname):
                hostname = CONF_HOSTNAME

            if m_name in stats_keys:
                m_tags = m_tags + [u"node_name:batman"]

            m_tags.sort()
            if desc[0] == "gauge":
                aggregator.metrics(m_name)
                if es_version < [2, 0, 0]:
                    aggregator.assert_metric(m_name, tags=m_tags)
                elif not (m_name in deprecated_metrics):
                    aggregator.assert_metric(m_name)

    good_sc_tags = ['host:{0}'.format(HOST), 'port:{0}'.format(PORT)]
    aggregator.assert_service_check(
        'elasticsearch.can_connect',
        status=ESCheck.OK,  # OK
        tags=good_sc_tags + TAGS)

    # Assert service metadata
    # self.assertServiceMetadata(['version'], count=3)
    # FIXME: 0.90.13 returns randomly a red status instead of yellow,
    # so we don't do a coverage test for it
    # Remove me when we stop supporting 0.90.x (not supported anymore by ES)
    if get_es_version() != [0, 90, 13]:
        # Warning because elasticsearch status should be yellow, according to
        # http://chrissimpson.co.uk/elasticsearch-yellow-cluster-status-explained.html
        aggregator.assert_service_check('elasticsearch.cluster_health')
Esempio n. 11
0
def test_from_instance_detailed_index_stats():
    c = from_instance({
        'url': 'http://example.com',
        'detailed_index_stats': True
    })
    assert c.detailed_index_stats is True