Skip to content

Commit

Permalink
feat: add onRequestResolve hook
Browse files Browse the repository at this point in the history
which enables f.e. adding custom tags to span
  • Loading branch information
mwieczorek authored and DanielMSchmidt committed May 28, 2019
1 parent 2c84156 commit c48a090
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ When the request is not traced there will also be no traces of the field resolve

There might be certain field resolvers that are not worth the tracing, e.g. when they get a value out of an object and need no further tracing. To control if you want a field resolver to be traced you can pass the `shouldTraceFieldResolver` option to the constructor. The function is called with the same arguments as your field resolver and you can get the name of the field by `info.fieldName`. When you return false no traces will be made of this field resolvers and all underlying ones.

## Modifying span metadata

If you'd like to add custom tags or logs to span you can construct the extension with `onRequestResolve`. The function is called with two arguments: span and infos `onRequestResolve?: (span: Span, info: RequestStart)`

## Contributing

Please feel free to add issues with new ideas, bugs and anything that might come up.
Expand Down
10 changes: 0 additions & 10 deletions src/__tests__/__snapshots__/integration-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ exports[`integration with apollo-server alias with fragment works 1`] = `
request:1
finished: true
tags:
1. {"key":"queryString","value":"\\n fragment F on A {\\n dos: two\\n }\\n\\n query {\\n a {\\n ...F\\n }\\n }"}
+-- a:2
finished: true
Expand All @@ -19,8 +17,6 @@ exports[`integration with apollo-server alias works 1`] = `
request:1
finished: true
tags:
1. {"key":"queryString","value":"query {\\n a {\\n uno: one\\n two\\n }\\n }"}
+-- a:2
finished: true
Expand All @@ -37,8 +33,6 @@ exports[`integration with apollo-server correct span nesting 1`] = `
request:1
finished: true
tags:
1. {"key":"queryString","value":"query {\\n a {\\n one\\n two\\n }\\n }"}
+-- a:2
finished: true
Expand All @@ -55,8 +49,6 @@ exports[`integration with apollo-server does not start a field resolver span if
request:1
finished: true
tags:
1. {"key":"queryString","value":"query {\\n a {\\n one\\n two\\n }\\n b {\\n four\\n }\\n }"}
+-- b:2
finished: true
Expand All @@ -70,8 +62,6 @@ exports[`integration with apollo-server implements traces for arrays 1`] = `
request:1
finished: true
tags:
1. {"key":"queryString","value":"query {\\n as {\\n one\\n two\\n }\\n }"}
+-- as:2
finished: true
Expand Down
2 changes: 0 additions & 2 deletions src/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ describe("Apollo Tracing", () => {
const cb = tracingMiddleware.requestDidStart({ queryString: "query {}" });
expect(server.startSpan).toHaveBeenCalled();
expect(local.startSpan).not.toHaveBeenCalled();
expect(server.span.setTag).toHaveBeenCalledWith("queryString", "query {}");

cb();
expect(server.span.finish).toHaveBeenCalled();
Expand All @@ -79,7 +78,6 @@ describe("Apollo Tracing", () => {
const cb = tracingMiddleware.requestDidStart({ queryString: "query {}" });
expect(server.startSpan).toHaveBeenCalled();
expect(local.startSpan).not.toHaveBeenCalled();
expect(server.span.setTag).toHaveBeenCalledWith("queryString", "query {}");

cb(new Error("ups"));
expect(server.span.finish).toHaveBeenCalled();
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Request } from "apollo-server-env";
import { SpanContext, addContextHelpers } from "./context";

const alwaysTrue = () => true;
const emptyFunction = () => {};

interface InitOptions {
server?: Tracer;
Expand All @@ -24,6 +25,7 @@ interface InitOptions {
context: SpanContext,
info: GraphQLResolveInfo
) => boolean;
onRequestResolve?: (span: Span, info: RequestStart) => void;
}

interface ExtendedGraphQLResolveInfo extends GraphQLResolveInfo {
Expand Down Expand Up @@ -74,14 +76,16 @@ export default class OpentracingExtension<TContext extends SpanContext>
context: SpanContext,
info: GraphQLResolveInfo
) => boolean;
private onRequestResolve: (span: Span, info: RequestStart) => void;

constructor({
server,
local,
shouldTraceRequest,
shouldTraceFieldResolver,
onFieldResolveFinish,
onFieldResolve
onFieldResolve,
onRequestResolve
}: InitOptions = {}) {
if (!server) {
throw new Error(
Expand All @@ -102,6 +106,7 @@ export default class OpentracingExtension<TContext extends SpanContext>
this.shouldTraceFieldResolver = shouldTraceFieldResolver || alwaysTrue;
this.onFieldResolveFinish = onFieldResolveFinish;
this.onFieldResolve = onFieldResolve;
this.onRequestResolve = onRequestResolve || emptyFunction;
}

mapToObj(inputMap: Map<string, any>) {
Expand Down Expand Up @@ -139,7 +144,7 @@ export default class OpentracingExtension<TContext extends SpanContext>
childOf: externalSpan ? externalSpan : undefined
});

rootSpan.setTag("queryString", infos.queryString);
this.onRequestResolve(rootSpan, infos);
this.requestSpan = rootSpan;

return () => {
Expand Down

0 comments on commit c48a090

Please sign in to comment.