working example of a metriced-list

This commit is contained in:
Ben 2025-02-05 14:05:18 +01:00
parent d2eac93c0f
commit faa9d2f4fd
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
4 changed files with 34 additions and 1 deletions

View File

@ -10,7 +10,7 @@ Usage:
Options:
--logLevel=<l> Sets log level [default: TRACE]
--metricsAddress=<ip> Listen address of the metrics server [default: 127.0.0.1]
--metricsAddress=<ip> Listen address of the metrics server [default: 0.0.0.0]
--metricsPort=<p> Listen HTTP port of the metrics server [default: 8008]
--dataDir=<dir> Directory for storing data [default: crawler_data]
--discoveryPort=<p> Port used for DHT [default: 8090]

20
codexcrawler/list.nim Normal file
View File

@ -0,0 +1,20 @@
import pkg/metrics
type
OnUpdateMetric = proc(value: int64): void {.gcsafe, raises:[].}
List*[T] = ref object
items: seq[T]
onMetric: OnUpdateMetric
proc new*[T](
_: type List[T],
onMetric: OnUpdateMetric
): List[T] =
List[T](
items: newSeq[T](),
onMetric: onMetric
)
proc add*[T](this: List[T], item: T) =
this.items.add(item)
this.onMetric(this.items.len.int64)

View File

@ -1,14 +1,26 @@
import pkg/chronicles
import pkg/chronos
import pkg/metrics
import ./list
logScope:
topics = "main"
declareGauge(example, "testing")
proc startApplication*() {.async.} =
proc onExampleMetric(value: int64) =
example.set(value)
var exampleList = List[string].new(onExampleMetric)
proc aaa() {.async.} =
while true:
notice "a"
await sleepAsync(1000)
exampleList.add("str!")
asyncSpawn aaa()

1
config.nims Normal file
View File

@ -0,0 +1 @@
--define:metrics