Skip to content

Commit 394aaaa

Browse files
fix review comments and fix merge conflicts (#95)
adding Sentry monitoring for runner
1 parent 97f38df commit 394aaaa

File tree

7 files changed

+48
-10
lines changed

7 files changed

+48
-10
lines changed

copilot/fsd-form-runner-adapter/manifest.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ network:
5151
#
5252
# Pass environment variables as key value pairs.
5353
variables:
54+
SENTRY_DSN: "https://[email protected]/4508573162864640"
55+
SENTRY_TRACES_SAMPLE_RATE: 1.0
5456
ACCESSIBILITY_STATEMENT_URL: "https://frontend.${COPILOT_ENVIRONMENT_NAME}.access-funding.test.levellingup.gov.uk/accessibility_statement"
57+
SENTRY_ENV: ${COPILOT_ENVIRONMENT_NAME}
5558
AWS_BUCKET_NAME:
5659
from_cfn: ${COPILOT_APPLICATION_NAME}-${COPILOT_ENVIRONMENT_NAME}-FormUploadsBucket
5760
BASIC_AUTH_ON: false
@@ -118,6 +121,7 @@ environments:
118121
PRIVACY_POLICY_URL: "https://frontend.access-funding.levellingup.gov.uk/privacy"
119122
SERVICE_START_PAGE: "https://frontend.access-funding.levellingup.gov.uk/account"
120123
ELIGIBILITY_RESULT_URL: "https://frontend.access-funding.levellingup.gov.uk/eligibility-result"
124+
SENTRY_TRACES_SAMPLE_RATE: 0.02
121125
count:
122126
range: 2-4
123127
cooldown:

runner/config/custom-environment-variables.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,8 @@
6767
"__name": "MIGRATION_BANNER_ENABLED",
6868
"__format": "boolean"
6969
},
70-
"ignoreSectionsFromSummary": "IGNORE_SECTIONS_FROM_SUMMARY_PAGE"
70+
"ignoreSectionsFromSummary": "IGNORE_SECTIONS_FROM_SUMMARY_PAGE",
71+
"sentryDsn": "SENTRY_DSN",
72+
"sentryTracesSampleRate": "SENTRY_TRACES_SAMPLE_RATE",
73+
"sentryEnv": "SENTRY_ENV"
7174
}

runner/config/default.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,9 @@ module.exports = {
109109
migrationBannerEnabled: false,
110110
eligibilityResultUrl: "",
111111
ignoreSectionsFromSummary: ["FabDefault"],
112+
113+
/*sentry configurations*/
114+
sentryDsn: "",
115+
sentryTracesSampleRate: "",
116+
sentryEnv: ""
112117
};

runner/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@aws-sdk/client-s3": "3.633.0",
2727
"@aws-sdk/lib-storage": "3.633.0",
2828
"@aws-sdk/s3-request-presigner": "3.633.0",
29+
"@sentry/node": "^8.47.0",
2930
"dropzone": "5.9.3",
3031
"hapi-i18n": "3.0.1",
3132
"joi": "17.13.3",

runner/src/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import createServer from "./server";
22

33
createServer({})
4-
.then((server) => server.start())
5-
.then(() => process.send && process.send("online"))
6-
.catch((err) => {
7-
console.error(err);
8-
process.exit(1);
9-
});
4+
.then((server) => server.start())
5+
.then(() => process.send && process.send("online"))
6+
.then(() => console.log("*** SERVER STARTED ***"))
7+
.catch((err) => {
8+
console.error(err);
9+
process.exit(1);
10+
});

runner/src/instrument.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {config} from "./server/plugins/utils/AdapterConfigurationSchema";
2+
import * as Sentry from "@sentry/node";
3+
import {NodeOptions} from "@sentry/node";
4+
5+
if (config.sentryDsn) {
6+
const sentryConfig: NodeOptions = {
7+
dsn: config.sentryDsn, // Replace with your Sentry DSN
8+
environment: config.sentryEnv || "development" // Use the provided environment or default to "development"
9+
};
10+
// Include tracesSampleRate only if it's available
11+
if (config.sentryTracesSampleRate) {
12+
sentryConfig.tracesSampleRate = Number(config.sentryTracesSampleRate);
13+
}
14+
console.log(`[SENTRY MONITORING ENABLED] Environment: ${sentryConfig.environment} Sample Rate: ${sentryConfig.tracesSampleRate} DSN Available`);
15+
Sentry.init(sentryConfig);
16+
}

runner/src/server/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @ts-ignore
22
import fs from "fs";
3+
import "../instrument";
34
// @ts-ignore
45
import hapi, {ServerOptions} from "@hapi/hapi";
56

@@ -40,6 +41,8 @@ import {TranslationLoaderService} from "./services/TranslationLoaderService";
4041
import {WebhookService} from "./services/WebhookService";
4142
import {pluginLog} from "./plugins/logging";
4243

44+
const Sentry = require('@sentry/node');
45+
4346
const serverOptions = async (): Promise<ServerOptions> => {
4447
const hasCertificate = config.sslKey && config.sslCert;
4548

@@ -109,11 +112,10 @@ function determineLocal(request: any) {
109112
}
110113

111114
async function createServer(routeConfig: RouteConfig) {
112-
console.log("SERVER CREATING")
115+
console.log("*** SERVER CREATING WITH PLUGINS ***")
113116
const server = hapi.server(await serverOptions());
114117
// @ts-ignore
115118
const {formFileName, formFilePath, options} = routeConfig;
116-
117119
if (config.rateLimit) {
118120
await server.register(configureRateLimitPlugin(routeConfig));
119121
}
@@ -147,7 +149,10 @@ async function createServer(routeConfig: RouteConfig) {
147149
(request: HapiRequest, h: HapiResponseToolkit) => {
148150
const {response} = request;
149151

150-
if ("isBoom" in response && response.isBoom) {
152+
if ("isBoom" in response && response.isBoom
153+
&& response?.output?.statusCode >= 500
154+
&& response?.output?.statusCode < 600) {
155+
Sentry.captureException(response);
151156
return h.continue;
152157
}
153158

@@ -207,6 +212,9 @@ async function createServer(routeConfig: RouteConfig) {
207212
server.state("cookies_policy", {
208213
encoding: "base64json",
209214
});
215+
216+
// Sentry error monitoring
217+
await Sentry.setupHapiErrorHandler(server);
210218
return server;
211219
}
212220

0 commit comments

Comments
 (0)