Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 90d972e

Browse files
committed
Add status
1 parent 811030c commit 90d972e

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ compileTestKotlin {
2828
}
2929

3030
group 'de.debuglevel.sparkmicroserviceutils'
31-
version '0.0.10'
31+
version '0.0.11'
3232

3333
sourceCompatibility = 1.8
3434

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package de.debuglevel.microservices.utils.status
2+
3+
import mu.KotlinLogging
4+
import org.eclipse.jetty.util.Uptime.getUptime
5+
import spark.Spark
6+
import spark.kotlin.get
7+
import java.lang.management.ManagementFactory
8+
9+
private val logger = KotlinLogging.logger {}
10+
11+
var status = "up"
12+
13+
/**
14+
* Sets a path to /status/ which returns a JSON object about the current server status and environment information
15+
*/
16+
fun status() {
17+
logger.info("Activating path /status/")
18+
19+
Spark.path("/status") {
20+
get("/") {
21+
logger.info("Got request for /status/")
22+
23+
type(contentType = "application/json")
24+
buildStatusJson()
25+
}
26+
}
27+
}
28+
29+
private fun buildStatusJson(): String {
30+
return """
31+
{
32+
"status":"$status",
33+
"uptime":"${getUptime()}",
34+
"jvmName":"${ManagementFactory.getRuntimeMXBean().name}",
35+
"jvmSpecName":"${ManagementFactory.getRuntimeMXBean().specName}",
36+
"jvmSpecVendor":"${ManagementFactory.getRuntimeMXBean().specVendor}",
37+
"jvmSpecVersion":"${ManagementFactory.getRuntimeMXBean().specVersion}",
38+
"vmName":"${ManagementFactory.getRuntimeMXBean().vmName}",
39+
"vmVendor":"${ManagementFactory.getRuntimeMXBean().vmVendor}",
40+
"vmVersion":"${ManagementFactory.getRuntimeMXBean().vmVersion}",
41+
"compilerName":"${ManagementFactory.getCompilationMXBean().name}"
42+
}
43+
""".trimIndent()
44+
}
45+
46+
/**
47+
* Gets the application uptime in seconds
48+
*/
49+
private fun getUptime() =
50+
ManagementFactory.getRuntimeMXBean().uptime / 1000

0 commit comments

Comments
 (0)