Files
tasks/common/metrics/tally_metrics_handler_test.go
Roey BermanandGitHub 243effc6e1 Forbid the use of pborman/uuid and remove uses (#8541)
## What

Used the `depguard` linter to prevent importing the package and modified
all uses of it.

Also fixed a bug in the membership code where we were not updating the
next page token:

https://github.com/temporalio/temporal/pull/8541/files#diff-985dd64ffa4c6bfe0248b982dc47439b725862baa3e1f2c559b7ff522bf21969R350


## Why?

From the package README in GitHub:

> This package now leverages the github.com/google/uuid package (which
is based off an earlier version of this package).

Also, there's no point in having two uuid implementations in the
codebase.

## Test

Added the invalid import and confirmed it works:

```
../service/frontend/admin_handler_test.go:13:2: import 'github.com/pborman/uuid' is not allowed from list 'main': Importing github.com/pborman/uuid is disallowed; use github.com/google/uuid instead (depguard)
        _ "github.com/pborman/uuid"

```
2025-11-26 01:33:02 +00:00

90 lines
3.1 KiB
Go

package metrics
import (
"math"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/uber-go/tally/v4"
)
var defaultConfig = ClientConfig{
Tags: nil,
ExcludeTags: map[string][]string{
"taskqueue": {"__sticky__"},
"activityType": {},
"workflowType": {},
},
Prefix: "",
PerUnitHistogramBoundaries: map[string][]float64{
Dimensionless: {0, 10, 100},
Bytes: {1024, 2048},
Milliseconds: {10, 500, 1000, 5000, 10000},
Seconds: {0.01, 0.5, 1, 5, 10},
},
}
func TestTallyScope(t *testing.T) {
scope := tally.NewTestScope("test", map[string]string{})
mp := NewTallyMetricsHandler(defaultConfig, scope)
recordTallyMetrics(mp)
snap := scope.Snapshot()
counters, gauges, timers, histograms := snap.Counters(), snap.Gauges(), snap.Timers(), snap.Histograms()
assert.EqualValues(t, 8, counters["test.hits+"].Value())
assert.EqualValues(t, map[string]string{}, counters["test.hits+"].Tags())
assert.EqualValues(t, 11, counters["test.hits-tagged+taskqueue=__sticky__"].Value())
assert.EqualValues(t, map[string]string{"taskqueue": "__sticky__"}, counters["test.hits-tagged+taskqueue=__sticky__"].Tags())
assert.EqualValues(t, 14, counters["test.hits-tagged-excluded+taskqueue="+tagExcludedValue].Value())
assert.EqualValues(t, map[string]string{"taskqueue": tagExcludedValue}, counters["test.hits-tagged-excluded+taskqueue="+tagExcludedValue].Tags())
assert.EqualValues(t, float64(-100), gauges["test.temp+location=Mare Imbrium"].Value())
assert.EqualValues(t, map[string]string{
"location": "Mare Imbrium",
}, gauges["test.temp+location=Mare Imbrium"].Tags())
assert.EqualValues(t, []time.Duration{
1248 * time.Millisecond,
5255 * time.Millisecond,
}, timers["test.latency+"].Values())
assert.EqualValues(t, map[string]string{}, timers["test.latency+"].Tags())
assert.EqualValues(t, map[float64]int64{
1024: 0,
2048: 0,
math.MaxFloat64: 1,
}, histograms["test.transmission+"].Values())
assert.EqualValues(t, map[time.Duration]int64(nil), histograms["test.transmission+"].Durations())
assert.EqualValues(t, map[string]string{}, histograms["test.transmission+"].Tags())
newTaggedHandler := mp.WithTags(NamespaceTag(uuid.NewString()))
recordTallyMetrics(newTaggedHandler)
snap = scope.Snapshot()
counters = snap.Counters()
assert.EqualValues(t, 11, counters["test.hits-tagged+taskqueue=__sticky__"].Value())
assert.EqualValues(t, map[string]string{"taskqueue": "__sticky__"}, counters["test.hits-tagged+taskqueue=__sticky__"].Tags())
}
func recordTallyMetrics(h Handler) {
hitsCounter := h.Counter("hits")
gauge := h.Gauge("temp")
timer := h.Timer("latency")
histogram := h.Histogram("transmission", Bytes)
hitsTaggedCounter := h.Counter("hits-tagged")
hitsTaggedExcludedCounter := h.Counter("hits-tagged-excluded")
hitsCounter.Record(8)
gauge.Record(-100, StringTag("location", "Mare Imbrium"))
timer.Record(1248 * time.Millisecond)
timer.Record(5255 * time.Millisecond)
histogram.Record(1234567)
hitsTaggedCounter.Record(11, UnsafeTaskQueueTag("__sticky__"))
hitsTaggedExcludedCounter.Record(14, UnsafeTaskQueueTag("filtered"))
}