Skip to content

Commit

Permalink
Provide ability to specify client info in traces (#1631)
Browse files Browse the repository at this point in the history
* Provide ability to specify client info in traces

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.

* Use extensions and context in createClientInfo

* Remove support for clientAddress

The frontend will not support it in the near future

* create -> generate and make default generator

createClientInfo -> generateClientInfo

* Clarify default values
  • Loading branch information
evans authored and abernix committed Sep 27, 2018
1 parent 261994c commit 81c4642
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 2 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
### vNEXT

- FIXME(@abernix): Allow context to be passed to all GraphQLExtension methods.
- FIXME(@abernix): client info in traces.
- 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)

Expand Down
10 changes: 10 additions & 0 deletions docs/source/api/apollo-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,13 @@ addMockFunctionsToSchema({
* `maskErrorDetails`: boolean

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

* generateClientInfo?: (o: { context: any, extensions?: Record<string, any>}) => ClientInfo;

Creates the client information that is attached to the traces sent to the
Apollo backend. The context field is the execution context passed to the
resolvers and the extensions field corresponds to the same value in the POST
body or GET parameters. `ClientInfo` contains fields for `clientName` and
`clientVersion`, which are both optional. The default generation copies the
respective fields from `extensions.clientInfo`. If `clientName` or
`clientVersion` is not present, the values are set to the empty string.
13 changes: 13 additions & 0 deletions packages/apollo-engine-reporting/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ Traces.encode = function(message, originalWriter) {
return writer;
};

export interface ClientInfo {
clientName?: 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 +88,14 @@ 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
generateClientInfo?: (
o: {
context: any;
extensions?: 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
25 changes: 24 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 All @@ -38,6 +38,12 @@ export class EngineReportingExtension<TContext = any>
operationName: string,
trace: Trace,
) => void;
private generateClientInfo: (
o: {
context: any;
extensions?: Record<string, any>;
},
) => ClientInfo;

public constructor(
options: EngineReportingOptions,
Expand All @@ -51,6 +57,11 @@ export class EngineReportingExtension<TContext = any>
const root = new Trace.Node();
this.trace.root = root;
this.nodes.set(responsePathAsString(undefined), root);
this.generateClientInfo =
options.generateClientInfo ||
// Default to using the clientInfo field of the request's extensions, when
// the ClientInfo fields are undefined, we send the empty string
(({ extensions }) => (extensions && extensions.clientInfo) || {});
}

public requestDidStart(o: {
Expand All @@ -60,6 +71,8 @@ export class EngineReportingExtension<TContext = any>
variables: Record<string, any>;
persistedQueryHit?: boolean;
persistedQueryRegister?: boolean;
context: any;
extensions?: Record<string, any>;
}): EndHandler {
this.trace.startTime = dateToTimestamp(new Date());
this.startHrTime = process.hrtime();
Expand Down Expand Up @@ -154,6 +167,16 @@ export class EngineReportingExtension<TContext = any>
});
}

// While clientAddress could be a part of the protobuf, we'll ignore it for
// now, since the backend does not group by it and Engine frontend will not
// support it in the short term
const { clientName, clientVersion } = this.generateClientInfo({
context: o.context,
extensions: o.extensions,
});
this.trace.clientName = clientName || '';
this.trace.clientVersion = clientVersion || '';

return () => {
this.trace.durationNs = durationHrTimeToNanos(
process.hrtime(this.startHrTime),
Expand Down
2 changes: 2 additions & 0 deletions packages/apollo-server-core/src/requestProcessing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface GraphQLRequestOptions<TContext = any> {
debug?: boolean;

extensions?: Array<() => GraphQLExtension>;
queryExtensions?: Record<string, any>;
tracing?: boolean;
persistedQueries?: PersistedQueryOptions;
cacheControl?: CacheControlExtensionOptions;
Expand Down Expand Up @@ -185,6 +186,7 @@ export class GraphQLRequestProcessor {
queryString: request.query,
operationName: request.operationName,
variables: request.variables,
extensions: request.extensions,
persistedQueryHit,
persistedQueryRegister,
});
Expand Down
2 changes: 2 additions & 0 deletions packages/apollo-server-core/src/runHttpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ export async function runHttpQuery(
formatResponse: optionsObject.formatResponse,

debug: optionsObject.debug,

queryExtensions: extensions,
});

// GET operations should only be queries (not mutations). We want to throw
Expand Down
1 change: 1 addition & 0 deletions packages/graphql-extensions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class GraphQLExtensionStack<TContext = any> {
variables?: { [key: string]: any };
persistedQueryHit?: boolean;
persistedQueryRegister?: boolean;
extensions?: Record<string, any>;
}): EndHandler {
return this.handleDidStart(
ext => ext.requestDidStart && ext.requestDidStart(o),
Expand Down

0 comments on commit 81c4642

Please sign in to comment.