Skip to content

Commit

Permalink
feat: added sentry node
Browse files Browse the repository at this point in the history
  • Loading branch information
froid1911 committed Apr 3, 2024
1 parent 93e35bf commit 9074d21
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 13 deletions.
3 changes: 3 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"dependencies": {
"@apollo/server": "^4.10.2",
"@prisma/client": "^5.11.0",
"@sentry/node": "^7.109.0",
"@sentry/profiling-node": "^7.109.0",
"@types/cookie-parser": "^1.4.7",
"body-parser": "^1.20.2",
"cookie-parser": "^1.4.6",
Expand All @@ -40,6 +42,7 @@
"pino": "^8.19.0",
"pino-http": "^8.6.1",
"pino-pretty": "^10.3.1",
"pino-sentry-transport": "^1.1.0",
"siwe": "^2.1.4",
"vite-node": "^0.29.8",
"vitest": "^0.32.4",
Expand Down
75 changes: 75 additions & 0 deletions api/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import basePrisma from './database';
import {
renderPlaygroundPage,
} from 'graphql-playground-html'
import * as Sentry from "@sentry/node";
import { nodeProfilingIntegration } from "@sentry/profiling-node";


const logger = getChildLogger({ msgPrefix: 'APP' });
const startupTime = new Date();
Expand All @@ -13,6 +16,31 @@ export const createApp = (): Express => {
logger.debug('Creating app');
const app = express();

if (process.env.SENTRY_DSN) {
Sentry.init({
dsn: process.env.SENTRY_DSN,
integrations: [
nodeProfilingIntegration(),
// enable Express.js middleware tracing
new Sentry.Integrations.Express({
// to trace all requests to the default router
app,
// alternatively, you can specify the routes you want to trace:
// router: someRouter,
}),
],

// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0,
});

// RequestHandler creates a separate execution context, so that all
// transactions/spans/breadcrumbs are isolated across requests
app.use(Sentry.Handlers.requestHandler());
// TracingHandler creates a trace for every incoming request
app.use(Sentry.Handlers.tracingHandler());
}
app.get('/healthz', async (_req, res) => {
try {
await basePrisma.user.findFirst();
Expand Down
5 changes: 4 additions & 1 deletion api/src/graphql/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Context as DriveContext, createContext as createDriveContext } from './
import { getChildLogger } from '../../logger';
import "express-async-errors";
import { errorHandler } from '../../middleware/errors';

import * as Sentry from "@sentry/node";
const logger = getChildLogger({ msgPrefix: 'SERVER' });

function loggerPlugin(): ApolloServerPlugin<Context> {
Expand Down Expand Up @@ -75,6 +75,9 @@ export const startServer = async (
);

app.use(errorHandler);
if (process.env.SENTRY_DSN) {
app.use(Sentry.Handlers.errorHandler());
}
return httpServer.listen({ port: PORT }, () => {
logger.info(`Running on ${PORT}`);
});
Expand Down
25 changes: 18 additions & 7 deletions api/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import pino from 'pino';
import pinoHttp from 'pino-http';
import { loggerConfig } from '../logger.config';

import { createWriteStream, Sentry } from "pino-sentry";
const {
moduleFilter, prefixFilter, logLevel, httpLogLevel,
} = loggerConfig;
Expand Down Expand Up @@ -48,19 +48,30 @@ const doesPassFilters = (config: {
bindings: pino.Bindings;
}): boolean => FILTERS.every((f) => f(config));

const transport = process.env.SENTRY_DSN ? {
target: "pino-sentry-transport",
options: {
sentry: {
dsn: process.env.SENTRY_DSN,
// additional options for sentry
},
withLogRecord: true, // default false - send the log record to sentry as a context.(if its more then 8Kb Sentry will throw an error)
tags: ['id'], // sentry tags to add to the event, uses lodash.get to get the value from the log record
context: ['hostname'], // sentry context to add to the event, uses lodash.get to get the value from the log record,
minLevel: 40, // which level to send to sentry
}
} : {
target: 'pino-pretty',
}
export const expressLogger = pinoHttp({
level: httpLogLevel,
msgPrefix: formatPrefix('express'),
transport: {
target: 'pino-pretty',
},
transport,
});

const logger = pino({
level: logLevel,
transport: {
target: 'pino-pretty',
},
transport,
});

export const getChildLogger = (
Expand Down
15 changes: 10 additions & 5 deletions api/src/modules/real-world-assets/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ export const listener: IReceiverOptions = {
export async function transmit(strands: InternalTransmitterUpdate<RealWorldAssetsDocument | DocumentDriveDocument, "global">[], prisma: Prisma.TransactionClient) {
// logger.debug(strands);
for (const strand of strands) {

if (strand.documentId === "") {
await handleDriveStrand(strand as InternalTransmitterUpdate<DocumentDriveDocument, "global">, prisma);
} else {
await handleRwaDocumentStrand(strand as InternalTransmitterUpdate<RealWorldAssetsDocument, "global">, prisma);
try {
if (strand.documentId === "") {
await handleDriveStrand(strand as InternalTransmitterUpdate<DocumentDriveDocument, "global">, prisma);
} else {
await handleRwaDocumentStrand(strand as InternalTransmitterUpdate<RealWorldAssetsDocument, "global">, prisma);
}
} catch (e) {
logger.error({ msg: "Error processing strand", error: e });
continue;
}
}

}

async function handleDriveStrand(strand: InternalTransmitterUpdate<DocumentDriveDocument, "global">, prisma: Prisma.TransactionClient) {
Expand Down

0 comments on commit 9074d21

Please sign in to comment.