Skip to content

Commit 5120c07

Browse files
committed
feat: implement Prometheus metrics parsing and JSON serialization with tests
1 parent f413959 commit 5120c07

5 files changed

Lines changed: 802 additions & 30 deletions

File tree

cmd/start.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func runAgent(cmd *cobra.Command, args []string) error {
128128

129129
// Scrape immediately on start with aligned timestamp (UTC)
130130
collectionTime := time.Now().UTC().Truncate(cfg.Agent.Interval)
131-
if err := scrapeAndSendWithTimestamp(scraper, sender, cfg.Agent.ServerID, collectionTime); err != nil {
131+
if err := scrapeAndBuffer(scraper, sender, cfg.Agent.ServerID, collectionTime); err != nil {
132132
logger.Error("Initial scrape failed", logger.Err(err))
133133
}
134134

@@ -140,15 +140,15 @@ func runAgent(cmd *cobra.Command, args []string) error {
140140
case tickTime := <-ticker.C:
141141
// Align collection time to interval boundary (UTC)
142142
collectionTime := tickTime.UTC().Truncate(cfg.Agent.Interval)
143-
if err := scrapeAndSendWithTimestamp(scraper, sender, cfg.Agent.ServerID, collectionTime); err != nil {
143+
if err := scrapeAndBuffer(scraper, sender, cfg.Agent.ServerID, collectionTime); err != nil {
144144
logger.Error("Scrape failed", logger.Err(err))
145145
}
146146
}
147147
}
148148
}
149149

150-
// scrapeAndSendWithTimestamp scrapes metrics and adds aligned collection timestamp
151-
func scrapeAndSendWithTimestamp(scraper *prometheus.Scraper, sender *report.Sender, serverID string, collectionTime time.Time) error {
150+
// scrapeAndBuffer scrapes metrics and saves raw Prometheus text to buffer
151+
func scrapeAndBuffer(scraper *prometheus.Scraper, sender *report.Sender, serverID string, collectionTime time.Time) error {
152152
// Scrape Prometheus exporter
153153
data, err := scraper.Scrape()
154154
if err != nil {
@@ -159,8 +159,8 @@ func scrapeAndSendWithTimestamp(scraper *prometheus.Scraper, sender *report.Send
159159
// This ensures all agents report metrics at the same logical time boundaries
160160
dataWithTimestamp := prometheus.AddTimestamps(data, collectionTime)
161161

162-
// Save to buffer (WAL pattern - actual sending happens in background)
163-
if err := sender.SendPrometheus(dataWithTimestamp, serverID); err != nil {
162+
// Save raw Prometheus text to buffer (WAL pattern - parsing happens during drain)
163+
if err := sender.BufferPrometheus(dataWithTimestamp, serverID); err != nil {
164164
return fmt.Errorf("failed to buffer prometheus data: %w", err)
165165
}
166166

internal/prometheus/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
metrics.txt

0 commit comments

Comments
 (0)