blob: ed9a33d87cc64e126f68f165e20411aa7cd7a813 [file] [log] [blame]
Michael Anthony Knyszek2159c262020-04-14 21:06:26 +00001// 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
5package metrics
6
7import (
8 "math"
9 "unsafe"
10)
11
12// ValueKind is a tag for a metric Value which indicates its type.
13type ValueKind int
14
15const (
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.
30type Value struct {
31 kind ValueKind
32 scalar uint64 // contains scalar values for scalar Kinds.
33 pointer unsafe.Pointer // contains non-scalar values.
34}
35
Ian Lance Taylorc9d6f452021-02-07 15:25:39 -080036// Kind returns the tag representing the kind of value this is.
Michael Anthony Knyszek2159c262020-04-14 21:06:26 +000037func (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.
44func (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.
54func (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.
64func (v Value) Float64Histogram() *Float64Histogram {
65 if v.kind != KindFloat64Histogram {
Michael Anthony Knyszek4c668b22021-01-06 18:22:17 +000066 panic("called Float64Histogram on non-Float64Histogram metric value")
Michael Anthony Knyszek2159c262020-04-14 21:06:26 +000067 }
68 return (*Float64Histogram)(v.pointer)
69}