Skip to content

CDRIVER-4485 string append and truncation fixes for structured logging #1826

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

Merged
24 commits merged into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e214629
structured logging json and truncation fixes, using the new mcommon_s…
Dec 31, 2024
c0ac63b
Add libmongoc API control for MONGODB_LOG_MAX_DOCUMENT_LENGTH setting
Jan 15, 2025
ba40de3
Unified tests use the suggested max document length of 10000 from the…
Jan 15, 2025
f616c88
command-logging-and-monitoring prose tests
Jan 15, 2025
c386923
Remove extra :man_page: tag
Jan 15, 2025
8bde27e
Mark prose tests as requiring a live server
Jan 15, 2025
a940fc4
Check more API return values in CLAM logging prose tests
Jan 15, 2025
e71b3ea
Merge branch 'master' into CDRIVER-4485-b
Jan 16, 2025
e58dc1e
Update for compatibility with #1821
Jan 16, 2025
fe5f948
Rename local to avoid shadowing
Jan 16, 2025
9bafef3
additional document sequence validation
Jan 16, 2025
7333080
Update src/libmongoc/doc/mongoc_structured_log_opts_get_max_document_…
Jan 16, 2025
039a78a
Update src/libmongoc/doc/mongoc_structured_log_opts_set_max_document_…
Jan 16, 2025
9e71dee
Update src/libmongoc/src/mongoc/mongoc-structured-log.c
Jan 16, 2025
c4f8167
see also
Jan 16, 2025
c4c6d16
Remove unintended printf
Jan 16, 2025
90909e7
Replace counter with flag for env error guards
Jan 16, 2025
c96b9d3
Explicit requirement for unset env option
Jan 16, 2025
7654e67
comment correction, not using the mock server
Jan 16, 2025
78634de
Update src/libmongoc/doc/mongoc_structured_log_opts_get_max_document_…
Jan 16, 2025
b2f6d52
getenv fixes, and skip testing env defaults when external values are set
Jan 16, 2025
989e2e4
Improved test skip for structured log env defaults, revert TestSuite …
Jan 17, 2025
fae30e3
Avoid environment dependencies
Jan 17, 2025
aae1bc1
Add comment to explain where this max_document_length comes from
Jan 17, 2025
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 src/libmongoc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,7 @@ set (test-libmongoc-sources
${PROJECT_SOURCE_DIR}/tests/test-mongoc-collection-find-with-opts.c
${PROJECT_SOURCE_DIR}/tests/test-mongoc-collection-find.c
${PROJECT_SOURCE_DIR}/tests/test-mongoc-collection.c
${PROJECT_SOURCE_DIR}/tests/test-mongoc-command-logging-and-monitoring.c
${PROJECT_SOURCE_DIR}/tests/test-mongoc-command-monitoring.c
${PROJECT_SOURCE_DIR}/tests/test-mongoc-connection-uri.c
${PROJECT_SOURCE_DIR}/tests/test-mongoc-counters.c
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
:man_page: mongoc_structured_log_opts_get_max_document_length

mongoc_structured_log_opts_get_max_document_length()
====================================================

Synopsis
--------

.. code-block:: c

size_t
mongoc_structured_log_opts_get_max_document_length (const mongoc_structured_log_opts_t *opts);

Parameters
----------

* ``opts``: Structured log options, allocated with :symbol:`mongoc_structured_log_opts_new`.

Returns
-------

Returns the current maximum document length set in ``opts``, as a ``size_t``.

.. seealso::

| :doc:`structured_log`
2 changes: 1 addition & 1 deletion src/libmongoc/doc/mongoc_structured_log_opts_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Environment Variables
This is a full list of the captured environment variables.

* ``MONGODB_LOG_MAX_DOCUMENT_LENGTH``: Maximum length for JSON-serialized documents that appear within a log message.
It may be a number, in bytes, or ``unlimited`` (case insensitive).
It may be a number, in bytes, or ``unlimited`` (case insensitive) to choose an implementation-specific value near the maximum representable length.
By default, the limit is 1000 bytes.
This limit affects interior documents like commands and replies, not the total length of a structured log message.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
:man_page: mongoc_structured_log_opts_set_max_document_length

mongoc_structured_log_opts_set_max_document_length()
====================================================

Synopsis
--------

.. code-block:: c

bool
mongoc_structured_log_opts_set_max_document_length (mongoc_structured_log_opts_t *opts,
size_t max_document_length);

Sets a maximum length for BSON documents that appear serialized in JSON form as part of a structured log message.

Serialized JSON will be truncated at this limit, interpreted as a count of UTF-8 encoded bytes. Truncation will be indicated with a ``...`` suffix, the length of which is not included in the max document length. If truncation at the exact indicated length would split a valid UTF-8 sequence, we instead truncate the document earlier at the nearest boundary between code points.

Parameters
----------

* ``opts``: Structured log options, allocated with :symbol:`mongoc_structured_log_opts_new`.
* ``max_document_length``: Maximum length for each embedded JSON document, in bytes, not including an ellipsis (``...``) added to indicate truncation. Values near or above ``INT_MAX`` will be rejected.

Returns
-------

Returns ``true`` on success, or ``false`` if the supplied maximum length is too large.

.. seealso::

| :doc:`structured_log`
| :symbol:`mongoc_structured_log_opts_set_max_document_length_from_env`
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
:man_page: mongoc_structured_log_opts_set_max_document_length_from_env

mongoc_structured_log_opts_set_max_document_length_from_env()
=============================================================

Synopsis
--------

.. code-block:: c

bool
mongoc_structured_log_opts_set_max_document_length_from_env (mongoc_structured_log_opts_t *opts);

Sets a maximum document length from the ``MONGODB_LOG_MAX_DOCUMENT_LENGTH`` environment variable, if a valid setting is found.
See :symbol:`mongoc_structured_log_opts_new` for a description of the supported environment variable formats.

Parse errors may cause a warning message, delivered via unstructured logging.

This happens automatically when :symbol:`mongoc_structured_log_opts_new` establishes defaults.
Any subsequent programmatic modifications to the :symbol:`mongoc_structured_log_opts_t` will override the environment variable settings.
For applications that desire the opposite behavior, where environment variables may override programmatic settings, they may call ``mongoc_structured_log_opts_set_max_document_length_from_env()`` after calling :symbol:`mongoc_structured_log_opts_set_max_document_length`.
This will process the environment a second time, allowing it to override customized defaults.

Returns
-------

Returns ``true`` on success: either a valid environment setting was found, or the value is unset and ``opts`` will not be modified.
If warnings are encountered in the environment, returns ``false`` and may log additional information to the unstructured logging facility.
Note that, by design, these errors are by default non-fatal.
When :symbol:`mongoc_structured_log_opts_new` internally calls this function, it ignores the return value.

.. seealso::

| :doc:`structured_log`
3 changes: 3 additions & 0 deletions src/libmongoc/doc/mongoc_structured_log_opts_t.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Functions
mongoc_structured_log_opts_set_max_level_for_all_components
mongoc_structured_log_opts_set_max_levels_from_env
mongoc_structured_log_opts_get_max_level_for_component
mongoc_structured_log_opts_set_max_document_length
mongoc_structured_log_opts_set_max_document_length_from_env
mongoc_structured_log_opts_get_max_document_length

.. seealso::

Expand Down
12 changes: 12 additions & 0 deletions src/libmongoc/src/mongoc/mongoc-structured-log-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ typedef struct mongoc_structured_log_instance_t mongoc_structured_log_instance_t
#define MONGOC_STRUCTURED_LOG_DEFAULT_LEVEL MONGOC_STRUCTURED_LOG_LEVEL_WARNING
#define MONGOC_STRUCTURED_LOG_DEFAULT_MAX_DOCUMENT_LENGTH 1000

/**
* @brief maximum possible value of the 'maximum document length' setting, enforced when reading settings from the
* environment.
*
* Maximum document length applies to a single serialized JSON document within the log.
* The overall log document, when serialized as BSON, will be subject to BSON_MAX_SIZE.
* At a minimum we should leave room for BSON headers and for the JSON truncation marker ("...").
* Choose to leave a little more room, as it's more useful to truncate the huge document early
* rather than fail in bson_append_utf8().
*/
#define MONGOC_STRUCTURED_LOG_MAXIMUM_MAX_DOCUMENT_LENGTH ((uint32_t) BSON_MAX_SIZE - 4096u)

/**
* @brief Allocate a new instance of the structured logging system
* @param opts Options, copied into the new instance.
Expand Down
Loading