github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/metrics/implementation/tsmetrics/metrics.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 tsmetrics 19 20 import ( 21 "github.com/facebookincubator/go-belt/tool/experimental/metrics/types" 22 tsmetrics "github.com/xaionaro-go/metrics" 23 ) 24 25 var _ types.Metrics = &Metrics{} 26 27 // Metrics implements a wrapper of github.com/xaionaro-go/metrics to implement 28 // metrics.Metrics. 29 type Metrics struct { 30 Registry *tsmetrics.Registry 31 Tags *tsmetrics.FastTags 32 } 33 34 // New returns a new instance of Metrics 35 func New() *Metrics { 36 m := &Metrics{ 37 Registry: tsmetrics.New(), 38 Tags: tsmetrics.NewFastTags().(*tsmetrics.FastTags), 39 } 40 m.Registry.SetDefaultGCEnabled(true) 41 m.Registry.SetDefaultIsRan(true) 42 m.Registry.SetSender(nil) 43 return m 44 } 45 46 // Count implements metrics.Metrics. 47 func (m *Metrics) Count(key string) types.Count { 48 return &Count{Metrics: m, MetricCount: m.Registry.Count(key, m.Tags)} 49 } 50 51 // CountFields implements metrics.Metrics. 52 func (m *Metrics) CountFields(key string, addFields AbstractFields) types.Count { 53 tags := m.addFields(addFields) 54 if tags == nil { 55 return m.Count(key) 56 } 57 defer tags.Release() 58 return &Count{Metrics: m, MetricCount: m.Registry.Count(key, tags)} 59 } 60 61 // ForEachCount iterates through all Count metrics. Stops at first `false` returned by the callback function. 62 func (m *Metrics) ForEachCount(callback func(types.Count) bool) bool { 63 list := m.Registry.List() 64 defer list.Release() 65 for _, abstractMetric := range *list { 66 metric := abstractMetric.(*tsmetrics.MetricCount) 67 if !callback(&Count{Metrics: m, MetricCount: metric}) { 68 return false 69 } 70 } 71 return true 72 } 73 74 // Gauge implements metrics.Metrics. 75 func (m *Metrics) Gauge(key string) types.Gauge { 76 return &Gauge{Metrics: m, MetricGaugeFloat64: m.Registry.GaugeFloat64(key, m.Tags)} 77 } 78 79 // GaugeFields implements metrics.Metrics. 80 func (m *Metrics) GaugeFields(key string, addFields AbstractFields) types.Gauge { 81 tags := m.addFields(addFields) 82 if tags == nil { 83 return m.Gauge(key) 84 } 85 defer tags.Release() 86 return &Gauge{Metrics: m, MetricGaugeFloat64: m.Registry.GaugeFloat64(key, tags)} 87 } 88 89 // ForEachGauge iterates through all Gauge metrics. Stops at first `false` returned by the callback function. 90 func (m *Metrics) ForEachGauge(callback func(types.Gauge) bool) bool { 91 list := m.Registry.List() 92 defer list.Release() 93 for _, abstractMetric := range *list { 94 metric := abstractMetric.(*tsmetrics.MetricGaugeFloat64) 95 if !callback(&Gauge{Metrics: m, MetricGaugeFloat64: metric}) { 96 return false 97 } 98 } 99 return true 100 } 101 102 // IntGauge implements metrics.Metrics. 103 func (m *Metrics) IntGauge(key string) types.IntGauge { 104 return &IntGauge{Metrics: m, MetricGaugeInt64: m.Registry.GaugeInt64(key, m.Tags)} 105 } 106 107 // IntGaugeFields implements metrics.Metrics. 108 func (m *Metrics) IntGaugeFields(key string, addFields AbstractFields) types.IntGauge { 109 tags := m.addFields(addFields) 110 if tags == nil { 111 return m.IntGauge(key) 112 } 113 defer tags.Release() 114 return &IntGauge{Metrics: m, MetricGaugeInt64: m.Registry.GaugeInt64(key, tags)} 115 } 116 117 // ForEachIntGauge iterates through all IntGauge metrics. Stops at first `false` returned by the callback function. 118 func (m *Metrics) ForEachIntGauge(callback func(types.IntGauge) bool) bool { 119 list := m.Registry.List() 120 defer list.Release() 121 for _, abstractMetric := range *list { 122 metric := abstractMetric.(*tsmetrics.MetricGaugeInt64) 123 if !callback(&IntGauge{Metrics: m, MetricGaugeInt64: metric}) { 124 return false 125 } 126 } 127 return true 128 } 129 130 func setTagsFromFields(tags *tsmetrics.FastTags, fields AbstractFields) { 131 fields.ForEachField(func(f *Field) bool { 132 tags.Set(f.Key, f.Value) 133 return true 134 }) 135 } 136 137 func (m *Metrics) addFields(addFields AbstractFields) *tsmetrics.FastTags { 138 if addFields == nil { 139 return nil 140 } 141 addLen := addFields.Len() 142 if addLen == 0 { 143 return nil 144 } 145 newTags := tsmetrics.NewFastTags().(*tsmetrics.FastTags) 146 newTags.Slice = make([]*tsmetrics.FastTag, len(m.Tags.Slice), len(m.Tags.Slice)+addLen) 147 copy(newTags.Slice, m.Tags.Slice) 148 setTagsFromFields(newTags, addFields) 149 return newTags 150 } 151 152 // WithContextFields implements metrics.Metrics. 153 func (m Metrics) WithContextFields(allFields *FieldsChain, newFieldsCount int) Tool { 154 newTags := tsmetrics.NewFastTags().(*tsmetrics.FastTags) 155 newTags.Slice = make([]*tsmetrics.FastTag, len(m.Tags.Slice)) 156 copy(newTags.Slice, m.Tags.Slice) 157 count := 0 158 allFields.ForEachField(func(f *Field) bool { 159 count++ 160 if count > newFieldsCount { 161 return false 162 } 163 if !f.Properties.Has(types.FieldPropInclude) { 164 return true 165 } 166 newTags.Set(f.Key, f.Value) 167 return true 168 }) 169 return &Metrics{ 170 Registry: m.Registry, 171 Tags: newTags, 172 } 173 } 174 175 // WithTraceIDs implements metrics.Metrics. 176 func (m *Metrics) WithTraceIDs(traceIDs TraceIDs, newTraceIDsCount int) Tool { 177 // Should be ignored per metrics.Metrics interface description, so returning 178 // as is: 179 return m 180 } 181 182 // Flush implements metrics.Metrics (or more specifically belt.Tool). 183 func (*Metrics) Flush() {}