Is there any way of getting the server IP from which db call is happening

Is there any way of getting the server IP from which db call is happening.
Mongodb is a sharded cluster of 3 and applications are running of kuberentes pods on different server

Hi @Aman_Saxena,

First, not every database query is logged by default , only the ‘slow’ queries that exceed 100ms.

If the slow queries are the one of interest that is fine, otherwise the slow query threshold can be adjusted by using https://www.mongodb.com/docs/manual/reference/method/db.setProfilingLevel for example db.setProfilingLevel(0,0) will log all queries.

Each query logged will have a attr.remote field which is the ip/port of the client issuing the query.

For a sharded cluster this will be logged on the mongos.

Example:

{“t”:{“$date”:“2024-05-01T15:33:28.725+00:00”},“s”:“I”, “c”:“COMMAND”, “id”:51803, “ctx”:“conn2”,“msg”:“Slow query”,“attr”:{“type”:“command”,“ns”:“test.foo”,“appName”:“mongosh 2.2.5”,“command”:{“find”:“foo”,“filter”:{},“lsid”:{“id”:{“$uuid”:“b153ca90-522d-4a3d-a884-c2972ea86684”}},“$db”:“test”},“planSummary”:“EOF”,“planningTimeMicros”:50,“keysExamined”:0,“docsExamined”:0,“nBatches”:1,“cursorExhausted”:true,“numYields”:0,“nreturned”:0,“queryFramework”:“classic”,“reslen”:97,“locks”:{“FeatureCompatibilityVersion”:{“acquireCount”:{“r”:1}},“Global”:{“acquireCount”:{“r”:1}}},“storage”:{},“cpuNanos”:99127,“remote”:“172.17.0.1:44562”,“protocol”:“op_msg”,“durationMillis”:0}}

The remote client for this query was:
remote":"172.17.0.1:44562

Edit:

Be sure to set the profiler back to the default of 100ms db.setProfilingLevel(0,100) as this can generate a LOT of logs if left at 0ms.

2 Likes