Source file src/runtime/metrics/value.go
1 // Copyright 2020 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package metrics 6 7 import ( 8 "math" 9 "unsafe" 10 ) 11 12 // ValueKind is a tag for a metric Value which indicates its type. 13 type ValueKind int 14 15 const ( 16 // KindBad indicates that the Value has no type and should not be used. 17 KindBad ValueKind = iota 18 19 // KindUint64 indicates that the type of the Value is a uint64. 20 KindUint64 21 22 // KindFloat64 indicates that the type of the Value is a float64. 23 KindFloat64 24 25 // KindFloat64Histogram indicates that the type of the Value is a *Float64Histogram. 26 KindFloat64Histogram 27 ) 28 29 // Value represents a metric value returned by the runtime. 30 type Value struct { 31 kind ValueKind 32 scalar uint64 // contains scalar values for scalar Kinds. 33 pointer unsafe.Pointer // contains non-scalar values. 34 } 35 36 // Kind returns the tag representing the kind of value this is. 37 func (v Value) Kind() ValueKind { 38 return v.kind 39 } 40 41 // Uint64 returns the internal uint64 value for the metric. 42 // 43 // If v.Kind() != KindUint64, this method panics. 44 func (v Value) Uint64() uint64 { 45 if v.kind != KindUint64 { 46 panic("called Uint64 on non-uint64 metric value") 47 } 48 return v.scalar 49 } 50 51 // Float64 returns the internal float64 value for the metric. 52 // 53 // If v.Kind() != KindFloat64, this method panics. 54 func (v Value) Float64() float64 { 55 if v.kind != KindFloat64 { 56 panic("called Float64 on non-float64 metric value") 57 } 58 return math.Float64frombits(v.scalar) 59 } 60 61 // Float64Histogram returns the internal *Float64Histogram value for the metric. 62 // 63 // If v.Kind() != KindFloat64Histogram, this method panics. 64 func (v Value) Float64Histogram() *Float64Histogram { 65 if v.kind != KindFloat64Histogram { 66 panic("called Float64Histogram on non-Float64Histogram metric value") 67 } 68 return (*Float64Histogram)(v.pointer) 69 } 70