Skip to content

Commit

Permalink
collect mongodb activity lsid and transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zhengda committed Feb 4, 2025
1 parent f3730ba commit 50f2a88
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 9 deletions.
26 changes: 25 additions & 1 deletion mongo/datadog_checks/mongo/dbm/operation_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under a 3-clause BSD style license (see LICENSE)


import binascii
import time
from typing import List, Optional

Expand Down Expand Up @@ -259,6 +260,28 @@ def _get_operation_cursor(self, operation: dict) -> Optional[OperationSampleOper
"operation_using_cursor_id": cursor.get("operationUsingCursorId"),
}

def _get_operation_lsid(self, operation: dict) -> Optional[dict]:
lsid = operation.get("lsid")
if not lsid or not lsid.get("id"):
return None

return {
"id": binascii.hexlify(lsid['id']).decode(),
}

def _get_operation_transaction(self, operation: dict) -> Optional[dict]:
transaction = operation.get("transaction")
if not transaction:
return None

return {
"txn_number": transaction.get("parameters", {}).get("txnNumber"),
"txn_retry_counter": transaction.get("parameters", {}).get("txnRetryCounter"),
"time_open_micros": transaction.get("timeOpenMicros"),
"time_active_micros": transaction.get("timeActiveMicros"),
"time_inactive_micros": transaction.get("timeInactiveMicros"),
}

def _get_operation_stats(self, operation: dict) -> OperationSampleOperationStats:
return {
"active": operation.get("active", False), # bool
Expand All @@ -269,7 +292,6 @@ def _get_operation_stats(self, operation: dict) -> OperationSampleOperationStats
"query_framework": operation.get("queryFramework"), # str
"current_op_time": operation.get("currentOpTime"), # str start time of the operation
"microsecs_running": operation.get("microsecs_running"), # int
"transaction_time_open_micros": operation.get("transaction", {}).get("timeOpenMicros"), # int
# Conflicts
"prepare_read_conflicts": operation.get("prepareReadConflicts", 0), # int
"write_conflicts": operation.get("writeConflicts", 0), # int
Expand All @@ -291,6 +313,8 @@ def _get_operation_stats(self, operation: dict) -> OperationSampleOperationStats
), # dict
# cursor
"cursor": self._get_operation_cursor(operation), # dict
"transaction": self._get_operation_transaction(operation), # dict
"lsid": self._get_operation_lsid(operation), # dict
}

def _get_query_signature(self, obfuscated_command: str) -> str:
Expand Down
11 changes: 11 additions & 0 deletions mongo/datadog_checks/mongo/dbm/slow_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under a 3-clause BSD style license (see LICENSE)


import binascii
import time
from datetime import datetime

Expand Down Expand Up @@ -353,6 +354,7 @@ def _create_slow_operation_explain_plan_payload(self, slow_operation: dict, expl
"lock_stats": self._get_slow_operation_lock_stats(slow_operation),
"flow_control_stats": self._get_slow_operation_flow_control_stats(slow_operation),
"cursor": self._get_slow_operation_cursor(slow_operation),
"lsid": self._get_slow_operation_lsid(slow_operation["command"]),
}
),
}
Expand All @@ -378,6 +380,15 @@ def _get_slow_operation_cursor(self, slow_operation):
}
return None

def _get_slow_operation_lsid(self, command):
lsid = command.get("lsid")
if not lsid or not lsid.get("id"):
return None

return {
"id": binascii.hexlify(lsid['id']).decode(),
}

def _get_slow_operation_lock_stats(self, slow_operation):
lock_stats = slow_operation.get("locks")
if lock_stats:
Expand Down
15 changes: 14 additions & 1 deletion mongo/datadog_checks/mongo/dbm/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ class OperationSampleOperationStatsCursor(TypedDict, total=False):
operation_using_cursor_id: Optional[str]


class OperationSampleOperationStatsTransaction(TypedDict, total=False):
txn_number: int
txn_retry_counter: int
time_open_micros: int
time_active_micros: int
time_inactive_micros: int


class OperationSampleOperationStatsLsid(TypedDict, total=False):
id: str


class OperationSampleOperationStats(TypedDict, total=False):
active: bool
desc: Optional[str]
Expand All @@ -134,7 +146,6 @@ class OperationSampleOperationStats(TypedDict, total=False):
query_framework: Optional[str]
current_op_time: str
microsecs_running: Optional[int]
transaction_time_open_micros: Optional[int]
prepare_read_conflicts: Optional[int]
write_conflicts: Optional[int]
num_yields: Optional[int]
Expand All @@ -145,6 +156,8 @@ class OperationSampleOperationStats(TypedDict, total=False):
flow_control_stats: Optional[OperationSampleOperationStatsFlowControlStats]
waiting_for_latch: Optional[OperationSampleOperationStatsWaitingForLatch]
cursor = Optional[OperationSampleOperationStatsCursor]
transaction = Optional[OperationSampleOperationStatsTransaction]
lsid = Optional[OperationSampleOperationStatsLsid]


