Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(apollo-engine-reporting): Add custom http agent support #1879

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
- Allow an optional function to resolve the `rootValue`, passing the `DocumentNode` AST to determine the value. [PR #1555](https://github.com/apollographql/apollo-server/pull/1555)
- Follow-up on the work in [PR #1516](https://github.com/apollographql/apollo-server/pull/1516) to also fix missing insertion cursor/caret when a custom GraphQL configuration is specified which doesn't specify its own `cursorShape` property. [PR #1607](https://github.com/apollographql/apollo-server/pull/1607)
- Allow JSON parsing in `RESTDataSource` of Content Type `application/hal+json`. [PR ##185](https://github.com/apollographql/apollo-server/pull/1853)
- Add support for a `requestAgent` configuration parameter within the `engine` configuration. This can be utilized when a proxy is necessary to transmit tracing and metrics data to Apollo Engine. It accepts either an [`http.Agent`](https://nodejs.org/docs/latest-v8.x/api/http.html#http_class_http_agent) or [`https.Agent`](https://nodejs.org/docs/latest-v8.x/api/https.html#https_class_https_agent) and behaves the same as the `agent` parameter to Node.js' [`http.request`](https://nodejs.org/docs/latest-v8.x/api/http.html#http_http_request_options_callback). [PR #1879](https://github.com/apollographql/apollo-server/pull/1879)

### v2.1.0

Expand Down
4 changes: 4 additions & 0 deletions docs/source/api/apollo-server.md
Expand Up @@ -322,6 +322,10 @@ addMockFunctionsToSchema({

The URL of the Engine report ingress server.

* `requestAgent`: `http.Agent | https.Agent | false`

HTTP(s) agent to be used for Apollo Engine metrics reporting. This accepts either an [`http.Agent`](https://nodejs.org/docs/latest-v10.x/api/http.html#http_class_http_agent) or [`https.Agent`](https://nodejs.org/docs/latest-v10.x/api/https.html#https_class_https_agent) and behaves the same as the `agent` parameter to Node.js' [`http.request`](https://nodejs.org/docs/latest-v8.x/api/http.html#http_http_request_options_callback).

* `debugPrintReports`: boolean

If set, prints all reports as JSON when they are sent.
Expand Down
5 changes: 4 additions & 1 deletion packages/apollo-engine-reporting/src/agent.ts
Expand Up @@ -8,7 +8,7 @@ import {
Trace,
} from 'apollo-engine-reporting-protobuf';

import { fetch, Response } from 'apollo-server-env';
import { fetch, RequestAgent, Response } from 'apollo-server-env';
import retry from 'async-retry';

import { EngineReportingExtension } from './extension';
Expand Down Expand Up @@ -60,6 +60,8 @@ export interface EngineReportingOptions {
endpointUrl?: string;
// If set, prints all reports as JSON when they are sent.
debugPrintReports?: boolean;
// HTTP(s) agent to be used on the fetch call to apollo-engine metrics endpoint
requestAgent?: RequestAgent | false;
// Reporting is retried with exponential backoff up to this many times
// (including the original request). Defaults to 5.
maxAttempts?: number;
Expand Down Expand Up @@ -256,6 +258,7 @@ export class EngineReportingAgent<TContext = any> {
'content-encoding': 'gzip',
},
body: compressed,
agent: this.options.requestAgent,
});

if (curResponse.status >= 500 && curResponse.status < 600) {
Expand Down
7 changes: 5 additions & 2 deletions packages/apollo-server-env/src/fetch.d.ts
@@ -1,10 +1,13 @@
import { Agent } from 'http';
import { Agent as HttpAgent } from 'http';
import { Agent as HttpsAgent } from 'https';

export declare function fetch(
input?: RequestInfo,
init?: RequestInit,
): Promise<Response>;

export type RequestAgent = HttpAgent | HttpsAgent;

export type RequestInfo = Request | string;

export declare class Headers implements Iterable<[string, string]> {
Expand Down Expand Up @@ -58,7 +61,7 @@ export interface RequestInit {
timeout?: number;
compress?: boolean;
size?: number;
agent?: Agent;
agent?: RequestAgent | false;

// Cloudflare Workers accept a `cf` property to control Cloudflare features
// See https://developers.cloudflare.com/workers/reference/cloudflare-features/
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-server-env/src/global.d.ts
Expand Up @@ -12,6 +12,7 @@ type Headers = import('./fetch').Headers;
type HeadersInit = import('./fetch').HeadersInit;
type Body = import('./fetch').Body;
type Request = import('./fetch').Request;
type RequestAgent = import('./fetch').RequestAgent;
type RequestInit = import('./fetch').RequestInit;
type RequestMode = import('./fetch').RequestMode;
type RequestCredentials = import('./fetch').RequestCredentials;
Expand Down