2019-06-14 16:32:09 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package stats provides support for recording telemetry statistics.
|
2019-07-08 13:15:11 -06:00
|
|
|
// It acts as a coordination point between things that want to record stats,
|
|
|
|
// and things that want to aggregate and report stats.
|
2019-06-14 16:32:09 -06:00
|
|
|
package stats
|
|
|
|
|
2019-07-08 13:15:11 -06:00
|
|
|
import (
|
|
|
|
"context"
|
2019-11-25 11:01:51 -07:00
|
|
|
"sync"
|
2019-10-06 17:49:04 -06:00
|
|
|
"time"
|
2019-08-13 14:34:14 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/telemetry/unit"
|
2019-07-08 13:15:11 -06:00
|
|
|
)
|
2019-06-14 16:32:09 -06:00
|
|
|
|
2019-07-08 13:15:11 -06:00
|
|
|
// Int64Measure is used to record integer values.
|
|
|
|
type Int64Measure struct {
|
|
|
|
name string
|
|
|
|
description string
|
2019-08-13 14:34:14 -06:00
|
|
|
unit unit.Unit
|
2019-11-25 11:01:51 -07:00
|
|
|
mu sync.Mutex
|
2019-07-08 13:15:11 -06:00
|
|
|
subscribers []Int64Subscriber
|
2019-06-14 16:32:09 -06:00
|
|
|
}
|
|
|
|
|
2020-01-14 16:48:43 -07:00
|
|
|
// Float64Measure is used to record floating point values.
|
2019-07-08 13:15:11 -06:00
|
|
|
type Float64Measure struct {
|
|
|
|
name string
|
|
|
|
description string
|
2019-08-13 14:34:14 -06:00
|
|
|
unit unit.Unit
|
2019-11-25 11:01:51 -07:00
|
|
|
mu sync.Mutex
|
2019-07-08 13:15:11 -06:00
|
|
|
subscribers []Float64Subscriber
|
2019-06-14 16:32:09 -06:00
|
|
|
}
|
|
|
|
|
2019-07-08 13:15:11 -06:00
|
|
|
// Int64Subscriber is the type for functions that want to listen to
|
|
|
|
// integer statistic events.
|
2019-10-06 17:49:04 -06:00
|
|
|
type Int64Subscriber func(ctx context.Context, im *Int64Measure, value int64, at time.Time)
|
2019-07-08 13:15:11 -06:00
|
|
|
|
|
|
|
// Float64Subscriber is the type for functions that want to listen to
|
|
|
|
// floating point statistic events.
|
2019-10-06 17:49:04 -06:00
|
|
|
type Float64Subscriber func(ctx context.Context, fm *Float64Measure, value float64, at time.Time)
|
2019-07-08 13:15:11 -06:00
|
|
|
|
|
|
|
// Int64 creates a new Int64Measure and prepares it for use.
|
2019-08-13 14:34:14 -06:00
|
|
|
func Int64(name string, description string, unit unit.Unit) *Int64Measure {
|
2019-07-08 13:15:11 -06:00
|
|
|
return &Int64Measure{
|
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
unit: unit,
|
|
|
|
}
|
2019-06-14 16:32:09 -06:00
|
|
|
}
|
|
|
|
|
2019-07-08 13:15:11 -06:00
|
|
|
// Float64 creates a new Float64Measure and prepares it for use.
|
2019-08-13 14:34:14 -06:00
|
|
|
func Float64(name string, description string, unit unit.Unit) *Float64Measure {
|
2019-07-08 13:15:11 -06:00
|
|
|
return &Float64Measure{
|
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
unit: unit,
|
|
|
|
}
|
2019-06-14 16:32:09 -06:00
|
|
|
}
|
|
|
|
|
2019-07-08 13:15:11 -06:00
|
|
|
// Name returns the name this measure was given on construction.
|
|
|
|
func (m *Int64Measure) Name() string { return m.name }
|
2019-06-14 16:32:09 -06:00
|
|
|
|
2019-07-08 13:15:11 -06:00
|
|
|
// Description returns the description this measure was given on construction.
|
|
|
|
func (m *Int64Measure) Description() string { return m.description }
|
2019-06-14 16:32:09 -06:00
|
|
|
|
2019-07-08 13:15:11 -06:00
|
|
|
// Unit returns the units this measure was given on construction.
|
2019-08-13 14:34:14 -06:00
|
|
|
func (m *Int64Measure) Unit() unit.Unit { return m.unit }
|
2019-06-14 16:32:09 -06:00
|
|
|
|
2019-07-08 13:15:11 -06:00
|
|
|
// Subscribe adds a new subscriber to this measure.
|
2019-11-25 11:01:51 -07:00
|
|
|
func (m *Int64Measure) Subscribe(s Int64Subscriber) {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.subscribers = append(m.subscribers, s)
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
2019-06-14 16:32:09 -06:00
|
|
|
|
2019-07-08 13:15:11 -06:00
|
|
|
// Record delivers a new value to the subscribers of this measure.
|
|
|
|
func (m *Int64Measure) Record(ctx context.Context, value int64) {
|
2019-10-06 17:49:04 -06:00
|
|
|
at := time.Now()
|
2019-11-25 11:01:51 -07:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
for _, s := range m.subscribers {
|
|
|
|
s(ctx, m, value, at)
|
|
|
|
}
|
2019-07-08 13:15:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name this measure was given on construction.
|
|
|
|
func (m *Float64Measure) Name() string { return m.name }
|
|
|
|
|
|
|
|
// Description returns the description this measure was given on construction.
|
|
|
|
func (m *Float64Measure) Description() string { return m.description }
|
|
|
|
|
|
|
|
// Unit returns the units this measure was given on construction.
|
2019-08-13 14:34:14 -06:00
|
|
|
func (m *Float64Measure) Unit() unit.Unit { return m.unit }
|
2019-07-08 13:15:11 -06:00
|
|
|
|
|
|
|
// Subscribe adds a new subscriber to this measure.
|
2019-11-25 11:01:51 -07:00
|
|
|
func (m *Float64Measure) Subscribe(s Float64Subscriber) {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.subscribers = append(m.subscribers, s)
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
2019-07-08 13:15:11 -06:00
|
|
|
|
|
|
|
// Record delivers a new value to the subscribers of this measure.
|
|
|
|
func (m *Float64Measure) Record(ctx context.Context, value float64) {
|
2019-10-06 17:49:04 -06:00
|
|
|
at := time.Now()
|
2019-11-25 11:01:51 -07:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
for _, s := range m.subscribers {
|
|
|
|
s(ctx, m, value, at)
|
|
|
|
}
|
2019-07-08 13:15:11 -06:00
|
|
|
}
|