Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support nim-metrics 0.1.0+ #651

Merged
merged 1 commit into from
Jan 5, 2024
Merged
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
72 changes: 40 additions & 32 deletions eth/db/kvstore_sqlite3.nim
Original file line number Diff line number Diff line change
Expand Up @@ -712,35 +712,43 @@ proc registerCustomScalarFunction*(db: SqStoreRef, name: string, fun: CustomFunc
ok()

when defined(metrics):
import locks, tables, times,
chronicles, 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.lock.initLock()
result.register(registry)

var sqlite3Info* {.global.} = newSqlite3Info("sqlite3_info", "SQLite3 info")

method collect*(collector: Sqlite3Info): Metrics =
result = initOrderedTable[Labels, seq[Metric]]()
result[@[]] = @[]
let timestamp = getTime().toMilliseconds()
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:
result[@[]] = @[
Metric(
name: "sqlite3_memory_used_bytes",
value: currentMem.float64,
timestamp: timestamp,
),
]
import chronicles, metrics

type Sqlite3Info = ref object of Collector

proc newSqlite3Info*(name: string, help: string, registry = defaultRegistry): Sqlite3Info {.raises: [CatchableError].} =
Sqlite3Info.newCollector(name, help, registry = registry)

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[@[]] = @[]
let timestamp = getTime().toMilliseconds()
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:
result[@[]] = @[
Metric(
name: "sqlite3_memory_used_bytes",
value: currentMem.float64,
timestamp: timestamp,
),
]
Loading