github.com/m3db/m3@v1.5.0/src/dbnode/storage/bootstrap_instrumentation_test.go (about) 1 // Copyright (c) 2021 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 storage 22 23 import ( 24 "errors" 25 "strings" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 "github.com/uber-go/tally" 30 31 "github.com/m3db/m3/src/dbnode/storage/bootstrap" 32 xerrors "github.com/m3db/m3/src/x/errors" 33 ) 34 35 func TestBootstrapRetryMetricReason(t *testing.T) { 36 tests := []struct { 37 name string 38 err error 39 expectedReason string 40 }{ 41 { 42 name: "any error", 43 err: xerrors.NewInvalidParamsError(errors.New("some error")), 44 expectedReason: "other", 45 }, 46 { 47 name: "obsolete ranges error", 48 err: xerrors.NewInvalidParamsError(bootstrap.ErrFileSetSnapshotTypeRangeAdvanced), 49 expectedReason: "obsolete-ranges", 50 }, 51 } 52 53 for _, tt := range tests { 54 t.Run(tt.name, func(t *testing.T) { 55 testScope := tally.NewTestScope("testScope", map[string]string{}) 56 options := NewOptions() 57 options = options.SetInstrumentOptions(options.InstrumentOptions().SetMetricsScope(testScope)) 58 instr := newBootstrapInstrumentation(options) 59 60 instr.bootstrapFailed(1, tt.err) 61 62 retryMetrics := getBootstrapRetryMetrics(testScope) 63 assert.NotEmpty(t, retryMetrics) 64 assert.Equal(t, 1, retryMetrics[tt.expectedReason], "metric with reason:%v should be set to 1") 65 for reason, value := range retryMetrics { 66 if reason != tt.expectedReason { 67 assert.Equal(t, 0, value, "metric with reason:%v should stay 0") 68 } 69 } 70 }) 71 } 72 } 73 74 func getBootstrapRetryMetrics(testScope tally.TestScope) map[string]int { 75 const ( 76 metricName = "bootstrap-retries" 77 reasonTag = "reason" 78 ) 79 valuesByReason := make(map[string]int) 80 for _, counter := range testScope.Snapshot().Counters() { 81 if strings.Contains(counter.Name(), metricName) { 82 reason := "" 83 if r, ok := counter.Tags()[reasonTag]; ok { 84 reason = r 85 } 86 valuesByReason[reason] = int(counter.Value()) 87 } 88 } 89 return valuesByReason 90 }