Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- **Prometheus application metrics** — Replace JSON `/api/metrics` endpoint with Prometheus exposition format using Ktor MicrometerMetrics plugin; exposes `shoppimo_websocket_connections_total` (active WebSocket connections) and `shoppimo_shopping_lists_total` (total lists in database) alongside JVM and HTTP request metrics

## [6.1.0] - 2026-03-26

### Added
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ A real-time collaborative shopping list app. Create a list, share the link, and
- **Dark Mode** — Light, dark, and system-preference theme toggle
- **Smart Autocomplete** — Suggests items from your cross-list history as you type
- **Push Notifications** — Opt-in Web Push alerts when list changes happen
- **Prometheus Metrics** — Application metrics (online users, list count, JVM, HTTP requests) exposed for Prometheus scraping

## Tech Stack

Expand Down Expand Up @@ -126,6 +127,7 @@ For production deployment, see [DEPLOYMENT.md](DEPLOYMENT.md).
| Method | Path | Description |
|--------|------|-------------|
| `GET` | `/api/health` | Health check |
| `GET` | `/api/metrics` | Prometheus metrics |
| `POST` | `/api/lists` | Create a new list |
| `GET` | `/api/lists/{id}` | Get a list |
| `POST` | `/api/lists/{id}/items` | Add an item |
Expand Down
4 changes: 4 additions & 0 deletions backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ dependencies {
implementation("com.zaxxer:HikariCP:$hikaricp_version")
implementation("org.postgresql:postgresql:$postgresql_version")

// Metrics
implementation("io.ktor:ktor-server-metrics-micrometer")
implementation("io.micrometer:micrometer-registry-prometheus:1.11.0")

// Logging
implementation("ch.qos.logback:logback-classic:$logback_version")

Expand Down
1 change: 1 addition & 0 deletions backend/src/main/kotlin/com/shoppinglist/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fun Application.module() {
DatabaseFactory.init()
configureSerialization()
configureCORS()
configureMetrics()

val pushRepository = PushSubscriptionRepositoryImpl()
val pushService = runCatching {
Expand Down
17 changes: 0 additions & 17 deletions backend/src/main/kotlin/com/shoppinglist/models/HealthResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,4 @@ data class HealthResponse(
data class ErrorResponse(
val status: String,
val message: String
)

@Serializable
data class MemoryInfo(
val total: Long,
val free: Long,
val used: Long,
val max: Long
)

@Serializable
data class MetricsResponse(
val timestamp: Long,
val uptime: Long,
val memory: MemoryInfo,
val threads: Int,
val database: String
)
62 changes: 62 additions & 0 deletions backend/src/main/kotlin/com/shoppinglist/plugins/Metrics.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.shoppinglist.plugins

import com.shoppinglist.database.ShoppingLists
import io.ktor.server.application.*
import io.ktor.server.metrics.micrometer.*
import io.micrometer.core.instrument.Gauge
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics
import io.micrometer.core.instrument.binder.system.ProcessorMetrics
import io.micrometer.prometheus.PrometheusConfig
import io.micrometer.prometheus.PrometheusMeterRegistry
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import java.util.concurrent.atomic.AtomicLong

lateinit var appMicrometerRegistry: PrometheusMeterRegistry
private set

// Falls back to last known value when the DB query fails inside the gauge supplier
private val cachedListCount = AtomicLong(0)

fun Application.configureMetrics() {
appMicrometerRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)

install(MicrometerMetrics) {
registry = appMicrometerRegistry
meterBinders = listOf(
JvmMemoryMetrics(),
JvmGcMetrics(),
JvmThreadMetrics(),
ProcessorMetrics()
)
}

Gauge.builder("shoppimo_websocket_connections_total", this) { app ->
try {
val wsService = app.attributes.getOrNull(WebSocketServiceKey)
wsService?.getConnectionManager()
?.getActiveListIds()
?.sumOf { listId -> wsService.getConnectionManager().getConnectionCount(listId) }
?.toDouble()
?: 0.0
} catch (_: Exception) {
0.0
}
}
.description("Total number of active WebSocket connections across all lists")
.register(appMicrometerRegistry)

Gauge.builder("shoppimo_shopping_lists_total", cachedListCount) { cached ->
try {
val count = transaction { ShoppingLists.selectAll().count() }
cached.set(count)
count.toDouble()
} catch (_: Exception) {
cached.get().toDouble()
}
}
.description("Total number of shopping lists in the database")
.register(appMicrometerRegistry)
}
36 changes: 2 additions & 34 deletions backend/src/main/kotlin/com/shoppinglist/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import com.shoppinglist.routes.listRoutes
import com.shoppinglist.routes.pushRoutes
import com.shoppinglist.models.HealthResponse
import com.shoppinglist.models.ErrorResponse
import com.shoppinglist.models.MetricsResponse
import com.shoppinglist.models.MemoryInfo

import com.shoppinglist.repository.PushSubscriptionRepository
import com.shoppinglist.services.PushNotificationService

Expand Down Expand Up @@ -52,38 +51,7 @@ fun Application.configureRouting(
}

get("/metrics") {
try {
val runtime = Runtime.getRuntime()
val memoryInfo = MemoryInfo(
total = runtime.totalMemory(),
free = runtime.freeMemory(),
used = runtime.totalMemory() - runtime.freeMemory(),
max = runtime.maxMemory()
)

val metrics = MetricsResponse(
timestamp = System.currentTimeMillis(),
uptime = java.lang.management.ManagementFactory.getRuntimeMXBean().uptime,
memory = memoryInfo,
threads = java.lang.management.ManagementFactory.getThreadMXBean().threadCount,
database = try {
com.shoppinglist.database.DatabaseFactory.testConnection()
"connected"
} catch (e: Exception) {
"disconnected"
}
)

call.respond(metrics)
} catch (e: Exception) {
call.respond(
status = HttpStatusCode.InternalServerError,
message = ErrorResponse(
status = "ERROR",
message = e.message ?: "Unknown error"
)
)
}
call.respond(appMicrometerRegistry.scrape())
}

if (pushRepository != null) {
Expand Down
Loading