class OperationSampleActivityBase(TypedDict, total=False):
Expand Down
22 changes: 22 additions & 0 deletions mongo/tests/fixtures/$currentOp-mongos
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@
}
}
},
"transaction": {
"parameters": {
"txnNumber": 392,
"txnRetryCounter": 0,
"autocommit": false,
"readConcern": {
"level": "local",
"provenance": "implicitDefault"
}
},
"readTimestamp": {
"$timestamp": {
"t": 0,
"i": 0
}
},
"startWallClockTime": "2025-02-04T18:45:22.915+00:00",
"timeOpenMicros": 1750,
"timeActiveMicros": 435,
"timeInactiveMicros": 1315,
"expiryTime": "2025-02-04T18:46:22.915+00:00"
},
"secs_running": 0,
"microsecs_running": 29232,
"op": "query",
Expand Down
19 changes: 16 additions & 3 deletions mongo/tests/results/operation-activities-mongos.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
"query_framework": null,
"current_op_time": "2024-05-20T14:36:56.231+00:00",
"microsecs_running": 29232,
"transaction_time_open_micros": null,
"prepare_read_conflicts": 0,
"write_conflicts": 0,
"num_yields": 0,
"waiting_for_lock": false,
"lsid": {
"id": "ff95bad791504c36ab749ae8c640d54e"
},
"locks": {
"feature_compatibility_version": "r",
"global": "r"
Expand Down Expand Up @@ -63,6 +65,13 @@
"type": "op",
"op": "query",
"shard": "shard04",
"transaction": {
"time_active_micros": 435,
"time_inactive_micros": 1315,
"time_open_micros": 1750,
"txn_number": 392,
"txn_retry_counter": 0
},
"dbname": "integration",
"application": null,
"collection": "users",
Expand Down Expand Up @@ -96,11 +105,13 @@
"query_framework": "classic",
"current_op_time": "2024-06-13T20:50:10.834+00:00",
"microsecs_running": 26789,
"transaction_time_open_micros": null,
"prepare_read_conflicts": 0,
"write_conflicts": 0,
"num_yields": 1,
"waiting_for_lock": false,
"lsid": {
"id": "ccb77e0cda6441daa458dd4067cdbc48"
},
"locks": {
"feature_compatibility_version": "r",
"global": "r"
Expand Down Expand Up @@ -142,6 +153,7 @@
"type": "op",
"op": "getmore",
"shard": null,
"transaction": null,
"dbname": "integration",
"application": "orders-mongo",
"collection": "movies",
Expand Down Expand Up @@ -173,11 +185,11 @@
"query_framework": "classic",
"current_op_time": "2024-08-06T20:59:35.394+00:00",
"microsecs_running": 13134,
"transaction_time_open_micros": null,
"prepare_read_conflicts": 0,
"write_conflicts": 0,
"num_yields": 2,
"waiting_for_lock": false,
"lsid": null,
"locks": {},
"lock_stats": {
"feature_compatibility_version": {
Expand Down Expand Up @@ -211,6 +223,7 @@
"type": "op",
"op": "getmore",
"shard": null,
"transaction": null,
"dbname": "local",
"application": "OplogFetcher",
"collection": "oplog.rs",
Expand Down
8 changes: 6 additions & 2 deletions mongo/tests/results/operation-activities-standalone.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
"query_framework": null,
"current_op_time": "2024-05-16T18:06:38.419+00:00",
"microsecs_running": 167,
"transaction_time_open_micros": null,
"prepare_read_conflicts": 0,
"write_conflicts": 0,
"num_yields": 0,
"waiting_for_lock": false,
"lsid": {
"id": "fe7f64529521475ca6263130bc2c926e"
},
"locks": {
"feature_compatibility_version": "r",
"global": "r"
Expand Down Expand Up @@ -64,6 +66,7 @@
"type": "op",
"op": "query",
"shard": null,
"transaction": null,
"dbname": "integration",
"application": null,
"collection": "products",
Expand Down Expand Up @@ -97,12 +100,12 @@
"query_framework": "classic",
"current_op_time": "2024-08-06T20:59:35.394+00:00",
"microsecs_running": 13134,
"transaction_time_open_micros": null,
"prepare_read_conflicts": 0,
"write_conflicts": 0,
"num_yields": 2,
"waiting_for_lock": false,
"locks": {},
"lsid": null,
"lock_stats": {
"feature_compatibility_version": {
"acquire_count": {
Expand Down Expand Up @@ -135,6 +138,7 @@
"type": "op",
"op": "getmore",
"shard": null,
"transaction": null,
"dbname": "local",
"application": "OplogFetcher",
"collection": "oplog.rs",
Expand Down
11 changes: 10 additions & 1 deletion mongo/tests/results/operation-samples-mongos.json
Original file line number Diff line number Diff line change
Expand Up @@ -723,11 +723,20 @@
"query_framework": null,
"current_op_time": "2024-05-20T14:36:56.231+00:00",
"microsecs_running": 29232,
"transaction_time_open_micros": null,
"transaction": {
"time_active_micros": 435,
"time_inactive_micros": 1315,
"time_open_micros": 1750,
"txn_number": 392,
"txn_retry_counter": 0
},
"prepare_read_conflicts": 0,
"write_conflicts": 0,
"num_yields": 0,
"waiting_for_lock": false,
"lsid": {
"id": "ff95bad791504c36ab749ae8c640d54e"
},
"locks": {
"feature_compatibility_version": "r",
"global": "r"
Expand Down
5 changes: 4 additions & 1 deletion mongo/tests/results/operation-samples-standalone.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,13 @@
"query_framework": null,
"current_op_time": "2024-05-16T18:06:38.419+00:00",
"microsecs_running": 167,
"transaction_time_open_micros": null,
"prepare_read_conflicts": 0,
"write_conflicts": 0,
"num_yields": 0,
"waiting_for_lock": false,
"lsid": {
"id": "fe7f64529521475ca6263130bc2c926e"
},
"locks": {
"feature_compatibility_version": "r",
"global": "r"
Expand Down Expand Up @@ -182,6 +184,7 @@
"type": "op",
"op": "query",
"shard": null,
"transaction": null,
"collection": "products",
"comment": "find",
"truncated": "not_truncated"
Expand Down

0 comments on commit 50f2a88

Please sign in to comment.