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

[mongo] skip explain administrative aggregation pipeline #18844

Merged
merged 3 commits into from
Oct 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
1 change: 1 addition & 0 deletions mongo/changelog.d/18844.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Skip explain plan collection for mongo administrative aggregation pipeline, including `$collStats`, `$currentOp`, `$indexStats`, `$listSearchIndexes`, `$sample` and `$shardedDataDistribution`.
17 changes: 17 additions & 0 deletions mongo/datadog_checks/mongo/dbm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@
]
)

UNEXPLAINABLE_PIPELINE_STAGES = frozenset(
[
"$collStats",
"$currentOp",
"$indexStats",
"$listSearchIndexes",
"$sample",
"$shardedDataDistribution",
]
)

COMMAND_KEYS_TO_REMOVE = frozenset(["comment", "lsid", "$clusterTime"])

EXPLAIN_PLAN_KEYS_TO_REMOVE = frozenset(
Expand Down Expand Up @@ -108,6 +119,12 @@ def should_explain_operation(
if any(command.get(key) for key in UNEXPLAINABLE_COMMANDS):
return False

# if UNEXPLAINABLE_PIPELINE_STAGES in command pipeline stages, skip
if pipeline := command.get("pipeline"):
stages = [list(stage.keys())[0] for stage in pipeline if isinstance(stage, dict)]
if any(stage in UNEXPLAINABLE_PIPELINE_STAGES for stage in stages):
return False

db, _ = namespace.split(".", 1)
if db in MONGODB_SYSTEM_DATABASES:
return False
Expand Down
114 changes: 114 additions & 0 deletions mongo/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@

import mock
import pytest
from bson import json_util
from pymongo.errors import ConnectionFailure, OperationFailure

from datadog_checks.base import ConfigurationError
from datadog_checks.base.utils.db.sql import compute_exec_plan_signature
from datadog_checks.mongo.api import CRITICAL_FAILURE, MongoApi
from datadog_checks.mongo.collectors import MongoCollector
from datadog_checks.mongo.common import MongosDeployment, ReplicaSetDeployment, get_state_name
from datadog_checks.mongo.dbm.utils import should_explain_operation
from datadog_checks.mongo.mongo import HostingType, MongoDb, metrics
from datadog_checks.mongo.utils import parse_mongo_uri

Expand Down Expand Up @@ -778,3 +781,114 @@ def seed_mock_client():
def load_json_fixture(name):
with open(os.path.join(common.HERE, "fixtures", name), 'r') as f:
return json.load(f)


@pytest.mark.parametrize(
'namespace,op,command,should_explain',
[
pytest.param(
"test.test",
"command",
{
"aggregate": "test",
"pipeline": [{"$collStats": {"latencyStats": {}, "storageStats": {}, "queryExecStats": {}}}],
"cursor": {},
"$db": "test",
"$readPreference": {"mode": "?"},
},
False,
id='no-explain $collStats',
),
pytest.param(
"test.test",
"command",
{
"aggregate": "test",
"pipeline": [{"$sample": {"size": "?"}}],
"cursor": {},
"$db": "test",
"$readPreference": {"mode": "?"},
},
False,
id='no explain $sample',
),
pytest.param(
"test.test",
"command",
{
"aggregate": "test",
"pipeline": [{"$indexStats": {}}],
"cursor": {},
"$db": "test",
"$readPreference": {"mode": "?"},
},
False,
id='no explain $indexStats',
),
pytest.param(
"test.test",
"command",
{"getMore": "?", "collection": "test", "$db": "test", "$readPreference": {"mode": "?"}},
False,
id='no explain getMore',
),
pytest.param(
"test.test",
"update",
{
"update": "test",
"updates": [{"q": {}, "u": {}, "multi": False, "upsert": False}],
"ordered": True,
"$db": "test",
"$readPreference": {"mode": "?"},
},
False,
id='no explain update',
),
pytest.param(
"test.test",
"insert",
{
"insert": "test",
"documents": [{"_id": "?", "a": 1}],
"ordered": True,
"$db": "test",
"$readPreference": {"mode": "?"},
},
False,
id='no explain insert',
),
pytest.param(
"test.test",
"remove",
{
"delete": "test",
"deletes": [{"q": {}, "limit": 1}],
"ordered": True,
"$db": "test",
"$readPreference": {"mode": "?"},
},
False,
id='no explain delete',
),
pytest.param(
"test.test",
"query",
{"find": "test", "filter": {}, "$db": "test", "$readPreference": {"mode": "?"}},
True,
id='explain find',
),
],
)
def test_should_explain_operation(namespace, op, command, should_explain):
check = MongoDb('mongo', {}, [{'hosts': ['localhost']}])
assert (
should_explain_operation(
namespace,
op,
command,
explain_plan_rate_limiter=check._operation_samples._explained_operations_ratelimiter,
explain_plan_cache_key=(namespace, op, compute_exec_plan_signature(json_util.dumps(command))),
)
== should_explain
)
Loading