-
Notifications
You must be signed in to change notification settings - Fork 7
/
instrumentation.ts
63 lines (61 loc) · 2.47 KB
/
instrumentation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { LogLevel } from "@fs/ppaas-common";
let logFunction: ((message: string, level?: LogLevel | undefined, ...datas: any[]) => void) | undefined;
let logger: typeof import("@fs/ppaas-common/dist/src/util/log") | undefined;
async function log (message: string, level?: LogLevel | undefined, ...datas: any[]) {
try {
if (logFunction === undefined && process.env.NEXT_RUNTIME === "nodejs") {
try {
({ log: logFunction, logger } = await import("@fs/ppaas-common"));
logger.config.LogFileName = "ppaas-controller";
logFunction("Instrumentation register log imported", logger?.LogLevel.DEBUG);
} catch (error) {
// eslint-disable-next-line no-console
console.error("Could not import log from @fs/ppaas-common", error);
}
}
try {
if (logFunction) {
logFunction(message, level, ...datas);
} else {
// eslint-disable-next-line no-console
console.log(message, ...datas);
}
} catch (error) {
// eslint-disable-next-line no-console
console.log(message, ...datas);
throw error;
}
} catch (error) {
// eslint-disable-next-line no-console
console.error("Instrumentation log error", error, message);
}
}
export async function register () {
// Await the first log so that logFunction will be populated
await log("Instrumentation register enter");
if (process.env.NEXT_RUNTIME === "nodejs") {
try {
// Load encryption key/openId secret before we start the loops
const { waitForSecrets } = await import("./pages/api/util/secrets");
log("Instrumentation register Secrets imported", logger?.LogLevel.DEBUG);
await waitForSecrets();
log("Instrumentation register Secrets finished", logger?.LogLevel.INFO);
} catch (error) {
log("Instrumentation register Secrets failed", logger?.LogLevel.ERROR, error);
throw error;
}
try {
const { start: startCommuncations } = await import("./pages/api/util/communications");
log("Instrumentation register startCommuncations imported", logger?.LogLevel.DEBUG);
const result = startCommuncations();
if (!result) {
throw new Error("Communications loops not started");
}
log("Instrumentation register startCommuncations finished: " + result, logger?.LogLevel.WARN);
} catch (error) {
log("Instrumentation register communications failed", logger?.LogLevel.ERROR, error);
throw error;
}
}
log("Instrumentation register exit");
}