-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompositeCounters.go
175 lines (159 loc) · 5.31 KB
/
CompositeCounters.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package count
import (
"context"
"time"
"github.com/pip-services3-gox/pip-services3-commons-gox/refer"
)
// CompositeCounters aggregates all counters from component references under a single component.
// It allows capturing metrics and conveniently send them to multiple destinations.
//
// References:
// - *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements
// see ICounters
//
// Example:
// type MyComponent {
// _counters CompositeCounters = new CompositeCounters();
// }
// func (mc *MyConponent) SetReferences(ctx context.Context, references refer.IReferences) {
// mc._counters.SetReferences(ctx, references);
// }
//
// func (mc * MyConponent) myMethod() {
// mc._counters.Increment(context.Background(), "mycomponent.mymethod.calls");
// timing := mc._counters.BeginTiming(context.Background(), "mycomponent.mymethod.exec_time");
// defer timing.EndTiming(context.Background());
// // do something
// }
// var mc MyComponent{};
// mc._counters = NewCompositeCounters();
type CompositeCounters struct {
counters []ICounters
}
// NewCompositeCounters creates a new instance of the counters.
// Returns: *CompositeCounters
func NewCompositeCounters() *CompositeCounters {
c := &CompositeCounters{
counters: []ICounters{},
}
return c
}
// NewCompositeCountersFromReferences creates a new instance of the counters.
// Parameters:
// - ctx context.Context
// - references is a refer.IReferences to locate the component dependencies.
// Returns: *CompositeCounters
func NewCompositeCountersFromReferences(ctx context.Context, references refer.IReferences) *CompositeCounters {
c := NewCompositeCounters()
c.SetReferences(ctx, references)
return c
}
// SetReferences references to dependent components.
// Parameters:
// - ctx context.Context
// - references refer.IReferences references to locate the component dependencies.
func (c *CompositeCounters) SetReferences(ctx context.Context, references refer.IReferences) {
if c.counters == nil {
c.counters = []ICounters{}
}
counters := references.GetOptional(
refer.NewDescriptor("*", "counters", "*", "*", "*"),
)
for _, l := range counters {
if l == c {
continue
}
counter, ok := l.(ICounters)
if ok {
c.counters = append(c.counters, counter)
}
}
}
// BeginTiming begins measurement of execution time interval.
// It returns Timing object which has to be called at
// Timing.endTiming to end the measurement and update the counter.
// Parameters:
// - ctx context.Context
// - name string a counter name of Interval type.
// Returns: *Timing a Timing callback object to end timing.
func (c *CompositeCounters) BeginTiming(ctx context.Context, name string) *CounterTiming {
return NewCounterTiming(name, c)
}
// EndTiming ends measurement of execution elapsed time and updates specified counter.
// see Timing.EndTiming
// Parameters:
// - ctx context.Context
// - name string a counter name
// - elapsed float64 execution elapsed time in milliseconds to update the counter.
func (c *CompositeCounters) EndTiming(ctx context.Context, name string, elapsed float64) {
for _, counter := range c.counters {
if counter != nil {
if callback, ok := counter.(ITimingCallback); ok {
callback.EndTiming(ctx, name, elapsed)
}
}
}
}
// Stats calculates min/average/max statistics based on the current and previous values.
// Parameters:
// - ctx context.Context
// - name string a counter name of Statistics type
// - value float64 a value to update statistics
func (c *CompositeCounters) Stats(ctx context.Context, name string, value float64) {
for _, counter := range c.counters {
if counter != nil {
counter.Stats(ctx, name, value)
}
}
}
// Last records the last calculated measurement value.
// Usually this method is used by metrics calculated externally.
// Parameters:
// - ctx context.Context
// - name string a counter name of Last type.
// - value float64 a last value to record.
func (c *CompositeCounters) Last(ctx context.Context, name string, value float64) {
for _, counter := range c.counters {
if counter != nil {
counter.Last(ctx, name, value)
}
}
}
// TimestampNow records the current time as a timestamp.
// Parameters:
// - ctx context.Context
// - name string a counter name of Timestamp type.
func (c *CompositeCounters) TimestampNow(ctx context.Context, name string) {
c.Timestamp(ctx, name, time.Now())
}
// Timestamp records the given timestamp.
// Parameters:
// - ctx context.Context
// - name string a counter name of Timestamp type.
// - value time.Time a timestamp to record.
func (c *CompositeCounters) Timestamp(ctx context.Context, name string, value time.Time) {
for _, counter := range c.counters {
if counter != nil {
counter.Timestamp(ctx, name, value)
}
}
}
// IncrementOne increments counter by 1.
// Parameters:
// - ctx context.Context
// - name string a counter name of Increment type.
func (c *CompositeCounters) IncrementOne(ctx context.Context, name string) {
c.Increment(ctx, name, 1)
}
// Increment increments counter by given value.
// Parameters:
// - ctx context.Context
// - name string a counter name of Increment type.
// - value number a value to add to the counter.
func (c *CompositeCounters) Increment(ctx context.Context, name string, value int64) {
for _, counter := range c.counters {
if counter != nil {
counter.Increment(ctx, name, value)
}
}
}