src/wsabi/stats.nim (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# parse xml stanzas and send stats to statsd import xmltree, xmlparser, asyncdispatch, asyncnet, nativesockets, strformat, tables var statsd: AsyncSocket clientAddr: string counter = initCountTable[string]() const prefix = "wsabi.dev." let tags = @["message", "auth", "success", "failure"] proc connectStatsd() {.async.} = statsd = await asyncnet.dial("127.0.0.1", 8125.Port, IPPROTO_UDP) proc resetCounter(fd: AsyncFD): bool {.gcsafe.} = for tag in tags: if counter[tag] != 0: waitFor statsd.send(&"{prefix}{tag}:{counter[tag]}|c") counter[tag] = 0 proc parseStanza*(stanza: string) {.async.} = try: let p = parseXml(stanza) try: if p.tag in tags: counter.inc(p.tag) addTimer(60_000, false, resetCounter) except OSError: echo "unable to reach statsd" except XmlError: discard asyncCheck connectStatsd() |