-
Notifications
You must be signed in to change notification settings - Fork 42
/
stats.go
112 lines (93 loc) · 3.35 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 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.
// It acts as a coordination point between things that want to record stats,
// and things that want to aggregate and report stats.
package stats
import (
"context"
"sync"
"time"
"github.com/anz-bank/sysl/pkg/lspimpl/lspframework/telemetry/unit"
)
// Int64Measure is used to record integer values.
type Int64Measure struct {
name string
description string
unit unit.Unit
mu sync.Mutex
subscribers []Int64Subscriber
}
// Float64Measure is used to record floating point values.
type Float64Measure struct {
name string
description string
unit unit.Unit
mu sync.Mutex
subscribers []Float64Subscriber
}
// Int64Subscriber is the type for functions that want to listen to
// integer statistic events.
type Int64Subscriber func(ctx context.Context, im *Int64Measure, value int64, at time.Time)
// Float64Subscriber is the type for functions that want to listen to
// floating point statistic events.
type Float64Subscriber func(ctx context.Context, fm *Float64Measure, value float64, at time.Time)
// Int64 creates a new Int64Measure and prepares it for use.
func Int64(name string, description string, unit unit.Unit) *Int64Measure {
return &Int64Measure{
name: name,
description: description,
unit: unit,
}
}
// Float64 creates a new Float64Measure and prepares it for use.
func Float64(name string, description string, unit unit.Unit) *Float64Measure {
return &Float64Measure{
name: name,
description: description,
unit: unit,
}
}
// Name returns the name this measure was given on construction.
func (m *Int64Measure) Name() string { return m.name }
// Description returns the description this measure was given on construction.
func (m *Int64Measure) Description() string { return m.description }
// Unit returns the units this measure was given on construction.
func (m *Int64Measure) Unit() unit.Unit { return m.unit }
// Subscribe adds a new subscriber to this measure.
func (m *Int64Measure) Subscribe(s Int64Subscriber) {
m.mu.Lock()
m.subscribers = append(m.subscribers, s)
m.mu.Unlock()
}
// Record delivers a new value to the subscribers of this measure.
func (m *Int64Measure) Record(ctx context.Context, value int64) {
at := time.Now()
m.mu.Lock()
defer m.mu.Unlock()
for _, s := range m.subscribers {
s(ctx, m, value, at)
}
}
// 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.
func (m *Float64Measure) Unit() unit.Unit { return m.unit }
// Subscribe adds a new subscriber to this measure.
func (m *Float64Measure) Subscribe(s Float64Subscriber) {
m.mu.Lock()
m.subscribers = append(m.subscribers, s)
m.mu.Unlock()
}
// Record delivers a new value to the subscribers of this measure.
func (m *Float64Measure) Record(ctx context.Context, value float64) {
at := time.Now()
m.mu.Lock()
defer m.mu.Unlock()
for _, s := range m.subscribers {
s(ctx, m, value, at)
}
}