From 22d70565ca260179693c3c983b8a1102da25582c Mon Sep 17 00:00:00 2001 From: Nathan Oorloff Date: Tue, 15 Oct 2024 13:19:29 -0400 Subject: [PATCH] Separate interface --- common/metrics/metrics.go | 11 +- common/metrics/metrics_mock.go | 167 ++++++++++++++++-- common/metrics/metricstest/capture_handler.go | 5 +- common/metrics/noop_impl.go | 5 +- common/metrics/otel_metrics_handler.go | 5 +- common/metrics/tally_metrics_handler.go | 5 +- .../history_builder_categorization_test.go | 5 +- service/history/workflow/metrics.go | 4 +- 8 files changed, 171 insertions(+), 36 deletions(-) diff --git a/common/metrics/metrics.go b/common/metrics/metrics.go index 5900698d1f7d..a5d83f5995dc 100644 --- a/common/metrics/metrics.go +++ b/common/metrics/metrics.go @@ -60,9 +60,14 @@ type ( Stop(log.Logger) - // BatchStart returns a Handler that where supported, can emit a series of metrics as a single "wide event". + // StartBatch returns a BatchHandler that can emit a series of metrics as a single "wide event". // If wide events aren't supported in the underlying implementation, metrics can still be sent individually. - BatchStart(string) (Handler, io.Closer) + StartBatch(string) BatchHandler + } + + BatchHandler interface { + Handler + io.Closer } // CounterIface is an ever-increasing counter. @@ -71,7 +76,7 @@ type ( // Tags provided are merged with the source MetricsHandler Record(int64, ...Tag) } - // GaugeIface can be set to any float and repesents a latest value instrument. + // GaugeIface can be set to any float and represents a latest value instrument. GaugeIface interface { // Record updates the gauge value. // Tags provided are merged with the source MetricsHandler diff --git a/common/metrics/metrics_mock.go b/common/metrics/metrics_mock.go index 0d2fd6b4fe09..8397357fc879 100644 --- a/common/metrics/metrics_mock.go +++ b/common/metrics/metrics_mock.go @@ -34,7 +34,6 @@ package metrics import ( - io "io" reflect "reflect" time "time" @@ -65,21 +64,6 @@ func (m *MockHandler) EXPECT() *MockHandlerMockRecorder { return m.recorder } -// BatchStart mocks base method. -func (m *MockHandler) BatchStart(arg0 string) (Handler, io.Closer) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "BatchStart", arg0) - ret0, _ := ret[0].(Handler) - ret1, _ := ret[1].(io.Closer) - return ret0, ret1 -} - -// BatchStart indicates an expected call of BatchStart. -func (mr *MockHandlerMockRecorder) BatchStart(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BatchStart", reflect.TypeOf((*MockHandler)(nil).BatchStart), arg0) -} - // Counter mocks base method. func (m *MockHandler) Counter(arg0 string) CounterIface { m.ctrl.T.Helper() @@ -122,6 +106,20 @@ func (mr *MockHandlerMockRecorder) Histogram(arg0, arg1 any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Histogram", reflect.TypeOf((*MockHandler)(nil).Histogram), arg0, arg1) } +// StartBatch mocks base method. +func (m *MockHandler) StartBatch(arg0 string) BatchHandler { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "StartBatch", arg0) + ret0, _ := ret[0].(BatchHandler) + return ret0 +} + +// StartBatch indicates an expected call of StartBatch. +func (mr *MockHandlerMockRecorder) StartBatch(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StartBatch", reflect.TypeOf((*MockHandler)(nil).StartBatch), arg0) +} + // Stop mocks base method. func (m *MockHandler) Stop(arg0 log.Logger) { m.ctrl.T.Helper() @@ -166,6 +164,143 @@ func (mr *MockHandlerMockRecorder) WithTags(arg0 ...any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithTags", reflect.TypeOf((*MockHandler)(nil).WithTags), arg0...) } +// MockBatchHandler is a mock of BatchHandler interface. +type MockBatchHandler struct { + ctrl *gomock.Controller + recorder *MockBatchHandlerMockRecorder +} + +// MockBatchHandlerMockRecorder is the mock recorder for MockBatchHandler. +type MockBatchHandlerMockRecorder struct { + mock *MockBatchHandler +} + +// NewMockBatchHandler creates a new mock instance. +func NewMockBatchHandler(ctrl *gomock.Controller) *MockBatchHandler { + mock := &MockBatchHandler{ctrl: ctrl} + mock.recorder = &MockBatchHandlerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockBatchHandler) EXPECT() *MockBatchHandlerMockRecorder { + return m.recorder +} + +// Close mocks base method. +func (m *MockBatchHandler) Close() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Close") + ret0, _ := ret[0].(error) + return ret0 +} + +// Close indicates an expected call of Close. +func (mr *MockBatchHandlerMockRecorder) Close() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockBatchHandler)(nil).Close)) +} + +// Counter mocks base method. +func (m *MockBatchHandler) Counter(arg0 string) CounterIface { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Counter", arg0) + ret0, _ := ret[0].(CounterIface) + return ret0 +} + +// Counter indicates an expected call of Counter. +func (mr *MockBatchHandlerMockRecorder) Counter(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Counter", reflect.TypeOf((*MockBatchHandler)(nil).Counter), arg0) +} + +// Gauge mocks base method. +func (m *MockBatchHandler) Gauge(arg0 string) GaugeIface { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Gauge", arg0) + ret0, _ := ret[0].(GaugeIface) + return ret0 +} + +// Gauge indicates an expected call of Gauge. +func (mr *MockBatchHandlerMockRecorder) Gauge(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Gauge", reflect.TypeOf((*MockBatchHandler)(nil).Gauge), arg0) +} + +// Histogram mocks base method. +func (m *MockBatchHandler) Histogram(arg0 string, arg1 MetricUnit) HistogramIface { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Histogram", arg0, arg1) + ret0, _ := ret[0].(HistogramIface) + return ret0 +} + +// Histogram indicates an expected call of Histogram. +func (mr *MockBatchHandlerMockRecorder) Histogram(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Histogram", reflect.TypeOf((*MockBatchHandler)(nil).Histogram), arg0, arg1) +} + +// StartBatch mocks base method. +func (m *MockBatchHandler) StartBatch(arg0 string) BatchHandler { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "StartBatch", arg0) + ret0, _ := ret[0].(BatchHandler) + return ret0 +} + +// StartBatch indicates an expected call of StartBatch. +func (mr *MockBatchHandlerMockRecorder) StartBatch(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StartBatch", reflect.TypeOf((*MockBatchHandler)(nil).StartBatch), arg0) +} + +// Stop mocks base method. +func (m *MockBatchHandler) Stop(arg0 log.Logger) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Stop", arg0) +} + +// Stop indicates an expected call of Stop. +func (mr *MockBatchHandlerMockRecorder) Stop(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Stop", reflect.TypeOf((*MockBatchHandler)(nil).Stop), arg0) +} + +// Timer mocks base method. +func (m *MockBatchHandler) Timer(arg0 string) TimerIface { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Timer", arg0) + ret0, _ := ret[0].(TimerIface) + return ret0 +} + +// Timer indicates an expected call of Timer. +func (mr *MockBatchHandlerMockRecorder) Timer(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Timer", reflect.TypeOf((*MockBatchHandler)(nil).Timer), arg0) +} + +// WithTags mocks base method. +func (m *MockBatchHandler) WithTags(arg0 ...Tag) Handler { + m.ctrl.T.Helper() + varargs := []any{} + for _, a := range arg0 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "WithTags", varargs...) + ret0, _ := ret[0].(Handler) + return ret0 +} + +// WithTags indicates an expected call of WithTags. +func (mr *MockBatchHandlerMockRecorder) WithTags(arg0 ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithTags", reflect.TypeOf((*MockBatchHandler)(nil).WithTags), arg0...) +} + // MockCounterIface is a mock of CounterIface interface. type MockCounterIface struct { ctrl *gomock.Controller diff --git a/common/metrics/metricstest/capture_handler.go b/common/metrics/metricstest/capture_handler.go index b88e9044e9e1..e5088b55f0b8 100644 --- a/common/metrics/metricstest/capture_handler.go +++ b/common/metrics/metricstest/capture_handler.go @@ -25,7 +25,6 @@ package metricstest import ( - "io" "sync" "time" @@ -147,8 +146,8 @@ func (c *CaptureHandler) Close() error { return nil } -func (c *CaptureHandler) BatchStart(_ string) (metrics.Handler, io.Closer) { - return c, c +func (c *CaptureHandler) StartBatch(_ string) metrics.BatchHandler { + return c } // Stop implements [metrics.Handler.Stop]. diff --git a/common/metrics/noop_impl.go b/common/metrics/noop_impl.go index d527f4aa9e5f..ba3504b60cf4 100644 --- a/common/metrics/noop_impl.go +++ b/common/metrics/noop_impl.go @@ -25,7 +25,6 @@ package metrics import ( - "io" "time" "go.temporal.io/server/common/log" @@ -73,8 +72,8 @@ func (*noopMetricsHandler) Close() error { return nil } -func (n *noopMetricsHandler) BatchStart(_ string) (Handler, io.Closer) { - return n, n +func (n *noopMetricsHandler) StartBatch(_ string) BatchHandler { + return n } var NoopCounterMetricFunc = CounterFunc(func(i int64, t ...Tag) {}) diff --git a/common/metrics/otel_metrics_handler.go b/common/metrics/otel_metrics_handler.go index 65c3bdf97fe9..3d52064c16cb 100644 --- a/common/metrics/otel_metrics_handler.go +++ b/common/metrics/otel_metrics_handler.go @@ -27,7 +27,6 @@ package metrics import ( "context" "fmt" - "io" "sync" "time" @@ -209,8 +208,8 @@ func (omp *otelMetricsHandler) Close() error { return nil } -func (omp *otelMetricsHandler) BatchStart(_ string) (Handler, io.Closer) { - return omp, omp +func (omp *otelMetricsHandler) StartBatch(_ string) BatchHandler { + return omp } // makeSet returns an otel attribute.Set with the given tags merged with the diff --git a/common/metrics/tally_metrics_handler.go b/common/metrics/tally_metrics_handler.go index c5150554c841..4d5f2846df44 100644 --- a/common/metrics/tally_metrics_handler.go +++ b/common/metrics/tally_metrics_handler.go @@ -25,7 +25,6 @@ package metrics import ( - "io" "time" "github.com/uber-go/tally/v4" @@ -125,8 +124,8 @@ func (*tallyMetricsHandler) Close() error { return nil } -func (tmh *tallyMetricsHandler) BatchStart(_ string) (Handler, io.Closer) { - return tmh, tmh +func (tmh *tallyMetricsHandler) StartBatch(_ string) BatchHandler { + return tmh } func tagsToMap(t1 []Tag, e excludeTags) map[string]string { diff --git a/service/history/historybuilder/history_builder_categorization_test.go b/service/history/historybuilder/history_builder_categorization_test.go index 430f828bec5b..2d5eb220b94f 100644 --- a/service/history/historybuilder/history_builder_categorization_test.go +++ b/service/history/historybuilder/history_builder_categorization_test.go @@ -25,7 +25,6 @@ package historybuilder import ( - "io" "testing" "time" @@ -71,8 +70,8 @@ func (h StubHandler) Close() error { return nil } -func (h StubHandler) BatchStart(_ string) (metrics.Handler, io.Closer) { - return h, h +func (h StubHandler) StartBatch(_ string) metrics.BatchHandler { + return h } func TestHistoryBuilder_IsDirty(t *testing.T) { diff --git a/service/history/workflow/metrics.go b/service/history/workflow/metrics.go index b9a3c3c8dc74..783d4ca741ff 100644 --- a/service/history/workflow/metrics.go +++ b/service/history/workflow/metrics.go @@ -61,8 +61,8 @@ func emitMutableStateStatus( return } - batchHandler, closer := metricsHandler.BatchStart("mutable_state_status") - defer closer.Close() + batchHandler := metricsHandler.StartBatch("mutable_state_status") + defer batchHandler.Close() metrics.MutableStateSize.With(batchHandler).Record(int64(stats.TotalSize)) metrics.ExecutionInfoSize.With(batchHandler).Record(int64(stats.ExecutionInfoSize)) metrics.ExecutionStateSize.With(batchHandler).Record(int64(stats.ExecutionStateSize))