Skip to content

Commit db01e74

Browse files
committed
add debugId to connections
1 parent 9eac0d5 commit db01e74

File tree

10 files changed

+72
-40
lines changed

10 files changed

+72
-40
lines changed

packages/firestore/src/core/component_provider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ import { OnlineStateSource } from './types';
8080
type Kind = 'memory' | 'persistent';
8181

8282
export interface ComponentConfiguration {
83+
readonly debugId: string;
84+
8385
asyncQueue: AsyncQueue;
8486
databaseInfo: DatabaseInfo;
8587
authCredentials: CredentialsProvider<User>;
@@ -486,6 +488,7 @@ export class OnlineComponentProvider {
486488
createDatastore(cfg: ComponentConfiguration): Datastore {
487489
const serializer = newSerializer(cfg.databaseInfo.databaseId);
488490
const connection = newConnection(cfg.databaseInfo);
491+
logDebug(`${connection.debugId} created for ${cfg.debugId}`);
489492
return newDatastore(
490493
cfg.authCredentials,
491494
cfg.appCheckCredentials,

packages/firestore/src/core/firestore_client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export class FirestoreClient {
162162

163163
get configuration(): ComponentConfiguration {
164164
return {
165+
debugId: this.debugId,
165166
asyncQueue: this.asyncQueue,
166167
databaseInfo: this.databaseInfo,
167168
clientId: this.clientId,

packages/firestore/src/platform/browser/webchannel_connection.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ import { logDebug, logWarn } from '../../util/log';
4747
import { Rejecter, Resolver } from '../../util/promise';
4848
import { StringMap } from '../../util/types';
4949

50-
const LOG_TAG = 'WebChannelConnection';
51-
5250
const RPC_STREAM_SERVICE = 'google.firestore.v1.Firestore';
5351

5452
const XHR_TIMEOUT_SECS = 15;
5553

5654
export class WebChannelConnection extends RestConnection {
55+
readonly debugId = `WebChannelConnection@${generateUniqueDebugId()}`;
56+
5757
private readonly forceLongPolling: boolean;
5858
private readonly autoDetectLongPolling: boolean;
5959
private readonly useFetchStreams: boolean;
@@ -77,7 +77,7 @@ export class WebChannelConnection extends RestConnection {
7777
body: Req,
7878
_forwardCredentials: boolean
7979
): Promise<Resp> {
80-
const streamId = generateUniqueDebugId();
80+
const streamId = `stream@${generateUniqueDebugId()}`;
8181
return new Promise((resolve: Resolver<Resp>, reject: Rejecter) => {
8282
const xhr = new XhrIo();
8383
xhr.setWithCredentials(true);
@@ -87,22 +87,22 @@ export class WebChannelConnection extends RestConnection {
8787
case ErrorCode.NO_ERROR:
8888
const json = xhr.getResponseJson() as Resp;
8989
logDebug(
90-
LOG_TAG,
90+
this.debugId,
9191
`XHR for RPC '${rpcName}' ${streamId} received:`,
9292
JSON.stringify(json)
9393
);
9494
resolve(json);
9595
break;
9696
case ErrorCode.TIMEOUT:
97-
logDebug(LOG_TAG, `RPC '${rpcName}' ${streamId} timed out`);
97+
logDebug(this.debugId, `RPC '${rpcName}' ${streamId} timed out`);
9898
reject(
9999
new FirestoreError(Code.DEADLINE_EXCEEDED, 'Request time out')
100100
);
101101
break;
102102
case ErrorCode.HTTP_ERROR:
103103
const status = xhr.getStatus();
104104
logDebug(
105-
LOG_TAG,
105+
this.debugId,
106106
`RPC '${rpcName}' ${streamId} failed with status:`,
107107
status,
108108
'response text:',
@@ -157,12 +157,16 @@ export class WebChannelConnection extends RestConnection {
157157
);
158158
}
159159
} finally {
160-
logDebug(LOG_TAG, `RPC '${rpcName}' ${streamId} completed.`);
160+
logDebug(this.debugId, `RPC '${rpcName}' ${streamId} completed.`);
161161
}
162162
});
163163

164164
const requestString = JSON.stringify(body);
165-
logDebug(LOG_TAG, `RPC '${rpcName}' ${streamId} sending request:`, body);
165+
logDebug(
166+
this.debugId,
167+
`RPC '${rpcName}' ${streamId} sending request:`,
168+
body
169+
);
166170
xhr.send(url, 'POST', requestString, headers, XHR_TIMEOUT_SECS);
167171
});
168172
}
@@ -237,7 +241,7 @@ export class WebChannelConnection extends RestConnection {
237241

238242
const url = urlParts.join('');
239243
logDebug(
240-
LOG_TAG,
244+
this.debugId,
241245
`Creating RPC '${rpcName}' stream ${streamId}: ${url}`,
242246
request
243247
);
@@ -261,21 +265,21 @@ export class WebChannelConnection extends RestConnection {
261265
if (!closed) {
262266
if (!opened) {
263267
logDebug(
264-
LOG_TAG,
268+
this.debugId,
265269
`Opening RPC '${rpcName}' stream ${streamId} transport.`
266270
);
267271
channel.open();
268272
opened = true;
269273
}
270274
logDebug(
271-
LOG_TAG,
275+
this.debugId,
272276
`RPC '${rpcName}' stream ${streamId} sending:`,
273277
msg
274278
);
275279
channel.send(msg);
276280
} else {
277281
logDebug(
278-
LOG_TAG,
282+
this.debugId,
279283
`Not sending because RPC '${rpcName}' stream ${streamId} ` +
280284
'is closed:',
281285
msg
@@ -310,7 +314,7 @@ export class WebChannelConnection extends RestConnection {
310314
unguardedEventListen(channel, WebChannel.EventType.OPEN, () => {
311315
if (!closed) {
312316
logDebug(
313-
LOG_TAG,
317+
this.debugId,
314318
`RPC '${rpcName}' stream ${streamId} transport opened.`
315319
);
316320
streamBridge.callOnConnected();
@@ -321,7 +325,7 @@ export class WebChannelConnection extends RestConnection {
321325
if (!closed) {
322326
closed = true;
323327
logDebug(
324-
LOG_TAG,
328+
this.debugId,
325329
`RPC '${rpcName}' stream ${streamId} transport closed`
326330
);
327331
streamBridge.callOnClose();
@@ -333,7 +337,7 @@ export class WebChannelConnection extends RestConnection {
333337
if (!closed) {
334338
closed = true;
335339
logWarn(
336-
LOG_TAG,
340+
this.debugId,
337341
`RPC '${rpcName}' stream ${streamId} transport errored. Name:`,
338342
err.name,
339343
'Message:',
@@ -377,7 +381,7 @@ export class WebChannelConnection extends RestConnection {
377381
(msgDataOrError as WebChannelError[])[0]?.error;
378382
if (error) {
379383
logDebug(
380-
LOG_TAG,
384+
this.debugId,
381385
`RPC '${rpcName}' stream ${streamId} received error:`,
382386
error
383387
);
@@ -399,7 +403,7 @@ export class WebChannelConnection extends RestConnection {
399403
channel.close();
400404
} else {
401405
logDebug(
402-
LOG_TAG,
406+
this.debugId,
403407
`RPC '${rpcName}' stream ${streamId} received:`,
404408
msgData
405409
);
@@ -412,12 +416,12 @@ export class WebChannelConnection extends RestConnection {
412416
unguardedEventListen<StatEvent>(requestStats, Event.STAT_EVENT, event => {
413417
if (event.stat === Stat.PROXY) {
414418
logDebug(
415-
LOG_TAG,
419+
this.debugId,
416420
`RPC '${rpcName}' stream ${streamId} detected buffering proxy`
417421
);
418422
} else if (event.stat === Stat.NOPROXY) {
419423
logDebug(
420-
LOG_TAG,
424+
this.debugId,
421425
`RPC '${rpcName}' stream ${streamId} detected no buffering proxy`
422426
);
423427
}

packages/firestore/src/platform/browser_lite/fetch_connection.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Token } from '../../api/credentials';
1919
import { Stream } from '../../remote/connection';
2020
import { RestConnection } from '../../remote/rest_connection';
2121
import { mapCodeFromHttpStatus } from '../../remote/rpc_error';
22+
import { generateUniqueDebugId } from '../../util/debug_uid';
2223
import { FirestoreError } from '../../util/error';
2324
import { StringMap } from '../../util/types';
2425

@@ -27,6 +28,8 @@ import { StringMap } from '../../util/types';
2728
* (e.g. `fetch` or a polyfill).
2829
*/
2930
export class FetchConnection extends RestConnection {
31+
readonly debugId = `FetchConnection@${generateUniqueDebugId()}`;
32+
3033
openStream<Req, Resp>(
3134
rpcName: string,
3235
token: Token | null

packages/firestore/src/platform/node/grpc_connection.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { Deferred } from '../../util/promise';
3737
// when there's a cleaner way to dynamic require JSON in both Node ESM and CJS
3838
const grpcVersion = '__GRPC_VERSION__';
3939

40-
const LOG_TAG = 'GrpcConnection';
4140
const X_GOOG_API_CLIENT_VALUE = `gl-node/${process.versions.node} fire/${SDK_VERSION} grpc/${grpcVersion}`;
4241

4342
function createMetadata(
@@ -81,6 +80,8 @@ type GeneratedGrpcStub = any;
8180
* A Connection implemented by GRPC-Node.
8281
*/
8382
export class GrpcConnection implements Connection {
83+
readonly debugId = `GrpcConnection@${generateUniqueDebugId()}`;
84+
8485
private readonly databasePath: string;
8586
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8687
private readonly firestore: any;
@@ -102,7 +103,7 @@ export class GrpcConnection implements Connection {
102103

103104
private ensureActiveStub(): GeneratedGrpcStub {
104105
if (!this.cachedStub) {
105-
logDebug(LOG_TAG, 'Creating Firestore stub.');
106+
logDebug(this.debugId, 'Creating Firestore stub.');
106107
const credentials = this.databaseInfo.ssl
107108
? grpc.credentials.createSsl()
108109
: grpc.credentials.createInsecure();
@@ -133,7 +134,7 @@ export class GrpcConnection implements Connection {
133134

134135
return nodePromise((callback: NodeCallback<Resp>) => {
135136
logDebug(
136-
LOG_TAG,
137+
this.debugId,
137138
`RPC '${rpcName}' ${streamId} invoked with request:`,
138139
request
139140
);
@@ -143,7 +144,7 @@ export class GrpcConnection implements Connection {
143144
(grpcError?: grpc.ServiceError, value?: Resp) => {
144145
if (grpcError) {
145146
logDebug(
146-
LOG_TAG,
147+
this.debugId,
147148
`RPC '${rpcName}' ${streamId} failed with error:`,
148149
grpcError
149150
);
@@ -155,7 +156,7 @@ export class GrpcConnection implements Connection {
155156
);
156157
} else {
157158
logDebug(
158-
LOG_TAG,
159+
this.debugId,
159160
`RPC '${rpcName}' ${streamId} completed with response:`,
160161
value
161162
);
@@ -178,7 +179,7 @@ export class GrpcConnection implements Connection {
178179
const results: Resp[] = [];
179180
const responseDeferred = new Deferred<Resp[]>();
180181
logDebug(
181-
LOG_TAG,
182+
this.debugId,
182183
`RPC '${rpcName}' ${streamId} invoked (streaming) with request:`,
183184
request
184185
);
@@ -194,7 +195,7 @@ export class GrpcConnection implements Connection {
194195
let callbackFired = false;
195196
stream.on('data', (response: Resp) => {
196197
logDebug(
197-
LOG_TAG,
198+
this.debugId,
198199
`RPC ${rpcName} ${streamId} received result:`,
199200
response
200201
);
@@ -208,15 +209,15 @@ export class GrpcConnection implements Connection {
208209
}
209210
});
210211
stream.on('end', () => {
211-
logDebug(LOG_TAG, `RPC '${rpcName}' ${streamId} completed.`);
212+
logDebug(this.debugId, `RPC '${rpcName}' ${streamId} completed.`);
212213
if (!callbackFired) {
213214
callbackFired = true;
214215
responseDeferred.resolve(results);
215216
}
216217
});
217218
stream.on('error', (grpcError: grpc.ServiceError) => {
218219
logDebug(
219-
LOG_TAG,
220+
this.debugId,
220221
`RPC '${rpcName}' ${streamId} failed with error:`,
221222
grpcError
222223
);
@@ -256,7 +257,7 @@ export class GrpcConnection implements Connection {
256257
sendFn: (msg: Req) => {
257258
if (!closed) {
258259
logDebug(
259-
LOG_TAG,
260+
this.debugId,
260261
`RPC '${rpcName}' stream ${streamId} sending:`,
261262
msg
262263
);
@@ -271,7 +272,7 @@ export class GrpcConnection implements Connection {
271272
}
272273
} else {
273274
logDebug(
274-
LOG_TAG,
275+
this.debugId,
275276
`RPC '${rpcName}' stream ${streamId} ` +
276277
'not sending because gRPC stream is closed:',
277278
msg
@@ -280,7 +281,7 @@ export class GrpcConnection implements Connection {
280281
},
281282
closeFn: () => {
282283
logDebug(
283-
LOG_TAG,
284+
this.debugId,
284285
`RPC '${rpcName}' stream ${streamId} closed locally via close().`
285286
);
286287
close();
@@ -290,7 +291,11 @@ export class GrpcConnection implements Connection {
290291
let onConnectedSent = false;
291292
grpcStream.on('data', (msg: Resp) => {
292293
if (!closed) {
293-
logDebug(LOG_TAG, `RPC '${rpcName}' stream ${streamId} received:`, msg);
294+
logDebug(
295+
this.debugId,
296+
`RPC '${rpcName}' stream ${streamId} received:`,
297+
msg
298+
);
294299
// Emulate the "onConnected" event that WebChannelConnection sends.
295300
if (!onConnectedSent) {
296301
stream.callOnConnected();
@@ -301,14 +306,14 @@ export class GrpcConnection implements Connection {
301306
});
302307

303308
grpcStream.on('end', () => {
304-
logDebug(LOG_TAG, `RPC '${rpcName}' stream ${streamId} ended.`);
309+
logDebug(this.debugId, `RPC '${rpcName}' stream ${streamId} ended.`);
305310
close();
306311
});
307312

308313
grpcStream.on('error', (grpcError: grpc.ServiceError) => {
309314
if (!closed) {
310315
logWarn(
311-
LOG_TAG,
316+
this.debugId,
312317
`RPC '${rpcName}' stream ${streamId} error. Code:`,
313318
grpcError.code,
314319
'Message:',
@@ -320,7 +325,7 @@ export class GrpcConnection implements Connection {
320325
});
321326

322327
logDebug(
323-
LOG_TAG,
328+
this.debugId,
324329
`Opening RPC '${rpcName}' stream ${streamId} ` +
325330
`to ${this.databaseInfo.host}`
326331
);

packages/firestore/src/remote/connection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import { FirestoreError } from '../util/error';
3434
* creating the equivalent protocol buffers for GRPC.
3535
*/
3636
export interface Connection {
37+
readonly debugId: string;
38+
3739
/**
3840
* Invokes an RPC by name, given a request message as a JavaScript object
3941
* representing the JSON to send.

0 commit comments

Comments
 (0)