Skip to content

Commit 3a9c34d

Browse files
author
Neo Bot
committed
Telemetry: fix health checks, node info, and docs
1 parent 77411df commit 3a9c34d

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

src/Plugins/Telemetry/Collectors/PluginMetricsCollector.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
using Neo.Network.P2P;
1313
using Neo.Plugins.Telemetry.Metrics;
14-
using System.Reflection;
1514

1615
namespace Neo.Plugins.Telemetry.Collectors
1716
{
@@ -32,7 +31,7 @@ public PluginMetricsCollector(NeoSystem system, string nodeId, string network)
3231
_network = network ?? throw new ArgumentNullException(nameof(network));
3332

3433
// Set node info metric
35-
var version = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown";
34+
var version = _system.GetType().Assembly.GetName().Version?.ToString() ?? "unknown";
3635
var protocolVersion = LocalNode.ProtocolVersion.ToString();
3736
MetricsDefinitions.NodeInfo.WithLabels(_nodeId, _network, version, protocolVersion).Set(1);
3837
}

src/Plugins/Telemetry/Health/HealthCheckEndpoint.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
// Redistribution and use in source and binary forms with or without
1010
// modifications are permitted.
1111

12+
using Akka.Actor;
1213
using Neo.Json;
14+
using Neo.Network.P2P;
1315
using Neo.SmartContract.Native;
1416
using System.Net;
1517
using System.Text;
@@ -27,6 +29,7 @@ public sealed class HealthCheckEndpoint : IDisposable
2729
private readonly Task _listenerTask;
2830
private readonly string _nodeId;
2931
private readonly string _network;
32+
private LocalNode? _localNode;
3033
private bool _disposed;
3134

