-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#IOPID-1876] Enable Application Insights (#52)
Co-authored-by: Daniele Manni <[email protected]>
- Loading branch information
1 parent
e2bf550
commit ef4f6e3
Showing
13 changed files
with
282 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@pagopa/io-session-manager": minor | ||
--- | ||
|
||
Enable AppInsights |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* eslint-disable turbo/no-undeclared-env-vars */ | ||
import * as O from "fp-ts/Option"; | ||
import * as E from "fp-ts/Either"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; | ||
|
||
export const APPINSIGHTS_CONNECTION_STRING = O.fromNullable( | ||
process.env.APPINSIGHTS_CONNECTION_STRING, | ||
); | ||
|
||
export const APPINSIGHTS_CLOUD_ROLE_NAME = pipe( | ||
process.env.APPINSIGHTS_CLOUD_ROLE_NAME, | ||
NonEmptyString.decode, | ||
E.getOrElseW(() => undefined), | ||
); | ||
|
||
export const APPINSIGHTS_DISABLED = process.env.APPINSIGHTS_DISABLED === "true"; | ||
|
||
// Application insights sampling percentage | ||
const DEFAULT_APPINSIGHTS_SAMPLING_PERCENTAGE = 5; | ||
export const APPINSIGHTS_SAMPLING_PERCENTAGE = process.env | ||
.APPINSIGHTS_SAMPLING_PERCENTAGE | ||
? parseInt(process.env.APPINSIGHTS_SAMPLING_PERCENTAGE, 10) | ||
: DEFAULT_APPINSIGHTS_SAMPLING_PERCENTAGE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,76 @@ | ||
import { Server } from "http"; | ||
import * as appInsights from "applicationinsights"; | ||
|
||
import * as O from "fp-ts/Option"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
|
||
import { newApp } from "./app"; | ||
import { AppInsightsConfig } from "./config"; | ||
import { | ||
StartupEventName, | ||
initAppInsights, | ||
trackStartupTime, | ||
} from "./utils/appinsights"; | ||
import { log } from "./utils/logger"; | ||
import { getCurrentBackendVersion } from "./utils/package"; | ||
import { TimeTracer } from "./utils/timer"; | ||
|
||
const timer = TimeTracer(); | ||
|
||
// eslint-disable-next-line turbo/no-undeclared-env-vars | ||
const port = process.env.WEBSITES_PORT ?? 3000; | ||
|
||
newApp({}) | ||
const maybeAppInsightsClient = pipe( | ||
AppInsightsConfig.APPINSIGHTS_CONNECTION_STRING, | ||
O.map((key) => | ||
initAppInsights(key, { | ||
cloudRole: AppInsightsConfig.APPINSIGHTS_CLOUD_ROLE_NAME, | ||
applicationVersion: getCurrentBackendVersion(), | ||
disableAppInsights: AppInsightsConfig.APPINSIGHTS_DISABLED, | ||
samplingPercentage: AppInsightsConfig.APPINSIGHTS_SAMPLING_PERCENTAGE, | ||
}), | ||
), | ||
O.toUndefined, | ||
); | ||
|
||
newApp({ appInsightsClient: maybeAppInsightsClient }) | ||
.then((app) => { | ||
app.listen(port, () => { | ||
log.info(`Example app listening on port ${port}`); | ||
}); | ||
const server = app | ||
.listen(port, () => { | ||
const startupTimeMs = timer.getElapsedMilliseconds(); | ||
|
||
log.info("Listening on port %d", port); | ||
log.info(`Startup time: %sms`, startupTimeMs.toString()); | ||
pipe( | ||
maybeAppInsightsClient, | ||
O.fromNullable, | ||
O.map((_) => | ||
trackStartupTime(_, StartupEventName.SERVER, startupTimeMs), | ||
), | ||
); | ||
}) | ||
.on("close", () => { | ||
log.info("On close: emit 'server:stop' event"); | ||
const result = app.emit("server:stop"); | ||
log.info( | ||
`On close: end emit 'server:stop' event. Listeners found: ${result}`, | ||
); | ||
|
||
maybeAppInsightsClient?.flush(); | ||
appInsights.dispose(); | ||
}); | ||
|
||
process.on("SIGTERM", shutDown(server, "SIGTERM")); | ||
process.on("SIGINT", shutDown(server, "SIGINT")); | ||
}) | ||
.catch((err) => { | ||
log.error("Error loading app: %s", err); | ||
process.exit(1); | ||
}); | ||
|
||
const shutDown = (server: Server, signal: string) => () => { | ||
log.info(`${signal} signal received: closing HTTP server`); | ||
server.close(() => { | ||
log.info("HTTP server closed"); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.