Skip to content

Commit 2707f63

Browse files
committed
fix: update Sentry instrumentation (#2021)
* fix: setup sentry for CJS * fix: make small changes to config * fix: send sourcemaps to sentry on build inside docker * fix: make requested changes fix: duplicated env in build image fix: add sentry token to dev deploy fix: send sourcemaps separately and do not include sourcemaps in prod code
1 parent 0ce5204 commit 2707f63

14 files changed

Lines changed: 889 additions & 227 deletions

File tree

.github/actions/build-images/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ inputs:
1414
aws-role-arn:
1515
description: 'AWS Role to assume'
1616
required: true
17+
sentry-token:
18+
description: 'Sentry auth token to push sourcemaps'
19+
required: true
1720

1821
runs:
1922
using: composite
@@ -31,5 +34,6 @@ runs:
3134
- name: Build and push the images
3235
env:
3336
TAG: ${{ inputs.tag }}
37+
SENTRY_AUTH_TOKEN: ${{ inputs.sentry-token }}
3438
run: bash ./docker/build.sh ${{ inputs.aws-ecr-uri }} $TAG
3539
shell: bash

.github/workflows/deploy-dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ jobs:
3737
aws-ecr-uri: ${{ vars.PRIVATE_ECR }}
3838
aws-role-arn: ${{ vars.AWS_ROLE_ARN }}
3939
aws-region: ${{ vars.AWS_REGION }}
40+
sentry-token: ${{ secrets.SENTRY_AUTH_TOKEN }}

.github/workflows/release-please.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
aws-ecr-uri: ${{ vars.GRAASP_PUBLIC_ECR }}
3434
aws-role-arn: ${{ vars.AWS_ROLE_ARN }}
3535
aws-region: us-east-1
36+
sentry-token: ${{ secrets.SENTRY_AUTH_TOKEN }}
3637

3738
- name: Auto Tag
3839
uses: graasp/graasp-deploy/.github/actions/auto-tag-after-release@v1

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,6 @@ secret-key
6060
# Generated
6161
openapi.json
6262
vacuum-report.html
63+
64+
# Sentry Config File
65+
.sentryclirc

docker/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ ENV NODE_ENV=production
2424
COPY . .
2525
RUN yarn build-ts
2626

27+
# -------------------------------------------------------
28+
FROM base AS build-sources
29+
30+
WORKDIR /app
31+
COPY package.json yarn.lock .yarnrc.yml ./
32+
# We need a specific command because we need to copy the folder with it, not just the content.
33+
COPY .yarn/releases ./.yarn/releases/
34+
RUN yarn set version berry && yarn install --immutable
35+
36+
ENV NODE_ENV=production
37+
COPY . .
38+
RUN yarn build-ts --sourceMap true --sourceRoot '/'
39+
40+
# send sourcemaps to Sentry
41+
RUN --mount=type=secret,id=SENTRY_AUTH_TOKEN,env=SENTRY_AUTH_TOKEN yarn sentry:sourcemaps
42+
2743
# -------------------------------------------------------
2844
FROM base AS deps
2945

docker/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ workers_tag_full="$aws_ecr_uri/$workers_tag_short"
4545
migrate_tag_short="graasp:migrate-$tag_version"
4646
migrate_tag_full="$aws_ecr_uri/$migrate_tag_short"
4747

48-
docker build -t $core_tag_full -f docker/Dockerfile --build-arg APP_VERSION=$tag_version --build-arg BUILD_TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%S) .
48+
docker build -t $core_tag_full -f docker/Dockerfile --build-arg APP_VERSION=$tag_version --build-arg BUILD_TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%S) --secret id=SENTRY_AUTH_TOKEN .
4949
docker push $core_tag_full
5050

