Skip to content

Commit f4924c7

Browse files
authored
Fix defaults in sentry (#217)
1 parent 6d72786 commit f4924c7

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

.changeset/calm-ligers-love.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@envelop/sentry': patch
3+
---
4+
5+
Fix defaults

packages/plugins/sentry/src/index.ts

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import * as Sentry from '@sentry/node';
66
import { Span } from '@sentry/types';
77
import { ExecutionArgs, Kind, OperationDefinitionNode, print, responsePathAsArray } from 'graphql';
88

9-
const tracingSpanSymbol = Symbol('SENTRY_GRAPHQL');
10-
119
export type SentryPluginOptions = {
1210
startTransaction?: boolean;
1311
renameTransaction?: boolean;
@@ -20,18 +18,20 @@ export type SentryPluginOptions = {
2018
operationName?: (args: ExecutionArgs) => string;
2119
};
2220

23-
interface PluginContext {
24-
[tracingSpanSymbol]: Span;
25-
}
26-
27-
export const useSentry = (
28-
options: SentryPluginOptions = {
29-
startTransaction: true,
30-
trackResolvers: true,
21+
export const useSentry = (options: SentryPluginOptions = {}): Plugin => {
22+
function pick<K extends keyof SentryPluginOptions>(key: K, defaultValue?: SentryPluginOptions[K]): SentryPluginOptions[K] {
23+
return prioritize(options[key], defaultValue);
3124
}
32-
): Plugin<PluginContext> => {
25+
26+
const startTransaction = pick('startTransaction', true);
27+
const trackResolvers = pick('trackResolvers', true);
28+
const includeResolverArgs = pick('includeResolverArgs', false);
29+
const includeRawResult = pick('includeRawResult', false);
30+
const includeExecuteVariables = pick('includeExecuteVariables', false);
31+
const renameTransaction = pick('renameTransaction', false);
32+
3333
return {
34-
onExecute({ args, extendContext }) {
34+
onExecute({ args }) {
3535
const rootOperation = args.document.definitions.find(o => o.kind === Kind.OPERATION_DEFINITION) as OperationDefinitionNode;
3636
const operationType = rootOperation.operation;
3737
const document = print(args.document);
@@ -48,7 +48,7 @@ export const useSentry = (
4848

4949
let rootSpan: Span;
5050

51-
if (options.startTransaction) {
51+
if (startTransaction) {
5252
rootSpan = Sentry.startTransaction({
5353
name: transactionName,
5454
op,
@@ -75,30 +75,26 @@ export const useSentry = (
7575

7676
rootSpan = span;
7777

78-
if (options.renameTransaction) {
78+
if (renameTransaction) {
7979
scope!.setTransactionName(transactionName);
8080
}
8181
}
8282

8383
rootSpan.setData('document', document);
8484

85-
extendContext({
86-
[tracingSpanSymbol]: rootSpan,
87-
});
88-
89-
const onResolverCalled: OnExecuteHookResult<PluginContext>['onResolverCalled'] = options.trackResolvers
90-
? ({ args: resolversArgs, info, context }) => {
91-
if (context && context[tracingSpanSymbol]) {
85+
const onResolverCalled: OnExecuteHookResult['onResolverCalled'] = trackResolvers
86+
? ({ args: resolversArgs, info }) => {
87+
if (rootSpan) {
9288
const { fieldName, returnType, parentType } = info;
93-
const parent: typeof rootSpan = context[tracingSpanSymbol];
89+
const parent = rootSpan;
9490
const tags: Record<string, string> = {
9591
fieldName,
9692
parentType: parentType.toString(),
9793
returnType: returnType.toString(),
9894
};
9995

100-
if (options.includeResolverArgs) {
101-
tags.args = options.includeResolverArgs ? JSON.stringify(resolversArgs || {}) : '';
96+
if (includeResolverArgs) {
97+
tags.args = JSON.stringify(resolversArgs || {});
10298
}
10399

104100
const childSpan = parent.startChild({
@@ -107,7 +103,7 @@ export const useSentry = (
107103
});
108104

109105
return ({ result }) => {
110-
if (options.includeRawResult) {
106+
if (includeRawResult) {
111107
childSpan.setData('result', result);
112108
}
113109

@@ -130,7 +126,7 @@ export const useSentry = (
130126
return {
131127
onResolverCalled,
132128
onExecuteDone({ result }) {
133-
if (options.includeRawResult) {
129+
if (includeRawResult) {
134130
rootSpan.setData('result', result);
135131
}
136132

@@ -144,11 +140,11 @@ export const useSentry = (
144140

145141
scope.setTags(addedTags || {});
146142

147-
if (options.includeRawResult) {
143+
if (includeRawResult) {
148144
scope.setExtra('result', result);
149145
}
150146

151-
if (options.includeExecuteVariables) {
147+
if (includeExecuteVariables) {
152148
scope.setExtra('variables', args.variableValues);
153149
}
154150

@@ -182,3 +178,16 @@ export const useSentry = (
182178
},
183179
};
184180
};
181+
182+
/**
183+
* Sets a priority for provided values from left (highest) to right (lowest)
184+
*/
185+
function prioritize<T>(...values: T[]): T {
186+
const picked = values.find(val => typeof val !== 'undefined');
187+
188+
if (typeof picked === 'undefined') {
189+
return values[values.length - 1];
190+
}
191+
192+
return picked;
193+
}

0 commit comments

Comments
 (0)