Skip to content

Commit

Permalink
Update Sentry and wait for 4.78.0 (#2018)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored Apr 19, 2023
1 parent 66fec76 commit cd510e6
Show file tree
Hide file tree
Showing 25 changed files with 232 additions and 556 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
},
"pnpm": {
"overrides": {
"tsup": "6.5.0",
"tsup": "6.7.0",
"@storybook/react-docgen-typescript-plugin": "1.0.6--canary.9.cd77847.0"
},
"patchedDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/services/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"@aws-sdk/s3-request-presigner": "3.312.0",
"@graphql-inspector/core": "4.0.3",
"@octokit/app": "13.1.2",
"@sentry/node": "7.44.2",
"@sentry/types": "7.44.2",
"@sentry/node": "7.48.0",
"@sentry/types": "7.48.0",
"@slack/web-api": "6.8.1",
"@theguild/buddy": "0.1.0",
"@trpc/client": "10.20.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/services/emails/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@sentry/node": "7.44.2",
"@sentry/node": "7.48.0",
"@trpc/server": "10.20.0",
"@whatwg-node/fetch": "0.8.5",
"bullmq": "3.10.4",
Expand Down
3 changes: 1 addition & 2 deletions packages/services/rate-limit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@sentry/node": "7.44.2",
"@sentry/tracing": "7.44.2",
"@sentry/node": "7.48.0",
"@trpc/client": "10.20.0",
"@trpc/server": "10.20.0",
"@whatwg-node/fetch": "0.8.5",
Expand Down
3 changes: 1 addition & 2 deletions packages/services/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"@apollo/federation": "0.38.1",
"@graphql-tools/stitch": "8.7.48",
"@graphql-tools/stitching-directives": "2.3.34",
"@sentry/node": "7.44.2",
"@sentry/tracing": "7.44.2",
"@sentry/node": "7.48.0",
"@trpc/server": "10.20.0",
"@whatwg-node/fetch": "0.8.5",
"dotenv": "16.0.3",
Expand Down
1 change: 1 addition & 0 deletions packages/services/server/.env.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ENVIRONMENT=development
LOG_LEVEL=debug
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
Expand Down
5 changes: 2 additions & 3 deletions packages/services/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
"@escape.tech/graphql-armor-max-depth": "1.8.4",
"@escape.tech/graphql-armor-max-directives": "1.6.5",
"@escape.tech/graphql-armor-max-tokens": "1.3.2",
"@sentry/integrations": "7.44.2",
"@sentry/node": "7.44.2",
"@sentry/tracing": "7.44.2",
"@sentry/integrations": "7.48.0",
"@sentry/node": "7.48.0",
"@trpc/server": "10.20.0",
"@whatwg-node/fetch": "0.8.5",
"@whatwg-node/server": "0.7.5",
Expand Down
46 changes: 19 additions & 27 deletions packages/services/server/src/graphql-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import type {
FastifyRequest,
RouteHandlerMethod,
} from 'fastify';
import {
GraphQLError,
Kind,
OperationDefinitionNode,
print,
ValidationContext,
ValidationRule,
} from 'graphql';
import { GraphQLError, print, ValidationContext, ValidationRule } from 'graphql';
import { createYoga, Plugin, useErrorHandler } from 'graphql-yoga';
import hyperid from 'hyperid';
import zod from 'zod';
Expand All @@ -22,6 +15,7 @@ import { useHive } from '@graphql-hive/client';
import { Registry, RegistryContext } from '@hive/api';
import { HiveError } from '@hive/api';
import { cleanRequestId } from '@hive/service-common';
import { runWithAsyncContext } from '@sentry/node';
import { fetch } from '@whatwg-node/fetch';
import { asyncStorage } from './async-storage';
import type { HiveConfig } from './environment';
Expand Down Expand Up @@ -65,7 +59,11 @@ interface Context extends RegistryContext {
const NoIntrospection: ValidationRule = (context: ValidationContext) => ({
Field(node) {
if (node.name.value === '__schema' || node.name.value === '__type') {
context.reportError(new GraphQLError('GraphQL introspection is not allowed', [node]));
context.reportError(
new GraphQLError('GraphQL introspection is not allowed', {
nodes: [node],
}),
);
}
},
});
Expand Down Expand Up @@ -98,22 +96,13 @@ export const graphqlHandler = (options: GraphQLHandlerOptions): RouteHandlerMeth
useArmor(),
useSentry({
startTransaction: false,
renameTransaction: true,
renameTransaction: false,
/**
* When it's not `null`, the plugin modifies the error object.
* We end up with an unintended error masking, because the GraphQLYogaError is replaced with GraphQLError (without error.originalError).
*/
eventIdKey: null,
operationName: () => 'graphql',
transactionName(args) {
const rootOperation = args.document.definitions.find(
o => o.kind === Kind.OPERATION_DEFINITION,
) as OperationDefinitionNode;
const operationType = rootOperation.operation;
const opName = args.operationName || rootOperation.name?.value || 'anonymous';

return `${operationType}.${opName}`;
},
includeRawResult: false,
includeResolverArgs: false,
includeExecuteVariables: true,
Expand All @@ -128,6 +117,7 @@ export const graphqlHandler = (options: GraphQLHandlerOptions): RouteHandlerMeth
: clientNameHeaderValue;

if (transaction) {
transaction.setName(`graphql.${args.operationName || 'unknown'}`);
transaction.setTag('graphql_client_name', clientName ?? 'unknown');
transaction.sampled = !!clientName && clientName !== 'Hive Client';
}
Expand All @@ -141,7 +131,7 @@ export const graphqlHandler = (options: GraphQLHandlerOptions): RouteHandlerMeth
},
appendTags: ({ contextValue }) => {
const supertokens_user_id = extractUserId(contextValue as any);
const request_id = cleanRequestId((contextValue as Context).req.headers['x-request-id']);
const request_id = (contextValue as Context).requestId;

return {
supertokens_user_id,
Expand Down Expand Up @@ -248,13 +238,15 @@ export const graphqlHandler = (options: GraphQLHandlerOptions): RouteHandlerMeth
requestId,
},
async () => {
const response = await server.handleNodeRequest(req, {
req,
reply,
headers: req.headers,
requestId,
session: null,
abortSignal: controller.signal,
const response = await runWithAsyncContext(() => {
return server.handleNodeRequest(req, {
req,
reply,
headers: req.headers,
requestId,
session: null,
abortSignal: controller.signal,
});
});

response.headers.forEach((value, key) => {
Expand Down
51 changes: 25 additions & 26 deletions packages/services/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '@hive/service-common';
import { createConnectionString, createStorage as createPostgreSQLStorage } from '@hive/storage';
import { Dedupe, ExtraErrorData } from '@sentry/integrations';
import * as Sentry from '@sentry/node';
import { captureException, init, Integrations, SeverityLevel } from '@sentry/node';
import { createServerAdapter } from '@whatwg-node/server';
import { createContext, internalApiRouter } from './api';
import { asyncStorage } from './async-storage';
Expand All @@ -36,28 +36,27 @@ const LegacyCheckAuth0EmailUserExistsPayloadModel = zod.object({
});

export async function main() {
if (env.sentry) {
Sentry.init({
serverName: 'api',
enabled: true,
environment: env.environment,
dsn: env.sentry.dsn,
tracesSampleRate: 1,
release: env.release,
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Sentry.Integrations.ContextLines(),
new Sentry.Integrations.LinkedErrors(),
new ExtraErrorData({
depth: 2,
}),
new Dedupe(),
],
maxBreadcrumbs: 5,
defaultIntegrations: false,
autoSessionTracking: false,
});
}
init({
serverName: 'api',
enabled: !!env.sentry,
environment: env.environment,
dsn: env.sentry?.dsn,
enableTracing: true,
tracesSampleRate: 1,
release: env.release,
integrations: [
new Integrations.Http({ tracing: true }),
new Integrations.ContextLines(),
new Integrations.LinkedErrors(),
new ExtraErrorData({
depth: 2,
}),
new Dedupe(),
],
maxBreadcrumbs: 5,
defaultIntegrations: false,
autoSessionTracking: false,
});

const server = await createServer({
name: 'graphql-api',
Expand Down Expand Up @@ -98,7 +97,7 @@ export async function main() {
},
});

function createErrorHandler(level: Sentry.SeverityLevel): LogFn {
function createErrorHandler(level: SeverityLevel): LogFn {
return (error: any, errorLike?: any, ...args: any[]) => {
server.log.error(error, errorLike, ...args);

Expand All @@ -110,7 +109,7 @@ export async function main() {
}

if (errorObj instanceof Error) {
Sentry.captureException(errorObj, {
captureException(errorObj, {
level,
extra: {
error,
Expand Down Expand Up @@ -445,7 +444,7 @@ export async function main() {
await server.listen(port, '::');
} catch (error) {
server.log.fatal(error);
Sentry.captureException(error, {
captureException(error, {
level: 'fatal',
});
process.exit(1);
Expand Down
8 changes: 3 additions & 5 deletions packages/services/service-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"private": true,
"peerDependencies": {
"@sentry/node": "^7.0.0",
"@sentry/tracing": "^7.0.0",
"@sentry/utils": "^7.0.0",
"@trpc/server": "10.20.0"
},
Expand All @@ -19,9 +18,8 @@
"zod": "3.21.4"
},
"devDependencies": {
"@sentry/node": "7.44.2",
"@sentry/tracing": "7.44.2",
"@sentry/types": "7.44.2",
"@sentry/utils": "7.44.2"
"@sentry/node": "7.48.0",
"@sentry/types": "7.48.0",
"@sentry/utils": "7.48.0"
}
}
17 changes: 7 additions & 10 deletions packages/services/service-common/src/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import type { FastifyInstance, FastifyPluginAsync, FastifyRequest } from 'fastify';
import fp from 'fastify-plugin';
import * as Sentry from '@sentry/node';
import '@sentry/tracing';
import { Transaction } from '@sentry/tracing';
import type { ExtractedNodeRequestData, TraceparentData } from '@sentry/types';
import type { ExtractedNodeRequestData, TraceparentData, Transaction } from '@sentry/types';
import { extractTraceparentData, normalize } from '@sentry/utils';
import { cleanRequestId } from './helpers';

const plugin: FastifyPluginAsync = async server => {
server.decorateRequest('sentryTransaction', null);
server.decorateReply('sentryTransaction', null);

function shouldIgnore(request: FastifyRequest) {
if (
Expand All @@ -22,7 +20,7 @@ const plugin: FastifyPluginAsync = async server => {
return false;
}

server.addHook('onRequest', async request => {
server.addHook('onRequest', async (request, reply) => {
if (shouldIgnore(request)) {
return;
}
Expand Down Expand Up @@ -53,11 +51,10 @@ const plugin: FastifyPluginAsync = async server => {
},
{ request: extractedRequestData },
);
(request as any).sentryTransaction = transaction;
(reply as any).sentryTransaction = transaction;
transaction.sampled = true;

Sentry.getCurrentHub().configureScope(scope => {
scope.setSpan(transaction);
});
Sentry.configureScope(scope => scope.setSpan(transaction));

return;
});
Expand All @@ -68,7 +65,7 @@ const plugin: FastifyPluginAsync = async server => {
}

setImmediate(() => {
const transaction: Transaction = (request as any).sentryTransaction;
const transaction: Transaction = (reply as any).sentryTransaction;

transaction.setData('url', request.url);
transaction.setData('query', request.query);
Expand Down
4 changes: 2 additions & 2 deletions packages/services/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"db:generate": "schemats generate --config schemats.cjs -o src/db/types.ts"
},
"dependencies": {
"@sentry/node": "7.44.2",
"@sentry/node": "7.48.0",
"@theguild/buddy": "0.1.0",
"dotenv": "16.0.3",
"got": "12.6.0",
Expand All @@ -23,7 +23,7 @@
"zod": "3.21.4"
},
"devDependencies": {
"@sentry/types": "7.44.2",
"@sentry/types": "7.48.0",
"@tgriesser/schemats": "9.0.1",
"@types/node": "18.15.11",
"@types/pg": "8.6.6",
Expand Down
3 changes: 1 addition & 2 deletions packages/services/stripe-billing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@sentry/node": "7.44.2",
"@sentry/tracing": "7.44.2",
"@sentry/node": "7.48.0",
"@trpc/client": "10.20.0",
"@trpc/server": "10.20.0",
"@whatwg-node/fetch": "0.8.5",
Expand Down
3 changes: 1 addition & 2 deletions packages/services/tokens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@sentry/node": "7.44.2",
"@sentry/tracing": "7.44.2",
"@sentry/node": "7.48.0",
"@trpc/server": "10.20.0",
"dotenv": "16.0.3",
"ioredis": "5.3.1",
Expand Down
3 changes: 1 addition & 2 deletions packages/services/usage-estimator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@sentry/node": "7.44.2",
"@sentry/tracing": "7.44.2",
"@sentry/node": "7.48.0",
"@trpc/server": "10.20.0",
"@whatwg-node/fetch": "0.8.5",
"dotenv": "16.0.3",
Expand Down
3 changes: 1 addition & 2 deletions packages/services/usage-ingestor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@sentry/node": "7.44.2",
"@sentry/tracing": "7.44.2",
"@sentry/node": "7.48.0",
"agentkeepalive": "4.3.0",
"date-fns": "2.29.3",
"dotenv": "16.0.3",
Expand Down
3 changes: 1 addition & 2 deletions packages/services/usage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@sentry/node": "7.44.2",
"@sentry/tracing": "7.44.2",
"@sentry/node": "7.48.0",
"@trpc/client": "10.20.0",
"@trpc/server": "10.20.0",
"@whatwg-node/fetch": "0.8.5",
Expand Down
Loading

0 comments on commit cd510e6

Please sign in to comment.