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
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ class JvmLogger : PlatformLogger {
}
}

override fun stats(msg: String, vararg args: Any?) {
if (logger.isInfoEnabled) logger.info(msg, *args)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand All @@ -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>): String = when (values.size) {
0 -> "-"
1 -> values[0]
else -> "MIXED"
}

/**
* Reset the session into the initial state.
*/
Expand Down
Loading