chore: Improve instrumentation loading (#9378)
* chore: Improve instrumentation loading * fix build * fix buildgst
This commit is contained in:
1 parent
7e73fe4794
commit
7484cef0fa
5 files changed
+118
-11
No files matched your search
@@ -0,0 +1,7 @@
|
|||||||
|
const registerMock = jest.fn().mockImplementation(() => {
|
||||||
|
throw new Error("Failed to register instrumentation")
|
||||||
|
})
|
||||||
|
module.exports = {
|
||||||
|
registerMock,
|
||||||
|
register: registerMock,
|
||||||
|
}
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
module.exports = {}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
const registerMock = jest.fn()
|
||||||
|
module.exports = {
|
||||||
|
registerMock,
|
||||||
|
register: registerMock,
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
import { registerInstrumentation } from "../start"
|
||||||
|
import * as utils from "@medusajs/framework/utils"
|
||||||
|
import * as logger from "@medusajs/framework/logger"
|
||||||
|
import * as instrumentationFixture from "../__fixtures__/instrumentation"
|
||||||
|
import * as instrumentationFailureFixture from "../__fixtures__/instrumentation-failure/instrumentation"
|
||||||
|
import path from "path"
|
||||||
|
|
||||||
|
describe("start", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("registerInstrumentation", () => {
|
||||||
|
it("should not throw when registering the instrumentation if the file is not ", async () => {
|
||||||
|
const fsSpy = jest.spyOn(
|
||||||
|
utils.FileSystem.prototype,
|
||||||
|
"exists",
|
||||||
|
"" as never
|
||||||
|
)
|
||||||
|
|
||||||
|
await registerInstrumentation(__dirname)
|
||||||
|
|
||||||
|
expect(fsSpy).toHaveBeenCalled()
|
||||||
|
expect(fsSpy).toHaveBeenCalledWith(
|
||||||
|
expect.stringContaining("instrumentation.js")
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should log an info message if the file is present but not register function is found", async () => {
|
||||||
|
const fsSpy = jest.spyOn(
|
||||||
|
utils.FileSystem.prototype,
|
||||||
|
"exists",
|
||||||
|
"" as never
|
||||||
|
)
|
||||||
|
const loggerSpy = jest.spyOn(logger.logger, "info", "" as never)
|
||||||
|
|
||||||
|
await registerInstrumentation(
|
||||||
|
path.join(__dirname, "../__fixtures__/instrumentation-no-register")
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(fsSpy).toHaveBeenCalled()
|
||||||
|
expect(fsSpy).toHaveBeenCalledWith(
|
||||||
|
expect.stringContaining("instrumentation.js")
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(loggerSpy).toHaveBeenCalled()
|
||||||
|
expect(loggerSpy).toHaveBeenCalledWith(
|
||||||
|
"Skipping instrumentation registration. No register function found."
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should register the instrumentation if the file is present and exports a register function", async () => {
|
||||||
|
const fsSpy = jest.spyOn(
|
||||||
|
utils.FileSystem.prototype,
|
||||||
|
"exists",
|
||||||
|
"" as never
|
||||||
|
)
|
||||||
|
|
||||||
|
instrumentationFixture.registerMock.mockReturnValue(true)
|
||||||
|
|
||||||
|
await registerInstrumentation(path.join(__dirname, "../__fixtures__"))
|
||||||
|
|
||||||
|
expect(fsSpy).toHaveBeenCalled()
|
||||||
|
expect(instrumentationFixture.registerMock).toHaveBeenCalled()
|
||||||
|
|
||||||
|
expect(fsSpy).toHaveBeenCalledWith(
|
||||||
|
expect.stringContaining("instrumentation.js")
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should throw if the instrumentation file exists but cannot be imported", async () => {
|
||||||
|
const fsSpy = jest.spyOn(
|
||||||
|
utils.FileSystem.prototype,
|
||||||
|
"exists",
|
||||||
|
"" as never
|
||||||
|
)
|
||||||
|
|
||||||
|
const err = await registerInstrumentation(
|
||||||
|
path.join(__dirname, "../__fixtures__/instrumentation-failure")
|
||||||
|
).catch((e) => e)
|
||||||
|
|
||||||
|
expect(fsSpy).toHaveBeenCalled()
|
||||||
|
expect(instrumentationFailureFixture.registerMock).toHaveBeenCalled()
|
||||||
|
|
||||||
|
expect(fsSpy).toHaveBeenCalledWith(
|
||||||
|
expect.stringContaining("instrumentation.js")
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(err).toBeInstanceOf(Error)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -5,6 +5,7 @@ import { scheduleJob } from "node-schedule"
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
dynamicImport,
|
dynamicImport,
|
||||||
|
FileSystem,
|
||||||
gqlSchemaToTypes,
|
gqlSchemaToTypes,
|
||||||
GracefulShutdownServer,
|
GracefulShutdownServer,
|
||||||
} from "@medusajs/framework/utils"
|
} from "@medusajs/framework/utils"
|
||||||
@@ -16,6 +17,7 @@ import { MedusaModule } from "@medusajs/framework/modules-sdk"
|
|||||||
|
|
||||||
const EVERY_SIXTH_HOUR = "0 */6 * * *"
|
const EVERY_SIXTH_HOUR = "0 */6 * * *"
|
||||||
const CRON_SCHEDULE = EVERY_SIXTH_HOUR
|
const CRON_SCHEDULE = EVERY_SIXTH_HOUR
|
||||||
|
const INSTRUMENTATION_FILE = "instrumentation.js"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Imports the "instrumentation.js" file from the root of the
|
* Imports the "instrumentation.js" file from the root of the
|
||||||
@@ -23,23 +25,23 @@ const CRON_SCHEDULE = EVERY_SIXTH_HOUR
|
|||||||
* of this file is optional, hence we ignore "ENOENT"
|
* of this file is optional, hence we ignore "ENOENT"
|
||||||
* errors.
|
* errors.
|
||||||
*/
|
*/
|
||||||
async function registerInstrumentation(directory: string) {
|
export async function registerInstrumentation(directory: string) {
|
||||||
try {
|
const fileSystem = new FileSystem(directory)
|
||||||
|
const exists = await fileSystem.exists(INSTRUMENTATION_FILE)
|
||||||
|
if (!exists) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const instrumentation = await dynamicImport(
|
const instrumentation = await dynamicImport(
|
||||||
path.join(directory, "instrumentation.js")
|
path.join(directory, INSTRUMENTATION_FILE)
|
||||||
)
|
)
|
||||||
if (typeof instrumentation.register === "function") {
|
if (typeof instrumentation.register === "function") {
|
||||||
logger.info("OTEL registered")
|
logger.info("OTEL registered")
|
||||||
instrumentation.register()
|
instrumentation.register()
|
||||||
}
|
} else {
|
||||||
} catch (error) {
|
logger.info(
|
||||||
if (
|
"Skipping instrumentation registration. No register function found."
|
||||||
!["ENOENT", "MODULE_NOT_FOUND", "ERR_MODULE_NOT_FOUND"].includes(
|
|
||||||
error.code
|
|
||||||
)
|
)
|
||||||
) {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user