Skip to content

Commit b640be4

Browse files
eberharaabernix
authored andcommitted
feature(apollo-engine-reporting): Add custom http agent support (#1879)
This PR fixes #1836. This PR enables developers to inject the http agent to be used on the network requests to apollo engine endpoint. <!-- Thanks for filing a pull request on GraphQL Server! Please look at the following checklist to ensure that your PR can be accepted quickly: --> TODO: * [x] Update CHANGELOG.md with your change (include reference to issue & this PR) * [x] Make sure all of the significant new logic is covered by tests * [x] Rebase your changes on master so that they can be merged easily * [x] Make sure all tests and linter rules pass <!--**Pull Request Labels** While not necessary, you can help organize our pull requests by labeling this issue when you open it. To add a label automatically, simply [x] mark the appropriate box below: - [ ] feature - [ ] blocking - [ ] docs To add a label not listed above, simply place `/label another-label-name` on a line by itself. -->
1 parent c229b93 commit b640be4

5 files changed

Lines changed: 15 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- 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)
77
- 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)
88
- Allow JSON parsing in `RESTDataSource` of Content Type `application/hal+json`. [PR ##185](https://github.com/apollographql/apollo-server/pull/1853)
9+
- 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)
910

1011
### v2.1.0
1112

docs/source/api/apollo-server.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ addMockFunctionsToSchema({
322322

323323
The URL of the Engine report ingress server.
324324

325+
* `requestAgent`: `http.Agent | https.Agent | false`
326+
327+
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).
328+
325329
* `debugPrintReports`: boolean
326330

327331
If set, prints all reports as JSON when they are sent.

packages/apollo-engine-reporting/src/agent.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
Trace,
99
} from 'apollo-engine-reporting-protobuf';
1010

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

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

261264
if (curResponse.status >= 500 && curResponse.status < 600) {

packages/apollo-server-env/src/fetch.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { Agent } from 'http';
1+
import { Agent as HttpAgent } from 'http';
2+
import { Agent as HttpsAgent } from 'https';
23

34
export declare function fetch(
45
input?: RequestInfo,
56
init?: RequestInit,
67
): Promise<Response>;
78

9+
export type RequestAgent = HttpAgent | HttpsAgent;
10+
811
export type RequestInfo = Request | string;
912

1013
export declare class Headers implements Iterable<[string, string]> {
@@ -58,7 +61,7 @@ export interface RequestInit {
5861
timeout?: number;
5962
compress?: boolean;
6063
size?: number;
61-
agent?: Agent;
64+
agent?: RequestAgent | false;
6265

6366
// Cloudflare Workers accept a `cf` property to control Cloudflare features
6467
// See https://developers.cloudflare.com/workers/reference/cloudflare-features/

packages/apollo-server-env/src/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Headers = import('./fetch').Headers;
1212
type HeadersInit = import('./fetch').HeadersInit;
1313
type Body = import('./fetch').Body;
1414
type Request = import('./fetch').Request;
15+
type RequestAgent = import('./fetch').RequestAgent;
1516
type RequestInit = import('./fetch').RequestInit;
1617
type RequestMode = import('./fetch').RequestMode;
1718
type RequestCredentials = import('./fetch').RequestCredentials;

0 commit comments

Comments
 (0)