1
1
/* eslint-disable mocha/max-top-level-suites */
2
2
import { expect } from 'chai' ;
3
3
import { MongoLogWriter } from 'mongodb-log-writer' ;
4
- import { setupLoggerAndTelemetry } from './' ;
5
4
import { EventEmitter } from 'events' ;
6
5
import { MongoshInvalidInputError } from '@mongosh/errors' ;
7
6
import type { MongoshBus } from '@mongosh/types' ;
8
- import { setupMongoLogWriter , toSnakeCase } from './setup-logger-and-telemetry' ;
9
7
import type { Writable } from 'stream' ;
8
+ import { MongoshLoggingAndTelemetry } from './logging-and-telemetry' ;
10
9
11
- describe ( 'toSnakeCase' , function ( ) {
12
- const useCases = [
13
- { input : 'MongoDB REPL' , output : 'mongo_db_repl' } ,
14
- {
15
- input : 'Node.js REPL Instantiation' ,
16
- output : 'node_js_repl_instantiation' ,
17
- } ,
18
- { input : 'A' , output : 'a' } ,
19
- {
20
- input : 'OneLongThingInPascalCase' ,
21
- output : 'one_long_thing_in_pascal_case' ,
22
- } ,
23
- { input : 'Removes .Dots in Node.js' , output : 'removes_dots_in_node_js' } ,
24
- ] ;
25
-
26
- for ( const { input, output } of useCases ) {
27
- it ( `should convert ${ input } to ${ output } ` , function ( ) {
28
- expect ( toSnakeCase ( input ) ) . to . equal ( output ) ;
29
- } ) ;
30
- }
31
- } ) ;
32
-
33
- describe ( 'setupLoggerAndTelemetry' , function ( ) {
10
+ describe ( 'MongoshLoggingAndTelemetry' , function ( ) {
34
11
let logOutput : any [ ] ;
35
12
let analyticsOutput : [ 'identify' | 'track' | 'log' , any ] [ ] ;
36
13
let bus : MongoshBus ;
37
14
38
15
const userId = '53defe995fa47e6c13102d9d' ;
39
16
const logId = '5fb3c20ee1507e894e5340f3' ;
40
17
41
- setupMongoLogWriter (
42
- new MongoLogWriter ( {
43
- logId,
44
- logFilePath : `/tmp/${ logId } _log` ,
45
- target : {
46
- write ( chunk : string , cb : ( ) => void ) {
47
- logOutput . push ( JSON . parse ( chunk ) ) ;
48
- cb ( ) ;
49
- } ,
50
- end ( cb : ( ) => void ) {
51
- cb ( ) ;
52
- } ,
53
- } as Writable ,
54
- } )
55
- ) ;
18
+ const logger = new MongoLogWriter ( logId , `/tmp/${ logId } _log` , {
19
+ write ( chunk : string , cb : ( ) => void ) {
20
+ logOutput . push ( JSON . parse ( chunk ) ) ;
21
+ cb ( ) ;
22
+ } ,
23
+ end ( cb : ( ) => void ) {
24
+ cb ( ) ;
25
+ } ,
26
+ } as Writable ) ;
27
+
56
28
const analytics = {
57
29
identify ( info : any ) {
58
30
analyticsOutput . push ( [ 'identify' , info ] ) ;
@@ -71,87 +43,41 @@ describe('setupLoggerAndTelemetry', function () {
71
43
bus = new EventEmitter ( ) ;
72
44
} ) ;
73
45
74
- it ( 'accepts user ID at setup' , function ( ) {
75
- setupLoggerAndTelemetry (
46
+ it ( 'throws when trying to setup writer prematurely ' , function ( ) {
47
+ const loggingAndTelemetry = new MongoshLoggingAndTelemetry (
76
48
bus ,
77
49
analytics ,
78
- { userId } ,
79
50
{
80
51
platform : process . platform ,
81
52
arch : process . arch ,
82
53
} ,
83
54
'1.0.0'
84
55
) ;
85
- expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
86
- expect ( analyticsOutput ) . to . be . empty ;
87
-
88
- bus . emit ( 'mongosh:connect' , {
89
- uri : 'mongodb://localhost/' ,
90
- is_localhost : true ,
91
- is_atlas : false ,
92
- resolved_hostname : 'localhost' ,
93
- node_version : 'v12.19.0' ,
94
- } ) ;
95
-
96
- expect ( analyticsOutput ) . to . deep . equal ( [
97
- [
98
- 'track' ,
99
- {
100
- anonymousId : userId ,
101
- event : 'New Connection' ,
102
- properties : {
103
- mongosh_version : '1.0.0' ,
104
- session_id : logId ,
105
- is_localhost : true ,
106
- is_atlas : false ,
107
- atlas_hostname : null ,
108
- node_version : 'v12.19.0' ,
109
- } ,
110
- } ,
111
- ] ,
112
- ] ) ;
113
- } ) ;
114
56
115
- it ( 'throws if an event occurs before the user is set' , function ( ) {
116
- setupLoggerAndTelemetry (
117
- bus ,
118
- analytics ,
119
- { } ,
120
- {
121
- platform : process . platform ,
122
- arch : process . arch ,
123
- } ,
124
- '1.0.0'
57
+ expect ( ( ) => loggingAndTelemetry . setupLogger ( logger ) ) . throws (
58
+ 'Run setup() before setting up the log writer.'
125
59
) ;
126
- expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
127
- expect ( analyticsOutput ) . to . be . empty ;
128
-
129
- expect ( ( ) =>
130
- bus . emit ( 'mongosh:connect' , {
131
- uri : 'mongodb://localhost/' ,
132
- is_localhost : true ,
133
- is_atlas : false ,
134
- resolved_hostname : 'localhost' ,
135
- node_version : 'v12.19.0' ,
136
- } )
137
- ) . throws ( 'No telemetry identity found' ) ;
138
60
} ) ;
139
61
140
62
it ( 'tracks new local connection events' , function ( ) {
141
- setupLoggerAndTelemetry (
63
+ const loggingAndTelemetry = new MongoshLoggingAndTelemetry (
142
64
bus ,
143
65
analytics ,
144
- { } ,
145
66
{
146
67
platform : process . platform ,
147
68
arch : process . arch ,
148
69
} ,
149
70
'1.0.0'
150
71
) ;
72
+
73
+ loggingAndTelemetry . setup ( ) ;
74
+ loggingAndTelemetry . setupLogger ( logger ) ;
75
+
151
76
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
152
77
expect ( analyticsOutput ) . to . be . empty ;
153
78
154
79
bus . emit ( 'mongosh:new-user' , { userId, anonymousId : userId } ) ;
80
+ bus . emit ( 'mongosh:logger-initialized' ) ;
155
81
156
82
bus . emit ( 'mongosh:connect' , {
157
83
uri : 'mongodb://localhost/' ,
@@ -199,20 +125,24 @@ describe('setupLoggerAndTelemetry', function () {
199
125
} ) ;
200
126
201
127
it ( 'tracks new atlas connection events' , function ( ) {
202
- setupLoggerAndTelemetry (
128
+ const loggingAndTelemetry = new MongoshLoggingAndTelemetry (
203
129
bus ,
204
130
analytics ,
205
- { } ,
206
131
{
207
132
platform : process . platform ,
208
133
arch : process . arch ,
209
134
} ,
210
135
'1.0.0'
211
136
) ;
137
+
138
+ loggingAndTelemetry . setup ( ) ;
139
+ loggingAndTelemetry . setupLogger ( logger ) ;
140
+
212
141
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
213
142
expect ( analyticsOutput ) . to . be . empty ;
214
143
215
144
bus . emit ( 'mongosh:new-user' , { userId, anonymousId : userId } ) ;
145
+ bus . emit ( 'mongosh:logger-initialized' ) ;
216
146
217
147
bus . emit ( 'mongosh:connect' , {
218
148
uri : 'mongodb://test-data-sets-a011bb.mongodb.net/' ,
@@ -264,20 +194,25 @@ describe('setupLoggerAndTelemetry', function () {
264
194
} ) ;
265
195
266
196
it ( 'tracks a sequence of events' , function ( ) {
267
- setupLoggerAndTelemetry (
197
+ const loggingAndTelemetry = new MongoshLoggingAndTelemetry (
268
198
bus ,
269
199
analytics ,
270
- { } ,
271
200
{
272
201
platform : process . platform ,
273
202
arch : process . arch ,
274
203
} ,
275
204
'1.0.0'
276
205
) ;
206
+
207
+ loggingAndTelemetry . setup ( ) ;
208
+ loggingAndTelemetry . setupLogger ( logger ) ;
209
+
277
210
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
278
211
expect ( analyticsOutput ) . to . be . empty ;
279
212
280
213
bus . emit ( 'mongosh:new-user' , { userId, anonymousId : userId } ) ;
214
+ bus . emit ( 'mongosh:logger-initialized' ) ;
215
+
281
216
bus . emit ( 'mongosh:update-user' , { userId, anonymousId : userId } ) ;
282
217
bus . emit ( 'mongosh:start-session' , {
283
218
isInteractive : true ,
@@ -728,11 +663,25 @@ describe('setupLoggerAndTelemetry', function () {
728
663
} ) ;
729
664
730
665
it ( 'buffers deprecated API calls' , function ( ) {
731
- setupLoggerAndTelemetry ( bus , analytics , { } , { } , '1.0.0' ) ;
666
+ const loggingAndTelemetry = new MongoshLoggingAndTelemetry (
667
+ bus ,
668
+ analytics ,
669
+ {
670
+ platform : process . platform ,
671
+ arch : process . arch ,
672
+ } ,
673
+ '1.0.0'
674
+ ) ;
675
+
676
+ loggingAndTelemetry . setup ( ) ;
677
+ loggingAndTelemetry . setupLogger ( logger ) ;
678
+
732
679
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
733
680
expect ( analyticsOutput ) . to . be . empty ;
734
681
735
682
bus . emit ( 'mongosh:new-user' , { userId, anonymousId : userId } ) ;
683
+ bus . emit ( 'mongosh:logger-initialized' ) ;
684
+
736
685
bus . emit ( 'mongosh:evaluate-started' ) ;
737
686
738
687
logOutput = [ ] ;
@@ -900,7 +849,19 @@ describe('setupLoggerAndTelemetry', function () {
900
849
} ) ;
901
850
902
851
it ( 'does not track database calls outside of evaluate-{started,finished}' , function ( ) {
903
- setupLoggerAndTelemetry ( bus , analytics , { } , { } , '1.0.0' ) ;
852
+ const loggingAndTelemetry = new MongoshLoggingAndTelemetry (
853
+ bus ,
854
+ analytics ,
855
+ {
856
+ platform : process . platform ,
857
+ arch : process . arch ,
858
+ } ,
859
+ '1.0.0'
860
+ ) ;
861
+
862
+ loggingAndTelemetry . setup ( ) ;
863
+ loggingAndTelemetry . setupLogger ( logger ) ;
864
+
904
865
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
905
866
expect ( analyticsOutput ) . to . be . empty ;
906
867
0 commit comments