Skip to content

Commit efc8d77

Browse files
authored
feat: read from a PID file when targeting Amaru (#363)
Signed-off-by: yHSJ <[email protected]>
1 parent fe1a743 commit efc8d77

File tree

6 files changed

+84
-11
lines changed

6 files changed

+84
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
# vendor/
1616

1717
nview
18+
19+
config.yaml

config.yaml.example

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ app:
88
# This can also be set via the NODE_NAME environment variable
99
nodeName: Cardano Node
1010

11-
# Named Cardano network for cardano-node
11+
# Named Cardano network for the node
1212
#
1313
# This is a short-cut to select the NetworkMagic and can be used to
1414
# select mainnet, preprod, preview, or sancho networks.
@@ -18,15 +18,24 @@ app:
1818
network:
1919

2020
node:
21-
# Named Cardano network for cardano-node
21+
# Name of the node you are running.
22+
#
23+
# Valid values are:
24+
# - `cardano-node`
25+
# - `amaru`
26+
#
27+
# This can also be set via the CARDANO_NODE_BINARY environment variable
28+
binary: cardano-node
29+
30+
# Named Cardano network for the node
2231
#
2332
# This is a short-cut to select the NetworkMagic and can be used to
2433
# select mainnet, preprod, preview, or sancho networks.
2534
#
2635
# This can also be set via the CARDANO_NETWORK environment variable
2736
network: mainnet
2837

29-
# NetworkMagic for network for cardano-node
38+
# NetworkMagic for network for the node
3039
#
3140
# This selects the correct network for operation and can be configured to
3241
# any network, not just the named networks.
@@ -35,29 +44,36 @@ node:
3544
# variable
3645
networkMagic:
3746

38-
# Port for cardano-node
47+
# Port for the node
3948
#
40-
# Listening port for cardano-node for NtN communication.
49+
# Listening port for the node for NtN communication.
4150
#
4251
# This can also be set via the CARDANO_PORT environment variable
4352
port: 3001
4453

45-
# Socket path for cardano-node
54+
# Socket path for the node
4655
#
47-
# Listening UNIX socket path and file name for cardano-node NtC
56+
# Listening UNIX socket path and file name for the node NtC
4857
# communication.
4958
#
5059
# This can also be set via the CARDANO_NODE_SOCKET_PATH environment variable
5160
socketPath:
5261

62+
# The path to the PID File of your running node
63+
#
64+
# This is only used when the `binary` value is `amaru`.
65+
# Otherwise it is ignored.
66+
pidFile:
67+
68+
5369
prometheus:
54-
# host/port for cardano-node Prometheus metrics
70+
# host/port for the node Prometheus metrics
5571
#
5672
# These can also be set via the PROM_HOST and PROM_PORT environment variables
5773
host: 127.0.0.1
5874
port: 12798
5975

60-
# Timeout for connections to cardano-node
76+
# Timeout for connections to the node
6177
#
6278
# This can also be set via the PROM_TIMEOUT environment variable
6379
timeout: 3

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type AppConfig struct {
4040
type NodeConfig struct {
4141
ByronGenesis ByronGenesisConfig `yaml:"byron"`
4242
Binary string `yaml:"binary" envconfig:"CARDANO_NODE_BINARY"`
43+
PidFile string `yaml:"pidFile" envconfig:"CARDANO_NODE_PID_FILE"`
4344
Network string `yaml:"network" envconfig:"CARDANO_NETWORK"`
4445
SocketPath string `yaml:"socketPath" envconfig:"CARDANO_NODE_SOCKET_PATH"`
4546
NetworkMagic uint32 `yaml:"networkMagic" envconfig:"CARDANO_NODE_NETWORK_MAGIC"`

main.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/json"
2020
"flag"
2121
"fmt"
22+
"math"
2223
"net"
2324
"os"
2425
"strconv"
@@ -36,6 +37,11 @@ import (
3637
terminal "golang.org/x/term"
3738
)
3839

40+
const (
41+
CARADNO_NODE_BINARY = "cardano-node"
42+
AMARU_BINARY = "amaru"
43+
)
44+
3945
// Global command line flags
4046
var cmdlineFlags struct {
4147
configFile string
@@ -1180,6 +1186,48 @@ func getResourceText(ctx context.Context) string {
11801186

11811187
func getProcessMetrics(ctx context.Context) (*process.Process, error) {
11821188
cfg := config.GetConfig()
1189+
1190+
if cfg.Node.Binary == AMARU_BINARY {
1191+
return getProcessMetricsByPidFile(cfg, ctx)
1192+
} else {
1193+
return getProcessMetricsByNameAndPort(cfg, ctx)
1194+
}
1195+
}
1196+
1197+
func getProcessMetricsByPidFile(cfg *config.Config, ctx context.Context) (*process.Process, error) {
1198+
data, err := os.ReadFile(cfg.Node.PidFile)
1199+
if err != nil {
1200+
return nil, fmt.Errorf("failed to read pid file: %w", err)
1201+
}
1202+
pidStr := strings.TrimSpace(string(data))
1203+
pid, err := strconv.Atoi(pidStr)
1204+
if err != nil {
1205+
return nil, fmt.Errorf("invalid pid in pid file: %w", err)
1206+
}
1207+
if pid <= 0 || pid > math.MaxInt32 {
1208+
return nil, fmt.Errorf("invalid pid %d: out of int32 range", pid)
1209+
}
1210+
1211+
// the overflow is checked above
1212+
//nolint:gosec
1213+
proc, err := process.NewProcessWithContext(ctx, int32(pid))
1214+
if err != nil {
1215+
return nil, fmt.Errorf("failed to get process %d: %w", pid, err)
1216+
}
1217+
1218+
exists, err := proc.IsRunning()
1219+
if err != nil {
1220+
return nil, fmt.Errorf("failed to check if process %d is running: %w", pid, err)
1221+
}
1222+
1223+
if !exists {
1224+
return nil, fmt.Errorf("process %d is not running", pid)
1225+
}
1226+
1227+
return proc, nil
1228+
}
1229+
1230+
func getProcessMetricsByNameAndPort(cfg *config.Config, ctx context.Context) (*process.Process, error) {
11831231
r, _ := process.NewProcessWithContext(ctx, 0)
11841232
processes, err := process.ProcessesWithContext(ctx)
11851233
if err != nil {
@@ -1188,17 +1236,20 @@ func getProcessMetrics(ctx context.Context) (*process.Process, error) {
11881236
for _, p := range processes {
11891237
n, err := p.NameWithContext(ctx)
11901238
if err != nil {
1191-
return r, fmt.Errorf("failed to get process name: %w", err)
1239+
continue
11921240
}
1241+
11931242
c, err := p.CmdlineWithContext(ctx)
11941243
if err != nil {
1195-
return r, fmt.Errorf("failed to get process cmdline: %w", err)
1244+
continue
11961245
}
1246+
11971247
if strings.Contains(n, cfg.Node.Binary) &&
11981248
strings.Contains(c, strconv.FormatUint(uint64(cfg.Node.Port), 10)) {
11991249
r = p
12001250
}
12011251
}
1252+
12021253
return r, nil
12031254
}
12041255

peers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func filterPeers(ctx context.Context) error {
4141
len(peerStats.RTTresultsSlice) == len(peersFiltered) {
4242
return nil
4343
}
44+
4445
if processMetrics == nil {
4546
return nil // TODO: what to do here
4647
}

prometheus.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ func getPromMetrics(ctx context.Context) (*PromMetrics, error) {
9797
failCount++
9898
return metrics, fmt.Errorf("failed JSON unmarshal: %w", err)
9999
}
100+
101+
// panic(string(b))
100102
failCount = 0
101103
return metrics, nil
102104
}

0 commit comments

Comments
 (0)