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

chore: fixing async resolvers for graphql #300

Merged
merged 2 commits into from
Jan 8, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';
import type * as graphqlTypes from 'graphql';
import * as api from '@opentelemetry/api';
import type { Maybe } from 'graphql/jsutils/Maybe';
Expand Down Expand Up @@ -358,8 +357,11 @@ export function wrapFieldResolver<TSource = any, TContext = any, TArgs = any>(
}

return tracer.withSpan(field.span, () => {
return safeExecuteInTheMiddle<
Maybe<graphqlTypes.GraphQLFieldResolver<TSource, TContext, TArgs>>
return safeExecuteInTheMiddleAsync<
| Maybe<graphqlTypes.GraphQLFieldResolver<TSource, TContext, TArgs>>
| Promise<
Maybe<graphqlTypes.GraphQLFieldResolver<TSource, TContext, TArgs>>
>
>(
() => {
return fieldResolver.call(this, source, args, contextValue, info);
Expand All @@ -377,3 +379,32 @@ export function wrapFieldResolver<TSource = any, TContext = any, TArgs = any>(

return wrappedFieldResolver;
}

/**
* Async version of safeExecuteInTheMiddle from instrumentation package
* can be removed once this will be added to instrumentation package
* @param execute
* @param onFinish
* @param preventThrowingError
*/
async function safeExecuteInTheMiddleAsync<T>(
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
execute: () => T,
onFinish: (e: Error | undefined, result: T | undefined) => void,
preventThrowingError?: boolean
): Promise<T> {
let error: Error | undefined;
let result: T | undefined;
try {
result = await execute();
} catch (e) {
error = e;
} finally {
onFinish(error, result);
if (error && !preventThrowingError) {
// eslint-disable-next-line no-unsafe-finally
throw error;
}
// eslint-disable-next-line no-unsafe-finally
return result as T;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('graphql', () => {
});

it('should instrument execute', () => {
const executeSpan = spans[3];
const executeSpan = spans[2];
const validateSpan = spans[1];

assert.deepStrictEqual(
Expand All @@ -165,8 +165,8 @@ describe('graphql', () => {
});

it('should instrument resolvers', () => {
const executeSpan = spans[3];
const resolveParentSpan = spans[2];
const executeSpan = spans[2];
const resolveParentSpan = spans[3];
const span1 = spans[4];
const span2 = spans[5];
const span3 = spans[6];
Expand Down Expand Up @@ -251,7 +251,7 @@ describe('graphql', () => {
});

it('should instrument execute', () => {
const executeSpan = spans[3];
const executeSpan = spans[2];
const validateSpan = spans[1];

assert.deepStrictEqual(
Expand All @@ -275,8 +275,8 @@ describe('graphql', () => {
});

it('should instrument resolvers', () => {
const executeSpan = spans[3];
const resolveParentSpan = spans[2];
const executeSpan = spans[2];
const resolveParentSpan = spans[3];
const span1 = spans[4];

assertResolveSpan(
Expand Down Expand Up @@ -345,7 +345,7 @@ describe('graphql', () => {
});

it('should instrument execute', () => {
const executeSpan = spans[3];
const executeSpan = spans[2];
const validateSpan = spans[1];

assert.deepStrictEqual(
Expand Down Expand Up @@ -373,8 +373,8 @@ describe('graphql', () => {
});

it('should instrument resolvers', () => {
const executeSpan = spans[3];
const resolveParentSpan = spans[2];
const executeSpan = spans[2];
const resolveParentSpan = spans[3];
const span1 = spans[4];

assertResolveSpan(
Expand Down Expand Up @@ -517,7 +517,7 @@ describe('graphql', () => {
});

it('should instrument execute', () => {
const executeSpan = spans[3];
const executeSpan = spans[2];
const validateSpan = spans[1];

assert.deepStrictEqual(
Expand Down Expand Up @@ -612,7 +612,7 @@ describe('graphql', () => {
});

it('should instrument execute', () => {
const executeSpan = spans[3];
const executeSpan = spans[2];
const validateSpan = spans[1];

assert.deepStrictEqual(
Expand All @@ -636,8 +636,8 @@ describe('graphql', () => {
});

it('should instrument resolvers', () => {
const executeSpan = spans[3];
const resolveParentSpan = spans[2];
const executeSpan = spans[2];
const resolveParentSpan = spans[3];
const span1 = spans[4];

assertResolveSpan(
Expand Down Expand Up @@ -709,7 +709,7 @@ describe('graphql', () => {
});

it('should instrument execute', () => {
const executeSpan = spans[3];
const executeSpan = spans[2];
const validateSpan = spans[1];

assert.deepStrictEqual(
Expand All @@ -736,8 +736,8 @@ describe('graphql', () => {
});

it('should instrument resolvers', () => {
const executeSpan = spans[3];
const resolveParentSpan = spans[2];
const executeSpan = spans[2];
const resolveParentSpan = spans[3];
const span1 = spans[4];

assertResolveSpan(
Expand Down Expand Up @@ -806,7 +806,7 @@ describe('graphql', () => {
});

it('should instrument execute', () => {
const executeSpan = spans[3];
const executeSpan = spans[2];
const validateSpan = spans[1];

assert.deepStrictEqual(
Expand Down Expand Up @@ -834,8 +834,8 @@ describe('graphql', () => {
});

it('should instrument resolvers', () => {
const executeSpan = spans[3];
const resolveParentSpan = spans[2];
const executeSpan = spans[2];
const resolveParentSpan = spans[3];
const span1 = spans[4];

assertResolveSpan(
Expand Down Expand Up @@ -909,7 +909,7 @@ describe('graphql', () => {
});

it('should instrument execute', () => {
const executeSpan = spans[3];
const executeSpan = spans[2];
const validateSpan = spans[1];

assert.deepStrictEqual(
Expand All @@ -936,8 +936,8 @@ describe('graphql', () => {
});

it('should instrument resolvers', () => {
const executeSpan = spans[3];
const resolveParentSpan = spans[2];
const executeSpan = spans[2];
const resolveParentSpan = spans[3];
const span1 = spans[4];

assertResolveSpan(
Expand Down