diff --git a/here-naksha-lib-base/src/commonMain/kotlin/naksha/base/PlatformLogger.kt b/here-naksha-lib-base/src/commonMain/kotlin/naksha/base/PlatformLogger.kt index 7330946df..d7b3dd404 100644 --- a/here-naksha-lib-base/src/commonMain/kotlin/naksha/base/PlatformLogger.kt +++ b/here-naksha-lib-base/src/commonMain/kotlin/naksha/base/PlatformLogger.kt @@ -63,4 +63,10 @@ interface PlatformLogger { * final message to log or _null_ to suppress logging for some other reason. */ fun atError(msgFn: () -> String?) + + /** + * Logs statistics (e.g. `StorageReqStats`) used by monitoring dashboards, independent of [PlatformUtil.ENABLE_INFO]. + * On JVM it is logged at INFO level, on JS (PLV8) it is a no-op. + */ + fun stats(msg: String, vararg args: Any?) } \ No newline at end of file diff --git a/here-naksha-lib-base/src/jsMain/kotlin/naksha/base/JsLogger.kt b/here-naksha-lib-base/src/jsMain/kotlin/naksha/base/JsLogger.kt index 9c63c4047..ec6f455a6 100644 --- a/here-naksha-lib-base/src/jsMain/kotlin/naksha/base/JsLogger.kt +++ b/here-naksha-lib-base/src/jsMain/kotlin/naksha/base/JsLogger.kt @@ -96,4 +96,8 @@ internal class JsLogger : PlatformLogger { val m = msgFn.invoke() if (plv8 != null) plv8.log(_ERROR, m) else console.info(m) } + + override fun stats(msg: String, vararg args: Any?) { + // Statistics are only collected on the JVM. + } } \ No newline at end of file diff --git a/here-naksha-lib-base/src/jvmMain/kotlin/naksha/base/JvmLogger.kt b/here-naksha-lib-base/src/jvmMain/kotlin/naksha/base/JvmLogger.kt index d31c8ff65..60ea139d4 100644 --- a/here-naksha-lib-base/src/jvmMain/kotlin/naksha/base/JvmLogger.kt +++ b/here-naksha-lib-base/src/jvmMain/kotlin/naksha/base/JvmLogger.kt @@ -50,4 +50,8 @@ class JvmLogger : PlatformLogger { } } + override fun stats(msg: String, vararg args: Any?) { + if (logger.isInfoEnabled) logger.info(msg, *args) + } + } \ No newline at end of file diff --git a/here-naksha-lib-psql/src/commonMain/kotlin/naksha/psql/PgSession.kt b/here-naksha-lib-psql/src/commonMain/kotlin/naksha/psql/PgSession.kt index 159f34977..33bca1287 100644 --- a/here-naksha-lib-psql/src/commonMain/kotlin/naksha/psql/PgSession.kt +++ b/here-naksha-lib-psql/src/commonMain/kotlin/naksha/psql/PgSession.kt @@ -260,6 +260,8 @@ open class PgSession( override fun executeWrite(request: WriteRequest): Response = execute(request) @Deprecated("Please use executeRead or executeWrite", level = DeprecationLevel.WARNING) override fun execute(request: Request): Response { + val startMillis = Platform.currentMillis() + var status = "NOK" try { val response = when (request) { is WriteRequest -> { @@ -281,15 +283,47 @@ open class PgSession( } if (response is SuccessResponse) { response.filterResults(*request.resultFilters.filterNotNull().toTypedArray()) + status = "OK" } return response } catch (t: Throwable) { logger.info("PgSession: execute failed", t) val nakshaException = PgExceptionMapper.map(t) return ErrorResponse(nakshaException.error) + } finally { + if (request is WriteRequest) logStorageReqStats(request, status, Platform.currentMillis() - startMillis) } } + /** + * Logs the write request stats. + */ + private fun logStorageReqStats(request: WriteRequest, status: String, timeTakenMs: Long) { + val writes = request.writes.filterNotNull() + val ops = writes.map { it.op.toString() }.distinct() + val ftypes = writes.map { if (it.isCollectionModification()) "Collection" else "Feature" }.distinct() + val collectionIds = writes.filter { !it.isCollectionModification() }.map { it.collectionId ?: "-" }.distinct() + val host = storage.config.master.host + logger.stats( + "[Storage Request stats => type,storageId,host,method,ftype,fCnt,collectionId,status,timeTakenMs] - StorageReqStats {} {} {} {} {} {} {} {} {}", + "PsqlStorage", + storage.id, + host, + mixedOrSingle(ops), + mixedOrSingle(ftypes), + writes.size, + mixedOrSingle(collectionIds), + status, + timeTakenMs + ) + } + + private fun mixedOrSingle(values: List): String = when (values.size) { + 0 -> "-" + 1 -> values[0] + else -> "MIXED" + } + /** * Reset the session into the initial state. */