Skip to content

Commit ee16861

Browse files
author
mdbmes
authored
CDRIVER-4485 string append and truncation fixes for structured logging (#1826)
This PR is a follow-up for #1795, #1821, and #1816. The new internal string and json APIs are used to implement missing functionality and tests for the structured logging subsystem. * Adds public APIs for the max_document_length within a mongoc_structured_log_opts_t. * Addresses the remainder of CDRIVER-4485: JSON documents within structured log messages are truncated correctly according to the rules set out in the Logging specification. * Addresses the remainder of CDRIVER-4486 by implementing prose tests. * Addresses CDRIVER-4814. Command or payload data beyond the truncation limit no longer degrades logging performance. (New serializer for command-with-payload) * Unified tests use the suggested max document length of 10000 from the spec * Replace atomic counter with atomic flag for environment error guards * Tests for environment defaults now skip if relevant variables are set externally
1 parent 63f469f commit ee16861

13 files changed

+863
-123
lines changed

src/libmongoc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,7 @@ set (test-libmongoc-sources
10961096
${PROJECT_SOURCE_DIR}/tests/test-mongoc-collection-find-with-opts.c
10971097
${PROJECT_SOURCE_DIR}/tests/test-mongoc-collection-find.c
10981098
${PROJECT_SOURCE_DIR}/tests/test-mongoc-collection.c
1099+
${PROJECT_SOURCE_DIR}/tests/test-mongoc-command-logging-and-monitoring.c
10991100
${PROJECT_SOURCE_DIR}/tests/test-mongoc-command-monitoring.c
11001101
${PROJECT_SOURCE_DIR}/tests/test-mongoc-connection-uri.c
11011102
${PROJECT_SOURCE_DIR}/tests/test-mongoc-counters.c
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
:man_page: mongoc_structured_log_opts_get_max_document_length
2+
3+
mongoc_structured_log_opts_get_max_document_length()
4+
====================================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
size_t
12+
mongoc_structured_log_opts_get_max_document_length (const mongoc_structured_log_opts_t *opts);
13+
14+
Parameters
15+
----------
16+
17+
* ``opts``: Structured log options, allocated with :symbol:`mongoc_structured_log_opts_new`.
18+
19+
Returns
20+
-------
21+
22+
Returns the current maximum document length set in ``opts``, as a ``size_t``.
23+
24+
.. seealso::
25+
26+
| :doc:`structured_log`

src/libmongoc/doc/mongoc_structured_log_opts_new.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Environment Variables
3131
This is a full list of the captured environment variables.
3232

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
:man_page: mongoc_structured_log_opts_set_max_document_length
2+
3+
mongoc_structured_log_opts_set_max_document_length()
4+
====================================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
bool
12+
mongoc_structured_log_opts_set_max_document_length (mongoc_structured_log_opts_t *opts,
13+
size_t max_document_length);
14+
15+
Sets a maximum length for BSON documents that appear serialized in JSON form as part of a structured log message.
16+
17+
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.
18+
19+
Parameters
20+
----------
21+
22+
* ``opts``: Structured log options, allocated with :symbol:`mongoc_structured_log_opts_new`.
23+
* ``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.
24+
25+
Returns
26+
-------
27+
28+
Returns ``true`` on success, or ``false`` if the supplied maximum length is too large.
29+
30+
.. seealso::
31+
32+
| :doc:`structured_log`
33+
| :symbol:`mongoc_structured_log_opts_set_max_document_length_from_env`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
:man_page: mongoc_structured_log_opts_set_max_document_length_from_env
2+
3+
mongoc_structured_log_opts_set_max_document_length_from_env()
4+
=============================================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
bool
12+
mongoc_structured_log_opts_set_max_document_length_from_env (mongoc_structured_log_opts_t *opts);
13+
14+
Sets a maximum document length from the ``MONGODB_LOG_MAX_DOCUMENT_LENGTH`` environment variable, if a valid setting is found.
15+
See :symbol:`mongoc_structured_log_opts_new` for a description of the supported environment variable formats.
16+
17+
Parse errors may cause a warning message, delivered via unstructured logging.
18+
19+
This happens automatically when :symbol:`mongoc_structured_log_opts_new` establishes defaults.
20+
Any subsequent programmatic modifications to the :symbol:`mongoc_structured_log_opts_t` will override the environment variable settings.
21+
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`.
22+
This will process the environment a second time, allowing it to override customized defaults.
23+
24+
Returns
25+
-------
26+
27+
Returns ``true`` on success: either a valid environment setting was found, or the value is unset and ``opts`` will not be modified.
28+
If warnings are encountered in the environment, returns ``false`` and may log additional information to the unstructured logging facility.
29+
Note that, by design, these errors are by default non-fatal.
30+
When :symbol:`mongoc_structured_log_opts_new` internally calls this function, it ignores the return value.
31+
32+
.. seealso::
33+
34+
| :doc:`structured_log`

src/libmongoc/doc/mongoc_structured_log_opts_t.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Functions
2929
mongoc_structured_log_opts_set_max_level_for_all_components
3030
mongoc_structured_log_opts_set_max_levels_from_env
3131
mongoc_structured_log_opts_get_max_level_for_component
32+
mongoc_structured_log_opts_set_max_document_length
33+
mongoc_structured_log_opts_set_max_document_length_from_env
34+
mongoc_structured_log_opts_get_max_document_length
3235

3336
.. seealso::
3437

src/libmongoc/src/mongoc/mongoc-structured-log-private.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ typedef struct mongoc_structured_log_instance_t mongoc_structured_log_instance_t
3434
#define MONGOC_STRUCTURED_LOG_DEFAULT_LEVEL MONGOC_STRUCTURED_LOG_LEVEL_WARNING
3535
#define MONGOC_STRUCTURED_LOG_DEFAULT_MAX_DOCUMENT_LENGTH 1000
3636

37+
/**
38+
* @brief maximum possible value of the 'maximum document length' setting, enforced when reading settings from the
39+
* environment.
40+
*
41+
* Maximum document length applies to a single serialized JSON document within the log.
42+
* The overall log document, when serialized as BSON, will be subject to BSON_MAX_SIZE.
43+
* At a minimum we should leave room for BSON headers and for the JSON truncation marker ("...").
44+
* Choose to leave a little more room, as it's more useful to truncate the huge document early
45+
* rather than fail in bson_append_utf8().
46+
*/
47+
#define MONGOC_STRUCTURED_LOG_MAXIMUM_MAX_DOCUMENT_LENGTH ((uint32_t) BSON_MAX_SIZE - 4096u)
48+
3749
/**
3850
* @brief Allocate a new instance of the structured logging system
3951
* @param opts Options, copied into the new instance.

0 commit comments

Comments
 (0)