Skip to content

Commit e9b4c96

Browse files
authored
DRIVERS-2516 Collection of follow-up logging changes (#1356)
1 parent 44d25db commit e9b4c96

File tree

2 files changed

+87
-16
lines changed

2 files changed

+87
-16
lines changed

source/command-logging-and-monitoring/command-logging-and-monitoring.rst

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ The following key-value pairs MUST be included in all command messages:
414414

415415
* - serverPort
416416
- Int
417-
- The port for the server the command is being run on. Optional; only present if a port was specified.
417+
- The port for the server the command is being run on. Optional; not present for Unix domain sockets. When
418+
the user does not specify a port and the default (27017) is used, the driver SHOULD include it here.
418419

419420
* - serverConnectionId
420421
- Int
@@ -452,8 +453,8 @@ In addition to the common fields, command started messages MUST contain the foll
452453

453454
The unstructured form SHOULD be as follows, using the values defined in the structured format above to fill in placeholders as appropriate:
454455

455-
Command "{{commandName}}" started on database "{{databaseName}}" using a connection with driver ID {{driverConnectionId}} and server ID
456-
{{serverConnectionId}} to a server with hostname {{serverHost}} on port {{serverPort}} with service ID {{serviceId}}. The requestID is
456+
Command "{{commandName}}" started on database "{{databaseName}}" using a connection with driver-generated ID {{driverConnectionId}}
457+
and server-generated ID {{serverConnectionId}} to {{serverHost}}:{{serverPort}} with service ID {{serviceId}}. The requestID is
457458
{{requestId}} and the operation ID is {{operationId}}. Command: {{command}}
458459

459460
Command Succeeded Message
@@ -485,9 +486,9 @@ In addition to the common fields, command succeeded messages MUST contain the fo
485486

486487
The unstructured form SHOULD be as follows, using the values defined in the structured format above to fill in placeholders as appropriate:
487488

488-
Command "{{commandName}}" succeeded in {{durationMS}} ms using a connection with driver ID {{driverConnectionId}} and server ID {{serverConnectionId}}
489-
to a host with hostname {{serverHost}} on port {{serverPort}} with service ID {{serviceId}}. The requestID is {{requestId}} and the operation ID is
490-
{{operationId}}. Command reply: {{command}}
489+
Command "{{commandName}}" succeeded in {{durationMS}} ms using a connection with driver-generated ID {{driverConnectionId}} and
490+
server-generated ID {{serverConnectionId}} to {{serverHost}}:{{serverPort}} with service ID {{serviceId}}. The requestID is
491+
{{requestId}} and the operation ID is {{operationId}}. Command reply: {{command}}
491492

492493
Command Failed Message
493494
----------------------
@@ -518,9 +519,9 @@ In addition to the common fields, command failed messages MUST contain the follo
518519

519520
The unstructured form SHOULD be as follows, using the values defined in the structured format above to fill in placeholders as appropriate:
520521

521-
Command "{{commandName}}" failed in {{durationMS}} ms using a connection with driver ID {{driverConnectionId}} and server ID {{serverConnectionId}}
522-
to a host with hostname {{serverHost}} on port {{serverPort}} with service ID {{serviceId}}. The requestID is {{requestId}} and the operation ID
523-
is {{operationId}}. Error: {{error}}
522+
Command "{{commandName}}" failed in {{durationMS}} ms using a connection with driver-generated ID {{driverConnectionId}} and
523+
server-generated ID {{serverConnectionId}} to {{serverHost}}:{{serverPort}} with service ID {{serviceId}}. The requestID is
524+
{{requestId}} and the operation ID is {{operationId}}. Error: {{error}}
524525

525526
-------
526527
Testing
@@ -556,3 +557,6 @@ Changelog
556557
:2022-10-05: Remove spec front matter and reformat changelog.
557558
:2022-10-11: Add command logging information and tests.
558559
:2022-11-16: Update sensitive command tests to only run on server versions where the commands are supported.
560+
:2022-12-13: Updated log message ``serverPort`` field description to clarify drivers should populate it with the
561+
default port 27017 when relevant. Updated suggested unstructured forms of log messages to more
562+
clearly label connection IDs and use more readable server address representations.

source/logging/logging.rst

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,31 @@ on a per-component level, for each component defined below in the `Components`_
9191

9292
The default is to not log anything.
9393

94-
If a variable is set to an invalid value, it MUST be treated as if it were not specified at all.
94+
If a variable is set to an invalid value, it MUST be treated as if it were not specified at all,
95+
and the driver MAY attempt to warn the user about the misconfiguration via a log message or otherwise
96+
but MUST NOT throw an exception.
9597

9698
Configurable Log Destination
9799
----------------------------
98-
Drivers MUST support configuring where log messages should be output, including the options: stdout,
99-
stderr, output file (path MUST be configurable).
100+
Drivers MUST support configuring where log messages should be output, including the options:
101+
102+
* stdout
103+
* stderr
104+
* Output file (path MUST be configurable). For languages that are not relying on a logging interface or framework to handle
105+
file support, the driver can choose to either support this directly (i.e. the driver allows the user to specify
106+
a path and itself handles writing to that path), or to instead provide a straightforward, idiomatic way to programmatically
107+
consume the messages and in turn write them to a file, e.g. via a Node.js `stream <https://nodejs.org/api/stream.html>`__, along with a
108+
documentation example of how to do this.
100109

101110
**Fallback implementation method**: If the environment variable ``MONGODB_LOG_PATH`` is provided:
102111
- If the value is "stdout" (case-insensitive), log to stdout.
103112
- If the value is "stderr" (case-insensitive), log to stderr.
104-
- Else, log to a file at the specified path. If the file already exists, it MUST be appended to.
113+
- Else, if direct logging to files is supported, log to a file at the specified path. If the file already exists, it MUST be appended to.
105114

106-
If the variable is not provided, the driver MUST log to stderr.
115+
If the variable is not provided or is set to an invalid value (which could be invalid for any
116+
reason, e.g. the path does not exist or is not writeable), the driver MUST log to stderr and
117+
the driver MAY attempt to warn the user about the misconfiguration via a log message or otherwise
118+
but MUST NOT throw an exception.
107119

108120
Configurable Max Document Length
109121
--------------------------------
@@ -125,8 +137,9 @@ truncation occurred. The ellipsis MUST NOT count toward the max length.
125137
default max length MUST be truncated to that length.
126138
When set to an integer value, any extended JSON document longer than that value MUST be
127139
truncated to that length.
128-
If the variable is set to an invalid value, it MUST be treated as if it were not specified at
129-
all.
140+
If a variable is set to an invalid value, it MUST be treated as if it were not specified at all,
141+
and the driver MAY attempt to warn the user about the misconfiguration via a log message or otherwise
142+
but MUST NOT throw an exception.
130143

131144
Components
132145
----------
@@ -315,6 +328,14 @@ Including Timestamps in Log Messages
315328
------------------------------------
316329
Drivers MAY add timestamps to their log messages if one will not be added automatically by the logging framework(s) they use.
317330

331+
Supporting Both Programmatic and Environment Variable Configuration
332+
-------------------------------------------------------------------
333+
If a driver supports configuration via both environment variables and programmatically via API,
334+
programmatic configuration MUST take precedence over environment variables.
335+
Drivers supporting both forms of configuration MUST document this behavior and MUST provide an
336+
example of how users can implement custom logic to allow an environment variable to override
337+
a programmatic default, so that users who prefer the opposite behavior have a way to achieve it.
338+
318339
Test Plan
319340
=========
320341
Tests for logging behavior are defined in each corresponding specification. The `unified test
@@ -392,6 +413,47 @@ Additionally, always including null values would increase the size of log messag
392413
null values is left to the drivers' discretion for any driver-specific logs not covered by common
393414
specification components.
394415

416+
---------------------------------------
417+
Invalid Values of Environment Variables
418+
---------------------------------------
419+
For drivers supporting configuration via environment variables, the spec requires that if an environment
420+
variable is set to an invalid value the driver behaves as if the value were not specified at all, and
421+
optionally warns the user but does not throw an error. We considered the following alternatives:
422+
423+
1. Drivers could be required to throw an exception if a value is invalid: This was rejected because of concerns
424+
around the implications for environments/applications where multiple versions of the driver or multiple
425+
drivers may be present and where the validation logic may not match, meaning a value considered valid for
426+
one driver/version might not be by another. Additionally, there is no obvious place to throw an exception
427+
from about invalid environment variables; ``MongoClient`` constructors would be one possibility, but not all
428+
languages will support per-client configuration so throwing there regarding an environment variable might be
429+
surprising to users.
430+
431+
Note that these same concerns do not apply to logging options that are specified via driver API: there is no
432+
risk of such options propagating to other drivers/driver versions present, and drivers can report exceptions at
433+
the point the options are specified, either globally or per-client. Therefore, drivers MUST validate
434+
programmatic logging options in a manner consistent with how they validate all other programmatic
435+
options, and if possible SHOULD prefer to throw exceptions for invalid configuration.
436+
437+
1. Drivers could be required to log a warning if a value is invalid: While drivers MAY do this, requiring
438+
it was rejected because depending on the language/framework log messages may not be a viable way to
439+
communicate a warning: if a language's default behavior is to log nothing, or only log messages at a
440+
more severe level than ``warn``, the user will not actually receive the message unless it is logged at a
441+
level and component they have successfully enabled.
442+
443+
--------------------------------------------
444+
Programmatic Configuration Taking Precedence
445+
--------------------------------------------
446+
We chose to have programmatic configuration win out over environment variables because:
447+
448+
1. This allows applications built atop drivers (e.g. mongosh) to fully control the driver's logging behavior
449+
by setting options for it programmatically.
450+
2. This is consistent with how many drivers treat options specified both in a connection string and programmatically:
451+
programmatic options win out.
452+
3. It is straightforward for users to override this behavior (by writing logic to read in environment variables and override
453+
programmatic defaults), but if we went with the opposite default, it would be more complicated for users to override:
454+
not all languages will necessarily have an easy way to override/unset an environment variable from within
455+
application code.
456+
395457
Backwards Compatibility
396458
=======================
397459
This specification takes the stance that the contents of log messages (both structured and
@@ -444,3 +506,8 @@ Changelog
444506
:2022-10-26: Allow drivers to add timestamps to log messages.
445507
:2022-11-10: Clarify driver-specific null omission.
446508
:2022-12-29: Fix typo in trace log level example
509+
:2023-01-04: Elaborate on treatment of invalid values of environment variables.
510+
Permit drivers to omit direct support for logging to file so long as they provide
511+
a straightforward way for users to consume the log messages programmatically and
512+
write to a file themselves.
513+
Require that programmatic configuration take precedence over environment variables.

0 commit comments

Comments
 (0)