Skip to content

Commit

Permalink
Set captureTraces to false if we don't report
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Segaran committed May 15, 2020
1 parent 8fe753b commit ce57d65
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
27 changes: 15 additions & 12 deletions packages/apollo-engine-reporting/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,19 +314,18 @@ function makeTestHTTP(): Trace.HTTP {
});
}

describe("tests for the shouldReportQuery reporting option", () => {
describe('tests for the shouldReportQuery reporting option', () => {
const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema });

const addTrace = jest.fn();
beforeEach(() => {
addTrace.mockClear();
})
it("report no traces", async () => {

});
it('report no traces', async () => {
const pluginInstance = plugin({ traceReporting: false }, addTrace);

await pluginTestHarness({
const context = await pluginTestHarness({
pluginInstance,
schema,
graphqlRequest: {
Expand All @@ -344,17 +343,20 @@ describe("tests for the shouldReportQuery reporting option", () => {
});
},
});
expect(context.metrics.captureTraces).toBeFalsy();
});

it('report traces based on operation name', async () => {
const pluginInstance = plugin(
{
traceReporting: async (request) => {
return request.request.operationName === 'report'
}
}, addTrace);
traceReporting: async request => {
return request.request.operationName === 'report';
},
},
addTrace,
);

await pluginTestHarness({
const context1 = await pluginTestHarness({
pluginInstance,
schema,
graphqlRequest: {
Expand All @@ -374,9 +376,10 @@ describe("tests for the shouldReportQuery reporting option", () => {
});

expect(addTrace).toBeCalledTimes(1);
expect(context1.metrics.captureTraces).toBeTruthy();
addTrace.mockClear();

await pluginTestHarness({
const context2 = await pluginTestHarness({
pluginInstance,
schema,
graphqlRequest: {
Expand All @@ -396,10 +399,10 @@ describe("tests for the shouldReportQuery reporting option", () => {
});

expect(addTrace).not.toBeCalled();
expect(context2.metrics.captureTraces).toBeFalsy();
});
});


/**
* TESTS FOR THE sendHeaders REPORTING OPTION
*/
Expand Down
14 changes: 12 additions & 2 deletions packages/apollo-engine-reporting/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export const plugin = <TContext>(
request: { http, variables },
}) {
// If the options are false don't do any metrics timing.
if (options.traceReporting === false) return {};
if (options.traceReporting === false) {
// If we are running in gateway mode don't send the ftv1 trace
metrics.captureTraces = false;
return {};
}
metrics.captureTraces = true;

const treeBuilder: EngineReportingTreeBuilder = new EngineReportingTreeBuilder(
{
Expand Down Expand Up @@ -95,7 +100,12 @@ export const plugin = <TContext>(
// Returning here if we aren't reporting the trace to make sure,
// endDone is set and the treeBuilder has stopped

if (!reportTrace) return;
if (!reportTrace) {
// If we are running in gateway mode don't send the ftv1 trace
// after we have stopped trace timing.
metrics.captureTraces = false;
return;
}

treeBuilder.trace.fullQueryCacheHit = !!metrics.responseCacheHit;
treeBuilder.trace.forbiddenOperation = !!metrics.forbiddenOperation;
Expand Down

0 comments on commit ce57d65

Please sign in to comment.