github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/metrics/tests/test_utils.go (about) 1 // Copyright 2022 Meta Platforms, Inc. and affiliates. 2 // 3 // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 // 5 // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 // 7 // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 // 9 // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 // 11 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 13 // Copyright (c) Facebook, Inc. and its affiliates. 14 // 15 // This source code is licensed under the MIT license found in the 16 // LICENSE file in the root directory of this source tree. 17 18 package tests 19 20 import ( 21 "testing" 22 23 "github.com/facebookincubator/go-belt/pkg/field" 24 "github.com/facebookincubator/go-belt/tool/experimental/metrics/types" 25 "github.com/stretchr/testify/require" 26 ) 27 28 // Fields is a type alias to field.Fields (for convenience) 29 type Fields = field.Fields 30 31 // FieldsChain is a type alias to field.FieldsChain (for convenience) 32 type FieldsChain = field.FieldsChain 33 34 // FieldProperties is a type alias to field.Properties (for convenience) 35 type FieldProperties = field.Properties 36 37 // testMetric tests metric and returns the resulting value of the metric 38 func testMetric( 39 t *testing.T, 40 m types.Metrics, 41 key string, 42 fields Fields, 43 resetFields Fields, 44 metricType string, 45 expectedValue float64, 46 ) float64 { 47 cFields := (*FieldsChain)(nil).WithFields(fields) 48 switch metricType { 49 case "Count": 50 metric := m.WithContextFields(cFields, cFields.Len()).(types.Metrics).Count(key) 51 if resetFields != nil { 52 metric = metric.WithResetFields(resetFields) 53 } 54 require.Equal(t, uint64(expectedValue+1), metric.Add(1).Value().(uint64)) 55 metric = m.WithContextFields(cFields, cFields.Len()).(types.Metrics).Count(key) 56 if resetFields != nil { 57 metric = metric.WithResetFields(resetFields) 58 } 59 require.Equal(t, uint64(expectedValue+2), metric.Add(1).Value().(uint64)) 60 scope := m 61 if resetFields != nil { 62 scope = scope.WithContextFields(nil, 0).(types.Metrics) 63 fields = resetFields 64 } 65 metric = scope.CountFields(key, fields) 66 require.Equal(t, uint64(expectedValue+3), metric.Add(1).Value().(uint64)) 67 return float64(metric.Value().(uint64)) 68 case "Gauge": 69 metric := m.WithContextFields(cFields, cFields.Len()).(types.Metrics).Gauge(key) 70 if resetFields != nil { 71 metric = metric.WithResetFields(resetFields) 72 } 73 require.Equal(t, expectedValue-1.5, metric.Add(-1.5).Value().(float64)) 74 metric = m.WithContextFields(cFields, cFields.Len()).(types.Metrics).Gauge(key) 75 if resetFields != nil { 76 metric = metric.WithResetFields(resetFields) 77 } 78 require.Equal(t, expectedValue-0.5, metric.Add(1).Value().(float64)) 79 scope := m 80 if resetFields != nil { 81 scope = scope.WithContextFields(nil, 0).(types.Metrics) 82 fields = resetFields 83 } 84 metric = scope.GaugeFields(key, fields) 85 require.Equal(t, expectedValue+0.5, metric.Add(1).Value().(float64)) 86 require.Equal(t, expectedValue, metric.Add(-0.5).Value().(float64)) 87 return metric.Value().(float64) 88 case "IntGauge": 89 metric := m.WithContextFields(cFields, cFields.Len()).(types.Metrics).IntGauge(key) 90 if resetFields != nil { 91 metric = metric.WithResetFields(resetFields) 92 } 93 require.Equal(t, int64(-1), metric.Add(-1).Value().(int64)) 94 metric = m.WithContextFields(cFields, cFields.Len()).(types.Metrics).IntGauge(key) 95 if resetFields != nil { 96 metric = metric.WithResetFields(resetFields) 97 } 98 require.Equal(t, int64(0), metric.Add(1).Value().(int64)) 99 scope := m 100 if resetFields != nil { 101 scope = scope.WithContextFields(nil, 0).(types.Metrics) 102 fields = resetFields 103 } 104 metric = scope.IntGaugeFields(key, fields) 105 require.Equal(t, int64(2), metric.Add(2).Value().(int64)) 106 require.Equal(t, int64(0), metric.Add(-2).Value().(int64)) 107 return float64(metric.Value().(int64)) 108 } 109 110 panic(metricType) 111 } 112 113 // TestMetrics performs generic tests for an abstract types.Metrics implementation. 114 func TestMetrics(t *testing.T, metricsFactory func() types.Metrics) { 115 m := metricsFactory() 116 117 for _, metricType := range []string{"Count", "Gauge", "IntGauge"} { 118 t.Run(metricType, func(t *testing.T) { 119 t.Run("hello_world", func(t *testing.T) { 120 testMetric(t, m, "hello_world", nil, nil, metricType, 0) 121 testMetric(t, m, "hello_world", Fields{ 122 {Key: "testField", Value: "testValue", Properties: FieldProperties{types.FieldPropInclude}}, 123 }, nil, metricType, 0) 124 testMetric(t, m, "hello_world", Fields{ 125 {Key: "testField", Value: "anotherValue", Properties: FieldProperties{types.FieldPropInclude}}, 126 }, nil, metricType, 0) 127 testMetric(t, m, "hello_world", Fields{ 128 {Key: "anotherField", Value: "testValue", Properties: FieldProperties{types.FieldPropInclude}}, 129 }, nil, metricType, 0) 130 testMetric(t, m, "hello_world", Fields{ 131 {Key: "testField", Value: "testValue", Properties: FieldProperties{types.FieldPropInclude}}, 132 {Key: "anotherField", Value: "anotherValue", Properties: FieldProperties{types.FieldPropInclude}}, 133 }, nil, metricType, 0) 134 }) 135 t.Run("WithResetFields", func(t *testing.T) { 136 key := "WithResetFields" 137 tags := Fields{ 138 {Key: "testField", Value: "testValue", Properties: FieldProperties{types.FieldPropInclude}}, 139 } 140 prevResult := testMetric(t, m, key, tags, nil, metricType, 0) 141 142 wrongTags := Fields{ 143 {Key: "testField", Value: "anotherValue", Properties: FieldProperties{types.FieldPropInclude}}, 144 } 145 prevResult = testMetric(t, m, key, wrongTags, tags, metricType, prevResult) 146 147 wrongTags = Fields{ 148 {Key: "anotherField", Value: "anotherValue", Properties: FieldProperties{types.FieldPropInclude}}, 149 } 150 testMetric(t, m, key, wrongTags, tags, metricType, prevResult) 151 }) 152 }) 153 } 154 }