Skip to content

Commit 9928199

Browse files
author
Peter Mogensen
committed
Update docs
1 parent 12c4bf1 commit 9928199

File tree

5 files changed

+80
-17
lines changed

5 files changed

+80
-17
lines changed

metric/client.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,30 +229,65 @@ func (c *Client) Flush() {
229229

230230
//--------------------------------------------------------------
231231

232+
// RegisterCounter is equivalent to Register(NewCounter(), opts) with the default Client.
232233
func (c *Client) RegisterCounter(name string, opts ...MOption) *Counter {
233234
meter := NewCounter(name)
234235
c.Register(meter, opts...)
235236
return meter
236237
}
237238

239+
// RegisterGauge is equivalent to Register(NewGauge(), opts) with the default Client.
238240
func (c *Client) RegisterGauge(name string, opts ...MOption) *GaugeUint64 {
239241
meter := NewGauge(name)
240242
c.Register(meter, opts...)
241243
return meter
242244
}
243245

246+
// RegisterTimer is equivalent to Register(NewTimer(), opts) with the default Client.
244247
func (c *Client) RegisterTimer(name string, opts ...MOption) Timer {
245248
meter := NewTimer(name)
246249
c.Register(meter, opts...)
247250
return meter
248251
}
249252

253+
// RegisterHistogram is equivalent to Register(NewHistogram(), opts) with the default Client.
250254
func (c *Client) RegisterHistogram(name string, opts ...MOption) Histogram {
251255
meter := NewHistogram(name)
252256
c.Register(meter, opts...)
253257
return meter
254258
}
255259

260+
261+
//--------------------------------------------------------------
262+
263+
// RegisterCounter is equivalent to Register(NewCounter(), opts) with the default Client.
264+
func RegisterCounter(name string, opts ...MOption) *Counter {
265+
meter := NewCounter(name)
266+
defaultClient.Register(meter, opts...)
267+
return meter
268+
}
269+
270+
// RegisterGauge is equivalent to Register(NewGauge(), opts) with the default Client.
271+
func RegisterGauge(name string, opts ...MOption) *GaugeUint64 {
272+
meter := NewGauge(name)
273+
defaultClient.Register(meter, opts...)
274+
return meter
275+
}
276+
277+
// RegisterTimer is equivalent to Register(NewTimer(), opts) with the default Client.
278+
func RegisterTimer(name string, opts ...MOption) Timer {
279+
meter := NewTimer(name)
280+
defaultClient.Register(meter, opts...)
281+
return meter
282+
}
283+
284+
// RegisterHistogram is equivalent to Register(NewHistogram(), opts) with the default Client.
285+
func RegisterHistogram(name string, opts ...MOption) Histogram {
286+
meter := NewHistogram(name)
287+
defaultClient.Register(meter, opts...)
288+
return meter
289+
}
290+
256291
//--------------------------------------------------------------
257292

258293
// AdhocCounter creates an ad-hoc counter metric event.

metric/doc.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,41 @@ Package metric is a generic metric package for the standard metric types gauges/
44
This implementation is aimed at being as fast as possible to not discourage metrics on values in hotpaths just because of locking overhead. This requires some client side buffering (and flusher go-routines) and, especially for the timer/histogram event type, a relatively large data structure to create a ring-buffer with mostly lock-free writes. (it uses condition variables for flushing).
55
This design is for the use case where you have a lot of timer/histogram metric events going to a few buckets.
66
7-
The API is accessed via Client objects. (There's a default global client). The low latency and speed is achieved by creating permanent buffering metrics objects for each metric. Alternatively you can manually send ad-hoc metrics directly to the Clients configured sink. This allows for use cases where you don't have a small set of known metrics receiving many events, but rather many metrics receiving few evetns each.
7+
The API consists of 3 main types of objects:
8+
9+
* Meter - the object doing the actual measuring. (possibly buffering measurements)
10+
* Sink - an object to which Meter readings can be sent. Sinks can have internal buffering which can be flushed
11+
* Client - an object responsible for sending Meter readings to an assigned Sink, possibly at configured flushing intervals.
12+
13+
The API supports 3 approaches to maintaining your metrics:
14+
15+
Ad-hoc
16+
17+
Ad-hoc generation of metric events without a Meter object (explicitly flushing the Client sink):
18+
This is a relative slow, but simple way to generate metric data.
19+
This allows for use cases where you don't have a small set of known metrics receiving many events, but rather many metrics receiving few events each:
20+
21+
metric.SetDefaultSink(sink)
22+
metric.AdhocGauge("gauge", 17, true)
23+
24+
Manually
25+
26+
Manually making readings of a meter directly to a Sink. (use this only if strictly needed)
27+
28+
gauge := metric.NewGauge("gauge")
29+
gauge.Set(17)
30+
gauge.FlushReading(sink)
31+
sink.Flush()
32+
33+
Managed
34+
35+
Registring a buffered Meter object with a client. This is the fastest method.
36+
The low latency API is accessed via Client objects. (There's a default global client). The speed is achieved by creating permanent buffering metrics objects for each metric.
37+
38+
client := metric.NewClient(sink, metric.FlushInterval(time.Second))
39+
gauge := client.RegisterGauge("gauge")
40+
gauge.Set(17)
41+
842
943
The statsd sink also does client side buffering before sending UDP packages and is flushed when asked, or when running full. You can set the max size of the UDP datagrams created.
1044
*/

metric/histotimer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func NewHistogram(name string) Histogram {
2121
return Histogram{t}
2222
}
2323

24-
// Sample record new event for the histogram
24+
// Sample records new event for the histogram
2525
func (e Histogram) Sample(d int64) {
2626
//e := (*eventStream)(h)
2727
e.enqueue(uint64(d))

metric/sink.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ package metric
22

33
import "github.com/One-com/gone/metric/num64"
44

5-
// Sink is a sink for metrics data which methods are guaranteed to be called
6-
// synchronized. Thus it can keep state like buffers without locking
7-
// metric Client should Record*() to a Sink.
8-
// The idea is that the Sink in principle needs to be go-routine safe,
9-
// but Sink.Record*() will be called synchronized and only from one go-routine, so you can spare
10-
// the synchronization in the Record*() implementation it self if the Sink can
11-
// be called upon to Clone a new instance for each go-routine using the sink.
5+
// Sink is a sink for metrics data which methods need to be go-routine safe
126
type Sink interface {
137
// Record a named value of any time with the Sink
148
Record(mtype int, name string, value interface{})
@@ -20,18 +14,18 @@ type Sink interface {
2014
// not being able to guarantee the values can be stack allocated.
2115
RecordNumeric64(mtype int, name string, value num64.Numeric64)
2216

23-
// Flush flushes the record values from Sink buffers.
17+
// Flush flushes the record values from the program internal Sink buffers.
2418
Flush()
2519
}
2620

27-
// SinkFactory is the interface of objects returned to the user by sink implementations.
28-
// It serves to be able for gone/metric to create new Sink objects which can be called.
29-
// concurrently under external locking.
21+
// UnlockedSink implmentors can return a Sink which does not need to protect it self
22+
// against access from multiple go-routines.
23+
// This is used by flushing go-routines to save a lot of locking when flushing metric
24+
// event buffers to the Sink.
3025
// The returned Sink is guaranteed to only be called from one go-routine under a lock.
3126
// This allows a sink implementation to avoid using several layers of locks.
32-
// A sink implementation can chose not to exploit this and a simple SinkFactory can
27+
// A sink implementation can chose not to exploit this and not implement the interface or
3328
// just return it self as a Sink an do locking on all access.
34-
3529
type unlockedSink interface {
3630
UnlockedSink() Sink
3731
}

metric/sink_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestReplaceNilSink(t *testing.T) {
2020
t.Fatal(err)
2121
}
2222

23-
gauge := metric.Default().RegisterGauge("gauge")
23+
gauge := metric.RegisterGauge("gauge")
2424

2525
gauge.Set(23)
2626
metric.Flush()
@@ -60,7 +60,7 @@ func TestFlushSink(t *testing.T) {
6060
metric.SetDefaultSink(sink)
6161
metric.SetDefaultOptions(metric.FlushInterval(500 * time.Millisecond))
6262

63-
gauge := metric.Default().RegisterGauge("auto")
63+
gauge := metric.RegisterGauge("auto")
6464

6565
gauge.Set(25)
6666

0 commit comments

Comments
 (0)