From 9da92b5415928100167f9eddfeb8158932d8b960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C8=98tefan=20Talpalaru?= Date: Sat, 9 May 2020 01:30:50 +0200 Subject: [PATCH] SQLite3 mem usage metric --- eth/db/kvstore_sqlite3.nim | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/eth/db/kvstore_sqlite3.nim b/eth/db/kvstore_sqlite3.nim index dcce9d5..23102f3 100644 --- a/eth/db/kvstore_sqlite3.nim +++ b/eth/db/kvstore_sqlite3.nim @@ -3,8 +3,8 @@ {.push raises: [Defect].} import - os, - sqlite3_abi, + os, tables, times, + chronicles, metrics, sqlite3_abi, ./kvstore export kvstore @@ -162,3 +162,35 @@ proc init*( delStmt: delStmt, containsStmt: containsStmt )) + +when defined(metrics): + type Sqlite3Info = ref object of Gauge + + proc newSqlite3Info*(name: string, help: string, registry = defaultRegistry): Sqlite3Info {.raises: [Exception].} = + validateName(name) + result = Sqlite3Info(name: name, + help: help, + typ: "gauge", + creationThreadId: getThreadId()) + result.register(registry) + + var sqlite3Info* {.global.} = newSqlite3Info("sqlite3_info", "SQLite3 info") + + method collect*(collector: Sqlite3Info): Metrics = + result = initOrderedTable[Labels, seq[Metric]]() + result[@[]] = @[] + var + timestamp = getTime().toMilliseconds() + currentMem, highwaterMem: cint + + if (let res = sqlite3_status(SQLITE_STATUS_MEMORY_USED, currentMem.addr, highwaterMem.addr, 0); res != SQLITE_OK): + error "SQLite3 error", msg = sqlite3_errstr(res) + else: + result[@[]] = @[ + Metric( + name: "sqlite3_memory_used_bytes", + value: currentMem.float64, + timestamp: timestamp, + ), + ] +