support nim-metrics 0.1.0+ (#651)

This commit is contained in:
Jacek Sieka 2024-01-05 15:17:27 +01:00 committed by GitHub
parent 5aaeb67ebe
commit 0fc5dc03c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 27 deletions

View File

@ -694,22 +694,30 @@ proc createCustomFunction*(
ok()
when defined(metrics):
import locks, tables, times,
chronicles, metrics
import chronicles, metrics
type Sqlite3Info = ref object of Gauge
type Sqlite3Info = ref object of Collector
proc newSqlite3Info*(name: string, help: string, registry = defaultRegistry): Sqlite3Info {.raises: [Exception].} =
validateName(name)
result = Sqlite3Info(name: name,
help: help,
typ: "gauge",
creationThreadId: getThreadId())
result.lock.initLock()
result.register(registry)
proc newSqlite3Info*(name: string, help: string, registry = defaultRegistry): Sqlite3Info {.raises: [CatchableError].} =
Sqlite3Info.newCollector(name, help, registry = registry)
var sqlite3Info* {.global.} = newSqlite3Info("sqlite3_info", "SQLite3 info")
let sqlite3Info* = newSqlite3Info("sqlite3_info", "SQLite3 info")
when declared(MetricHandler): # nim-metrics 0.1.0+
method collect*(collector: Sqlite3Info, output: MetricHandler) =
let timestamp = collector.now()
var currentMem, highwaterMem: int64
if (let res = sqlite3_status64(SQLITE_STATUS_MEMORY_USED, currentMem.addr, highwaterMem.addr, 0); res != SQLITE_OK):
error "SQLite3 error", msg = sqlite3_errstr(res)
else:
output(
name = "sqlite3_memory_used_bytes",
value = currentMem.float64,
timestamp = timestamp,
)
else: # nim-metrics 0.0.1
method collect*(collector: Sqlite3Info): Metrics =
result = initOrderedTable[Labels, seq[Metric]]()
result[@[]] = @[]