Skip to content

Commit 994422f

Browse files
authored
[PLAT-112239] Add e2e latency metrics for Thanos receive (#56)
2 parents e2cca9c + 7ed62f5 commit 994422f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pkg/receive/handler.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ type Handler struct {
128128

129129
writeSamplesTotal *prometheus.HistogramVec
130130
writeTimeseriesTotal *prometheus.HistogramVec
131+
writeE2eLatency *prometheus.HistogramVec
131132

132133
Limiter *Limiter
133134
}
@@ -208,6 +209,15 @@ func NewHandler(logger log.Logger, o *Options) *Handler {
208209
Buckets: []float64{10, 50, 100, 500, 1000, 5000, 10000},
209210
}, []string{"code", "tenant"},
210211
),
212+
writeE2eLatency: promauto.With(registerer).NewHistogramVec(
213+
prometheus.HistogramOpts{
214+
Namespace: "thanos",
215+
Subsystem: "receive",
216+
Name: "write_e2e_latency_seconds",
217+
Help: "The end-to-end latency of the oldest sample in write requests.",
218+
Buckets: []float64{1, 5, 10, 20, 30, 40, 50, 60, 90, 120, 300, 600, 900, 1200, 1800, 3600},
219+
}, []string{"code", "tenant"},
220+
),
211221
}
212222

213223
h.forwardRequests.WithLabelValues(labelSuccess)
@@ -462,6 +472,16 @@ func newWriteResponse(seriesIDs []int, err error, er endpointReplica) writeRespo
462472
}
463473
}
464474

475+
func secondsSinceOldestSample(toMS int64, ts prompb.TimeSeries) float64 {
476+
fromMS := toMS
477+
for _, s := range ts.Samples {
478+
if s.Timestamp < fromMS {
479+
fromMS = s.Timestamp
480+
}
481+
}
482+
return float64(toMS-fromMS) / 1000
483+
}
484+
465485
func (h *Handler) receiveHTTP(w http.ResponseWriter, r *http.Request) {
466486
var err error
467487
span, ctx := tracing.StartSpan(r.Context(), "receive_http")
@@ -606,6 +626,12 @@ func (h *Handler) receiveHTTP(w http.ResponseWriter, r *http.Request) {
606626
h.writeTimeseriesTotal.WithLabelValues(strconv.Itoa(responseStatusCode), tenant).Observe(float64(stats.timeseries))
607627
h.writeSamplesTotal.WithLabelValues(strconv.Itoa(responseStatusCode), tenant).Observe(float64(stats.totalSamples))
608628
}
629+
nowMS := time.Now().UnixNano() / int64(time.Millisecond)
630+
for _, ts := range wreq.Timeseries {
631+
if lat := secondsSinceOldestSample(nowMS, ts); lat > 0 {
632+
h.writeE2eLatency.WithLabelValues(strconv.Itoa(responseStatusCode), tenantHTTP).Observe(lat)
633+
}
634+
}
609635
}
610636

611637
type requestStats struct {

0 commit comments

Comments
 (0)