Files
tasks/service/history/archival_queue_factory_test.go
Ben EddyandGitHub 6eb213b779 Use a concrete type for metrics.Tag (#8384)
## What changed?

Changes `metrics.Tag` from an interface to a concrete type. 

## Why?

To reduce allocations and GC pressure. Tags are often created in hot
paths as part of metrics instrumentation, and interface boxing can
trigger heap allocations.

Here's a benchmark for `metrics.OperationTag("test")`
```
BenchmarkTags-14    	1000000000	       0.3070 ns/op	       0 B/op	       0 allocs/op
BenchmarkTags-14    	69621046	        17.08 ns/op	      32 B/op	       1 allocs/op
```

## How did you test it?
- [x] built
- [x] run locally and tested manually
- [x] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)
2025-10-07 09:50:06 -07:00

70 lines
2.2 KiB
Go

package history
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace/noop"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common/clock"
"go.temporal.io/server/common/cluster"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/primitives/timestamp"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/tasks"
"go.temporal.io/server/service/history/tests"
"go.uber.org/mock/gomock"
)
func TestArchivalQueueFactory(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
metricsHandler := metrics.NewMockHandler(ctrl)
metricsHandler.EXPECT().WithTags(gomock.Any()).DoAndReturn(
func(tags ...metrics.Tag) metrics.Handler {
require.Len(t, tags, 1)
assert.Equal(t, metrics.OperationTagName, tags[0].Key)
assert.Equal(t, "ArchivalQueueProcessor", tags[0].Value)
return metricsHandler
},
).Times(1)
metricsHandler.EXPECT().WithTags(gomock.Any()).Return(metricsHandler).Times(1)
mockShard := shard.NewTestContext(
ctrl,
&persistencespb.ShardInfo{
ShardId: 0,
RangeId: 1,
QueueStates: map[int32]*persistencespb.QueueState{
int32(tasks.CategoryIDArchival): {
ReaderStates: nil,
ExclusiveReaderHighWatermark: &persistencespb.TaskKey{
FireTime: timestamp.TimeNowPtrUtc(),
},
},
},
},
tests.NewDynamicConfig(),
)
mockShard.Resource.ClusterMetadata.EXPECT().GetCurrentClusterName().Return(cluster.TestCurrentClusterName).AnyTimes()
queueFactory := NewArchivalQueueFactory(ArchivalQueueFactoryParams{
QueueFactoryBaseParams: QueueFactoryBaseParams{
NamespaceRegistry: mockShard.Resource.GetNamespaceRegistry(),
ClusterMetadata: mockShard.Resource.GetClusterMetadata(),
Config: tests.NewDynamicConfig(),
TimeSource: clock.NewEventTimeSource(),
MetricsHandler: metricsHandler,
Logger: log.NewNoopLogger(),
TracerProvider: noop.NewTracerProvider(),
},
})
queue := queueFactory.CreateQueue(mockShard)
require.NotNil(t, queue)
assert.Equal(t, tasks.CategoryArchival, queue.Category())
}