Skip to content

Commit

Permalink
feat: add memory debug log when requested via env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
kaladivo committed Oct 18, 2024
1 parent 920d200 commit dcc1be7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/server-utils/src/commonConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ export const metricsConfig = Config.option(
export const internalServerPortConfig = Config.option(
Config.number('INTERNAL_SERVER_PORT')
)

export const memoryDebugIntervalMsConfig = Config.option(
Config.number('MEMORY_DEBUG_INTERVAL_MS')
)
27 changes: 27 additions & 0 deletions packages/server-utils/src/makeMemoryDebugLayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {Effect, Layer} from 'effect'
import {type DurationInput} from 'effect/Duration'

const formatMemoryUsage = (data: number): string =>
`${Math.round((data / 1024 / 1024) * 100) / 100} MB`

export const makeMemoryDebugLayer = (
logInterval: DurationInput
): Layer.Layer<never> =>
Layer.effectDiscard(
Effect.gen(function* (_) {
const memoryData = yield* _(Effect.sync(() => process.memoryUsage()))

const memoryUsage = {
rss: `${formatMemoryUsage(memoryData.rss)} -> Resident Set Size - total memory allocated for the process execution`,
heapTotal: `${formatMemoryUsage(memoryData.heapTotal)} -> total size of the allocated heap`,
heapUsed: `${formatMemoryUsage(memoryData.heapUsed)} -> actual memory used during the execution`,
external: `${formatMemoryUsage(memoryData.external)} -> V8 external memory`,
}

yield* _(Effect.logInfo('Memory usage', memoryUsage))
}).pipe(
Effect.zipLeft(Effect.sleep(logInterval)),
Effect.forever,
Effect.fork
)
)
10 changes: 10 additions & 0 deletions packages/server-utils/src/runMainInNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import {BatchSpanProcessor} from '@opentelemetry/sdk-trace-base'
import {Effect, Layer, Logger} from 'effect'
import {
isRunningInProductionConfig,
memoryDebugIntervalMsConfig,
metricsConfig,
nodeEnvConfig,
otlpTraceExporterUrlConfig,
serviceNameConfig,
serviceVersionConfig,
} from './commonConfigs'
import {devToolsLayer} from './devToolsLayer'
import {makeMemoryDebugLayer} from './makeMemoryDebugLayer'

const stringifyCircular = (
obj: unknown,
Expand Down Expand Up @@ -60,6 +62,13 @@ const logger = isRunningInProductionConfig.pipe(
Layer.unwrapEffect
)

const memoryDebugLayer = memoryDebugIntervalMsConfig.pipe(
Effect.flatten,
Effect.map((interval) => makeMemoryDebugLayer(interval)),
Effect.catchTag('NoSuchElementException', () => Effect.succeed(Layer.empty)),
Layer.unwrapEffect
)

const NodeSdkLive = Effect.gen(function* (_) {
const serviceName = yield* _(serviceNameConfig)
const serviceVersion = yield* _(serviceVersionConfig)
Expand Down Expand Up @@ -119,6 +128,7 @@ export const runMainInNode: RunMain = (effect, options) => {
effect.pipe(
Effect.catchAll((error) => Effect.logFatal('Error', error)),
Effect.catchAllDefect((error) => Effect.logFatal('Defect', error)),
Effect.provide(memoryDebugLayer),
Effect.provide(NodeSdkLive),
Effect.provide(devToolsLayer(nodeEnvConfig)),
Effect.provide(logger)
Expand Down

0 comments on commit dcc1be7

Please sign in to comment.