Skip to content

Commit

Permalink
Merge pull request #4410 from telefonicaid/hardening/improve-mongodb-…
Browse files Browse the repository at this point in the history
…logs

FIX improve logs in MongoDB query logic
  • Loading branch information
mapedraza authored Aug 30, 2023
2 parents b8773c7 + 2d3c324 commit 2f74717
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- Add: servicePath field to builtin attributes (#2877)
- Fix: logDeprecate not working correctly (`geo:json` wrongly considered as deprecated)
- Fix: improve error traces (#4387)
- Add: CLI parameter -dbURI / env var ORION_MONGO_URI (#3794)
- Add: CLI parameter -dbUri / env var ORION_MONGO_URI (#3794)
- Fix: improve logs in MongoDB query logic
15 changes: 10 additions & 5 deletions src/lib/mongoDriver/connectionOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ bool orion::collectionRangedQuery
// Getting low level driver object
bson_t* q = _q.get();
bson_t* qc = _countQuery.get();
bson_t* sort = _sort.get();

char* bsonStr = bson_as_relaxed_extended_json(q, NULL);
char* bsonStrCount = bson_as_relaxed_extended_json(qc, NULL);
char* bsonStrSort = bson_as_relaxed_extended_json(sort, NULL);

LM_T(LmtMongo, ("query() in '%s' collection limit=%d, offset=%d, query='%s', count='%s'",
ns.c_str(), limit, offset, bsonStr, bsonStrCount));
LM_T(LmtMongo, ("query() in '%s' collection limit=%d, offset=%d, query='%s', count='%s', sort='%s'",
ns.c_str(), limit, offset, bsonStr, bsonStrCount, bsonStrSort));
mongoc_collection_t *collection = mongoc_client_get_collection(connection, db.c_str(), col.c_str());

// First, set countP (if used). In the case of error, we return early and the actual query doesn't take place
Expand All @@ -172,7 +174,7 @@ bool orion::collectionRangedQuery
else
{
std::string msg = std::string("collection: ") + ns.c_str() +
" - count_documents(): " + bsonStrCount +
" - count_documents: " + bsonStrCount +
" - exception: " + error.message;

*err = "Database Error (" + msg + ")";
Expand All @@ -181,14 +183,15 @@ bool orion::collectionRangedQuery
mongoc_collection_destroy(collection);
bson_free(bsonStr);
bson_free(bsonStrCount);
bson_free(bsonStrSort);

return false;
}
}

// Build the options document
bson_t* opt = bson_new();
BSON_APPEND_DOCUMENT(opt, "sort", _sort.get());
BSON_APPEND_DOCUMENT(opt, "sort", sort);
BSON_APPEND_INT32(opt, "limit", limit);
BSON_APPEND_INT32(opt, "skip", offset);

Expand All @@ -204,7 +207,8 @@ bool orion::collectionRangedQuery
if (mongoc_cursor_error(c, &error))
{
std::string msg = std::string("collection: ") + ns +
" - query(): " + bsonStr +
" - query: " + bsonStr +
" - opt: " + bsonOptStr +
" - exception: " + error.message;

*err = "Database Error (" + msg + ")";
Expand All @@ -224,6 +228,7 @@ bool orion::collectionRangedQuery

bson_free(bsonStr);
bson_free(bsonStrCount);
bson_free(bsonStrSort);
bson_free(bsonOptStr);

return r;
Expand Down

0 comments on commit 2f74717

Please sign in to comment.