Skip to content

Commit 4b8fbb1

Browse files
committed
Add MetricsService interface
1 parent 79165fd commit 4b8fbb1

16 files changed

+86
-78
lines changed

internal/data/accounts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
type AccountModel struct {
1313
DB db.ConnectionPool
14-
MetricsService *metrics.MetricsService
14+
MetricsService metrics.MetricsService
1515
}
1616

1717
func (m *AccountModel) Insert(ctx context.Context, address string) error {

internal/data/models.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Models struct {
1212
Account *AccountModel
1313
}
1414

15-
func NewModels(db db.ConnectionPool, metricsService *metrics.MetricsService) (*Models, error) {
15+
func NewModels(db db.ConnectionPool, metricsService metrics.MetricsService) (*Models, error) {
1616
if db == nil {
1717
return nil, errors.New("ConnectionPool must be initialized")
1818
}

internal/data/payments.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
type PaymentModel struct {
1515
DB db.ConnectionPool
16-
MetricsService *metrics.MetricsService
16+
MetricsService metrics.MetricsService
1717
}
1818

1919
type Payment struct {

internal/metrics/metrics.go

+49-25
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,32 @@ import (
1010
"github.com/prometheus/client_golang/prometheus"
1111
)
1212

13+
type MetricsService interface {
14+
RegisterPoolMetrics(channel string, pool *pond.WorkerPool)
15+
GetRegistry() *prometheus.Registry
16+
SetNumPaymentOpsIngestedPerLedger(operationType string, value int)
17+
SetNumTssTransactionsIngestedPerLedger(status string, value float64)
18+
SetLatestLedgerIngested(value float64)
19+
ObserveIngestionDuration(ingestionType string, duration float64)
20+
IncNumTSSTransactionsSubmitted()
21+
ObserveTSSTransactionInclusionTime(status string, durationSeconds float64)
22+
IncActiveAccount()
23+
DecActiveAccount()
24+
IncRPCRequests(endpoint string)
25+
ObserveRPCRequestDuration(endpoint string, duration float64)
26+
IncRPCEndpointFailure(endpoint string)
27+
IncRPCEndpointSuccess(endpoint string)
28+
SetRPCServiceHealth(healthy bool)
29+
SetRPCLatestLedger(ledger int64)
30+
IncNumRequests(endpoint, method string, statusCode int)
31+
ObserveRequestDuration(endpoint, method string, duration float64)
32+
ObserveDBQueryDuration(queryType, table string, duration float64)
33+
IncDBQuery(queryType, table string)
34+
RecordTSSTransactionStatusTransition(oldStatus, newStatus string)
35+
}
36+
1337
// MetricsService handles all metrics for the wallet-backend
14-
type MetricsService struct {
38+
type metricsService struct {
1539
registry *prometheus.Registry
1640
db *sqlx.DB
1741

@@ -49,8 +73,8 @@ type MetricsService struct {
4973
}
5074

5175
// NewMetricsService creates a new metrics service with all metrics registered
52-
func NewMetricsService(db *sqlx.DB) *MetricsService {
53-
m := &MetricsService{
76+
func NewMetricsService(db *sqlx.DB) MetricsService {
77+
m := &metricsService{
5478
registry: prometheus.NewRegistry(),
5579
db: db,
5680
}
@@ -197,7 +221,7 @@ func NewMetricsService(db *sqlx.DB) *MetricsService {
197221
return m
198222
}
199223

200-
func (m *MetricsService) registerMetrics() {
224+
func (m *metricsService) registerMetrics() {
201225
collector := sqlstats.NewStatsCollector("wallet-backend-db", m.db)
202226
m.registry.MustRegister(
203227
collector,
@@ -223,7 +247,7 @@ func (m *MetricsService) registerMetrics() {
223247
}
224248

225249
// RegisterPool registers a worker pool for metrics collection
226-
func (m *MetricsService) RegisterPoolMetrics(channel string, pool *pond.WorkerPool) {
250+
func (m *metricsService) RegisterPoolMetrics(channel string, pool *pond.WorkerPool) {
227251
m.registry.MustRegister(prometheus.NewGaugeFunc(
228252
prometheus.GaugeOpts{
229253
Name: fmt.Sprintf("pool_workers_running_%s", channel),
@@ -303,95 +327,95 @@ func (m *MetricsService) RegisterPoolMetrics(channel string, pool *pond.WorkerPo
303327
}
304328

305329
// GetRegistry returns the prometheus registry
306-
func (m *MetricsService) GetRegistry() *prometheus.Registry {
330+
func (m *metricsService) GetRegistry() *prometheus.Registry {
307331
return m.registry
308332
}
309333

310334
// Ingest Service Metrics
311-
func (m *MetricsService) SetNumPaymentOpsIngestedPerLedger(operationType string, value int) {
335+
func (m *metricsService) SetNumPaymentOpsIngestedPerLedger(operationType string, value int) {
312336
m.numPaymentOpsIngestedPerLedger.WithLabelValues(operationType).Set(float64(value))
313337
}
314338

315-
func (m *MetricsService) SetNumTssTransactionsIngestedPerLedger(status string, value float64) {
339+
func (m *metricsService) SetNumTssTransactionsIngestedPerLedger(status string, value float64) {
316340
m.numTssTransactionsIngestedPerLedger.WithLabelValues(status).Set(value)
317341
}
318342

319-
func (m *MetricsService) SetLatestLedgerIngested(value float64) {
343+
func (m *metricsService) SetLatestLedgerIngested(value float64) {
320344
m.latestLedgerIngested.Set(value)
321345
}
322346

323-
func (m *MetricsService) ObserveIngestionDuration(ingestionType string, duration float64) {
347+
func (m *metricsService) ObserveIngestionDuration(ingestionType string, duration float64) {
324348
m.ingestionDuration.WithLabelValues(ingestionType).Observe(duration)
325349
}
326350

327351
// TSS Service Metrics
328-
func (m *MetricsService) IncNumTSSTransactionsSubmitted() {
352+
func (m *metricsService) IncNumTSSTransactionsSubmitted() {
329353
m.numTSSTransactionsSubmitted.Inc()
330354
}
331355

332356
// ObserveTSSTransactionInclusionTime records the time taken for a transaction to be included in the ledger
333-
func (m *MetricsService) ObserveTSSTransactionInclusionTime(status string, durationSeconds float64) {
357+
func (m *metricsService) ObserveTSSTransactionInclusionTime(status string, durationSeconds float64) {
334358
m.timeUntilTSSTransactionInclusion.WithLabelValues(status).Observe(durationSeconds)
335359
}
336360

337361
// Account Service Metrics
338-
func (m *MetricsService) IncActiveAccount() {
362+
func (m *metricsService) IncActiveAccount() {
339363
m.activeAccounts.Inc()
340364
}
341365

342-
func (m *MetricsService) DecActiveAccount() {
366+
func (m *metricsService) DecActiveAccount() {
343367
m.activeAccounts.Dec()
344368
}
345369

346370
// RPC Service Metrics
347-
func (m *MetricsService) IncRPCRequests(endpoint string) {
371+
func (m *metricsService) IncRPCRequests(endpoint string) {
348372
m.rpcRequestsTotal.WithLabelValues(endpoint).Inc()
349373
}
350374

351-
func (m *MetricsService) ObserveRPCRequestDuration(endpoint string, duration float64) {
375+
func (m *metricsService) ObserveRPCRequestDuration(endpoint string, duration float64) {
352376
m.rpcRequestsDuration.WithLabelValues(endpoint).Observe(duration)
353377
}
354378

355-
func (m *MetricsService) IncRPCEndpointFailure(endpoint string) {
379+
func (m *metricsService) IncRPCEndpointFailure(endpoint string) {
356380
m.rpcEndpointFailures.WithLabelValues(endpoint).Inc()
357381
}
358382

359-
func (m *MetricsService) IncRPCEndpointSuccess(endpoint string) {
383+
func (m *metricsService) IncRPCEndpointSuccess(endpoint string) {
360384
m.rpcEndpointSuccesses.WithLabelValues(endpoint).Inc()
361385
}
362386

363-
func (m *MetricsService) SetRPCServiceHealth(healthy bool) {
387+
func (m *metricsService) SetRPCServiceHealth(healthy bool) {
364388
if healthy {
365389
m.rpcServiceHealth.Set(1)
366390
} else {
367391
m.rpcServiceHealth.Set(0)
368392
}
369393
}
370394

371-
func (m *MetricsService) SetRPCLatestLedger(ledger int64) {
395+
func (m *metricsService) SetRPCLatestLedger(ledger int64) {
372396
m.rpcLatestLedger.Set(float64(ledger))
373397
}
374398

375399
// HTTP Request Metrics
376-
func (m *MetricsService) IncNumRequests(endpoint, method string, statusCode int) {
400+
func (m *metricsService) IncNumRequests(endpoint, method string, statusCode int) {
377401
m.numRequestsTotal.WithLabelValues(endpoint, method, strconv.Itoa(statusCode)).Inc()
378402
}
379403

380-
func (m *MetricsService) ObserveRequestDuration(endpoint, method string, duration float64) {
404+
func (m *metricsService) ObserveRequestDuration(endpoint, method string, duration float64) {
381405
m.requestsDuration.WithLabelValues(endpoint, method).Observe(duration)
382406
}
383407

384408
// DB Query Metrics
385-
func (m *MetricsService) ObserveDBQueryDuration(queryType, table string, duration float64) {
409+
func (m *metricsService) ObserveDBQueryDuration(queryType, table string, duration float64) {
386410
m.dbQueryDuration.WithLabelValues(queryType, table).Observe(duration)
387411
}
388412

389-
func (m *MetricsService) IncDBQuery(queryType, table string) {
413+
func (m *metricsService) IncDBQuery(queryType, table string) {
390414
m.dbQueriesTotal.WithLabelValues(queryType, table).Inc()
391415
}
392416

393417
// TSS Transaction Status Metrics
394-
func (m *MetricsService) RecordTSSTransactionStatusTransition(oldStatus, newStatus string) {
418+
func (m *metricsService) RecordTSSTransactionStatusTransition(oldStatus, newStatus string) {
395419
if oldStatus == newStatus {
396420
return
397421
}

internal/metrics/metrics_test.go

+15-31
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,7 @@ func TestNewMetricsService(t *testing.T) {
2424

2525
ms := NewMetricsService(db)
2626
assert.NotNil(t, ms)
27-
assert.NotNil(t, ms.registry)
28-
29-
// Test that all metric vectors are initialized
30-
assert.NotNil(t, ms.numPaymentOpsIngestedPerLedger)
31-
assert.NotNil(t, ms.numTssTransactionsIngestedPerLedger)
32-
assert.NotNil(t, ms.latestLedgerIngested)
33-
assert.NotNil(t, ms.ingestionDuration)
34-
assert.NotNil(t, ms.activeAccounts)
35-
assert.NotNil(t, ms.rpcRequestsTotal)
36-
assert.NotNil(t, ms.rpcRequestsDuration)
37-
assert.NotNil(t, ms.rpcEndpointFailures)
38-
assert.NotNil(t, ms.rpcEndpointSuccesses)
39-
assert.NotNil(t, ms.rpcServiceHealth)
40-
assert.NotNil(t, ms.numRequestsTotal)
41-
assert.NotNil(t, ms.requestsDuration)
42-
assert.NotNil(t, ms.dbQueryDuration)
43-
assert.NotNil(t, ms.dbQueriesTotal)
27+
assert.NotNil(t, ms.GetRegistry())
4428
}
4529

4630
func TestIngestMetrics(t *testing.T) {
@@ -54,7 +38,7 @@ func TestIngestMetrics(t *testing.T) {
5438
ms.SetNumPaymentOpsIngestedPerLedger("payment", 10)
5539

5640
// We can't directly access the metric values, but we can verify they're collected
57-
metricFamilies, err := ms.registry.Gather()
41+
metricFamilies, err := ms.GetRegistry().Gather()
5842
require.NoError(t, err)
5943

6044
found := false
@@ -70,7 +54,7 @@ func TestIngestMetrics(t *testing.T) {
7054
t.Run("latest ledger metrics", func(t *testing.T) {
7155
ms.SetLatestLedgerIngested(1234)
7256

73-
metricFamilies, err := ms.registry.Gather()
57+
metricFamilies, err := ms.GetRegistry().Gather()
7458
require.NoError(t, err)
7559

7660
found := false
@@ -87,7 +71,7 @@ func TestIngestMetrics(t *testing.T) {
8771
ms.ObserveIngestionDuration("payment", 0.5)
8872
ms.ObserveIngestionDuration("transaction", 1.0)
8973

90-
metricFamilies, err := ms.registry.Gather()
74+
metricFamilies, err := ms.GetRegistry().Gather()
9175
require.NoError(t, err)
9276

9377
found := false
@@ -113,7 +97,7 @@ func TestTSSMetrics(t *testing.T) {
11397
ms.IncNumTSSTransactionsSubmitted()
11498
ms.IncNumTSSTransactionsSubmitted()
11599

116-
metricFamilies, err := ms.registry.Gather()
100+
metricFamilies, err := ms.GetRegistry().Gather()
117101
require.NoError(t, err)
118102

119103
found := false
@@ -133,7 +117,7 @@ func TestTSSMetrics(t *testing.T) {
133117
// Test failed transaction
134118
ms.ObserveTSSTransactionInclusionTime("failed", 2.3)
135119

136-
metricFamilies, err := ms.registry.Gather()
120+
metricFamilies, err := ms.GetRegistry().Gather()
137121
require.NoError(t, err)
138122

139123
found := false
@@ -163,7 +147,7 @@ func TestAccountMetrics(t *testing.T) {
163147

164148
t.Run("active accounts counter", func(t *testing.T) {
165149
// Initial state should be 0
166-
_, err := ms.registry.Gather()
150+
_, err := ms.GetRegistry().Gather()
167151
require.NoError(t, err)
168152

169153
// Increment and verify
@@ -173,7 +157,7 @@ func TestAccountMetrics(t *testing.T) {
173157
// Decrement and verify
174158
ms.DecActiveAccount()
175159

176-
metricFamilies, err := ms.registry.Gather()
160+
metricFamilies, err := ms.GetRegistry().Gather()
177161
require.NoError(t, err)
178162

179163
found := false
@@ -200,7 +184,7 @@ func TestRPCMetrics(t *testing.T) {
200184
ms.ObserveRPCRequestDuration(endpoint, 0.1)
201185
ms.IncRPCEndpointSuccess(endpoint)
202186
ms.IncRPCEndpointFailure(endpoint)
203-
metricFamilies, err := ms.registry.Gather()
187+
metricFamilies, err := ms.GetRegistry().Gather()
204188
require.NoError(t, err)
205189

206190
foundRequests := false
@@ -242,7 +226,7 @@ func TestRPCMetrics(t *testing.T) {
242226
t.Run("RPC health metrics", func(t *testing.T) {
243227
ms.SetRPCServiceHealth(true)
244228

245-
metricFamilies, err := ms.registry.Gather()
229+
metricFamilies, err := ms.GetRegistry().Gather()
246230
require.NoError(t, err)
247231

248232
found := false
@@ -256,7 +240,7 @@ func TestRPCMetrics(t *testing.T) {
256240
assert.True(t, found)
257241

258242
ms.SetRPCServiceHealth(false)
259-
metricFamilies, err = ms.registry.Gather()
243+
metricFamilies, err = ms.GetRegistry().Gather()
260244
require.NoError(t, err)
261245

262246
for _, mf := range metricFamilies {
@@ -280,7 +264,7 @@ func TestHTTPMetrics(t *testing.T) {
280264
ms.IncNumRequests(endpoint, method, statusCode)
281265
ms.ObserveRequestDuration(endpoint, method, 0.05)
282266

283-
metricFamilies, err := ms.registry.Gather()
267+
metricFamilies, err := ms.GetRegistry().Gather()
284268
require.NoError(t, err)
285269

286270
foundRequests := false
@@ -330,7 +314,7 @@ func TestDBMetrics(t *testing.T) {
330314
ms.IncDBQuery(queryType, table)
331315
ms.ObserveDBQueryDuration(queryType, table, 0.01)
332316

333-
metricFamilies, err := ms.registry.Gather()
317+
metricFamilies, err := ms.GetRegistry().Gather()
334318
require.NoError(t, err)
335319

336320
foundQueries := false
@@ -390,7 +374,7 @@ func TestPoolMetrics(t *testing.T) {
390374
// Wait for tasks to complete
391375
time.Sleep(20 * time.Millisecond)
392376

393-
metricFamilies, err := ms.registry.Gather()
377+
metricFamilies, err := ms.GetRegistry().Gather()
394378
require.NoError(t, err)
395379

396380
// Map to store metric values
@@ -461,7 +445,7 @@ func TestPoolMetrics(t *testing.T) {
461445
// Wait for tasks to complete
462446
time.Sleep(10 * time.Millisecond)
463447

464-
metricFamilies, err := ms.registry.Gather()
448+
metricFamilies, err := ms.GetRegistry().Gather()
465449
require.NoError(t, err)
466450

467451
metricValues := make(map[string]float64)

internal/serve/httphandler/tss_handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type TSSHandler struct {
2323
AppTracker apptracker.AppTracker
2424
NetworkPassphrase string
2525
TransactionService tssservices.TransactionService
26-
MetricsService *metrics.MetricsService
26+
MetricsService metrics.MetricsService
2727
}
2828

2929
type Transaction struct {

internal/serve/middleware/metrics_middleware.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
// MetricsMiddleware creates a middleware that tracks HTTP request metrics
11-
func MetricsMiddleware(metricsService *metrics.MetricsService) func(next http.Handler) http.Handler {
11+
func MetricsMiddleware(metricsService metrics.MetricsService) func(next http.Handler) http.Handler {
1212
return func(next http.Handler) http.Handler {
1313
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1414
startTime := time.Now()

internal/serve/serve.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ type handlerDeps struct {
9797
AccountService services.AccountService
9898
AccountSponsorshipService services.AccountSponsorshipService
9999
PaymentService services.PaymentService
100-
MetricsService *metrics.MetricsService
100+
MetricsService metrics.MetricsService
101101
// TSS
102102
RPCCallerChannel tss.Channel
103103
ErrorJitterChannel tss.Channel

internal/services/account_service.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ var _ AccountService = (*accountService)(nil)
2121

2222
type accountService struct {
2323
models *data.Models
24-
metricsService *metrics.MetricsService
24+
metricsService metrics.MetricsService
2525
}
2626

27-
func NewAccountService(models *data.Models, metricsService *metrics.MetricsService) (*accountService, error) {
27+
func NewAccountService(models *data.Models, metricsService metrics.MetricsService) (*accountService, error) {
2828
if models == nil {
2929
return nil, errors.New("models cannot be nil")
3030
}

0 commit comments

Comments
 (0)