mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-07-30 20:13:20 +00:00
* refactor(metrics): prefix node metrics with logos_delivery_
Every metric the node exports now starts with logos_delivery_. There was no
prefix mechanism before: nim-metrics derives the exported name from the Nim
identifier, no declaration passed an explicit `name = "..."`, and the waku_
convention was maintained by hand -- 69 of the 80 node metrics followed it and
11 did not (query_count, query_time_secs, event_loop_load,
event_loop_accumulated_lag_secs, postgres_payload_size_bytes, reconciliation_*,
total_* and the camelCase rendezvousPeerFoundTotal).
Identifiers are renamed rather than given a `name = "..."` argument, keeping the
invariant that the Nim identifier is the exported name and letting the compiler
check every call site.
rendezvousPeerFoundTotal becomes logos_delivery_rendezvous_peer_found: it was the
only camelCase metric, and the trailing Total was redundant since nim-metrics
already appends _total to counters at exposition time.
library/ is untouched on purpose -- `proc waku_version()` in
kernel_api/debug_node_api.nim is the exported libwaku C ABI symbol, not the gauge
of the same name in node_telemetry.nim.
BREAKING CHANGE: metric names change. Dashboards, alert rules and recording rules
that reference waku_* must be updated; see docs/operators/how-to/monitor.md.
* refactor(metrics): prefix auxiliary app metrics with logos_delivery_
Applies the same prefix to the tools shipped from this repo: liteprotocoltester
(lpt_*), networkmonitor (networkmonitor_*), chat2bridge (chat2_*) and the
lightpush_mix example (lp_mix_*).
These tools are not the delivery node and already had their own consistent
prefixes, so this commit is separable from the node rename if the intent was to
namespace only the node itself.
* chore(metrics): query old and new metric names in Grafana dashboards
212 expressions across 9 dashboards now match both the waku_* and the
logos_delivery_* spelling, so panels keep working across the upgrade and over
historical data:
sum by (type)((increase(waku_node_errors_total{...}[$__rate_interval])
or increase(logos_delivery_node_errors_total{...}[$__rate_interval])))
The `or` is placed around the leaf, inside every aggregation. That depth is
load-bearing: `or` keeps its right operand only for label sets absent from the
left, so `sum by (type)(old) or sum by (type)(new)` aggregates each half of the
fleet separately and then discards the right one entirely -- silently dropping
every already-upgraded node. 36 panels here collapse `instance`.
Measured against a local Prometheus scraping two targets, one exporting old names
at 10/s and one exporting new names at 20/s (truth 30/s): union outside the
aggregation gives 10, union around the leaf gives 30.
Where the leaf sits in a range vector the whole call is duplicated, since
`(a or b)[5m]` is not valid PromQL.
Once every scraped node runs a release with the new names and the old samples
have aged out of retention, the `or` half can be deleted.
* test(e2e): expect logos_delivery_-prefixed metric names
The e2e suite asserts against a live /metrics endpoint, which serves only the new
names, so these are replaced rather than unioned. libp2p_* entries are unchanged.
* docs(operators): document the logos_delivery_ metric prefix
Records that every metric the node exports is prefixed, that dependency metrics
(libp2p_*, nim_gc_*, process_*) keep their own names, and shows where the `or`
has to sit if operators maintain their own dashboards or alert rules.
* refactor(metrics): name the store fleet metrics after store, not relay
logos_delivery_relay_fleet_store_msg_size_bytes and _msg_count are declared in
waku_store/protocol_metrics.nim and recorded by the store client, but carried a
relay prefix. Renamed to logos_delivery_store_fleet_msg_size_bytes and
logos_delivery_store_fleet_msg_count.
The dashboard keeps matching the old exported name, which was
waku_relay_fleet_store_*.
Note that both metrics are wrong independently of their name, see the PR
description.
337 lines
11 KiB
Nim
337 lines
11 KiB
Nim
{.push raises: [].}
|
|
|
|
import
|
|
std/[sets, tables, sequtils, strformat],
|
|
chronos/timer as chtimer,
|
|
chronicles,
|
|
chronos,
|
|
results,
|
|
libp2p/peerid
|
|
|
|
from std/sugar import `=>`
|
|
|
|
import ./tester_message, ./lpt_metrics
|
|
|
|
type
|
|
ArrivalInfo = object
|
|
arrivedAt: Moment
|
|
prevArrivedAt: Moment
|
|
prevIndex: uint32
|
|
|
|
MessageInfo = tuple[msg: ProtocolTesterMessage, info: ArrivalInfo]
|
|
DupStat = tuple[hash: string, dupCount: int, size: uint64]
|
|
|
|
StatHelper = object
|
|
prevIndex: uint32
|
|
prevArrivedAt: Moment
|
|
lostIndices: HashSet[uint32]
|
|
seenIndices: HashSet[uint32]
|
|
maxIndex: uint32
|
|
duplicates: OrderedTable[uint32, DupStat]
|
|
|
|
Statistics* = object
|
|
received: Table[uint32, MessageInfo]
|
|
firstReceivedIdx*: uint32
|
|
allMessageCount*: uint32
|
|
receivedMessages*: uint32
|
|
misorderCount*: uint32
|
|
lateCount*: uint32
|
|
duplicateCount*: uint32
|
|
helper: StatHelper
|
|
|
|
PerPeerStatistics* = Table[string, Statistics]
|
|
|
|
func `$`*(a: Duration): string {.inline.} =
|
|
## Original stringify implementation from chronos/timer.nim is not capable of printing 0ns
|
|
## Returns string representation of Duration ``a`` as nanoseconds value.
|
|
|
|
if a.isZero:
|
|
return "0ns"
|
|
|
|
return chtimer.`$`(a)
|
|
|
|
proc init*(T: type Statistics, expectedMessageCount: int = 1000): T =
|
|
result.helper.prevIndex = 0
|
|
result.helper.maxIndex = 0
|
|
result.helper.seenIndices.init(expectedMessageCount)
|
|
result.received = initTable[uint32, MessageInfo](expectedMessageCount)
|
|
return result
|
|
|
|
proc addMessage*(
|
|
self: var Statistics, sender: string, msg: ProtocolTesterMessage, msgHash: string
|
|
) =
|
|
if self.allMessageCount == 0:
|
|
self.allMessageCount = msg.count
|
|
self.firstReceivedIdx = msg.index
|
|
elif self.allMessageCount != msg.count:
|
|
error "Message count mismatch at message",
|
|
index = msg.index, expected = self.allMessageCount, got = msg.count
|
|
|
|
let currentArrived: MessageInfo = (
|
|
msg: msg,
|
|
info: ArrivalInfo(
|
|
arrivedAt: Moment.now(),
|
|
prevArrivedAt: self.helper.prevArrivedAt,
|
|
prevIndex: self.helper.prevIndex,
|
|
),
|
|
)
|
|
logos_delivery_lpt_receiver_received_bytes.inc(
|
|
labelValues = [sender], amount = msg.size.int64
|
|
)
|
|
if self.received.hasKeyOrPut(msg.index, currentArrived):
|
|
inc(self.duplicateCount)
|
|
self.helper.duplicates.mgetOrPut(msg.index, (msgHash, 0, msg.size)).dupCount.inc()
|
|
warn "Duplicate message",
|
|
index = msg.index,
|
|
hash = msgHash,
|
|
times_duplicated = self.helper.duplicates[msg.index].dupCount
|
|
logos_delivery_lpt_receiver_duplicate_messages_count.inc(labelValues = [sender])
|
|
logos_delivery_lpt_receiver_distinct_duplicate_messages_count.set(
|
|
labelValues = [sender], value = self.helper.duplicates.len()
|
|
)
|
|
return
|
|
|
|
## detect misorder arrival and possible lost messages
|
|
if self.helper.prevIndex + 1 < msg.index:
|
|
inc(self.misorderCount)
|
|
warn "Misordered message arrival",
|
|
index = msg.index, expected = self.helper.prevIndex + 1
|
|
elif self.helper.prevIndex > msg.index:
|
|
inc(self.lateCount)
|
|
warn "Late message arrival", index = msg.index, expected = self.helper.prevIndex + 1
|
|
|
|
self.helper.maxIndex = max(self.helper.maxIndex, msg.index)
|
|
self.helper.prevIndex = msg.index
|
|
self.helper.prevArrivedAt = currentArrived.info.arrivedAt
|
|
inc(self.receivedMessages)
|
|
logos_delivery_lpt_receiver_received_messages_count.inc(labelValues = [sender])
|
|
logos_delivery_lpt_receiver_missing_messages_count.set(
|
|
labelValues = [sender], value = (self.helper.maxIndex - self.receivedMessages).int64
|
|
)
|
|
|
|
proc addMessage*(
|
|
self: var PerPeerStatistics,
|
|
peerId: string,
|
|
msg: ProtocolTesterMessage,
|
|
msgHash: string,
|
|
) =
|
|
if not self.contains(peerId):
|
|
self[peerId] = Statistics.init()
|
|
|
|
let shortSenderId = PeerId.init(msg.sender).map(p => p.shortLog()).valueOr(msg.sender)
|
|
|
|
discard catch:
|
|
self[peerId].addMessage(shortSenderId, msg, msgHash)
|
|
|
|
logos_delivery_lpt_receiver_sender_peer_count.set(value = self.len)
|
|
|
|
proc lastMessageArrivedAt*(self: Statistics): Opt[Moment] =
|
|
if self.receivedMessages > 0:
|
|
return Opt.some(self.helper.prevArrivedAt)
|
|
return Opt.none(Moment)
|
|
|
|
proc lossCount*(self: Statistics): uint32 =
|
|
self.helper.maxIndex - self.receivedMessages
|
|
|
|
proc calcLatency*(self: Statistics): tuple[min, max, avg: Duration] =
|
|
var
|
|
minLatency = nanos(0)
|
|
maxLatency = nanos(0)
|
|
avgLatency = nanos(0)
|
|
|
|
if self.receivedMessages > 2:
|
|
try:
|
|
var prevArrivedAt = self.received[self.firstReceivedIdx].info.arrivedAt
|
|
|
|
for idx, (msg, arrival) in self.received.pairs:
|
|
if idx <= 1:
|
|
continue
|
|
let expectedDelay = nanos(msg.sincePrev)
|
|
|
|
## latency will be 0 if arrived in shorter time than expected
|
|
var latency = arrival.arrivedAt - arrival.prevArrivedAt - expectedDelay
|
|
|
|
## will not measure zero latency, it is unlikely to happen but in case happens could
|
|
## ditort the min latency calulculation as we want to calculate the feasible minimum.
|
|
if latency > nanos(0):
|
|
if minLatency == nanos(0):
|
|
minLatency = latency
|
|
else:
|
|
minLatency = min(minLatency, latency)
|
|
|
|
maxLatency = max(maxLatency, latency)
|
|
avgLatency += latency
|
|
|
|
avgLatency = avgLatency div (self.receivedMessages - 1)
|
|
except KeyError:
|
|
error "Error while calculating latency: " & getCurrentExceptionMsg()
|
|
|
|
return (minLatency, maxLatency, avgLatency)
|
|
|
|
proc missingIndices*(self: Statistics): seq[uint32] =
|
|
var missing: seq[uint32] = @[]
|
|
for idx in 1 .. self.helper.maxIndex:
|
|
if not self.received.hasKey(idx):
|
|
missing.add(idx)
|
|
return missing
|
|
|
|
proc distinctDupCount(self: Statistics): int {.inline.} =
|
|
return self.helper.duplicates.len()
|
|
|
|
proc allDuplicates(self: Statistics): int {.inline.} =
|
|
var total = 0
|
|
for _, (_, dupCount, _) in self.helper.duplicates.pairs:
|
|
total += dupCount
|
|
return total
|
|
|
|
proc dupMsgs(self: Statistics): string =
|
|
var dupMsgs: string = ""
|
|
for idx, (hash, dupCount, size) in self.helper.duplicates.pairs:
|
|
dupMsgs.add(
|
|
" index: " & $idx & " | hash: " & hash & " | count: " & $dupCount & " | size: " &
|
|
$size & "\n"
|
|
)
|
|
return dupMsgs
|
|
|
|
proc echoStat*(self: Statistics, peerId: string) =
|
|
let (minL, maxL, avgL) = self.calcLatency()
|
|
logos_delivery_lpt_receiver_latencies.set(
|
|
labelValues = [peerId, "min"], value = minL.nanos()
|
|
)
|
|
logos_delivery_lpt_receiver_latencies.set(
|
|
labelValues = [peerId, "avg"], value = avgL.nanos()
|
|
)
|
|
logos_delivery_lpt_receiver_latencies.set(
|
|
labelValues = [peerId, "max"], value = maxL.nanos()
|
|
)
|
|
|
|
let printable = catch:
|
|
"""*------------------------------------------------------------------------------------------*
|
|
| Expected | Received | Target | Loss | Misorder | Late | |
|
|
|{self.helper.maxIndex:>11} |{self.receivedMessages:>11} |{self.allMessageCount:>11} |{self.lossCount():>11} |{self.misorderCount:>11} |{self.lateCount:>11} | |
|
|
*------------------------------------------------------------------------------------------*
|
|
| Latency stat: |
|
|
| min latency: {$minL:<73}|
|
|
| avg latency: {$avgL:<73}|
|
|
| max latency: {$maxL:<73}|
|
|
*------------------------------------------------------------------------------------------*
|
|
| Duplicate stat: |
|
|
| distinct duplicate messages: {$self.distinctDupCount():<57}|
|
|
| sum duplicates : {$self.allDuplicates():<57}|
|
|
Duplicated messages:
|
|
{self.dupMsgs()}
|
|
*------------------------------------------------------------------------------------------*
|
|
| Lost indices: |
|
|
| {self.missingIndices()} |
|
|
*------------------------------------------------------------------------------------------*""".fmt()
|
|
|
|
echo printable.valueOr("Error while printing statistics: " & error.msg)
|
|
|
|
proc jsonStat*(self: Statistics): string =
|
|
let minL, maxL, avgL = self.calcLatency()
|
|
|
|
let json = catch:
|
|
"""{{"expected":{self.helper.maxIndex},
|
|
"received": {self.receivedMessages},
|
|
"target": {self.allMessageCount},
|
|
"loss": {self.lossCount()},
|
|
"misorder": {self.misorderCount},
|
|
"late": {self.lateCount},
|
|
"duplicate": {self.duplicateCount},
|
|
"latency":
|
|
{{"avg": "{avgL}",
|
|
"min": "{minL}",
|
|
"max": "{maxL}"
|
|
}},
|
|
"lostIndices": {self.missingIndices()}
|
|
}}""".fmt()
|
|
|
|
return json.valueOr("{\"result:\": \"" & error.msg & "\"}")
|
|
|
|
proc echoStats*(self: var PerPeerStatistics) =
|
|
for peerId, stats in self.pairs:
|
|
let peerLine = catch:
|
|
"Receiver statistics from peer {peerId}".fmt()
|
|
peerLine.isOkOr:
|
|
echo "Error while printing statistics"
|
|
continue
|
|
echo peerLine.get()
|
|
stats.echoStat(peerId)
|
|
|
|
proc jsonStats*(self: PerPeerStatistics): string =
|
|
try:
|
|
#!fmt: off
|
|
var json = "{\"statistics\": ["
|
|
var first = true
|
|
for peerId, stats in self.pairs:
|
|
if first:
|
|
first = false
|
|
else:
|
|
json.add(", ")
|
|
json.add("{{\"sender\": \"{peerId}\", \"stat\":".fmt())
|
|
json.add(stats.jsonStat())
|
|
json.add("}")
|
|
json.add("]}")
|
|
return json
|
|
#!fmt: on
|
|
except CatchableError:
|
|
return
|
|
"{\"result:\": \"Error while generating json stats: " & getCurrentExceptionMsg() &
|
|
"\"}"
|
|
|
|
proc lastMessageArrivedAt*(self: PerPeerStatistics): Opt[Moment] =
|
|
var lastArrivedAt = Moment.init(0, Millisecond)
|
|
for stat in self.values:
|
|
let lastMsgFromPeerAt = stat.lastMessageArrivedAt().valueOr:
|
|
continue
|
|
|
|
if lastMsgFromPeerAt > lastArrivedAt:
|
|
lastArrivedAt = lastMsgFromPeerAt
|
|
|
|
if lastArrivedAt == Moment.init(0, Millisecond):
|
|
return Opt.none(Moment)
|
|
|
|
return Opt.some(lastArrivedAt)
|
|
|
|
proc checkIfAllMessagesReceived*(
|
|
self: PerPeerStatistics, maxWaitForLastMessage: Duration
|
|
): Future[bool] {.async.} =
|
|
# if there are no peers have sent messages, assume we just have started.
|
|
if self.len == 0:
|
|
return false
|
|
|
|
# check if numerically all messages are received.
|
|
# this suggest we received at least one message already from one peer
|
|
var isAlllMessageReceived = true
|
|
for stat in self.values:
|
|
if (stat.allMessageCount == 0 and stat.receivedMessages == 0) or
|
|
stat.helper.maxIndex < stat.allMessageCount:
|
|
isAlllMessageReceived = false
|
|
break
|
|
|
|
if not isAlllMessageReceived:
|
|
# if not all message received we still need to check if last message arrived within a time frame
|
|
# to avoid endless waiting while publishers are already quit.
|
|
let lastMessageAt = self.lastMessageArrivedAt()
|
|
if lastMessageAt.isNone():
|
|
return false
|
|
|
|
# last message shall arrived within time limit
|
|
if Moment.now() - lastMessageAt.get() < maxWaitForLastMessage:
|
|
return false
|
|
else:
|
|
info "No message since max wait time", maxWait = $maxWaitForLastMessage
|
|
|
|
## Ok, we see last message arrived from all peers,
|
|
## lets check if all messages are received
|
|
## and if not let's wait another 20 secs to give chance the system will send them.
|
|
var shallWait = false
|
|
for stat in self.values:
|
|
if stat.receivedMessages < stat.allMessageCount:
|
|
shallWait = true
|
|
|
|
if shallWait:
|
|
await sleepAsync(20.seconds)
|
|
|
|
return true
|