merged from gramsrv upstream

This commit is contained in:
onysd 2026-09-01 12:06:31 +03:00
parent 79c64ee916
commit 21a0856587
651 changed files with 54774 additions and 4590 deletions

View file

@ -265,6 +265,20 @@ func (r *Registry) RPCHandled(method string, d time.Duration, err error) {
r.observe("telesrv_mtproto_rpc_duration_seconds", d, labels...)
}
// RPCDatabase implements mtprotoedge.RPCDatabaseMetrics.
func (r *Registry) RPCDatabase(method string, queries int64, d time.Duration, errors int64) {
labels := []Label{{Name: "method", Value: method}}
if queries > 0 {
r.add("telesrv_rpc_db_queries_total", uint64(queries), labels...)
}
if errors > 0 {
r.add("telesrv_rpc_db_errors_total", uint64(errors), labels...)
}
if d > 0 {
r.observe("telesrv_rpc_db_time_seconds", d, labels...)
}
}
// InboundRPCQueued implements mtprotoedge.Metrics.
func (r *Registry) InboundRPCQueued(method string, length, capacity int) {
r.inc("telesrv_mtproto_inbound_rpc_queued_total", Label{Name: "method", Value: method})
@ -384,6 +398,76 @@ func (r *Registry) OutboxFailed(err error) {
r.inc("telesrv_rpc_outbox_failed_total", Label{Name: "outcome", Value: errorOutcome(err)})
}
// BootstrapReadyBatch implements postgres.BootstrapReadyBatchMetrics without
// importing store identities into the observability layer.
func (r *Registry) BootstrapReadyBatch(inputs int, matched int, d time.Duration, err error) {
outcome := errorOutcome(err)
r.inc("telesrv_bootstrap_ready_batches_total", Label{Name: "outcome", Value: outcome})
r.observe("telesrv_bootstrap_ready_batch_duration_seconds", d, Label{Name: "outcome", Value: outcome})
inputs = max(inputs, 0)
matched = max(min(matched, inputs), 0)
if err != nil {
r.add("telesrv_bootstrap_ready_selectors_total", uint64(inputs), Label{Name: "outcome", Value: "error"})
return
}
r.add("telesrv_bootstrap_ready_selectors_total", uint64(matched), Label{Name: "outcome", Value: "matched"})
r.add("telesrv_bootstrap_ready_selectors_total", uint64(inputs-matched), Label{Name: "outcome", Value: "miss"})
}
// BootstrapReadyPending implements postgres.BootstrapReadyBatchMetrics.
func (r *Registry) BootstrapReadyPending(delta int) {
r.addGauge("telesrv_bootstrap_ready_pending", int64(delta))
}
// ActiveChannelIDsCache implements channels.ActiveChannelIDsReadModelMetrics.
func (r *Registry) ActiveChannelIDsCache(outcome string) {
r.inc("telesrv_active_channel_ids_cache_total", Label{Name: "outcome", Value: outcome})
}
// ActiveChannelIDsBatch implements postgres.ActiveChannelIDsBatchMetrics.
func (r *Registry) ActiveChannelIDsBatch(selectors int, rows int, d time.Duration, err error) {
outcome := errorOutcome(err)
r.inc("telesrv_active_channel_ids_batches_total", Label{Name: "outcome", Value: outcome})
r.add("telesrv_active_channel_ids_selectors_total", uint64(max(selectors, 0)), Label{Name: "outcome", Value: outcome})
if err == nil {
r.add("telesrv_active_channel_ids_rows_total", uint64(max(rows, 0)))
}
r.observe("telesrv_active_channel_ids_batch_duration_seconds", d, Label{Name: "outcome", Value: outcome})
}
// ActiveChannelIDsPending implements postgres.ActiveChannelIDsBatchMetrics.
func (r *Registry) ActiveChannelIDsPending(delta int) {
r.addGauge("telesrv_active_channel_ids_pending", int64(delta))
}
// PresenceLastSeenBatch implements rpc.Metrics.
func (r *Registry) PresenceLastSeenBatch(count int, d time.Duration, err error) {
labels := []Label{{Name: "outcome", Value: errorOutcome(err)}}
r.inc("telesrv_presence_last_seen_batches_total", labels...)
r.add("telesrv_presence_last_seen_updates_total", uint64(max(count, 0)), labels...)
r.observe("telesrv_presence_last_seen_batch_duration_seconds", d, labels...)
}
// PresenceLastSeenSubmitted implements rpc.Metrics.
func (r *Registry) PresenceLastSeenSubmitted() {
r.inc("telesrv_presence_last_seen_submitted_total")
}
// PresenceLastSeenPending implements rpc.Metrics.
func (r *Registry) PresenceLastSeenPending(delta int) {
r.addGauge("telesrv_presence_last_seen_pending", int64(delta))
}
// PresenceLastSeenOverflow implements rpc.Metrics.
func (r *Registry) PresenceLastSeenOverflow() {
r.inc("telesrv_presence_last_seen_overflow_total")
}
// PresenceLastSeenDrainDropped implements rpc.Metrics.
func (r *Registry) PresenceLastSeenDrainDropped(count int) {
r.add("telesrv_presence_last_seen_drain_dropped_total", uint64(max(count, 0)))
}
// ServeHTTP writes Prometheus text format.
func (r *Registry) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")

View file

@ -13,12 +13,111 @@ import (
var (
_ mtprotoedge.Metrics = (*Registry)(nil)
_ mtprotoedge.RPCDatabaseMetrics = (*Registry)(nil)
_ mtprotoedge.RPCResultMetrics = (*Registry)(nil)
_ mtprotoedge.LogicalOutboxMetrics = (*Registry)(nil)
_ mtprotoedge.ConnectionIntakeMetrics = (*Registry)(nil)
_ rpc.Metrics = (*Registry)(nil)
)
func TestRegistryExportsRPCDatabaseWork(t *testing.T) {
registry := New()
registry.RPCDatabase("messages.getDialogs", 17, 25*time.Millisecond, 0)
recorder := httptest.NewRecorder()
registry.ServeHTTP(recorder, httptest.NewRequest("GET", "/metrics", nil))
body := recorder.Body.String()
for _, want := range []string{
`telesrv_rpc_db_queries_total{method="messages.getDialogs"} 17`,
`telesrv_rpc_db_time_seconds_sum{method="messages.getDialogs"} 0.025`,
`telesrv_rpc_db_time_seconds_count{method="messages.getDialogs"} 1`,
} {
if !strings.Contains(body, want) {
t.Fatalf("missing %q from:\n%s", want, body)
}
}
}
func TestRegistryExportsPresenceLastSeenBatchWork(t *testing.T) {
registry := New()
registry.PresenceLastSeenBatch(37, 25*time.Millisecond, nil)
registry.PresenceLastSeenBatch(12, 50*time.Millisecond, errors.New("temporary"))
registry.PresenceLastSeenSubmitted()
registry.PresenceLastSeenSubmitted()
registry.PresenceLastSeenPending(9)
registry.PresenceLastSeenPending(-4)
registry.PresenceLastSeenOverflow()
registry.PresenceLastSeenDrainDropped(3)
recorder := httptest.NewRecorder()
registry.ServeHTTP(recorder, httptest.NewRequest("GET", "/metrics", nil))
body := recorder.Body.String()
for _, want := range []string{
`telesrv_presence_last_seen_batches_total{outcome="ok"} 1`,
`telesrv_presence_last_seen_batches_total{outcome="error"} 1`,
`telesrv_presence_last_seen_updates_total{outcome="ok"} 37`,
`telesrv_presence_last_seen_updates_total{outcome="error"} 12`,
`telesrv_presence_last_seen_submitted_total 2`,
`telesrv_presence_last_seen_pending 5`,
`telesrv_presence_last_seen_overflow_total 1`,
`telesrv_presence_last_seen_drain_dropped_total 3`,
} {
if !strings.Contains(body, want) {
t.Fatalf("missing %q from:\n%s", want, body)
}
}
}
func TestRegistryExportsBootstrapReadyBatchWork(t *testing.T) {
registry := New()
registry.BootstrapReadyPending(7)
registry.BootstrapReadyBatch(7, 2, 25*time.Millisecond, nil)
registry.BootstrapReadyPending(-7)
registry.BootstrapReadyBatch(3, 0, 50*time.Millisecond, errors.New("temporary"))
recorder := httptest.NewRecorder()
registry.ServeHTTP(recorder, httptest.NewRequest("GET", "/metrics", nil))
body := recorder.Body.String()
for _, want := range []string{
`telesrv_bootstrap_ready_batches_total{outcome="ok"} 1`,
`telesrv_bootstrap_ready_batches_total{outcome="error"} 1`,
`telesrv_bootstrap_ready_selectors_total{outcome="matched"} 2`,
`telesrv_bootstrap_ready_selectors_total{outcome="miss"} 5`,
`telesrv_bootstrap_ready_selectors_total{outcome="error"} 3`,
`telesrv_bootstrap_ready_pending 0`,
} {
if !strings.Contains(body, want) {
t.Fatalf("missing %q from:\n%s", want, body)
}
}
}
func TestRegistryExportsActiveChannelIDsReadModelWork(t *testing.T) {
registry := New()
registry.ActiveChannelIDsCache("hit")
registry.ActiveChannelIDsCache("miss")
registry.ActiveChannelIDsCache("served")
registry.ActiveChannelIDsPending(7)
registry.ActiveChannelIDsBatch(7, 19, 25*time.Millisecond, nil)
registry.ActiveChannelIDsPending(-7)
registry.ActiveChannelIDsBatch(3, 0, 50*time.Millisecond, errors.New("temporary"))
recorder := httptest.NewRecorder()
registry.ServeHTTP(recorder, httptest.NewRequest("GET", "/metrics", nil))
body := recorder.Body.String()
for _, want := range []string{
`telesrv_active_channel_ids_cache_total{outcome="hit"} 1`,
`telesrv_active_channel_ids_cache_total{outcome="miss"} 1`,
`telesrv_active_channel_ids_cache_total{outcome="served"} 1`,
`telesrv_active_channel_ids_batches_total{outcome="ok"} 1`,
`telesrv_active_channel_ids_batches_total{outcome="error"} 1`,
`telesrv_active_channel_ids_selectors_total{outcome="ok"} 7`,
`telesrv_active_channel_ids_selectors_total{outcome="error"} 3`,
`telesrv_active_channel_ids_rows_total 19`,
`telesrv_active_channel_ids_pending 0`,
} {
if !strings.Contains(body, want) {
t.Fatalf("missing %q from:\n%s", want, body)
}
}
}
func TestRegistryExportsBoundedAggregateMetrics(t *testing.T) {
registry := New()
registry.maxSeries = 2