Skip to content

Commit

Permalink
Enable engine reporting in Lambda (#1313)
Browse files Browse the repository at this point in the history
* add disableInterval option to engine reporting

* set engine options for lambda

* lambda: add comment on constructor

* lambda: update readme typo and include callout to use graphql.js

* disableInterval -> sendReportsImmediately

* use sendReportsImmediately correctly and fix compilation

* uses new Header to fix different header combination, fixes #1301

* add apollo-server-env dependency

* fix logic error in engine reporting
  • Loading branch information
evans authored Jul 10, 2018
1 parent f551e2b commit e29f804
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
22 changes: 16 additions & 6 deletions packages/apollo-engine-reporting/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export interface EngineReportingOptions {
// 'sendReport()' on other signals if you'd like. Note that 'sendReport()'
// does not run synchronously so it cannot work usefully in an 'exit' handler.
handleSignals?: boolean;
// Sends the trace report immediately. This options is useful for stateless environments
sendReportsImmediately?: boolean;

// XXX Provide a way to set client_name, client_version, client_address,
// service, and service_version fields. They are currently not revealed in the
Expand All @@ -103,6 +105,8 @@ export class EngineReportingAgent<TContext = any> {
private report!: FullTracesReport;
private reportSize!: number;
private reportTimer: any; // timer typing is weird and node-specific
private sendReportsImmediately?: boolean;
private stopped: boolean = false;

public constructor(options: EngineReportingOptions = {}) {
this.options = options;
Expand All @@ -115,10 +119,13 @@ export class EngineReportingAgent<TContext = any> {

this.resetReport();

this.reportTimer = setInterval(
() => this.sendReportAndReportErrors(),
this.options.reportIntervalMs || 10 * 1000,
);
this.sendReportsImmediately = options.sendReportsImmediately;
if (!this.sendReportsImmediately) {
this.reportTimer = setInterval(
() => this.sendReportAndReportErrors(),
this.options.reportIntervalMs || 10 * 1000,
);
}

if (this.options.handleSignals !== false) {
const signals: NodeJS.Signals[] = ['SIGINT', 'SIGTERM'];
Expand All @@ -141,7 +148,7 @@ export class EngineReportingAgent<TContext = any> {

public addTrace(signature: string, operationName: string, trace: Trace) {
// Ignore traces that come in after stop().
if (!this.reportTimer) {
if (this.stopped) {
return;
}

Expand All @@ -165,8 +172,9 @@ export class EngineReportingAgent<TContext = any> {

// If the buffer gets big (according to our estimate), send.
if (
this.sendReportsImmediately ||
this.reportSize >=
(this.options.maxUncompressedReportSize || 4 * 1024 * 1024)
(this.options.maxUncompressedReportSize || 4 * 1024 * 1024)
) {
this.sendReportAndReportErrors();
}
Expand Down Expand Up @@ -270,6 +278,8 @@ export class EngineReportingAgent<TContext = any> {
clearInterval(this.reportTimer);
this.reportTimer = undefined;
}

this.stopped = true;
}

private sendReportAndReportErrors(): Promise<void> {
Expand Down
4 changes: 3 additions & 1 deletion packages/apollo-server-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ To deploy the AWS Lambda function we must create a Cloudformation Template and a

#### 1. Write the API handlers

In a file named `graphql.js`, place the following code:

```js
const { ApolloServer, gql } = require('apollo-server-lambda');

Expand Down Expand Up @@ -136,7 +138,7 @@ exports.graphqlHandler = server.createHandler();

## Modifying the Lambda Response (Enable CORS)

To enable CORS the response HTTP headers need to be modified. To accomplish this use `cors` options.
To enable CORS the response HTTP headers need to be modified. To accomplish this use the `cors` option.

```js
const { ApolloServer, gql } = require('apollo-server-lambda');
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-server-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dependencies": {
"@apollographql/graphql-playground-html": "^1.6.0",
"apollo-server-core": "^2.0.0-rc.6",
"apollo-server-env": "^2.0.0-rc.6",
"graphql-tools": "^3.0.4"
},
"devDependencies": {
Expand Down
15 changes: 14 additions & 1 deletion packages/apollo-server-lambda/src/ApolloServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as lambda from 'aws-lambda';
import { ApolloServerBase } from 'apollo-server-core';
export { GraphQLOptions, GraphQLExtension } from 'apollo-server-core';
import { GraphQLOptions } from 'apollo-server-core';
import { GraphQLOptions, Config } from 'apollo-server-core';
import {
renderPlaygroundPage,
RenderPageOptions as PlaygroundRenderPageOptions,
Expand All @@ -22,6 +22,19 @@ export interface CreateHandlerOptions {
}

export class ApolloServer extends ApolloServerBase {
// If you feel tempted to add an option to this constructor. Please consider
// another place, since the documentation becomes much more complicated when
// the constructor is not longer shared between all integration
constructor(options: Config) {
if (process.env.ENGINE_API_KEY || options.engine) {
options.engine = {
sendReportsImmediately: true,
...(typeof options.engine !== 'boolean' ? options.engine : {}),
};
}
super(options);
}

// This translates the arguments from the middleware into graphQL options It
// provides typings for the integration specific behavior, ideally this would
// be propagated with a generic to the super class
Expand Down
3 changes: 2 additions & 1 deletion packages/apollo-server-lambda/src/lambdaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
HttpQueryError,
runHttpQuery,
} from 'apollo-server-core';
import { Headers } from 'apollo-server-env';

export interface LambdaGraphQLOptionsFunction {
(event: lambda.APIGatewayProxyEvent, context: lambda.Context):
Expand Down Expand Up @@ -45,7 +46,7 @@ export function graphqlLambda(
request: {
url: event.path,
method: event.httpMethod,
headers: event.headers as any,
headers: new Headers(event.headers),
},
}).then(
({ graphqlResponse, responseInit }) => {
Expand Down

0 comments on commit e29f804

Please sign in to comment.