3235
public HealthCheckEndpoint(NeoSystem system, string host, int port, string nodeId, string network)
@@ -41,6 +44,8 @@ public HealthCheckEndpoint(NeoSystem system, string host, int port, string nodeI
4144
_listener.Prefixes.Add($"http://{host}:{port}/ready/");
4245
_listener.Prefixes.Add($"http://{host}:{port}/live/");
4346

47+
InitializeLocalNode();
48+
4449
try
4550
{
4651
_listener.Start();
@@ -57,6 +62,19 @@ public HealthCheckEndpoint(NeoSystem system, string host, int port, string nodeI
5762
}
5863
}
5964

65+
private async void InitializeLocalNode()
66+
{
67+
try
68+
{
69+
_localNode = await _system.LocalNode.Ask<LocalNode>(new LocalNode.GetInstance());
70+
}
71+
catch (Exception ex)
72+
{
73+
Utility.Log(nameof(HealthCheckEndpoint), LogLevel.Debug,
74+
$"Failed to get LocalNode instance for health checks: {ex.Message}");
75+
}
76+
}
77+
6078
private async Task ListenAsync()
6179
{
6280
while (!_cts.Token.IsCancellationRequested && _listener?.IsListening == true)
@@ -121,15 +139,16 @@ private JObject GetHealthStatus()
121139
{
122140
var currentHeight = NativeContract.Ledger.CurrentIndex(_system.StoreView);
123141
var headerHeight = _system.HeaderCache.Last?.Index ?? currentHeight;
124-
var blocksBehind = headerHeight - currentHeight;
142+
var blocksBehind = headerHeight >= currentHeight ? headerHeight - currentHeight : 0;
125143
var isSynced = blocksBehind <= 2;
126-
var peerCount = 0;
144+
int? peerCount = _localNode?.ConnectedCount;
145+
var hasPeers = peerCount is null || peerCount > 0;
127146

128147
var memPool = _system.MemPool;
129148
var mempoolCount = memPool.Count;
130149
var mempoolCapacity = memPool.Capacity;
131150

132-
var status = isSynced && peerCount >= 0 ? "healthy" : "degraded";
151+
var status = isSynced && hasPeers ? "healthy" : "degraded";
133152

134153
return new JObject
135154
{
@@ -147,6 +166,11 @@ private JObject GetHealthStatus()
147166
["blocks_behind"] = blocksBehind,
148167
["synced"] = isSynced
149168
},
169+
["network"] = new JObject
170+
{
171+
["status"] = hasPeers ? "healthy" : "degraded",
172+
["connected_peers"] = peerCount ?? 0
173+
},
150174
["mempool"] = new JObject
151175
{
152176
["status"] = "healthy",
@@ -174,13 +198,19 @@ private JObject GetReadinessStatus()
174198
{
175199
var currentHeight = NativeContract.Ledger.CurrentIndex(_system.StoreView);
176200
var headerHeight = _system.HeaderCache.Last?.Index ?? currentHeight;
177-
var isSynced = (headerHeight - currentHeight) <= 2;
201+
var blocksBehind = headerHeight >= currentHeight ? headerHeight - currentHeight : 0;
202+
var isSynced = blocksBehind <= 2;
203+
int? peerCount = _localNode?.ConnectedCount;
204+
var hasPeers = peerCount is null || peerCount > 0;
205+
var ready = isSynced && hasPeers;
178206

179207
return new JObject
180208
{
181-
["status"] = isSynced ? "healthy" : "not_ready",
209+
["status"] = ready ? "healthy" : "not_ready",
182210
["timestamp"] = DateTime.UtcNow.ToString("O"),
183211
["synced"] = isSynced,
212+
["blocks_behind"] = blocksBehind,
213+
["connected_peers"] = peerCount ?? 0,
184214
["block_height"] = currentHeight
185215
};
186216
}

src/Plugins/Telemetry/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ A comprehensive telemetry and metrics collection plugin for Neo N3 full nodes, p
1515

1616
1. Build the plugin:
1717
```bash
18-
dotnet build src/Plugins/TelemetryPlugin/TelemetryPlugin.csproj
18+
dotnet build src/Plugins/Telemetry/Telemetry.csproj
1919
```
2020

21-
2. Copy the output to your Neo node's `Plugins/TelemetryPlugin/` directory:
21+
2. Copy the output to your Neo node's `Plugins/Telemetry/` directory:
2222
```bash
23-
cp -r bin/Debug/net10.0/* /path/to/neo-node/Plugins/TelemetryPlugin/
23+
cp -r bin/Debug/net10.0/* /path/to/neo-node/Plugins/Telemetry/
2424
```
2525

2626
3. Configure the plugin by editing `config.json` in the plugin directory.
2727

2828
## Configuration
2929

30-
Create or edit `config.json` in the `Plugins/TelemetryPlugin/` directory:
30+
Create or edit `config.json` in the `Plugins/Telemetry/` directory:
3131

3232
```json
3333
{
@@ -170,7 +170,7 @@ Import the provided Grafana dashboard JSON for a pre-configured monitoring view:
170170

171171
1. Open Grafana
172172
2. Go to Dashboards → Import
173-
3. Upload `grafana-dashboard.json` or paste the JSON content
173+
3. Upload `dashboards/neo-node-dashboard.json` or paste the JSON content
174174
4. Select your Prometheus data source
175175
5. Click Import
176176

src/Plugins/Telemetry/Telemetry.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ protected override void Configure()
5050
{
5151
TelemetrySettings.Load(GetConfiguration());
5252
Log($"Configuration loaded - Enabled: {TelemetrySettings.Default.Enabled}, " +
53-
$"Port: {TelemetrySettings.Default.PrometheusPort}", LogLevel.Info);
53+
$"Port: {TelemetrySettings.Default.PrometheusPort}, " +
54+
$"ExceptionPolicy: {TelemetrySettings.Default.ExceptionPolicy}", LogLevel.Info);
5455
}
5556

5657
protected internal override void OnSystemLoaded(NeoSystem system)

0 commit comments

Comments
 (0)