5151
docker build -t $workers_tag_full -f docker/workers.Dockerfile --build-arg APP_VERSION=$tag_version --build-arg BUILD_TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%S) .

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"migration:apply-test": "DB_CONNECTION=postgres://test:test@db:5432/test yarn migration:run",
4545
"migration:generate": "yarn drizzle-kit generate",
4646
"migration:run": "yarn drizzle-kit migrate",
47-
"migration:check": "yarn drizzle-kit generate --name=migrationtest --out=./src/drizzle --schema=./src/drizzle/schema.ts --dialect=postgresql && [ -f '$(find ./src/drizzle | grep migrationtest)' ] && exit 1 || exit 0"
47+
"migration:check": "yarn drizzle-kit generate --name=migrationtest --out=./src/drizzle --schema=./src/drizzle/schema.ts --dialect=postgresql && [ -f '$(find ./src/drizzle | grep migrationtest)' ] && exit 1 || exit 0",
48+
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org graasp --project graasp-core ./dist && sentry-cli sourcemaps upload --org graasp --project graasp-core ./dist"
4849
},
4950
"dependencies": {
5051
"@aws-sdk/client-s3": "3.787.0",
@@ -69,7 +70,10 @@
6970
"@graasp/etherpad-api": "2.1.1",
7071
"@graasp/sdk": "5.16.3",
7172
"@rapideditor/country-coder": "5.4.0",
72-
"@sentry/node": "7.119.2",
73+
"@sentry/cli": "2.57.0",
74+
"@sentry/node": "10.20.0",
75+
"@sentry/opentelemetry": "10.20.0",
76+
"@sentry/profiling-node": "10.20.0",
7377
"@sentry/tracing": "7.120.3",
7478
"@sinclair/typebox": "0.34.33",
7579
"ajv": "8.17.1",

src/0-instrument.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* This file is names with a leading 0- so that the import reorder does not change its place.
3+
*
4+
* It NEEDS to be the first imported file otherwise sentry instrumentation does not work.
5+
*/
6+
import * as Sentry from '@sentry/node';
7+
import { nodeProfilingIntegration } from '@sentry/profiling-node';
8+
9+
// Ensure to call this before importing any other modules!
10+
Sentry.init({
11+
dsn: process.env.SENTRY_DSN,
12+
environment: process.env.SENTRY_ENV,
13+
release: process.env.APP_VERSION ?? 'not-specified',
14+
// Adds request headers and IP for users, for more info visit:
15+
// https://docs.sentry.io/platforms/javascript/guides/node/configuration/options/#sendDefaultPii
16+
sendDefaultPii: true,
17+
18+
integrations: [
19+
Sentry.fastifyIntegration(),
20+
// Add our Profiling integration
21+
nodeProfilingIntegration(),
22+
],
23+
24+
// Set tracesSampleRate to 1.0 to capture 100%
25+
// of transactions for tracing.
26+
// We recommend adjusting this value in production
27+
// Learn more at
28+
// https://docs.sentry.io/platforms/javascript/guides/node/configuration/options/#tracesSampleRate
29+
tracesSampleRate: 1.0,
30+
31+
// Set profilesSampleRate to 1.0 to profile 100%
32+
// of sampled transactions.
33+
// This is relative to tracesSampleRate
34+
// Learn more at
35+
// https://docs.sentry.io/platforms/javascript/guides/node/configuration/options/#profilesSampleRate
36+
profilesSampleRate: 1.0,
37+
38+
// Enable if you need to troubleshoot the Sentry config
39+
// debug: true,
40+
});

src/fastify.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as Sentry from '@sentry/node';
2+
13
import { fastifyHelmet } from '@fastify/helmet';
24
import type { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
35
import { fastify } from 'fastify';
@@ -6,7 +8,6 @@ import registerAppPlugins from './app';
68
import { DEV, NODE_ENV, PROD } from './config/env';
79
import { client } from './drizzle/db';
810
import ajvFormats from './schemas/ajvFormats';
9-
import { initSentry } from './sentry';
1011
import { APP_VERSION, CORS_ORIGIN_REGEX, HOST_LISTEN_ADDRESS, PORT } from './utils/config';
1112
import { GREETING } from './utils/constants';
1213
import { queueDashboardPlugin } from './workers/dashboard.controller';
@@ -42,8 +43,7 @@ instance.addHook('onClose', async () => {
4243
});
4344

4445
const start = async () => {
45-
const { Sentry } = initSentry(instance);
46-
46+
Sentry.setupFastifyErrorHandler(instance);
4747
instance.register(fastifyHelmet);
4848

4949
if (CORS_ORIGIN_REGEX) {

src/sentry.ts

Lines changed: 0 additions & 143 deletions
This file was deleted.

0 commit comments

Comments
 (0)