go.temporal.io/server@v1.23.0/common/metrics/tally_metrics_handler_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package metrics 26 27 import ( 28 "math" 29 "testing" 30 "time" 31 32 "github.com/pborman/uuid" 33 "github.com/stretchr/testify/assert" 34 "github.com/uber-go/tally/v4" 35 ) 36 37 var defaultConfig = ClientConfig{ 38 Tags: nil, 39 ExcludeTags: map[string][]string{ 40 "taskqueue": {"__sticky__"}, 41 "activityType": {}, 42 "workflowType": {}, 43 }, 44 Prefix: "", 45 PerUnitHistogramBoundaries: map[string][]float64{Dimensionless: {0, 10, 100}, Bytes: {1024, 2048}, Milliseconds: {10, 500, 1000, 5000, 10000}}, 46 } 47 48 func TestTallyScope(t *testing.T) { 49 scope := tally.NewTestScope("test", map[string]string{}) 50 mp := NewTallyMetricsHandler(defaultConfig, scope) 51 recordTallyMetrics(mp) 52 53 snap := scope.Snapshot() 54 counters, gauges, timers, histograms := snap.Counters(), snap.Gauges(), snap.Timers(), snap.Histograms() 55 56 assert.EqualValues(t, 8, counters["test.hits+"].Value()) 57 assert.EqualValues(t, map[string]string{}, counters["test.hits+"].Tags()) 58 59 assert.EqualValues(t, 11, counters["test.hits-tagged+taskqueue=__sticky__"].Value()) 60 assert.EqualValues(t, map[string]string{"taskqueue": "__sticky__"}, counters["test.hits-tagged+taskqueue=__sticky__"].Tags()) 61 62 assert.EqualValues(t, 14, counters["test.hits-tagged-excluded+taskqueue="+tagExcludedValue].Value()) 63 assert.EqualValues(t, map[string]string{"taskqueue": tagExcludedValue}, counters["test.hits-tagged-excluded+taskqueue="+tagExcludedValue].Tags()) 64 65 assert.EqualValues(t, float64(-100), gauges["test.temp+location=Mare Imbrium"].Value()) 66 assert.EqualValues(t, map[string]string{ 67 "location": "Mare Imbrium", 68 }, gauges["test.temp+location=Mare Imbrium"].Tags()) 69 70 assert.EqualValues(t, []time.Duration{ 71 1248 * time.Millisecond, 72 5255 * time.Millisecond, 73 }, timers["test.latency+"].Values()) 74 assert.EqualValues(t, map[string]string{}, timers["test.latency+"].Tags()) 75 76 assert.EqualValues(t, map[float64]int64{ 77 1024: 0, 78 2048: 0, 79 math.MaxFloat64: 1, 80 }, histograms["test.transmission+"].Values()) 81 assert.EqualValues(t, map[time.Duration]int64(nil), histograms["test.transmission+"].Durations()) 82 assert.EqualValues(t, map[string]string{}, histograms["test.transmission+"].Tags()) 83 84 newTaggedHandler := mp.WithTags(NamespaceTag(uuid.New())) 85 recordTallyMetrics(newTaggedHandler) 86 snap = scope.Snapshot() 87 counters = snap.Counters() 88 89 assert.EqualValues(t, 11, counters["test.hits-tagged+taskqueue=__sticky__"].Value()) 90 assert.EqualValues(t, map[string]string{"taskqueue": "__sticky__"}, counters["test.hits-tagged+taskqueue=__sticky__"].Tags()) 91 } 92 93 func recordTallyMetrics(h Handler) { 94 hitsCounter := h.Counter("hits") 95 gauge := h.Gauge("temp") 96 timer := h.Timer("latency") 97 histogram := h.Histogram("transmission", Bytes) 98 hitsTaggedCounter := h.Counter("hits-tagged") 99 hitsTaggedExcludedCounter := h.Counter("hits-tagged-excluded") 100 101 hitsCounter.Record(8) 102 gauge.Record(-100, StringTag("location", "Mare Imbrium")) 103 timer.Record(1248 * time.Millisecond) 104 timer.Record(5255 * time.Millisecond) 105 histogram.Record(1234567) 106 hitsTaggedCounter.Record(11, TaskQueueTag("__sticky__")) 107 hitsTaggedExcludedCounter.Record(14, TaskQueueTag("filtered")) 108 }