1
0
mirror of https://github.com/golang/go synced 2024-11-18 12:34:42 -07:00

internal/telemetry: make metrics take a strongly typed key

Now that keys are solidly typed, we can use them for the metrics.
This prevents accidentally using the wrong type of key, and
allows us to use the typed accesorrs rather than the raw value.

Change-Id: I553bd8e12128d3f00a3e926dbd3bfd420cd3f135
Reviewed-on: https://go-review.googlesource.com/c/tools/+/225378
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Ian Cottrell 2020-03-24 21:30:31 -04:00
parent 063d392fe0
commit 1249273038
4 changed files with 93 additions and 55 deletions

View File

@ -53,6 +53,6 @@ func registerMetrics(m *metric.Config) {
receivedBytes.Record(m, tag.ReceivedBytes)
sentBytes.Record(m, tag.SentBytes)
latency.Record(m, tag.Latency)
started.CountInt64(m, tag.Started)
completed.CountFloat64(m, tag.Latency)
started.Count(m, tag.Started)
completed.Count(m, tag.Latency)
}

View File

@ -42,11 +42,14 @@ func (k *ValueKey) Description() string { return k.description }
// Get can be used to get a tag for the key from a TagMap.
func (k *ValueKey) Get(tags TagMap) interface{} {
if t := tags.Find(k); t.Valid() {
return t.Value
return k.From(t)
}
return nil
}
// From can be used to get a value from a Tag.
func (k *ValueKey) From(t Tag) interface{} { return t.Value }
// Of creates a new Tag with this key and the supplied value.
func (k *ValueKey) Of(value interface{}) Tag { return Tag{Key: k, Value: value} }
@ -70,11 +73,14 @@ func (k *IntKey) Of(v int) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *IntKey) Get(tags TagMap) int {
if t := tags.Find(k); t.Valid() {
return t.Value.(int)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *IntKey) From(t Tag) int { return t.Value.(int) }
// Int8Key represents a key
type Int8Key struct {
name string
@ -95,11 +101,14 @@ func (k *Int8Key) Of(v int8) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *Int8Key) Get(tags TagMap) int8 {
if t := tags.Find(k); t.Valid() {
return t.Value.(int8)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *Int8Key) From(t Tag) int8 { return t.Value.(int8) }
// Int16Key represents a key
type Int16Key struct {
name string
@ -120,11 +129,14 @@ func (k *Int16Key) Of(v int16) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *Int16Key) Get(tags TagMap) int16 {
if t := tags.Find(k); t.Valid() {
return t.Value.(int16)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *Int16Key) From(t Tag) int16 { return t.Value.(int16) }
// Int32Key represents a key
type Int32Key struct {
name string
@ -145,11 +157,14 @@ func (k *Int32Key) Of(v int32) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *Int32Key) Get(tags TagMap) int32 {
if t := tags.Find(k); t.Valid() {
return t.Value.(int32)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *Int32Key) From(t Tag) int32 { return t.Value.(int32) }
// Int64Key represents a key
type Int64Key struct {
name string
@ -170,11 +185,14 @@ func (k *Int64Key) Of(v int64) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *Int64Key) Get(tags TagMap) int64 {
if t := tags.Find(k); t.Valid() {
return t.Value.(int64)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *Int64Key) From(t Tag) int64 { return t.Value.(int64) }
// UIntKey represents a key
type UIntKey struct {
name string
@ -195,11 +213,14 @@ func (k *UIntKey) Of(v uint) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *UIntKey) Get(tags TagMap) uint {
if t := tags.Find(k); t.Valid() {
return t.Value.(uint)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *UIntKey) From(t Tag) uint { return t.Value.(uint) }
// UInt8Key represents a key
type UInt8Key struct {
name string
@ -220,11 +241,14 @@ func (k *UInt8Key) Of(v uint8) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *UInt8Key) Get(tags TagMap) uint8 {
if t := tags.Find(k); t.Valid() {
return t.Value.(uint8)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *UInt8Key) From(t Tag) uint8 { return t.Value.(uint8) }
// UInt16Key represents a key
type UInt16Key struct {
name string
@ -245,11 +269,14 @@ func (k *UInt16Key) Of(v uint16) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *UInt16Key) Get(tags TagMap) uint16 {
if t := tags.Find(k); t.Valid() {
return t.Value.(uint16)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *UInt16Key) From(t Tag) uint16 { return t.Value.(uint16) }
// UInt32Key represents a key
type UInt32Key struct {
name string
@ -270,11 +297,14 @@ func (k *UInt32Key) Of(v uint32) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *UInt32Key) Get(tags TagMap) uint32 {
if t := tags.Find(k); t.Valid() {
return t.Value.(uint32)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *UInt32Key) From(t Tag) uint32 { return t.Value.(uint32) }
// UInt64Key represents a key
type UInt64Key struct {
name string
@ -295,11 +325,14 @@ func (k *UInt64Key) Of(v uint64) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *UInt64Key) Get(tags TagMap) uint64 {
if t := tags.Find(k); t.Valid() {
return t.Value.(uint64)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *UInt64Key) From(t Tag) uint64 { return t.Value.(uint64) }
// Float32Key represents a key
type Float32Key struct {
name string
@ -320,11 +353,14 @@ func (k *Float32Key) Of(v float32) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *Float32Key) Get(tags TagMap) float32 {
if t := tags.Find(k); t.Valid() {
return t.Value.(float32)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *Float32Key) From(t Tag) float32 { return t.Value.(float32) }
// Float64Key represents a key
type Float64Key struct {
name string
@ -345,11 +381,14 @@ func (k *Float64Key) Of(v float64) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *Float64Key) Get(tags TagMap) float64 {
if t := tags.Find(k); t.Valid() {
return t.Value.(float64)
return k.From(t)
}
return 0
}
// From can be used to get a value from a Tag.
func (k *Float64Key) From(t Tag) float64 { return t.Value.(float64) }
// StringKey represents a key
type StringKey struct {
name string
@ -370,11 +409,14 @@ func (k *StringKey) Of(v string) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *StringKey) Get(tags TagMap) string {
if t := tags.Find(k); t.Valid() {
return t.Value.(string)
return k.From(t)
}
return ""
}
// From can be used to get a value from a Tag.
func (k *StringKey) From(t Tag) string { return t.Value.(string) }
// BooleanKey represents a key
type BooleanKey struct {
name string
@ -395,11 +437,14 @@ func (k *BooleanKey) Of(v bool) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *BooleanKey) Get(tags TagMap) bool {
if t := tags.Find(k); t.Valid() {
return t.Value.(bool)
return k.From(t)
}
return false
}
// From can be used to get a value from a Tag.
func (k *BooleanKey) From(t Tag) bool { return t.Value.(bool) }
// ErrorKey represents a key
type ErrorKey struct {
name string
@ -420,7 +465,10 @@ func (k *ErrorKey) Of(v error) Tag { return Tag{Key: k, Value: v} }
// Get can be used to get a tag for the key from a TagMap.
func (k *ErrorKey) Get(tags TagMap) error {
if t := tags.Find(k); t.Valid() {
return t.Value.(error)
return k.From(t)
}
return nil
}
// From can be used to get a value from a Tag.
func (k *ErrorKey) From(t Tag) error { return t.Value.(error) }

View File

@ -37,6 +37,7 @@ type Int64Data struct {
EndTime time.Time
groups [][]event.Tag
key *event.Int64Key
}
// Float64Data is a concrete implementation of Data for float64 scalar metrics.
@ -51,6 +52,7 @@ type Float64Data struct {
EndTime time.Time
groups [][]event.Tag
key *event.Float64Key
}
// HistogramInt64Data is a concrete implementation of Data for int64 histogram metrics.
@ -63,6 +65,7 @@ type HistogramInt64Data struct {
EndTime time.Time
groups [][]event.Tag
key *event.Int64Key
}
// HistogramInt64Row holds the values for a single row of a HistogramInt64Data.
@ -89,6 +92,7 @@ type HistogramFloat64Data struct {
EndTime time.Time
groups [][]event.Tag
key *event.Float64Key
}
// HistogramFloat64Row holds the values for a single row of a HistogramFloat64Data.
@ -158,13 +162,7 @@ func (data *Int64Data) modify(at time.Time, tagMap event.TagMap, f func(v int64)
return &frozen
}
func (data *Int64Data) countInt64(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
return data.modify(at, tagMap, func(v int64) int64 {
return v + 1
})
}
func (data *Int64Data) countFloat64(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
func (data *Int64Data) count(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
return data.modify(at, tagMap, func(v int64) int64 {
return v + 1
})
@ -172,13 +170,13 @@ func (data *Int64Data) countFloat64(at time.Time, tagMap event.TagMap, tag event
func (data *Int64Data) sum(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
return data.modify(at, tagMap, func(v int64) int64 {
return v + tag.Value.(int64)
return v + data.key.From(tag)
})
}
func (data *Int64Data) latest(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
return data.modify(at, tagMap, func(v int64) int64 {
return tag.Value.(int64)
return data.key.From(tag)
})
}
@ -204,13 +202,13 @@ func (data *Float64Data) modify(at time.Time, tagMap event.TagMap, f func(v floa
func (data *Float64Data) sum(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
return data.modify(at, tagMap, func(v float64) float64 {
return v + tag.Value.(float64)
return v + data.key.From(tag)
})
}
func (data *Float64Data) latest(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
return data.modify(at, tagMap, func(v float64) float64 {
return tag.Value.(float64)
return data.key.From(tag)
})
}
@ -242,7 +240,7 @@ func (data *HistogramInt64Data) modify(at time.Time, tagMap event.TagMap, f func
func (data *HistogramInt64Data) record(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
return data.modify(at, tagMap, func(v *HistogramInt64Row) {
value := tag.Value.(int64)
value := data.key.From(tag)
v.Sum += value
if v.Min > value || v.Count == 0 {
v.Min = value
@ -287,7 +285,7 @@ func (data *HistogramFloat64Data) modify(at time.Time, tagMap event.TagMap, f fu
func (data *HistogramFloat64Data) record(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
return data.modify(at, tagMap, func(v *HistogramFloat64Row) {
value := tag.Value.(float64)
value := data.key.From(tag)
v.Sum += value
if v.Min > value || v.Count == 0 {
v.Min = value

View File

@ -42,66 +42,58 @@ type HistogramFloat64 struct {
Buckets []float64
}
// CountInt64 creates a new metric based on the Scalar information that counts
// Count creates a new metric based on the Scalar information that counts
// the number of times the supplied int64 measure is set.
// Metrics of this type will use Int64Data.
func (info Scalar) CountInt64(e *Config, key event.Key) {
data := &Int64Data{Info: &info}
e.subscribe(key, data.countInt64)
func (info Scalar) Count(e *Config, key event.Key) {
data := &Int64Data{Info: &info, key: nil}
e.subscribe(key, data.count)
}
// SumInt64 creates a new metric based on the Scalar information that sums all
// the values recorded on the int64 measure.
// Metrics of this type will use Int64Data.
func (info Scalar) SumInt64(e *Config, key event.Key) {
data := &Int64Data{Info: &info}
func (info Scalar) SumInt64(e *Config, key *event.Int64Key) {
data := &Int64Data{Info: &info, key: key}
e.subscribe(key, data.sum)
}
// LatestInt64 creates a new metric based on the Scalar information that tracks
// the most recent value recorded on the int64 measure.
// Metrics of this type will use Int64Data.
func (info Scalar) LatestInt64(e *Config, key event.Key) {
data := &Int64Data{Info: &info, IsGauge: true}
func (info Scalar) LatestInt64(e *Config, key *event.Int64Key) {
data := &Int64Data{Info: &info, IsGauge: true, key: key}
e.subscribe(key, data.latest)
}
// CountFloat64 creates a new metric based on the Scalar information that counts
// the number of times the supplied float64 measure is set.
// Metrics of this type will use Int64Data.
func (info Scalar) CountFloat64(e *Config, key event.Key) {
data := &Int64Data{Info: &info}
e.subscribe(key, data.countFloat64)
}
// SumFloat64 creates a new metric based on the Scalar information that sums all
// the values recorded on the float64 measure.
// Metrics of this type will use Float64Data.
func (info Scalar) SumFloat64(e *Config, key event.Key) {
data := &Float64Data{Info: &info}
func (info Scalar) SumFloat64(e *Config, key *event.Float64Key) {
data := &Float64Data{Info: &info, key: key}
e.subscribe(key, data.sum)
}
// LatestFloat64 creates a new metric based on the Scalar information that tracks
// the most recent value recorded on the float64 measure.
// Metrics of this type will use Float64Data.
func (info Scalar) LatestFloat64(e *Config, key event.Key) {
data := &Float64Data{Info: &info, IsGauge: true}
func (info Scalar) LatestFloat64(e *Config, key *event.Float64Key) {
data := &Float64Data{Info: &info, IsGauge: true, key: key}
e.subscribe(key, data.latest)
}
// Record creates a new metric based on the HistogramInt64 information that
// tracks the bucketized counts of values recorded on the int64 measure.
// Metrics of this type will use HistogramInt64Data.
func (info HistogramInt64) Record(e *Config, key event.Key) {
data := &HistogramInt64Data{Info: &info}
func (info HistogramInt64) Record(e *Config, key *event.Int64Key) {
data := &HistogramInt64Data{Info: &info, key: key}
e.subscribe(key, data.record)
}
// Record creates a new metric based on the HistogramFloat64 information that
// tracks the bucketized counts of values recorded on the float64 measure.
// Metrics of this type will use HistogramFloat64Data.
func (info HistogramFloat64) Record(e *Config, key event.Key) {
data := &HistogramFloat64Data{Info: &info}
func (info HistogramFloat64) Record(e *Config, key *event.Float64Key) {
data := &HistogramFloat64Data{Info: &info, key: key}
e.subscribe(key, data.record)
}