github.com/google/cloudprober@v0.11.3/metrics/float.go (about) 1 // Copyright 2017 The Cloudprober Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package metrics 16 17 import ( 18 "errors" 19 "strconv" 20 ) 21 22 // Float implements NumValue with float64 storage. Note that Float is not concurrency 23 // safe. 24 type Float struct { 25 f float64 26 // If Str is defined, this is method used to convert Float into a string. 27 Str func(float64) string 28 } 29 30 // NewFloat returns a new Float. 31 func NewFloat(f float64) *Float { 32 return &Float{f: f} 33 } 34 35 // Clone returns a copy the receiver Float 36 func (f *Float) Clone() Value { 37 return &Float{ 38 f: f.f, 39 Str: f.Str, 40 } 41 } 42 43 // Int64 returns the stored float64 as int64. 44 func (f *Float) Int64() int64 { 45 return int64(f.f) 46 } 47 48 // Float64 returns the stored float64. 49 func (f *Float) Float64() float64 { 50 return f.f 51 } 52 53 // Inc increments the receiver Float by one. 54 // It's part of the NumValue interface. 55 func (f *Float) Inc() { 56 f.f++ 57 } 58 59 // IncBy increments the receiver Float by "delta" NumValue. 60 // It's part of the NumValue interface. 61 func (f *Float) IncBy(delta NumValue) { 62 f.f += delta.Float64() 63 } 64 65 // Add adds a Value to the receiver Float. If Value is not Float, an error is returned. 66 // It's part of the Value interface. 67 func (f *Float) Add(val Value) error { 68 delta, ok := val.(*Float) 69 if !ok { 70 return errors.New("incompatible value to add") 71 } 72 f.f += delta.f 73 return nil 74 } 75 76 // SubtractCounter subtracts the provided "lastVal", assuming that value 77 // represents a counter, i.e. if "value" is less than "lastVal", we assume that 78 // counter has been reset and don't subtract. 79 func (f *Float) SubtractCounter(lastVal Value) (bool, error) { 80 lv, ok := lastVal.(*Float) 81 if !ok { 82 return false, errors.New("incompatible value to add") 83 } 84 if f.f < lv.f { 85 return true, nil 86 } 87 88 f.f -= lv.f 89 return false, nil 90 } 91 92 // AddInt64 adds an int64 to the receiver Float. 93 func (f *Float) AddInt64(i int64) { 94 f.f += float64(i) 95 } 96 97 // AddFloat64 adds a float64 to the receiver Float. 98 func (f *Float) AddFloat64(ff float64) { 99 f.f += ff 100 } 101 102 // String returns the string representation of Float. 103 // It's part of the Value interface. 104 func (f *Float) String() string { 105 if f.Str != nil { 106 return f.Str(f.Float64()) 107 } 108 return strconv.FormatFloat(f.f, 'f', 3, 64) 109 }