Skip to content

Commit 1249273

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 063d392 commit 1249273

File tree

4 files changed

+93
-55
lines changed

4 files changed

+93
-55
lines changed

internal/lsp/debug/metrics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ func registerMetrics(m *metric.Config) {
5353
receivedBytes.Record(m, tag.ReceivedBytes)
5454
sentBytes.Record(m, tag.SentBytes)
5555
latency.Record(m, tag.Latency)
56-
started.CountInt64(m, tag.Started)
57-
completed.CountFloat64(m, tag.Latency)
56+
started.Count(m, tag.Started)
57+
completed.Count(m, tag.Latency)
5858
}

internal/telemetry/event/key.go

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ func (k *ValueKey) Description() string { return k.description }
4242
// Get can be used to get a tag for the key from a TagMap.
4343
func (k *ValueKey) Get(tags TagMap) interface{} {
4444
if t := tags.Find(k); t.Valid() {
45-
return t.Value
45+
return k.From(t)
4646
}
4747
return nil
4848
}
4949

50+
// From can be used to get a value from a Tag.
51+
func (k *ValueKey) From(t Tag) interface{} { return t.Value }
52+
5053
// Of creates a new Tag with this key and the supplied value.
5154
func (k *ValueKey) Of(value interface{}) Tag { return Tag{Key: k, Value: value} }
5255

@@ -70,11 +73,14 @@ func (k *IntKey) Of(v int) Tag { return Tag{Key: k, Value: v} }
7073
// Get can be used to get a tag for the key from a TagMap.
7174
func (k *IntKey) Get(tags TagMap) int {
7275
if t := tags.Find(k); t.Valid() {
73-
return t.Value.(int)
76+
return k.From(t)
7477
}
7578
return 0
7679
}
7780

