Enable engine reporting in Lambda (#1313)

* 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
This commit is contained in:
Evans Hauser 2018-07-09 19:29:05 -07:00 committed by GitHub
parent f551e2bb44
commit e29f8048e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 9 deletions

View file

@ -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
@ -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;
@ -115,10 +119,13 @@ export class EngineReportingAgent<TContext = any> {
this.resetReport();
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'];
@ -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;
}
@ -165,6 +172,7 @@ 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)
) {
@ -270,6 +278,8 @@ export class EngineReportingAgent<TContext = any> {
clearInterval(this.reportTimer);
this.reportTimer = undefined;
}
this.stopped = true;
}
private sendReportAndReportErrors(): Promise<void> {

View file

@ -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');
@ -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');

View file

@ -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": {

View file

@ -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,
@ -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

View file

@ -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):
@ -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 }) => {