go.uber.org/yarpc@v1.72.1/internal/testutils/metricutils.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package testutils 22 23 import ( 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 "go.uber.org/net/metrics" 29 ) 30 31 // CounterAssertion holds expected counter metric 32 type CounterAssertion struct { 33 Name string 34 Tags map[string]string 35 Value int 36 } 37 38 // HistogramAssertion holds expected histogram metric 39 type HistogramAssertion struct { 40 Name string 41 Tags map[string]string 42 Value []int64 43 44 // Values are not compared, rather length of values is asserted 45 IgnoreValueCompare bool 46 ValueLength int 47 } 48 49 // AssertCounters asserts expected counters with metrics snapshot 50 func AssertCounters(t *testing.T, counterAssertions []CounterAssertion, snapshot []metrics.Snapshot) { 51 require.Len(t, counterAssertions, len(snapshot), snapshot) 52 53 for i, wantCounter := range counterAssertions { 54 require.Equal(t, wantCounter.Name, snapshot[i].Name, "unexpected counter %s", snapshot[i].Name) 55 assert.EqualValues(t, wantCounter.Value, snapshot[i].Value, "unexpected counter value for %s", wantCounter.Name) 56 for wantTagKey, wantTagVal := range wantCounter.Tags { 57 assert.Equal(t, wantTagVal, snapshot[i].Tags[wantTagKey], "unexpected value for %q", wantTagKey) 58 } 59 } 60 } 61 62 // AssertHistograms asserts expected histograms with histogram snapshot 63 func AssertHistograms(t *testing.T, histogramAssertions []HistogramAssertion, snapshot []metrics.HistogramSnapshot) { 64 require.Len(t, histogramAssertions, len(snapshot), "unexpected number of histograms") 65 66 for i, wantHistogram := range histogramAssertions { 67 require.Equal(t, wantHistogram.Name, snapshot[i].Name, "unexpected histogram %s", wantHistogram.Name) 68 if wantHistogram.IgnoreValueCompare { 69 assert.Equal(t, wantHistogram.ValueLength, len(snapshot[i].Values), 70 "unexpected histogram value length for %s", wantHistogram.Name) 71 } else { 72 assert.EqualValues(t, wantHistogram.Value, snapshot[i].Values, "unexpected histogram value for %s", wantHistogram.Name) 73 } 74 for wantTagKey, wantTagVal := range wantHistogram.Tags { 75 assert.Equal(t, wantTagVal, snapshot[i].Tags[wantTagKey], "unexpected value for %q", wantTagKey) 76 } 77 } 78 } 79 80 // AssertClientAndServerCounters asserts expected counters on client and server snapshots 81 func AssertClientAndServerCounters(t *testing.T, counterAssertions []CounterAssertion, clientSnapshot, serverSnapshot *metrics.Root) { 82 t.Run("inbound", func(t *testing.T) { 83 AssertCounters(t, counterAssertions, serverSnapshot.Snapshot().Counters) 84 }) 85 t.Run("outbound", func(t *testing.T) { 86 AssertCounters(t, counterAssertions, clientSnapshot.Snapshot().Counters) 87 }) 88 } 89 90 // AssertClientAndServerHistograms asserts expected histograms on client and server snapshots 91 func AssertClientAndServerHistograms(t *testing.T, histogramAssertions []HistogramAssertion, 92 clientSnapshot, serverSnapshot *metrics.Root) { 93 t.Run("inbound", func(t *testing.T) { 94 AssertHistograms(t, histogramAssertions, serverSnapshot.Snapshot().Histograms) 95 }) 96 t.Run("outbound", func(t *testing.T) { 97 AssertHistograms(t, histogramAssertions, clientSnapshot.Snapshot().Histograms) 98 }) 99 }