2024-06-28 10:34:57 +00:00
|
|
|
|
{.push raises: [].}
|
2022-12-05 19:02:21 +00:00
|
|
|
|
|
2022-11-10 09:29:34 +00:00
|
|
|
|
import
|
2024-08-07 18:58:28 +00:00
|
|
|
|
std/[net, json, tables, sequtils],
|
2022-11-10 09:29:34 +00:00
|
|
|
|
chronicles,
|
|
|
|
|
chronicles/topics_registry,
|
|
|
|
|
chronos,
|
|
|
|
|
json_serialization,
|
|
|
|
|
metrics,
|
|
|
|
|
metrics/chronos_httpserver,
|
|
|
|
|
presto/route,
|
|
|
|
|
presto/server,
|
2024-08-07 18:58:28 +00:00
|
|
|
|
results
|
2022-11-10 09:29:34 +00:00
|
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
|
topics = "networkmonitor_metrics"
|
|
|
|
|
|
|
|
|
|
# On top of our custom metrics, the following are reused from nim-eth
|
|
|
|
|
#routing_table_nodes{state=""}
|
|
|
|
|
#routing_table_nodes{state="seen"}
|
|
|
|
|
#discovery_message_requests_outgoing_total{response=""}
|
|
|
|
|
#discovery_message_requests_outgoing_total{response="no_response"}
|
|
|
|
|
|
2023-09-28 08:07:27 +00:00
|
|
|
|
declarePublicGauge networkmonitor_peer_type_as_per_enr,
|
2024-03-15 23:08:47 +00:00
|
|
|
|
"Number of peers supporting each capability according to the ENR",
|
|
|
|
|
labels = ["capability"]
|
2022-11-10 09:29:34 +00:00
|
|
|
|
|
2024-08-07 18:58:28 +00:00
|
|
|
|
declarePublicGauge networkmonitor_peer_cluster_as_per_enr,
|
|
|
|
|
"Number of peers on each cluster according to the ENR", labels = ["cluster"]
|
|
|
|
|
|
2023-09-28 08:07:27 +00:00
|
|
|
|
declarePublicGauge networkmonitor_peer_type_as_per_protocol,
|
2024-03-15 23:08:47 +00:00
|
|
|
|
"Number of peers supporting each protocol, after a successful connection) ",
|
|
|
|
|
labels = ["protocols"]
|
2022-11-10 09:29:34 +00:00
|
|
|
|
|
2023-09-28 08:07:27 +00:00
|
|
|
|
declarePublicGauge networkmonitor_peer_user_agents,
|
2024-03-15 23:08:47 +00:00
|
|
|
|
"Number of peers with each user agent", labels = ["user_agent"]
|
2022-11-10 09:29:34 +00:00
|
|
|
|
|
2023-09-28 08:07:27 +00:00
|
|
|
|
declarePublicHistogram networkmonitor_peer_ping,
|
2024-03-15 23:08:47 +00:00
|
|
|
|
"Histogram tracking ping durations for discovered peers",
|
2024-11-08 07:59:02 +00:00
|
|
|
|
buckets = [10.0, 20.0, 50.0, 100.0, 200.0, 300.0, 500.0, 800.0, 1000.0, 2000.0, Inf]
|
2023-09-25 12:38:59 +00:00
|
|
|
|
|
2023-09-28 08:07:27 +00:00
|
|
|
|
declarePublicGauge networkmonitor_peer_count,
|
2024-03-15 23:08:47 +00:00
|
|
|
|
"Number of discovered peers", labels = ["connected"]
|
2023-09-28 08:07:27 +00:00
|
|
|
|
|
|
|
|
|
declarePublicGauge networkmonitor_peer_country_count,
|
2024-03-15 23:08:47 +00:00
|
|
|
|
"Number of peers per country", labels = ["country"]
|
2023-09-28 08:07:27 +00:00
|
|
|
|
|
2022-11-10 09:29:34 +00:00
|
|
|
|
type
|
2024-03-15 23:08:47 +00:00
|
|
|
|
CustomPeerInfo* = object # populated after discovery
|
2023-09-25 12:38:59 +00:00
|
|
|
|
lastTimeDiscovered*: int64
|
|
|
|
|
discovered*: int64
|
2022-11-10 09:29:34 +00:00
|
|
|
|
peerId*: string
|
|
|
|
|
enr*: string
|
|
|
|
|
ip*: string
|
|
|
|
|
enrCapabilities*: seq[string]
|
|
|
|
|
country*: string
|
|
|
|
|
city*: string
|
2024-07-30 13:56:49 +00:00
|
|
|
|
maddrs*: seq[string]
|
2022-11-10 09:29:34 +00:00
|
|
|
|
|
|
|
|
|
# only after ok connection
|
2023-09-25 12:38:59 +00:00
|
|
|
|
lastTimeConnected*: int64
|
|
|
|
|
retries*: int64
|
2022-11-10 09:29:34 +00:00
|
|
|
|
supportedProtocols*: seq[string]
|
|
|
|
|
userAgent*: string
|
2023-09-25 12:38:59 +00:00
|
|
|
|
lastPingDuration*: Duration
|
|
|
|
|
avgPingDuration*: Duration
|
2022-11-10 09:29:34 +00:00
|
|
|
|
|
2022-12-05 19:02:21 +00:00
|
|
|
|
# only after a ok/nok connection
|
|
|
|
|
connError*: string
|
|
|
|
|
|
2023-09-28 08:07:27 +00:00
|
|
|
|
CustomPeerInfoRef* = ref CustomPeerInfo
|
|
|
|
|
|
2022-11-14 07:33:36 +00:00
|
|
|
|
# Stores information about all discovered/connected peers
|
2023-09-28 08:07:27 +00:00
|
|
|
|
CustomPeersTableRef* = TableRef[string, CustomPeerInfoRef]
|
2022-11-10 09:29:34 +00:00
|
|
|
|
|
2022-11-14 07:33:36 +00:00
|
|
|
|
# stores the content topic and the count of rx messages
|
|
|
|
|
ContentTopicMessageTableRef* = TableRef[string, int]
|
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
|
proc installHandler*(
|
|
|
|
|
router: var RestRouter,
|
|
|
|
|
allPeers: CustomPeersTableRef,
|
|
|
|
|
numMessagesPerContentTopic: ContentTopicMessageTableRef,
|
|
|
|
|
) =
|
|
|
|
|
router.api(MethodGet, "/allpeersinfo") do() -> RestApiResponse:
|
2022-11-10 09:29:34 +00:00
|
|
|
|
let values = toSeq(allPeers.values())
|
2024-03-15 23:08:47 +00:00
|
|
|
|
return RestApiResponse.response(values.toJson(), contentType = "application/json")
|
|
|
|
|
router.api(MethodGet, "/contenttopics") do() -> RestApiResponse:
|
2022-11-14 07:33:36 +00:00
|
|
|
|
# TODO: toJson() includes the hash
|
2024-03-15 23:08:47 +00:00
|
|
|
|
return RestApiResponse.response(
|
|
|
|
|
$(%numMessagesPerContentTopic), contentType = "application/json"
|
|
|
|
|
)
|
2022-11-10 09:29:34 +00:00
|
|
|
|
|
2023-12-14 06:16:39 +00:00
|
|
|
|
proc startMetricsServer*(serverIp: IpAddress, serverPort: Port): Result[void, string] =
|
2024-03-15 23:08:47 +00:00
|
|
|
|
info "Starting metrics HTTP server", serverIp, serverPort
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
startMetricsHttpServer($serverIp, serverPort)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
error(
|
|
|
|
|
"Failed to start metrics HTTP server",
|
|
|
|
|
serverIp = serverIp,
|
|
|
|
|
serverPort = serverPort,
|
|
|
|
|
msg = e.msg,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
info "Metrics HTTP server started", serverIp, serverPort
|
|
|
|
|
ok()
|