Skip to content

Commit 9b8cb09

Browse files
committed
Refactor metrics emitter interface
Use a single metrics emitter abstraction for gauge, count, and histogram samples so metric helpers share one testable client contract.
1 parent 7b25a86 commit 9b8cb09

8 files changed

Lines changed: 28 additions & 7 deletions

File tree

go/base/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ type MigrationContext struct {
238238
AbortError error
239239
abortMutex *sync.Mutex
240240

241-
Metrics metrics.MemStatsGaugeEmitter
241+
Metrics metrics.Emitter
242242

243243
OriginalTableColumnsOnApplier *sql.ColumnList
244244
OriginalTableColumns *sql.ColumnList

go/logic/migrator_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,12 @@ func (s *progressGaugeSpy) Gauge(name string, value float64, tags ...string) {
419419
s.tags = append(s.tags, append([]string(nil), tags...))
420420
}
421421

422+
func (s *progressGaugeSpy) Count(name string, value int64, tags ...string) {
423+
}
424+
425+
func (s *progressGaugeSpy) Histogram(name string, value float64, tags ...string) {
426+
}
427+
422428
func TestReportStatusEmitsProgressGaugesEveryTick(t *testing.T) {
423429
spy := &progressGaugeSpy{}
424430
ctx := base.NewMigrationContext()

go/metrics/binlog_backlog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package metrics
77

88
// EmitBinlogBacklogGauges emits apply-events queue depth gauges (namespace is applied by the client):
99
// gh_ost.binlog.backlog_size, gh_ost.binlog.backlog_capacity, gh_ost.binlog.backlog_utilization.
10-
func EmitBinlogBacklogGauges(emit MemStatsGaugeEmitter, backlogSize, backlogCapacity int) {
10+
func EmitBinlogBacklogGauges(emit Emitter, backlogSize, backlogCapacity int) {
1111
if emit == nil {
1212
return
1313
}

go/metrics/client.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ type Client struct {
2121
sd *statsd.Client
2222
}
2323

24-
// MemStatsGaugeEmitter is implemented by *Client; used for tests without UDP.
25-
type MemStatsGaugeEmitter interface {
24+
// Emitter is implemented by *Client; used for tests without UDP.
25+
type Emitter interface {
2626
Gauge(name string, value float64, tags ...string)
27+
Count(name string, value int64, tags ...string)
28+
Histogram(name string, value float64, tags ...string)
2729
}
2830

2931
// NewClient connects to addr for StatsD. If addr is empty, returns Noop and nil error.
@@ -65,6 +67,13 @@ func (c *Client) Count(name string, value int64, tags ...string) {
6567
_ = c.sd.Count(name, value, tags, 1.0)
6668
}
6769

70+
func (c *Client) Histogram(name string, value float64, tags ...string) {
71+
if c.sd == nil {
72+
return
73+
}
74+
_ = c.sd.Histogram(name, value, tags, 1.0)
75+
}
76+
6877
// Close flushes buffered metrics; safe for Noop.
6978
func (c *Client) Close() error {
7079
if c.sd == nil {

go/metrics/go_runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
// EmitGoRuntimeGauges emits gh_ost.go_runtime.* gauges (namespace is applied by the client).
1515
// m and numGoroutine are typically from runtime.ReadMemStats and runtime.NumGoroutine.
16-
func EmitGoRuntimeGauges(emit MemStatsGaugeEmitter, m *runtime.MemStats, numGoroutine int) {
16+
func EmitGoRuntimeGauges(emit Emitter, m *runtime.MemStats, numGoroutine int) {
1717
if emit == nil || m == nil {
1818
return
1919
}

go/metrics/go_runtime_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ func (g *gaugeSpy) Gauge(name string, value float64, tags ...string) {
2424
g.tags = append(g.tags, append([]string(nil), tags...))
2525
}
2626

27+
func (g *gaugeSpy) Count(name string, value int64, tags ...string) {
28+
}
29+
30+
func (g *gaugeSpy) Histogram(name string, value float64, tags ...string) {
31+
}
32+
2733
func TestEmitGoRuntimeGauges(t *testing.T) {
2834
spy := &gaugeSpy{}
2935
m := &runtime.MemStats{

go/metrics/lag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import "fmt"
1313
// These are point-in-time readings each status tick (not a distribution), so gauges are used
1414
// rather than histograms — DogStatsD histogram aggregation exposes count/max series that do not
1515
// match the log line lag values in Prometheus/Grafana.
16-
func EmitLagGauges(emit MemStatsGaugeEmitter, replicationLagSeconds, heartbeatLagSeconds float64, throttled bool) {
16+
func EmitLagGauges(emit Emitter, replicationLagSeconds, heartbeatLagSeconds float64, throttled bool) {
1717
if emit == nil {
1818
return
1919
}

go/metrics/progress.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package metrics
77

88
// EmitProgressGauges emits row-copy and DML progress gauges (namespace is applied by the client):
99
// gh_ost.row_copy.rows_copied, gh_ost.row_copy.rows_estimate, gh_ost.dml.events_applied.
10-
func EmitProgressGauges(emit MemStatsGaugeEmitter, rowsCopied, rowsEstimate, dmlEventsApplied int64) {
10+
func EmitProgressGauges(emit Emitter, rowsCopied, rowsEstimate, dmlEventsApplied int64) {
1111
if emit == nil {
1212
return
1313
}

0 commit comments

Comments
 (0)