Skip to content

Add configuration value to Consul check to not send service metrics containing service name #20309

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions consul/assets/configuration/spec.yaml
Original file line number Diff line number Diff line change
@@ -109,6 +109,14 @@ files:
- <SERVICE_1>
- <SERVICE_2>

- name: services_tags_include_service_name
description: |
Whether to produce additional service tags, which include the service
name in the tag key, for each service.
value:
type: boolean
example: true

- name: max_services
description: |
Increase the maximum number of queried services.
1 change: 1 addition & 0 deletions consul/changelog.d/20309.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a new feature to remove Consul service name from service check tag keys sent to Datadog.
4 changes: 4 additions & 0 deletions consul/datadog_checks/consul/config_models/defaults.py
Original file line number Diff line number Diff line change
@@ -84,6 +84,10 @@ def instance_self_leader_check():
return False


def instance_services_tags_include_service_name():
return True


def instance_single_node_install():
return False

1 change: 1 addition & 0 deletions consul/datadog_checks/consul/config_models/instance.py
Original file line number Diff line number Diff line change
@@ -91,6 +91,7 @@ class InstanceConfig(BaseModel):
service: Optional[str] = None
services_exclude: Optional[tuple[str, ...]] = None
services_include: Optional[tuple[str, ...]] = None
services_tags_include_service_name: Optional[bool] = None
single_node_install: Optional[bool] = None
skip_proxy: Optional[bool] = None
tags: Optional[tuple[str, ...]] = None
10 changes: 7 additions & 3 deletions consul/datadog_checks/consul/consul.py
Original file line number Diff line number Diff line change
@@ -106,6 +106,9 @@ def __init__(self, name, init_config, instances):
'service_whitelist', self.instance.get('services_include', default_services_include)
)
self.services_exclude = set(self.instance.get('services_exclude', self.init_config.get('services_exclude', [])))
self.services_tags_include_service_name = is_affirmative(
self.instance.get('services_tags_include_service_name', True)
)
self.max_services = self.instance.get('max_services', self.init_config.get('max_services', MAX_SERVICES))
self.threads_count = self.instance.get('threads_count', self.init_config.get('threads_count', THREADS_COUNT))
if self.threads_count > 1:
@@ -313,11 +316,12 @@ def _cull_services_list(self, services):
return services

@staticmethod
def _get_service_tags(service, tags):
def _get_service_tags(service, tags, include_service_name):
service_tags = ['consul_service_id:{}'.format(service)]

for tag in tags:
service_tags.append('consul_{}_service_tag:{}'.format(service, tag))
if include_service_name:
service_tags.append('consul_{}_service_tag:{}'.format(service, tag))
service_tags.append('consul_service_tag:{}'.format(tag))

return service_tags
@@ -483,7 +487,7 @@ def _submit_service_status(
# `consul.catalog.nodes_passing` : # of Nodes with service status `passing` from those registered
# `consul.catalog.nodes_warning` : # of Nodes with service status `warning` from those registered
# `consul.catalog.nodes_critical` : # of Nodes with service status `critical` from those registered
all_service_tags = self._get_service_tags(service, service_tags)
all_service_tags = self._get_service_tags(service, service_tags, self.services_tags_include_service_name)
# {'up': 0, 'passing': 0, 'warning': 0, 'critical': 0}
node_count_per_status = defaultdict(int)
for node in nodes_with_service:
6 changes: 6 additions & 0 deletions consul/datadog_checks/consul/data/conf.yaml.example
Original file line number Diff line number Diff line change
@@ -126,6 +126,12 @@ instances:
# - <SERVICE_1>
# - <SERVICE_2>

## @param services_tags_include_service_name - boolean - optional - default: true
## Whether to produce additional service tags, which include the service
## name in the tag key, for each service.
#
# services_tags_include_service_name: true

## @param max_services - number - optional - default: 50
## Increase the maximum number of queried services.
#
19 changes: 19 additions & 0 deletions consul/tests/test_unit.py
Original file line number Diff line number Diff line change
@@ -54,6 +54,25 @@ def test_get_nodes_with_service(aggregator):
aggregator.assert_metric('consul.catalog.services_count', value=1, tags=expected_tags)


def test_get_nodes_with_service_remove_service_tagkey(aggregator):
consul_check = ConsulCheck(common.CHECK_NAME, {}, [consul_mocks.MOCK_CONFIG])
consul_check.services_tags_include_service_name = False
consul_mocks.mock_check(consul_check, consul_mocks._get_consul_mocks())
consul_check.check(None)
Comment on lines +58 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
consul_check = ConsulCheck(common.CHECK_NAME, {}, [consul_mocks.MOCK_CONFIG])
consul_check.services_tags_include_service_name = False
consul_mocks.mock_check(consul_check, consul_mocks._get_consul_mocks())
consul_check.check(None)
no_tag_service_name_config = consul_mocks.MOCK_CONFIG
no_tag_service_name_config['services_tags_include_service_name'] = False
consul_check = ConsulCheck(common.CHECK_NAME, {}, [consul_mocks.MOCK_CONFIG])
consul_mocks.mock_check(consul_check, consul_mocks._get_consul_mocks())
consul_check.check(None)

Nit- can you set the config option here?


expected_tags = [
'consul_datacenter:dc1',
'consul_service_id:service-1',
'consul_service_tag:active',
'consul_service_tag:standby',
]

aggregator.assert_metric('consul.catalog.nodes_up', value=4, tags=expected_tags)
aggregator.assert_metric('consul.catalog.nodes_passing', value=4, tags=expected_tags)
aggregator.assert_metric('consul.catalog.nodes_warning', value=0, tags=expected_tags)
aggregator.assert_metric('consul.catalog.nodes_critical', value=0, tags=expected_tags)


def test_get_peers_in_cluster(aggregator):
my_mocks = consul_mocks._get_consul_mocks()
consul_check = ConsulCheck(common.CHECK_NAME, {}, [consul_mocks.MOCK_CONFIG])
Loading
Oops, something went wrong.