Skip to content

Commit

Permalink
Provide ability to specify client info in traces
Browse files Browse the repository at this point in the history
Adds the createClientInfo to apollo-engine-reporting, which enables the
differentiation of clients based on the request, operation, and
variables. This could be extended to include the response. However for
the first release. It doesn't quite make sense.
  • Loading branch information
Evans Hauser committed Sep 7, 2018
1 parent 8875022 commit c9a415d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/source/api/apollo-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,9 @@ addMockFunctionsToSchema({
* `maskErrorDetails`: boolean

Set to true to remove error details from the traces sent to Apollo's servers. Defaults to false.

* createClientInfo?: (o: { request: Request, queryString?: string, parsedQuery?: DocumentNode, variables: Record<string, any>}) => ClientInfo;

Creates the client information that is attached to the traces sent to the
Apollo backend. `ClientInfo` contains fields for `clientName`,
`clientVersion`, and `clientAddress`, which can all be optional.
18 changes: 17 additions & 1 deletion packages/apollo-engine-reporting/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Trace,
} from 'apollo-engine-reporting-protobuf';

import { fetch, Response } from 'apollo-server-env';
import { fetch, Request, Response } from 'apollo-server-env';
import * as retry from 'async-retry';

import { EngineReportingExtension } from './extension';
Expand All @@ -34,6 +34,12 @@ Traces.encode = function(message, originalWriter) {
return writer;
};

export interface ClientInfo {
clientName?: string;
clientAddress?: string;
clientVersion?: string;
}

export interface EngineReportingOptions {
// API key for the service. Get this from
// [Engine](https://engine.apollographql.com) by logging in and creating
Expand Down Expand Up @@ -83,6 +89,16 @@ export interface EngineReportingOptions {
sendReportsImmediately?: boolean;
// To remove the error message from traces, set this to true. Defaults to false
maskErrorDetails?: boolean;
// Creates the client information attached to the traces sent to the Apollo
// backend
createClientInfo?: (
o: {
request: Request;
queryString?: string;
parsedQuery?: DocumentNode;
variables: Record<string, any>;
},
) => ClientInfo;

// 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 Down
15 changes: 14 additions & 1 deletion packages/apollo-engine-reporting/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from 'graphql-extensions';
import { Trace, google } from 'apollo-engine-reporting-protobuf';

import { EngineReportingOptions } from './agent';
import { EngineReportingOptions, ClientInfo } from './agent';
import { defaultSignature } from './signature';

// EngineReportingExtension is the per-request GraphQLExtension which creates a
Expand Down Expand Up @@ -145,6 +145,19 @@ export class EngineReportingExtension<TContext = any>
});
}

const { clientName, clientAddress, clientVersion } =
(this.options.createClientInfo &&
this.options.createClientInfo({
request: o.request,
queryString: o.queryString,
parsedQuery: o.parsedQuery,
variables: o.variables,
})) ||
({} as ClientInfo);
this.trace.clientName = clientName || '';
this.trace.clientAddress = clientAddress || '';
this.trace.clientVersion = clientVersion || '';

return () => {
this.trace.durationNs = durationHrTimeToNanos(
process.hrtime(this.startHrTime),
Expand Down

0 comments on commit c9a415d

Please sign in to comment.