Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sy/elastic sc tags #18687

Merged
merged 6 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions elastic/assets/configuration/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ files:
example: false
- name: disable_legacy_cluster_tag
description: Enable to stop submitting the tag `cluster_name`, which has been renamed to `elastic_cluster`.
value:
type: boolean
display_default: false
example: true
enabled: false
- name: disable_legacy_service_check_tags
description: |
Disable the submission of the elasticsearch `host` and `port` as tags in service checks.
Submit them as `url` instead.
value:
type: boolean
display_default: false
Expand Down
1 change: 1 addition & 0 deletions elastic/changelog.d/18687.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add functionality to not use `host` and `port` tags in service checks
5 changes: 4 additions & 1 deletion elastic/datadog_checks/elastic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ def from_instance(instance):
host = parsed.hostname

custom_tags = instance.get('tags', [])
service_check_tags = ['host:{}'.format(host), 'port:{}'.format(port)]
if is_affirmative(instance.get('disable_legacy_service_check_tags', False)):
service_check_tags = ['url:{}'.format(url)]
else:
service_check_tags = ['host:{}'.format(host), 'port:{}'.format(port)]
service_check_tags.extend(custom_tags)

custom_queries = instance.get('custom_queries', [])
Expand Down
4 changes: 4 additions & 0 deletions elastic/datadog_checks/elastic/config_models/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def instance_disable_legacy_cluster_tag():
return False


def instance_disable_legacy_service_check_tags():
return False


def instance_empty_default_hostname():
return False

Expand Down
1 change: 1 addition & 0 deletions elastic/datadog_checks/elastic/config_models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class InstanceConfig(BaseModel):
detailed_index_stats: Optional[bool] = None
disable_generic_tags: Optional[bool] = None
disable_legacy_cluster_tag: Optional[bool] = None
disable_legacy_service_check_tags: Optional[bool] = None
empty_default_hostname: Optional[bool] = None
extra_headers: Optional[MappingProxyType[str, Any]] = None
gc_collectors_as_rate: Optional[bool] = None
Expand Down
8 changes: 7 additions & 1 deletion elastic/datadog_checks/elastic/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ instances:
## @param disable_legacy_cluster_tag - boolean - optional - default: false
## Enable to stop submitting the tag `cluster_name`, which has been renamed to `elastic_cluster`.
#
disable_legacy_cluster_tag: true
# disable_legacy_cluster_tag: true

## @param disable_legacy_service_check_tags - boolean - optional - default: false
## Disable the submission of the elasticsearch `host` and `port` as tags in service checks.
## Submit them as `url` instead.
#
disable_legacy_service_check_tags: true

## @param custom_queries - list of mappings - optional
## Run custom queries for Elasticsearch. Each custom query endpoint can collect multiple metrics and tags.
Expand Down
38 changes: 37 additions & 1 deletion elastic/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import mock
import pytest

from datadog_checks.base import ConfigurationError
from datadog_checks.base import ConfigurationError, is_affirmative
from datadog_checks.dev.http import MockResponse
from datadog_checks.elastic import ESCheck
from datadog_checks.elastic.elastic import AuthenticationError, get_value_from_path
Expand Down Expand Up @@ -178,6 +178,42 @@ def test__get_data_creates_critical_service_alert(aggregator, instance):
)


@pytest.mark.parametrize(
'es_instance',
[
pytest.param(
{'url': 'http://localhost:9200', 'disable_legacy_service_check_tags': 'true'},
id="disable legacy service check behavior",
),
pytest.param(
{'url': 'http://localhost:9200', 'disable_legacy_service_check_tags': 'false'},
id="use legacy service check behavior",
),
],
)
def test_disable_legacy_sc_tags(aggregator, es_instance):
with mock.patch(
'requests.get',
return_value=MockResponse(status_code=500),
):
check = ESCheck('elastic', {}, instances=[es_instance])

with pytest.raises(Exception):
check._get_data(url='test.com')

if is_affirmative(es_instance['disable_legacy_service_check_tags']):
expected_tags = ['url:http://localhost:9200']
else:
expected_tags = ['host:localhost', 'port:9200']

aggregator.assert_service_check(
check.SERVICE_CHECK_CONNECT_NAME,
status=check.CRITICAL,
tags=expected_tags,
message="Error 500 Server Error: None for url: None when hitting test.com",
)


@pytest.mark.parametrize(
'version, return_value',
[
Expand Down
Loading