Search code examples
node.jskotlinmonitoringktorktor-client

Server uptime in Ktor


How do I calculate server uptime in Ktor. In nodejs, there is process.uptime() which returns server uptime. I don't see an equivalent method in Ktor. Any suggestions ?


Solution

  • Import:

    import kotlin.time.toDuration
    import kotlin.time.DurationUnit
    import java.lang.management.ManagementFactory
    

    You can try with ManagementFactory api to get uptime of current running process and convert into duration like below.

    routing {
        get("/") {
            val millis = ManagementFactory.getRuntimeMXBean().uptime
            val duration = millis.toDuration(DurationUnit.MILLISECONDS)
            call.respond(duration.toString())
        }
    }
    

    enter image description here