Skip to content

Commit c5e867c

Browse files
committed
feat(auditor): add metrics instrumentation to SetStatus, GetStatus and GetTokenRequest
Audit(), Append() and Release() already recorded duration histograms and error counters, but the three query methods SetStatus, GetStatus and GetTokenRequest had no instrumentation at all. In production these are high-frequency calls on the audit path and without metrics operators have no way to detect slow queries or elevated error rates. Add duration histograms and error counters for all three methods using the same pattern already established in the same file, and update the internal metrics tests to reflect the new counter and histogram counts. Signed-off-by: Rama542 <Rama542@users.noreply.github.com>
1 parent 7f05782 commit c5e867c

3 files changed

Lines changed: 95 additions & 7 deletions

File tree

token/services/auditor/auditor.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,44 @@ func (a *Service) Release(ctx context.Context, tx Transaction) {
187187

188188
// SetStatus sets the status of the audit records with the passed transaction id to the passed status
189189
func (a *Service) SetStatus(ctx context.Context, txID string, status storage.TxStatus, message string) error {
190-
return a.auditDB.SetStatus(ctx, txID, status, message)
190+
start := time.Now()
191+
defer func() { a.metrics.SetStatusDuration.Observe(time.Since(start).Seconds()) }()
192+
if err := a.auditDB.SetStatus(ctx, txID, status, message); err != nil {
193+
a.metrics.SetStatusErrors.Add(1)
194+
195+
return err
196+
}
197+
198+
return nil
191199
}
192200

193201
// GetStatus return the status of the given transaction id.
194202
// It returns an error if no transaction with that id is found
195203
func (a *Service) GetStatus(ctx context.Context, txID string) (TxStatus, string, error) {
196-
return a.auditDB.GetStatus(ctx, txID)
204+
start := time.Now()
205+
defer func() { a.metrics.GetStatusDuration.Observe(time.Since(start).Seconds()) }()
206+
status, message, err := a.auditDB.GetStatus(ctx, txID)
207+
if err != nil {
208+
a.metrics.GetStatusErrors.Add(1)
209+
210+
return status, message, err
211+
}
212+
213+
return status, message, nil
197214
}
198215

199216
// GetTokenRequest returns the token request bound to the passed transaction id, if available.
200217
func (a *Service) GetTokenRequest(ctx context.Context, txID string) ([]byte, error) {
201-
return a.auditDB.GetTokenRequest(ctx, txID)
218+
start := time.Now()
219+
defer func() { a.metrics.GetTokenRequestDuration.Observe(time.Since(start).Seconds()) }()
220+
data, err := a.auditDB.GetTokenRequest(ctx, txID)
221+
if err != nil {
222+
a.metrics.GetTokenRequestErrors.Add(1)
223+
224+
return nil, err
225+
}
226+
227+
return data, nil
202228
}
203229

204230
func (a *Service) Check(ctx context.Context) ([]string, error) {

token/services/auditor/auditor_internal_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ func TestNewMetrics_NilProvider(t *testing.T) {
5050
assert.NotNil(t, m.AppendDuration)
5151
assert.NotNil(t, m.AppendErrors)
5252
assert.NotNil(t, m.ReleasesTotal)
53+
assert.NotNil(t, m.SetStatusDuration)
54+
assert.NotNil(t, m.SetStatusErrors)
55+
assert.NotNil(t, m.GetStatusDuration)
56+
assert.NotNil(t, m.GetStatusErrors)
57+
assert.NotNil(t, m.GetTokenRequestDuration)
58+
assert.NotNil(t, m.GetTokenRequestErrors)
5359
}
5460

5561
func TestNewMetrics_WithProvider(t *testing.T) {
@@ -60,10 +66,12 @@ func TestNewMetrics_WithProvider(t *testing.T) {
6066

6167
m := newMetrics(mp)
6268
require.NotNil(t, m)
63-
// AuditLockConflicts, AppendErrors, ReleasesTotal = 3 counters
64-
assert.Equal(t, 3, mp.NewCounterCallCount())
65-
// AuditDuration, AppendDuration = 2 histograms
66-
assert.Equal(t, 2, mp.NewHistogramCallCount())
69+
// AuditLockConflicts, AppendErrors, ReleasesTotal,
70+
// SetStatusErrors, GetStatusErrors, GetTokenRequestErrors = 6 counters
71+
assert.Equal(t, 6, mp.NewCounterCallCount())
72+
// AuditDuration, AppendDuration,
73+
// SetStatusDuration, GetStatusDuration, GetTokenRequestDuration = 5 histograms
74+
assert.Equal(t, 5, mp.NewHistogramCallCount())
6775
}
6876

6977
func TestNoopCounter_With_ReturnsSelf(t *testing.T) {
@@ -182,9 +190,15 @@ func TestMetricsProviderCall(t *testing.T) {
182190
m.AuditLockConflicts.Add(1)
183191
m.AppendErrors.Add(1)
184192
m.ReleasesTotal.Add(1)
193+
m.SetStatusErrors.Add(1)
194+
m.GetStatusErrors.Add(1)
195+
m.GetTokenRequestErrors.Add(1)
185196

186197
m.AuditDuration.Observe(1.0)
187198
m.AppendDuration.Observe(1.0)
199+
m.SetStatusDuration.Observe(1.0)
200+
m.GetStatusDuration.Observe(1.0)
201+
m.GetTokenRequestDuration.Observe(1.0)
188202
})
189203

190204
nc := &noopCounter{}

token/services/auditor/metrics.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ type Metrics struct {
3131
// ReleasesTotal counts all calls to Release(), whether invoked explicitly
3232
// or via the defer inside Append().
3333
ReleasesTotal metrics.Counter
34+
35+
// SetStatusDuration is a histogram of the wall-clock time for each
36+
// SetStatus() invocation, in seconds.
37+
SetStatusDuration metrics.Histogram
38+
39+
// SetStatusErrors counts calls to SetStatus() that returned an error.
40+
SetStatusErrors metrics.Counter
41+
42+
// GetStatusDuration is a histogram of the wall-clock time for each
43+
// GetStatus() invocation, in seconds.
44+
GetStatusDuration metrics.Histogram
45+
46+
// GetStatusErrors counts calls to GetStatus() that returned an error.
47+
GetStatusErrors metrics.Counter
48+
49+
// GetTokenRequestDuration is a histogram of the wall-clock time for each
50+
// GetTokenRequest() invocation, in seconds.
51+
GetTokenRequestDuration metrics.Histogram
52+
53+
// GetTokenRequestErrors counts calls to GetTokenRequest() that returned an error.
54+
GetTokenRequestErrors metrics.Counter
3455
}
3556

3657
func newMetrics(p metrics.Provider) *Metrics {
@@ -61,6 +82,33 @@ func newMetrics(p metrics.Provider) *Metrics {
6182
Name: "auditor_releases_total",
6283
Help: "Total number of Release() calls (explicit and deferred)",
6384
}),
85+
SetStatusDuration: p.NewHistogram(metrics.HistogramOpts{
86+
Name: "auditor_set_status_duration_seconds",
87+
Help: "Histogram of SetStatus() processing time per call, in seconds",
88+
Buckets: []float64{.001, .005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10},
89+
}),
90+
SetStatusErrors: p.NewCounter(metrics.CounterOpts{
91+
Name: "auditor_set_status_errors_total",
92+
Help: "Total number of SetStatus() calls that returned an error",
93+
}),
94+
GetStatusDuration: p.NewHistogram(metrics.HistogramOpts{
95+
Name: "auditor_get_status_duration_seconds",
96+
Help: "Histogram of GetStatus() processing time per call, in seconds",
97+
Buckets: []float64{.001, .005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10},
98+
}),
99+
GetStatusErrors: p.NewCounter(metrics.CounterOpts{
100+
Name: "auditor_get_status_errors_total",
101+
Help: "Total number of GetStatus() calls that returned an error",
102+
}),
103+
GetTokenRequestDuration: p.NewHistogram(metrics.HistogramOpts{
104+
Name: "auditor_get_token_request_duration_seconds",
105+
Help: "Histogram of GetTokenRequest() processing time per call, in seconds",
106+
Buckets: []float64{.001, .005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10},
107+
}),
108+
GetTokenRequestErrors: p.NewCounter(metrics.CounterOpts{
109+
Name: "auditor_get_token_request_errors_total",
110+
Help: "Total number of GetTokenRequest() calls that returned an error",
111+
}),
64112
}
65113
}
66114

0 commit comments

Comments
 (0)