Skip to content

Commit a35dc6e

Browse files
committed
feat: add NodeExporter metrics parsing and update sender to use new structure
1 parent 7780619 commit a35dc6e

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

internal/prometheus/parser.go renamed to internal/prometheus/node_exporter_parser.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
"time"
1010
)
1111

12-
// MetricSnapshot represents a parsed snapshot of all essential metrics
12+
// NodeExporterMetricSnapshot represents a parsed snapshot of node_exporter metrics
1313
// This matches the admiral.metrics table schema (raw values, no percentages)
14-
type MetricSnapshot struct {
14+
// Specifically designed for Prometheus node_exporter metrics only
15+
type NodeExporterMetricSnapshot struct {
1516
Timestamp time.Time `json:"timestamp"`
1617

1718
// CPU Metrics (seconds, raw values from counters)
@@ -72,10 +73,11 @@ type MetricSnapshot struct {
7273
UptimeSeconds int64 `json:"uptime_seconds"`
7374
}
7475

75-
// ParsePrometheusMetrics parses Prometheus text format and extracts essential metrics
76-
// Returns a MetricSnapshot with raw counter values (no percentages calculated)
77-
func ParsePrometheusMetrics(data []byte) (*MetricSnapshot, error) {
78-
snapshot := &MetricSnapshot{
76+
// ParseNodeExporterMetrics parses Prometheus node_exporter text format and extracts essential metrics
77+
// Returns a NodeExporterMetricSnapshot with raw counter values (no percentages calculated)
78+
// This parser is specifically designed for node_exporter metrics only
79+
func ParseNodeExporterMetrics(data []byte) (*NodeExporterMetricSnapshot, error) {
80+
snapshot := &NodeExporterMetricSnapshot{
7981
Timestamp: time.Now().UTC(),
8082
}
8183

@@ -155,7 +157,7 @@ type diskMetrics struct {
155157
ioTimeSeconds float64
156158
}
157159

158-
func parseLine(line string, snapshot *MetricSnapshot,
160+
func parseLine(line string, snapshot *NodeExporterMetricSnapshot,
159161
cpuIdle, cpuUser, cpuSystem, cpuIowait, cpuSteal map[string]float64,
160162
networkDevices map[string]*networkMetrics,
161163
diskDevices map[string]*diskMetrics) error {
@@ -429,7 +431,7 @@ func isPhysicalNetwork(device string) bool {
429431
return true
430432
}
431433

432-
func selectPrimaryNetwork(snapshot *MetricSnapshot, devices map[string]*networkMetrics) {
434+
func selectPrimaryNetwork(snapshot *NodeExporterMetricSnapshot, devices map[string]*networkMetrics) {
433435
// Priority: eth0 > en0 > first available
434436
var primary *networkMetrics
435437
if devices["eth0"] != nil {
@@ -456,7 +458,7 @@ func selectPrimaryNetwork(snapshot *MetricSnapshot, devices map[string]*networkM
456458
}
457459
}
458460

459-
func selectPrimaryDisk(snapshot *MetricSnapshot, devices map[string]*diskMetrics) {
461+
func selectPrimaryDisk(snapshot *NodeExporterMetricSnapshot, devices map[string]*diskMetrics) {
460462
// Priority: vda > sda > nvme0n1 > first available
461463
var primary *diskMetrics
462464
if devices["vda"] != nil {

internal/prometheus/parser_test.go renamed to internal/prometheus/node_exporter_parser_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import (
66
"testing"
77
)
88

9-
func TestParsePrometheusMetrics(t *testing.T) {
9+
func TestParseNodeExporterMetrics(t *testing.T) {
1010
// Read the metrics.txt file from the current directory
1111
metricFile := "metrics.txt"
1212
data, err := os.ReadFile(metricFile)
1313
if err != nil {
1414
t.Fatalf("Failed to read metrics.txt: %v", err)
1515
}
1616

17-
snapshot, err := ParsePrometheusMetrics(data)
17+
snapshot, err := ParseNodeExporterMetrics(data)
1818
if err != nil {
19-
t.Fatalf("ParsePrometheusMetrics failed: %v", err)
19+
t.Fatalf("ParseNodeExporterMetrics failed: %v", err)
2020
}
2121

2222
// Verify timestamp is set
@@ -189,7 +189,7 @@ func TestParsePrometheusMetrics(t *testing.T) {
189189
t.Log(string(jsonData))
190190

191191
// Verify JSON can be unmarshaled back
192-
var decoded MetricSnapshot
192+
var decoded NodeExporterMetricSnapshot
193193
if err := json.Unmarshal(jsonData, &decoded); err != nil {
194194
t.Fatalf("Failed to unmarshal JSON: %v", err)
195195
}
@@ -220,8 +220,8 @@ func TestParsePrometheusMetrics(t *testing.T) {
220220
})
221221
}
222222

223-
func TestParsePrometheusMetrics_EmptyInput(t *testing.T) {
224-
snapshot, err := ParsePrometheusMetrics([]byte{})
223+
func TestParseNodeExporterMetrics_EmptyInput(t *testing.T) {
224+
snapshot, err := ParseNodeExporterMetrics([]byte{})
225225
if err != nil {
226226
t.Fatalf("Should handle empty input: %v", err)
227227
}
@@ -236,9 +236,9 @@ func TestParsePrometheusMetrics_EmptyInput(t *testing.T) {
236236
}
237237
}
238238

239-
func TestParsePrometheusMetrics_InvalidInput(t *testing.T) {
239+
func TestParseNodeExporterMetrics_InvalidInput(t *testing.T) {
240240
invalidData := []byte("invalid prometheus format\ngarbage data")
241-
snapshot, err := ParsePrometheusMetrics(invalidData)
241+
snapshot, err := ParseNodeExporterMetrics(invalidData)
242242

243243
if err != nil {
244244
t.Fatalf("Should handle invalid input gracefully: %v", err)

internal/report/sender.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (s *Sender) processBatch(filePaths []string) error {
185185
}
186186

187187
// Group entries by exporter name
188-
exporterMetrics := make(map[string][]prometheus.MetricSnapshot)
188+
exporterMetrics := make(map[string][]prometheus.NodeExporterMetricSnapshot)
189189
processedFiles := []string{}
190190
var serverID string
191191

@@ -217,14 +217,15 @@ func (s *Sender) processBatch(filePaths []string) error {
217217
}
218218

219219
// Parse Prometheus text to structured metrics
220-
snapshot, err := prometheus.ParsePrometheusMetrics(entry.Data)
220+
// Note: Currently only node_exporter is parsed, other exporters need their own parsers
221+
snapshot, err := prometheus.ParseNodeExporterMetrics(entry.Data)
221222
if err != nil {
222-
logger.Warn("Failed to parse Prometheus metrics, using zero values",
223+
logger.Warn("Failed to parse node_exporter metrics, using zero values",
223224
logger.String("exporter", entry.ExporterName),
224225
logger.String("file", filePath),
225226
logger.Err(err))
226227
// Use zero-value snapshot
227-
snapshot = &prometheus.MetricSnapshot{
228+
snapshot = &prometheus.NodeExporterMetricSnapshot{
228229
Timestamp: time.Now().UTC(),
229230
}
230231
}

0 commit comments

Comments
 (0)