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

Add host tags to triggered events #17287

Merged
merged 6 commits into from
Mar 28, 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
7 changes: 7 additions & 0 deletions mongo/assets/configuration/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ files:
value:
type: boolean
example: false
- name: add_node_tag_to_events
description: |
Adds the Mongo node to events as a tag rather than creating a seperate host for the event.
hidden: true
value:
type: boolean
example: true
- name: custom_queries
description: |
Define custom queries to collect custom metrics on your Mongo
Expand Down
1 change: 1 addition & 0 deletions mongo/changelog.d/17287.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add host tags triggered events
9 changes: 9 additions & 0 deletions mongo/datadog_checks/mongo/collectors/replica.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,18 @@ def _report_replica_set_states(self, members, replset_name):
'replset:' + replset_name,
],
}

if self.check._config.add_node_tag_to_events:
event_payload['host'] = self.hostname
event_payload['tags'].append('mongo_node:' + node_hostname)

if node_hostname == 'localhost':
# Do not submit events with a 'localhost' hostname.
if self.check._config.add_node_tag_to_events:
event_payload['tags'][4] = "mongo_node:{}".format(self.hostname)

event_payload['host'] = self.hostname

self.check.event(event_payload)

def get_votes_config(self, api):
Expand Down
2 changes: 1 addition & 1 deletion mongo/datadog_checks/mongo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def __init__(self, instance, log):
'Setting the `replicaSet` option is not supported. '
'Configure one check instance for each node instead'
)

self.auth_source = self.additional_options.get('authSource') or self.db_name or 'admin'

if not self.hosts:
Expand Down Expand Up @@ -90,6 +89,7 @@ def __init__(self, instance, log):

self.replica_check = is_affirmative(instance.get('replica_check', True))

self.add_node_tag_to_events = is_affirmative(instance.get('add_node_tag_to_events', True))
self.collections_indexes_stats = is_affirmative(instance.get('collections_indexes_stats'))
self.coll_names = instance.get('collections', [])
self.custom_queries = instance.get("custom_queries", [])
Expand Down
4 changes: 4 additions & 0 deletions mongo/datadog_checks/mongo/config_models/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
# ddev -x validate models -s <INTEGRATION_NAME>


def instance_add_node_tag_to_events():
return True


def instance_collections_indexes_stats():
return False

Expand Down
1 change: 1 addition & 0 deletions mongo/datadog_checks/mongo/config_models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class InstanceConfig(BaseModel):
arbitrary_types_allowed=True,
frozen=True,
)
add_node_tag_to_events: Optional[bool] = None
additional_metrics: Optional[tuple[str, ...]] = None
collections: Optional[tuple[str, ...]] = None
collections_indexes_stats: Optional[bool] = None
Expand Down
1 change: 1 addition & 0 deletions mongo/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def instance_integration(instance_custom_queries):
instance["additional_metrics"] = ["metrics.commands", "tcmalloc", "collection", "top", "jumbo_chunks"]
instance["collections"] = ["foo", "bar"]
instance["collections_indexes_stats"] = True
instance["add_node_tag_to_events"] = False
return instance


Expand Down
79 changes: 79 additions & 0 deletions mongo/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,85 @@ def test_integration_replicaset_primary(instance_integration, aggregator, check,
)


def test_integration_replicaset_primary_config(instance_integration, aggregator, check, dd_run_check):
instance_integration.update({'add_node_tag_to_events': True})
mongo_check = check(instance_integration)
mongo_check.last_states_by_server = {0: 2, 1: 1, 2: 7, 3: 2}

with mock_pymongo("replica-primary"):
dd_run_check(mongo_check)

replica_tags = ['replset_name:replset', 'replset_state:primary']
metrics_categories = [
'count-dbs',
'serverStatus',
'custom-queries',
'oplog',
'replset-primary',
'top',
'dbstats-local',
'fsynclock',
'dbstats',
'indexes-stats',
'collection',
]
_assert_metrics(aggregator, metrics_categories, replica_tags)
# Lag metrics are tagged with the state of the member and not with the current one.
_assert_metrics(aggregator, ['replset-lag-from-primary'])

aggregator.assert_all_metrics_covered()
aggregator.assert_metrics_using_metadata(
get_metadata_metrics(),
exclude=[
'dd.custom.mongo.aggregate.total',
'dd.custom.mongo.count',
'dd.custom.mongo.query_a.amount',
'dd.custom.mongo.query_a.el',
],
check_submission_type=True,
)
assert len(aggregator._events) == 3
aggregator.assert_event(
"MongoDB replset-data-0.mongo.default.svc.cluster.local:27017 "
"(_id: 0, mongodb://testUser2:*****@localhost:27017/test) just reported as Primary (PRIMARY) for "
"replset; it was SECONDARY before.",
tags=[
'action:mongo_replset_member_status_change',
'member_status:PRIMARY',
'previous_member_status:SECONDARY',
'replset:replset',
'mongo_node:replset-data-0.mongo.default.svc.cluster.local:27017',
],
count=1,
)
aggregator.assert_event(
"MongoDB replset-arbiter-0.mongo.default.svc.cluster.local:27017 "
"(_id: 1, mongodb://testUser2:*****@localhost:27017/test) just reported as Arbiter (ARBITER) for "
"replset; it was PRIMARY before.",
tags=[
'action:mongo_replset_member_status_change',
'member_status:ARBITER',
'previous_member_status:PRIMARY',
'replset:replset',
'mongo_node:replset-arbiter-0.mongo.default.svc.cluster.local:27017',
],
count=1,
)
aggregator.assert_event(
"MongoDB replset-data-1.mongo.default.svc.cluster.local:27017 "
"(_id: 2, mongodb://testUser2:*****@localhost:27017/test) just reported as Secondary (SECONDARY) for "
"replset; it was ARBITER before.",
tags=[
'action:mongo_replset_member_status_change',
'member_status:SECONDARY',
'previous_member_status:ARBITER',
'replset:replset',
'mongo_node:replset-data-1.mongo.default.svc.cluster.local:27017',
],
count=1,
)


@pytest.mark.parametrize('collect_custom_queries', [True, False])
def test_integration_replicaset_secondary(
instance_integration, aggregator, check, collect_custom_queries, dd_run_check
Expand Down
Loading