Skip to content

Commit

Permalink
Add metrics for the number of stored events
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Nov 22, 2023
1 parent 652ee06 commit 6b51f07
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
13 changes: 13 additions & 0 deletions service/adapters/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Prometheus struct {
relayConnectionReceivedMessagesCounter *prometheus.CounterVec
relayConnectionDisconnectionsCounter *prometheus.CounterVec
storedRelayAddressesGauge prometheus.Gauge
storedEventsGauge prometheus.Gauge

registry *prometheus.Registry

Expand Down Expand Up @@ -126,6 +127,12 @@ func NewPrometheus(logger logging.Logger) (*Prometheus, error) {
Help: "Number of stored relay addresses.",
},
)
storedEventsGauge := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "stored_events_gauge",
Help: "Number of stored events.",
},
)

reg := prometheus.NewRegistry()
for _, v := range []prometheus.Collector{
Expand All @@ -140,6 +147,7 @@ func NewPrometheus(logger logging.Logger) (*Prometheus, error) {
relayConnectionReceivedMessagesCounter,
relayConnectionDisconnectionsCounter,
storedRelayAddressesGauge,
storedEventsGauge,
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
collectors.NewGoCollector(),
} {
Expand Down Expand Up @@ -173,6 +181,7 @@ func NewPrometheus(logger logging.Logger) (*Prometheus, error) {
relayConnectionReceivedMessagesCounter: relayConnectionReceivedMessagesCounter,
relayConnectionDisconnectionsCounter: relayConnectionDisconnectionsCounter,
storedRelayAddressesGauge: storedRelayAddressesGauge,
storedEventsGauge: storedEventsGauge,

registry: reg,

Expand Down Expand Up @@ -234,6 +243,10 @@ func (p *Prometheus) ReportNumberOfStoredRelayAddresses(n int) {
p.storedRelayAddressesGauge.Set(float64(n))
}

func (p *Prometheus) ReportNumberOfStoredEvents(n int) {
p.storedEventsGauge.Set(float64(n))
}

func (p *Prometheus) Registry() *prometheus.Registry {
return p.registry
}
Expand Down
11 changes: 11 additions & 0 deletions service/adapters/sqlite/event_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ func (r *EventRepository) Get(ctx context.Context, eventID domain.EventId) (doma
return r.readEvent(result)
}

func (r *EventRepository) Count(ctx context.Context) (int, error) {
row := r.tx.QueryRow(`SELECT COUNT(*) FROM events`)

var count int
if err := row.Scan(&count); err != nil {
return 0, errors.Wrap(err, "error scanning")
}

return count, nil
}

func (m *EventRepository) readEvent(result *sql.Row) (domain.Event, error) {
var payload []byte

Expand Down
33 changes: 33 additions & 0 deletions service/adapters/sqlite/event_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,36 @@ func TestEventRepository_ItIsPossibleToSaveAndGetEvents(t *testing.T) {
})
require.NoError(t, err)
}

func TestEventRepository_CountCountsSavedEvents(t *testing.T) {
ctx := fixtures.TestContext(t)
adapters := NewTestAdapters(ctx, t)

err := adapters.TransactionProvider.Transact(ctx, func(ctx context.Context, adapters sqlite.TestAdapters) error {
n, err := adapters.EventRepository.Count(ctx)
require.NoError(t, err)
require.Equal(t, 0, n)

return nil
})
require.NoError(t, err)

for i := 0; i < 5; i++ {
err = adapters.TransactionProvider.Transact(ctx, func(ctx context.Context, adapters sqlite.TestAdapters) error {
err := adapters.EventRepository.Save(ctx, fixtures.SomeEvent())
require.NoError(t, err)

return nil
})
require.NoError(t, err)

err = adapters.TransactionProvider.Transact(ctx, func(ctx context.Context, adapters sqlite.TestAdapters) error {
n, err := adapters.EventRepository.Count(ctx)
require.NoError(t, err)
require.Equal(t, i+1, n)

return nil
})
require.NoError(t, err)
}
}
3 changes: 3 additions & 0 deletions service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type EventRepository interface {

// Get returns ErrEventNotFound.
Get(ctx context.Context, eventID domain.EventId) (domain.Event, error)

Count(ctx context.Context) (int, error)
}

type RelayRepository interface {
Expand Down Expand Up @@ -96,6 +98,7 @@ type Metrics interface {
ReportReceivedEvent(address domain.RelayAddress)
ReportQueueLength(topic string, n int)
ReportNumberOfStoredRelayAddresses(n int)
ReportNumberOfStoredEvents(n int)
}

type ApplicationCall interface {
Expand Down
6 changes: 6 additions & 0 deletions service/app/handler_update_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func (h *UpdateMetricsHandler) Handle(ctx context.Context) (err error) {
}
h.metrics.ReportNumberOfStoredRelayAddresses(n)

n, err = adapters.Events.Count(ctx)
if err != nil {
return errors.Wrap(err, "error counting events")
}
h.metrics.ReportNumberOfStoredEvents(n)

return nil
}); err != nil {
return errors.Wrap(err, "transaction error")
Expand Down

0 comments on commit 6b51f07

Please sign in to comment.