@@ -32,6 +32,7 @@ import type {
32
32
SpResolveSrvSucceededEvent ,
33
33
SpMissingOptionalDependencyEvent
34
34
} from '@mongosh/types' ;
35
+ import { inspect } from 'util' ;
35
36
36
37
interface MongoshAnalytics {
37
38
identify ( message : {
@@ -49,6 +50,22 @@ interface MongoshAnalytics {
49
50
} ) : void ;
50
51
}
51
52
53
+ // The default serializer for pino replaces circular properties it finds
54
+ // anywhere in the object graph. In extreme cases, e.g. when a mongosh cursor
55
+ // is passed to it, it follows the graph all the way to the network socket and
56
+ // replaces random properties of its internal state with the string
57
+ // '"[Circular"]':
58
+ // https://github.com/davidmarkclements/fast-safe-stringify/blob/0e011f068962e8f8974133a47afcabfc003f2183/index.js#L36
59
+ // As a workaround, we do a JSON-based clone for any input that can contain
60
+ // arbitrary values.
61
+ function jsonClone < T > ( input : T ) : T | { _inspected : string } {
62
+ try {
63
+ return JSON . parse ( JSON . stringify ( input ) ) ;
64
+ } catch ( error ) {
65
+ return { _inspected : inspect ( input ) } ;
66
+ }
67
+ }
68
+
52
69
// set up a noop, in case we are not able to connect to segment.
53
70
class NoopAnalytics implements MongoshAnalytics {
54
71
identify ( _info : any ) : void { } // eslint-disable-line @typescript-eslint/no-unused-vars
@@ -102,7 +119,7 @@ export default function setupLoggerAndTelemetry(
102
119
const connectionUri = redactCredentials ( args . uri ) ;
103
120
const { uri : _uri , ...argsWithoutUri } = args ; // eslint-disable-line @typescript-eslint/no-unused-vars
104
121
const params = { session_id : logId , userId, connectionUri, ...argsWithoutUri } ;
105
- log . info ( 'mongosh:connect' , params ) ;
122
+ log . info ( 'mongosh:connect' , jsonClone ( params ) ) ;
106
123
107
124
if ( telemetry ) {
108
125
analytics . track ( {
@@ -118,7 +135,7 @@ export default function setupLoggerAndTelemetry(
118
135
} ) ;
119
136
120
137
bus . on ( 'mongosh:driver-initialized' , function ( driverMetadata : any ) {
121
- log . info ( 'mongosh:driver-initialized' , driverMetadata ) ;
138
+ log . info ( 'mongosh:driver-initialized' , jsonClone ( driverMetadata ) ) ;
122
139
} ) ;
123
140
124
141
bus . on ( 'mongosh:new-user' , function ( id : string , enableTelemetry : boolean ) {
@@ -186,11 +203,11 @@ export default function setupLoggerAndTelemetry(
186
203
} ) ;
187
204
188
205
bus . on ( 'mongosh:setCtx' , function ( args : ApiEvent ) {
189
- log . info ( 'mongosh:setCtx' , args ) ;
206
+ log . info ( 'mongosh:setCtx' , jsonClone ( args ) ) ;
190
207
} ) ;
191
208
192
209
bus . on ( 'mongosh:api-call' , function ( args : ApiEvent ) {
193
- log . info ( 'mongosh:api-call' , redactInfo ( args ) ) ;
210
+ log . info ( 'mongosh:api-call' , redactInfo ( jsonClone ( args ) ) ) ;
194
211
} ) ;
195
212
196
213
bus . on ( 'mongosh:api-load-file' , function ( args : ScriptLoadFileEvent ) {
@@ -355,7 +372,10 @@ export default function setupLoggerAndTelemetry(
355
372
} ) ;
356
373
357
374
bus . on ( 'mongosh-sp:connect-heartbeat-failure' , function ( ev : SpConnectHeartbeatFailureEvent ) {
358
- log . info ( 'mongosh-sp:connect-heartbeat-failure' , ev ) ;
375
+ log . info ( 'mongosh-sp:connect-heartbeat-failure' , {
376
+ ...ev ,
377
+ failure : ev . failure ?. message
378
+ } ) ;
359
379
} ) ;
360
380
361
381
bus . on ( 'mongosh-sp:connect-heartbeat-succeeded' , function ( ev : SpConnectHeartbeatSucceededEvent ) {
0 commit comments