@@ -66,8 +66,11 @@ describe('mongoose instrumentation', () => {
66
66
beforeEach ( async ( ) => {
67
67
instrumentation . disable ( ) ;
68
68
instrumentation . setConfig ( {
69
- dbStatementSerializer : ( _operation : string , payload ) =>
70
- JSON . stringify ( payload ) ,
69
+ dbStatementSerializer : ( _operation : string , payload ) => {
70
+ return JSON . stringify ( payload , ( key , value ) => {
71
+ return key === 'session' ? '[Session]' : value ;
72
+ } ) ;
73
+ } ,
71
74
} ) ;
72
75
instrumentation . enable ( ) ;
73
76
await loadUsers ( ) ;
@@ -97,23 +100,90 @@ describe('mongoose instrumentation', () => {
97
100
expect ( statement . document ) . toEqual ( expect . objectContaining ( document ) ) ;
98
101
} ) ;
99
102
100
- it ( 'instrumenting save operation with callback' , done => {
101
- const document = {
102
- firstName : 'Test first name' ,
103
- lastName : 'Test last name' ,
104
-
105
- } ;
106
- const user : IUser = new User ( document ) ;
103
+ describe ( 'when save call does not have callback' , async ( ) => {
104
+ it ( 'instrumenting save operation with option property set' , async ( ) => {
105
+ const document = {
106
+ firstName : 'Test first name' ,
107
+ lastName : 'Test last name' ,
108
+
109
+ } ;
110
+ const user : IUser = new User ( document ) ;
111
+ await user . save ( { wtimeout : 42 } ) ;
107
112
108
- user . save ( ( ) => {
109
113
const spans = getTestSpans ( ) ;
110
-
111
114
expect ( spans . length ) . toBe ( 1 ) ;
112
115
assertSpan ( spans [ 0 ] as ReadableSpan ) ;
113
116
expect ( spans [ 0 ] . attributes [ SEMATTRS_DB_OPERATION ] ) . toBe ( 'save' ) ;
114
117
const statement = getStatement ( spans [ 0 ] as ReadableSpan ) ;
115
118
expect ( statement . document ) . toEqual ( expect . objectContaining ( document ) ) ;
116
- done ( ) ;
119
+ expect ( statement . options . wtimeout ) . toEqual ( 42 ) ;
120
+
121
+ const createdUser = await User . findById ( user . _id ) . lean ( ) ;
122
+ expect ( createdUser ?. _id . toString ( ) ) . toEqual ( user . _id . toString ( ) ) ;
123
+ } ) ;
124
+ } ) ;
125
+
126
+ describe ( 'when save call has callback' , async ( ) => {
127
+ it ( 'instrumenting save operation with promise and option property set' , done => {
128
+ const document = {
129
+ firstName : 'Test first name' ,
130
+ lastName : 'Test last name' ,
131
+
132
+ } ;
133
+ const user : IUser = new User ( document ) ;
134
+ user . save ( { wtimeout : 42 } , async ( ) => {
135
+ const spans = getTestSpans ( ) ;
136
+ expect ( spans . length ) . toBe ( 1 ) ;
137
+ assertSpan ( spans [ 0 ] as ReadableSpan ) ;
138
+ expect ( spans [ 0 ] . attributes [ SEMATTRS_DB_OPERATION ] ) . toBe ( 'save' ) ;
139
+ const statement = getStatement ( spans [ 0 ] as ReadableSpan ) ;
140
+ expect ( statement . document ) . toEqual ( expect . objectContaining ( document ) ) ;
141
+ expect ( statement . options . wtimeout ) . toEqual ( 42 ) ;
142
+
143
+ const createdUser = await User . findById ( user . _id ) . lean ( ) ;
144
+ expect ( createdUser ?. _id . toString ( ) ) . toEqual ( user . _id . toString ( ) ) ;
145
+ done ( ) ;
146
+ } ) ;
147
+ } ) ;
148
+
149
+ it ( 'instrumenting save operation with generic options and callback' , done => {
150
+ const document = {
151
+ firstName : 'Test first name' ,
152
+ lastName : 'Test last name' ,
153
+
154
+ } ;
155
+ const user : IUser = new User ( document ) ;
156
+
157
+ user . save ( { } , ( ) => {
158
+ const spans = getTestSpans ( ) ;
159
+
160
+ expect ( spans . length ) . toBe ( 1 ) ;
161
+ assertSpan ( spans [ 0 ] as ReadableSpan ) ;
162
+ expect ( spans [ 0 ] . attributes [ SEMATTRS_DB_OPERATION ] ) . toBe ( 'save' ) ;
163
+ const statement = getStatement ( spans [ 0 ] as ReadableSpan ) ;
164
+ expect ( statement . document ) . toEqual ( expect . objectContaining ( document ) ) ;
165
+ done ( ) ;
166
+ } ) ;
167
+ } ) ;
168
+
169
+ it ( 'instrumenting save operation with only callback' , done => {
170
+ const document = {
171
+ firstName : 'Test first name' ,
172
+ lastName : 'Test last name' ,
173
+
174
+ } ;
175
+ const user : IUser = new User ( document ) ;
176
+
177
+ user . save ( ( ) => {
178
+ const spans = getTestSpans ( ) ;
179
+
180
+ expect ( spans . length ) . toBe ( 1 ) ;
181
+ assertSpan ( spans [ 0 ] as ReadableSpan ) ;
182
+ expect ( spans [ 0 ] . attributes [ SEMATTRS_DB_OPERATION ] ) . toBe ( 'save' ) ;
183
+ const statement = getStatement ( spans [ 0 ] as ReadableSpan ) ;
184
+ expect ( statement . document ) . toEqual ( expect . objectContaining ( document ) ) ;
185
+ done ( ) ;
186
+ } ) ;
117
187
} ) ;
118
188
} ) ;
119
189
0 commit comments