Skip to content

Commit 95606c5

Browse files
boonefbfacebook-github-bot
authored andcommitted
Ensure network requests are always logged
Reviewed By: jstejada Differential Revision: D23540723 fbshipit-source-id: a32faa8bfbc70bbd2814225923d014e5cdff83af
1 parent 02686ef commit 95606c5

File tree

4 files changed

+156
-56
lines changed

4 files changed

+156
-56
lines changed

packages/relay-experimental/__tests__/useLazyLoadQueryNode-test.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -611,15 +611,27 @@ describe('useLazyLoadQueryNode', () => {
611611
name: 'execute.start',
612612
transactionID: 100000,
613613
},
614+
{
615+
name: 'network.start',
616+
transactionID: 100001,
617+
},
614618
{
615619
name: 'queryresource.fetch',
616620
resourceID: 200000,
617621
profilerContext: expect.objectContaining({}),
618622
},
623+
{
624+
name: 'network.next',
625+
transactionID: 100001,
626+
},
619627
{
620628
name: 'execute.next',
621629
transactionID: 100000,
622630
},
631+
{
632+
name: 'network.complete',
633+
transactionID: 100001,
634+
},
623635
{
624636
name: 'execute.complete',
625637
transactionID: 100000,
@@ -709,6 +721,11 @@ describe('useLazyLoadQueryNode', () => {
709721
transactionID: 100000,
710722
variables: variablesOne,
711723
},
724+
{
725+
name: 'network.start',
726+
transactionID: 100001,
727+
variables: variablesOne,
728+
},
712729
{
713730
// fetch event for variables one
714731
name: 'queryresource.fetch',
@@ -724,7 +741,12 @@ describe('useLazyLoadQueryNode', () => {
724741
{
725742
// request for variables two starts
726743
name: 'execute.start',
727-
transactionID: 100001,
744+
transactionID: 100002,
745+
variables: variablesTwo,
746+
},
747+
{
748+
name: 'network.start',
749+
transactionID: 100003,
728750
variables: variablesTwo,
729751
},
730752
{
@@ -741,10 +763,18 @@ describe('useLazyLoadQueryNode', () => {
741763
},
742764
// fetch event for variables one is skipped
743765
// since it's already cached and reused
766+
{
767+
name: 'network.next',
768+
transactionID: 100001,
769+
},
744770
{
745771
name: 'execute.next',
746772
transactionID: 100000,
747773
},
774+
{
775+
name: 'network.complete',
776+
transactionID: 100001,
777+
},
748778
{
749779
name: 'execute.complete',
750780
transactionID: 100000,

packages/relay-experimental/preloadQuery_DEPRECATED.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,7 @@ function preloadQueryDeduped<TQuery: OperationType>(
188188
}
189189
} else if (prevQueryEntry == null || prevQueryEntry.kind !== 'network') {
190190
// Should fetch but we're not already fetching: fetch!
191-
const [logObserver, logRequestInfo] = environment.__createLogObserver(
192-
params,
193-
variables,
194-
);
195-
const source = network.execute(
196-
params,
197-
variables,
198-
networkCacheConfig,
199-
null,
200-
logRequestInfo,
201-
);
191+
const source = network.execute(params, variables, networkCacheConfig, null);
202192
const subject = new ReplaySubject();
203193
nextQueryEntry = {
204194
cacheKey,
@@ -226,7 +216,6 @@ function preloadQueryDeduped<TQuery: OperationType>(
226216
}
227217
}, DEFAULT_PREFETCH_TIMEOUT);
228218
})
229-
.do(logObserver)
230219
.subscribe({
231220
complete: () => {
232221
subject.complete();

packages/relay-runtime/store/RelayModernEnvironment.js

+95-38
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class RelayModernEnvironment implements IEnvironment {
147147
: 'full';
148148
this._operationLoader = operationLoader;
149149
this._operationExecutions = new Map();
150-
this._network = config.network;
150+
this._network = this.__wrapNetworkWithLogObserver(config.network);
151151
this._getDataID = config.UNSTABLE_DO_NOT_USE_getDataID ?? defaultGetDataID;
152152
this._publishQueue = new RelayPublishQueue(
153153
config.store,
@@ -159,7 +159,8 @@ class RelayModernEnvironment implements IEnvironment {
159159
this.options = config.options;
160160
this._isServer = config.isServer ?? false;
161161

162-
(this: any).__setNet = newNet => (this._network = newNet);
162+
(this: any).__setNet = newNet =>
163+
(this._network = this.__wrapNetworkWithLogObserver(newNet));
163164

164165
if (__DEV__) {
165166
const {inspect} = require('./StoreInspector');
@@ -366,20 +367,17 @@ class RelayModernEnvironment implements IEnvironment {
366367
operation: OperationDescriptor,
367368
updater?: ?SelectorStoreUpdater,
368369
|}): RelayObservable<GraphQLResponse> {
369-
const [logObserver, logRequestInfo] = this.__createLogObserver(
370+
const logObserver = this.__createExecuteLogObserver(
370371
operation.request.node.params,
371372
operation.request.variables,
372373
);
373374
return RelayObservable.create(sink => {
374-
const source = this._network
375-
.execute(
376-
operation.request.node.params,
377-
operation.request.variables,
378-
operation.request.cacheConfig || {},
379-
null,
380-
logRequestInfo,
381-
)
382-
.do(logObserver);
375+
const source = this._network.execute(
376+
operation.request.node.params,
377+
operation.request.variables,
378+
operation.request.cacheConfig || {},
379+
null,
380+
);
383381
const executor = RelayModernQueryExecutor.execute({
384382
operation,
385383
operationExecutions: this._operationExecutions,
@@ -397,7 +395,7 @@ class RelayModernEnvironment implements IEnvironment {
397395
treatMissingFieldsAsNull: this._treatMissingFieldsAsNull,
398396
});
399397
return () => executor.cancel();
400-
});
398+
}).do(logObserver);
401399
}
402400

403401
/**
@@ -423,7 +421,7 @@ class RelayModernEnvironment implements IEnvironment {
423421
updater?: ?SelectorStoreUpdater,
424422
uploadables?: ?UploadableMap,
425423
|}): RelayObservable<GraphQLResponse> {
426-
const [logObserver, logRequestInfo] = this.__createLogObserver(
424+
const logObserver = this.__createExecuteLogObserver(
427425
operation.request.node.params,
428426
operation.request.variables,
429427
);
@@ -436,18 +434,15 @@ class RelayModernEnvironment implements IEnvironment {
436434
updater: optimisticUpdater,
437435
};
438436
}
439-
const source = this._network
440-
.execute(
441-
operation.request.node.params,
442-
operation.request.variables,
443-
{
444-
...operation.request.cacheConfig,
445-
force: true,
446-
},
447-
uploadables,
448-
logRequestInfo,
449-
)
450-
.do(logObserver);
437+
const source = this._network.execute(
438+
operation.request.node.params,
439+
operation.request.variables,
440+
{
441+
...operation.request.cacheConfig,
442+
force: true,
443+
},
444+
uploadables,
445+
);
451446
const executor = RelayModernQueryExecutor.execute({
452447
operation,
453448
operationExecutions: this._operationExecutions,
@@ -465,7 +460,7 @@ class RelayModernEnvironment implements IEnvironment {
465460
treatMissingFieldsAsNull: this._treatMissingFieldsAsNull,
466461
});
467462
return () => executor.cancel();
468-
});
463+
}).do(logObserver);
469464
}
470465

471466
/**
@@ -484,6 +479,10 @@ class RelayModernEnvironment implements IEnvironment {
484479
operation: OperationDescriptor,
485480
source: RelayObservable<GraphQLResponse>,
486481
|}): RelayObservable<GraphQLResponse> {
482+
const logObserver = this.__createExecuteLogObserver(
483+
operation.request.node.params,
484+
operation.request.variables,
485+
);
487486
return RelayObservable.create(sink => {
488487
const executor = RelayModernQueryExecutor.execute({
489488
operation,
@@ -501,20 +500,20 @@ class RelayModernEnvironment implements IEnvironment {
501500
treatMissingFieldsAsNull: this._treatMissingFieldsAsNull,
502501
});
503502
return () => executor.cancel();
504-
});
503+
}).do(logObserver);
505504
}
506505

507506
toJSON(): mixed {
508507
return `RelayModernEnvironment(${this.configName ?? ''})`;
509508
}
510509

511-
__createLogObserver(
510+
__createExecuteLogObserver(
512511
params: RequestParameters,
513512
variables: Variables,
514-
): [Observer<GraphQLResponse>, LogRequestInfoFunction] {
513+
): Observer<GraphQLResponse> {
515514
const transactionID = generateID();
516515
const log = this.__log;
517-
const logObserver = {
516+
return {
518517
start: subscription => {
519518
log({
520519
name: 'execute.start',
@@ -550,14 +549,72 @@ class RelayModernEnvironment implements IEnvironment {
550549
});
551550
},
552551
};
553-
const logRequestInfo = info => {
554-
log({
555-
name: 'execute.info',
556-
transactionID,
557-
info,
558-
});
552+
}
553+
554+
/**
555+
* Wraps the network with logging to ensure that network requests are
556+
* always logged. Relying on each network callsite to be wrapped is
557+
* untenable and will eventually lead to holes in the logging.
558+
*/
559+
__wrapNetworkWithLogObserver(network: INetwork): INetwork {
560+
const that = this;
561+
return {
562+
execute(
563+
params: RequestParameters,
564+
variables: Variables,
565+
cacheConfig: CacheConfig,
566+
uploadables?: ?UploadableMap,
567+
): RelayObservable<GraphQLResponse> {
568+
const transactionID = generateID();
569+
const log = that.__log;
570+
const logObserver = {
571+
start: subscription => {
572+
log({
573+
name: 'network.start',
574+
transactionID,
575+
params,
576+
variables,
577+
});
578+
},
579+
next: response => {
580+
log({
581+
name: 'network.next',
582+
transactionID,
583+
response,
584+
});
585+
},
586+
error: error => {
587+
log({
588+
name: 'network.error',
589+
transactionID,
590+
error,
591+
});
592+
},
593+
complete: () => {
594+
log({
595+
name: 'network.complete',
596+
transactionID,
597+
});
598+
},
599+
unsubscribe: () => {
600+
log({
601+
name: 'network.unsubscribe',
602+
transactionID,
603+
});
604+
},
605+
};
606+
const logRequestInfo = info => {
607+
log({
608+
name: 'network.info',
609+
transactionID,
610+
info,
611+
});
612+
};
613+
return network
614+
.execute(params, variables, cacheConfig, uploadables, logRequestInfo)
615+
.do(logObserver);
616+
},
559617
};
560-
return [logObserver, logRequestInfo];
561618
}
562619
}
563620

packages/relay-runtime/store/RelayStoreTypes.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,6 @@ export type LogEvent =
430430
// value from ProfilerContext
431431
+profilerContext: mixed,
432432
|}
433-
| {|
434-
+name: 'execute.info',
435-
+transactionID: number,
436-
+info: mixed,
437-
|}
438433
| {|
439434
+name: 'execute.start',
440435
+transactionID: number,
@@ -459,6 +454,35 @@ export type LogEvent =
459454
+name: 'execute.unsubscribe',
460455
+transactionID: number,
461456
|}
457+
| {|
458+
+name: 'network.info',
459+
+transactionID: number,
460+
+info: mixed,
461+
|}
462+
| {|
463+
+name: 'network.start',
464+
+transactionID: number,
465+
+params: RequestParameters,
466+
+variables: Variables,
467+
|}
468+
| {|
469+
+name: 'network.next',
470+
+transactionID: number,
471+
+response: GraphQLResponse,
472+
|}
473+
| {|
474+
+name: 'network.error',
475+
+transactionID: number,
476+
+error: Error,
477+
|}
478+
| {|
479+
+name: 'network.complete',
480+
+transactionID: number,
481+
|}
482+
| {|
483+
+name: 'network.unsubscribe',
484+
+transactionID: number,
485+
|}
462486
| {|
463487
+name: 'store.publish',
464488
+source: RecordSource,

0 commit comments

Comments
 (0)