81+
// From can be used to get a value from a Tag.
82+
func (k *IntKey) From(t Tag) int { return t.Value.(int) }
83+
7884
// Int8Key represents a key
7985
type Int8Key struct {
8086
name string
@@ -95,11 +101,14 @@ func (k *Int8Key) Of(v int8) Tag { return Tag{Key: k, Value: v} }
95101
// Get can be used to get a tag for the key from a TagMap.
96102
func (k *Int8Key) Get(tags TagMap) int8 {
97103
if t := tags.Find(k); t.Valid() {
98-
return t.Value.(int8)
104+
return k.From(t)
99105
}
100106
return 0
101107
}
102108

109+
// From can be used to get a value from a Tag.
110+
func (k *Int8Key) From(t Tag) int8 { return t.Value.(int8) }
111+
103112
// Int16Key represents a key
104113
type Int16Key struct {
105114
name string
@@ -120,11 +129,14 @@ func (k *Int16Key) Of(v int16) Tag { return Tag{Key: k, Value: v} }
120129
// Get can be used to get a tag for the key from a TagMap.
121130
func (k *Int16Key) Get(tags TagMap) int16 {
122131
if t := tags.Find(k); t.Valid() {
123-
return t.Value.(int16)
132+
return k.From(t)
124133
}
125134
return 0
126135
}
127136

137+
// From can be used to get a value from a Tag.
138+
func (k *Int16Key) From(t Tag) int16 { return t.Value.(int16) }
139+
128140
// Int32Key represents a key
129141
type Int32Key struct {
130142
name string
@@ -145,11 +157,14 @@ func (k *Int32Key) Of(v int32) Tag { return Tag{Key: k, Value: v} }
145157
// Get can be used to get a tag for the key from a TagMap.
146158
func (k *Int32Key) Get(tags TagMap) int32 {
147159
if t := tags.Find(k); t.Valid() {
148-
return t.Value.(int32)
160+
return k.From(t)
149161
}
150162
return 0
151163
}
152164

165+
// From can be used to get a value from a Tag.
166+
func (k *Int32Key) From(t Tag) int32 { return t.Value.(int32) }
167+
153168
// Int64Key represents a key
154169
type Int64Key struct {
155170
name string
@@ -170,11 +185,14 @@ func (k *Int64Key) Of(v int64) Tag { return Tag{Key: k, Value: v} }
170185
// Get can be used to get a tag for the key from a TagMap.
171186
func (k *Int64Key) Get(tags TagMap) int64 {
172187
if t := tags.Find(k); t.Valid() {
173-
return t.Value.(int64)
188+
return k.From(t)
174189
}
175190
return 0
176191
}
177192

193+
// From can be used to get a value from a Tag.
194+
func (k *Int64Key) From(t Tag) int64 { return t.Value.(int64) }
195+
178196
// UIntKey represents a key
179197
type UIntKey struct {
180198
name string
@@ -195,11 +213,14 @@ func (k *UIntKey) Of(v uint) Tag { return Tag{Key: k, Value: v} }
195213
// Get can be used to get a tag for the key from a TagMap.
196214
func (k *UIntKey) Get(tags TagMap) uint {
197215
if t := tags.Find(k); t.Valid() {
198-
return t.Value.(uint)
216+
return k.From(t)
199217
}
200218
return 0
201219
}
202220

221+
// From can be used to get a value from a Tag.
222+
func (k *UIntKey) From(t Tag) uint { return t.Value.(uint) }
223+
203224
// UInt8Key represents a key
204225
type UInt8Key struct {
205226
name string
@@ -220,11 +241,14 @@ func (k *UInt8Key) Of(v uint8) Tag { return Tag{Key: k, Value: v} }
220241
// Get can be used to get a tag for the key from a TagMap.
221242
func (k *UInt8Key) Get(tags TagMap) uint8 {
222243
if t := tags.Find(k); t.Valid() {
223-
return t.Value.(uint8)
244+
return k.From(t)
224245
}
225246
return 0
226247
}
227248

249+
// From can be used to get a value from a Tag.
250+
func (k *UInt8Key) From(t Tag) uint8 { return t.Value.(uint8) }
251+
228252
// UInt16Key represents a key
229253
type UInt16Key struct {
230254
name string
@@ -245,11 +269,14 @@ func (k *UInt16Key) Of(v uint16) Tag { return Tag{Key: k, Value: v} }
245269
// Get can be used to get a tag for the key from a TagMap.
246270
func (k *UInt16Key) Get(tags TagMap) uint16 {
247271
if t := tags.Find(k); t.Valid() {
248-
return t.Value.(uint16)
272+
return k.From(t)
249273
}
250274
return 0
251275
}
252276

277+
// From can be used to get a value from a Tag.
278+
func (k *UInt16Key) From(t Tag) uint16 { return t.Value.(uint16) }
279+
253280
// UInt32Key represents a key
254281
type UInt32Key struct {
255282
name string
@@ -270,11 +297,14 @@ func (k *UInt32Key) Of(v uint32) Tag { return Tag{Key: k, Value: v} }
270297
// Get can be used to get a tag for the key from a TagMap.
271298
func (k *UInt32Key) Get(tags TagMap) uint32 {
272299
if t := tags.Find(k); t.Valid() {
273-
return t.Value.(uint32)
300+
return k.From(t)
274301
}
275302
return 0
276303
}
277304

305+
// From can be used to get a value from a Tag.
306+
func (k *UInt32Key) From(t Tag) uint32 { return t.Value.(uint32) }
307+
278308
// UInt64Key represents a key
279309
type UInt64Key struct {
280310
name string
@@ -295,11 +325,14 @@ func (k *UInt64Key) Of(v uint64) Tag { return Tag{Key: k, Value: v} }
295325
// Get can be used to get a tag for the key from a TagMap.
296326
func (k *UInt64Key) Get(tags TagMap) uint64 {
297327
if t := tags.Find(k); t.Valid() {
298-
return t.Value.(uint64)
328+
return k.From(t)
299329
}
300330
return 0
301331
}
302332

333+
// From can be used to get a value from a Tag.
334+
func (k *UInt64Key) From(t Tag) uint64 { return t.Value.(uint64) }
335+
303336
// Float32Key represents a key
304337
type Float32Key struct {
305338
name string
@@ -320,11 +353,14 @@ func (k *Float32Key) Of(v float32) Tag { return Tag{Key: k, Value: v} }
320353
// Get can be used to get a tag for the key from a TagMap.
321354
func (k *Float32Key) Get(tags TagMap) float32 {
322355
if t := tags.Find(k); t.Valid() {
323-
return t.Value.(float32)
356+
return k.From(t)
324357
}
325358
return 0
326359
}
327360

361+
// From can be used to get a value from a Tag.
362+
func (k *Float32Key) From(t Tag) float32 { return t.Value.(float32) }
363+
328364
// Float64Key represents a key
329365
type Float64Key struct {
330366
name string
@@ -345,11 +381,14 @@ func (k *Float64Key) Of(v float64) Tag { return Tag{Key: k, Value: v} }
345381
// Get can be used to get a tag for the key from a TagMap.
346382
func (k *Float64Key) Get(tags TagMap) float64 {
347383
if t := tags.Find(k); t.Valid() {
348-
return t.Value.(float64)
384+
return k.From(t)
349385
}
350386
return 0
351387
}
352388

389+
// From can be used to get a value from a Tag.
390+
func (k *Float64Key) From(t Tag) float64 { return t.Value.(float64) }
391+
353392
// StringKey represents a key
354393
type StringKey struct {
355394
name string
@@ -370,11 +409,14 @@ func (k *StringKey) Of(v string) Tag { return Tag{Key: k, Value: v} }
370409
// Get can be used to get a tag for the key from a TagMap.
371410
func (k *StringKey) Get(tags TagMap) string {
372411
if t := tags.Find(k); t.Valid() {
373-
return t.Value.(string)
412+
return k.From(t)
374413
}
375414
return ""
376415
}
377416

417+
// From can be used to get a value from a Tag.
418+
func (k *StringKey) From(t Tag) string { return t.Value.(string) }
419+
378420
// BooleanKey represents a key
379421
type BooleanKey struct {
380422
name string
@@ -395,11 +437,14 @@ func (k *BooleanKey) Of(v bool) Tag { return Tag{Key: k, Value: v} }
395437
// Get can be used to get a tag for the key from a TagMap.
396438
func (k *BooleanKey) Get(tags TagMap) bool {
397439
if t := tags.Find(k); t.Valid() {
398-
return t.Value.(bool)
440+
return k.From(t)
399441
}
400442
return false
401443
}
402444

445+
// From can be used to get a value from a Tag.
446+
func (k *BooleanKey) From(t Tag) bool { return t.Value.(bool) }
447+
403448
// ErrorKey represents a key
404449
type ErrorKey struct {
405450
name string
@@ -420,7 +465,10 @@ func (k *ErrorKey) Of(v error) Tag { return Tag{Key: k, Value: v} }
420465
// Get can be used to get a tag for the key from a TagMap.
421466
func (k *ErrorKey) Get(tags TagMap) error {
422467
if t := tags.Find(k); t.Valid() {
423-
return t.Value.(error)
468+
return k.From(t)
424469
}
425470
return nil
426471
}
472+
473+
// From can be used to get a value from a Tag.
474+
func (k *ErrorKey) From(t Tag) error { return t.Value.(error) }

internal/telemetry/export/metric/data.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Int64Data struct {
3737
EndTime time.Time
3838

3939
groups [][]event.Tag
40+
key *event.Int64Key
4041
}
4142

4243
// Float64Data is a concrete implementation of Data for float64 scalar metrics.
@@ -51,6 +52,7 @@ type Float64Data struct {
5152
EndTime time.Time
5253

5354
groups [][]event.Tag
55+
key *event.Float64Key
5456
}
5557

5658
// HistogramInt64Data is a concrete implementation of Data for int64 histogram metrics.
@@ -63,6 +65,7 @@ type HistogramInt64Data struct {
6365
EndTime time.Time
6466

6567
groups [][]event.Tag
68+
key *event.Int64Key
6669
}
6770

6871
// HistogramInt64Row holds the values for a single row of a HistogramInt64Data.
@@ -89,6 +92,7 @@ type HistogramFloat64Data struct {
8992
EndTime time.Time
9093

9194
groups [][]event.Tag
95+
key *event.Float64Key
9296
}
9397

9498
// HistogramFloat64Row holds the values for a single row of a HistogramFloat64Data.
@@ -158,27 +162,21 @@ func (data *Int64Data) modify(at time.Time, tagMap event.TagMap, f func(v int64)
158162
return &frozen
159163
}
160164

161-
func (data *Int64Data) countInt64(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
162-
return data.modify(at, tagMap, func(v int64) int64 {
163-
return v + 1
164-
})
165-
}
166-
167-
func (data *Int64Data) countFloat64(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
165+
func (data *Int64Data) count(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
168166
return data.modify(at, tagMap, func(v int64) int64 {
169167
return v + 1
170168
})
171169
}
172170

173171
func (data *Int64Data) sum(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
174172
return data.modify(at, tagMap, func(v int64) int64 {
175-
return v + tag.Value.(int64)
173+
return v + data.key.From(tag)
176174
})
177175
}
178176

179177
func (data *Int64Data) latest(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
180178
return data.modify(at, tagMap, func(v int64) int64 {
181-
return tag.Value.(int64)
179+
return data.key.From(tag)
182180
})
183181
}
184182

@@ -204,13 +202,13 @@ func (data *Float64Data) modify(at time.Time, tagMap event.TagMap, f func(v floa
204202

205203
func (data *Float64Data) sum(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
206204
return data.modify(at, tagMap, func(v float64) float64 {
207-
return v + tag.Value.(float64)
205+
return v + data.key.From(tag)
208206
})
209207
}
210208

211209
func (data *Float64Data) latest(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
212210
return data.modify(at, tagMap, func(v float64) float64 {
213-
return tag.Value.(float64)
211+
return data.key.From(tag)
214212
})
215213
}
216214

@@ -242,7 +240,7 @@ func (data *HistogramInt64Data) modify(at time.Time, tagMap event.TagMap, f func
242240

243241
func (data *HistogramInt64Data) record(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
244242
return data.modify(at, tagMap, func(v *HistogramInt64Row) {
245-
value := tag.Value.(int64)
243+
value := data.key.From(tag)
246244
v.Sum += value
247245
if v.Min > value || v.Count == 0 {
248246
v.Min = value
@@ -287,7 +285,7 @@ func (data *HistogramFloat64Data) modify(at time.Time, tagMap event.TagMap, f fu
287285

288286
func (data *HistogramFloat64Data) record(at time.Time, tagMap event.TagMap, tag event.Tag) Data {
289287
return data.modify(at, tagMap, func(v *HistogramFloat64Row) {
290-
value := tag.Value.(float64)
288+
value := data.key.From(tag)
291289
v.Sum += value
292290
if v.Min > value || v.Count == 0 {
293291
v.Min = value

0 commit comments

Comments
 (0)