Skip to content

Commit cf431e7

Browse files
committed
fix(metrics): Remove scraping duration timer, as it's unreliable
The Prometheus server (central component) will also record the scrape time for this particular scrape. So we just keep that one.
1 parent 0701b5b commit cf431e7

2 files changed

Lines changed: 3 additions & 44 deletions

File tree

internal/agent/metrics/metrics_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,36 +1078,6 @@ func TestHealthEndpointRedisDown(t *testing.T) {
10781078
}
10791079
}
10801080

1081-
// --- Scrape Duration Test ---
1082-
1083-
func TestScrapeDurationMetric(t *testing.T) {
1084-
// Suppress expected "failed to connect" logs from collectors whose DBs aren't mocked
1085-
log.SetOutput(io.Discard)
1086-
defer log.SetOutput(os.Stderr)
1087-
1088-
mc := newMockConnector("CONFIG_DB")
1089-
// DeviceCollector will read DEVICE_METADATA
1090-
mc.mocks["CONFIG_DB"].ExpectHGetAll("DEVICE_METADATA|localhost").SetVal(map[string]string{
1091-
"mac": "aa:bb:cc:dd:ee:ff",
1092-
})
1093-
// InterfaceCollector will list ports
1094-
mc.mocks["CONFIG_DB"].ExpectKeys("PORT|*").SetVal([]string{})
1095-
1096-
srv := NewMetricsServer(":0", mc, nil, "")
1097-
1098-
req := httptest.NewRequest("GET", "/metrics", nil)
1099-
w := httptest.NewRecorder()
1100-
srv.Handler.ServeHTTP(w, req)
1101-
1102-
if w.Code != http.StatusOK {
1103-
t.Fatalf("expected 200, got %d", w.Code)
1104-
}
1105-
body := w.Body.String()
1106-
if !strings.Contains(body, "sonic_scrape_duration_seconds") {
1107-
t.Error("response missing sonic_scrape_duration_seconds metric")
1108-
}
1109-
}
1110-
11111081
// --- Error Handling Test ---
11121082

11131083
func TestDeviceCollectorRedisDown(t *testing.T) {

internal/agent/metrics/server.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package metrics
66
import (
77
"fmt"
88
"net/http"
9-
"time"
109

1110
"github.com/prometheus/client_golang/prometheus"
1211
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -44,22 +43,12 @@ func NewMetricsServer(addr string, connector RedisConnector, versionInfo Version
4443
}
4544
}
4645

47-
// Scrape duration gauge — updated after each /metrics response is written,
48-
// so it reports the duration of the previous scrape (not the current one).
49-
// This is consistent with how node_exporter and similar exporters work.
50-
scrapeDuration := prometheus.NewGauge(prometheus.GaugeOpts{
51-
Name: "sonic_scrape_duration_seconds",
52-
Help: "Duration of the last metrics scrape in seconds",
53-
})
54-
registry.MustRegister(scrapeDuration)
46+
// NOTE: Scrape duration is not emitted by this exporter. Prometheus records
47+
// scrape_duration_seconds server-side as part of every scrape automatically.
5548

5649
mux := http.NewServeMux()
5750
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
58-
mux.Handle("GET /metrics", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
59-
start := time.Now()
60-
handler.ServeHTTP(w, r)
61-
scrapeDuration.Set(time.Since(start).Seconds())
62-
}))
51+
mux.Handle("GET /metrics", handler)
6352
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
6453
client, err := connector.Connect("CONFIG_DB")
6554
if err != nil {

0 commit comments

Comments
 (0)