Skip to content

Commit a953f2b

Browse files
author
Peter Mogensen
committed
More Go doc for metric
1 parent 6d41fd3 commit a953f2b

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

metric/client.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"time"
66
)
77

8+
// Client is the main object the applications holds to do metrics.
9+
// It can be used directly for ad-hoc events, or be used to create persistent
10+
// gauge/conter/timer/... objects optimised for bulk metric generation.
811
type Client struct {
912

1013
// Wait for all flushers to empty before exiting Stop()
@@ -32,7 +35,7 @@ func init() {
3235

3336
// NewClient returns you a client handle directly if you do not want to use the
3437
// global default client.
35-
// Create a new metric client with a factory object for the sink.
38+
// Creates a new metric client with a factory object for the sink.
3639
// If sink == nil, the client will not emit metrics until a Sink is set.
3740
func NewClient(sinkf SinkFactory, opts ...MOption) (client *Client) {
3841

@@ -51,13 +54,13 @@ func NewClient(sinkf SinkFactory, opts ...MOption) (client *Client) {
5154
return
5255
}
5356

54-
// Set options on the default metric client
57+
// SetDefaultOptions sets options on the default metric client
5558
func SetDefaultOptions(opts ...MOption) {
5659
c := defaultClient
5760
c.SetOptions(opts...)
5861
}
5962

60-
// Set options on a client - like the flush interval for metrics which
63+
// SetOptions sets options on a client - like the flush interval for metrics which
6164
// haven't them selves a fixed flush interval
6265
func (c *Client) SetOptions(opts ...MOption) {
6366
conf := make(map[string]interface{})
@@ -74,12 +77,14 @@ func (c *Client) SetOptions(opts ...MOption) {
7477
c.fmu.Unlock()
7578
}
7679

80+
// SetDefaultSink sets the sink for the default metics client
7781
func SetDefaultSink(sinkf SinkFactory) {
7882
c := defaultClient
7983
c.SetSink(sinkf)
8084
}
8185

82-
// The the Sink factory of the client
86+
// SetSink sets the Sink factory of the client.
87+
// You'll need to set a sink before any metrics will be emitted.
8388
func (c *Client) SetSink(sinkf SinkFactory) {
8489
c.fmu.Lock()
8590

@@ -119,7 +124,7 @@ func (c *Client) Start() {
119124
c.running = true
120125
}
121126

122-
// Stops the global default metrics client
127+
// Stop the global default metrics client
123128
func Stop() {
124129
defaultClient.Stop()
125130
}
@@ -182,19 +187,27 @@ func (c *Client) Flush() {
182187
c.defaultFlusher.FlushSink()
183188
}
184189

190+
// Counter creates an ad-hoc counter metric event.
191+
// If flush is true, the sink will be instructed to flush data immediately
185192
func (c *Client) Counter(name string, val int, flush bool) {
186193
c.defaultFlusher.RecordNumeric64(MeterCounter, name, Numeric64{Type: Int64, value: uint64(val)}, flush)
187194
}
188195

196+
// Gauge creates an ad-hoc gauge metric event.
197+
// If flush is true, the sink will be instructed to flush data immediately
189198
func (c *Client) Gauge(name string, val uint64, flush bool) {
190199
c.defaultFlusher.RecordNumeric64(MeterGauge, name, Numeric64{Type: Uint64, value: uint64(val)}, flush)
191200
}
192201

202+
// Timer creates an ad-hoc timer metric event.
203+
// If flush is true, the sink will be instructed to flush data immediately
193204
func (c *Client) Timer(name string, d time.Duration, flush bool) {
194205
val := d.Nanoseconds()/int64(1000000)
195206
c.defaultFlusher.RecordNumeric64(MeterTimer, name, Numeric64{Type: Uint64, value: uint64(val)}, flush)
196207
}
197208

209+
// Sample creates an ad-hoc histogram metric event.
210+
// If flush is true, the sink will be instructed to flush data immediately
198211
func (c *Client) Sample(name string, val int64, flush bool) {
199212
c.defaultFlusher.RecordNumeric64(MeterHistogram, name, Numeric64{Type: Int64, value: uint64(val)}, flush)
200213
}

metric/gauge.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ import (
99
// wrt. the absolute value.
1010
// Can be used as a client side maintained counter too.
1111

12-
// The default gauge type using a uint64
12+
// GaugeUint64 is the default gauge type using a uint64
1313
type GaugeUint64 struct {
1414
name string
1515
val uint64
1616
}
1717

18-
// A gauge using an int64 - meaning it can be decremented to negaive values
18+
// GaugeInt64 is a gauge using an int64 - meaning it can be decremented to negaive values
1919
type GaugeInt64 struct {
2020
name string
2121
val int64
2222
}
2323

24-
// A go-metric compatible float64 gauge which stores its value as a uint64
24+
// GaugeFloat64 is a float64 gauge which stores its value as a uint64
2525
// to implement Flush() fast
2626
type GaugeFloat64 struct {
2727
name string
@@ -59,17 +59,17 @@ func (g *GaugeUint64) Name() string {
5959
return g.name
6060
}
6161

62-
// Mtype to implement Meter interface
62+
// MeterType to implement Meter interface
6363
func (g *GaugeUint64) MeterType() int {
6464
return MeterGauge
6565
}
6666

67-
// Update the gauge value
67+
// Set will update the gauge value
6868
func (g *GaugeUint64) Set(val uint64) {
6969
atomic.StoreUint64(&g.val, val)
7070
}
7171

72-
// Get the gauge value
72+
// Value returns the gauge value
7373
func (g *GaugeUint64) Value() uint64 {
7474
return atomic.LoadUint64(&g.val)
7575
}

metric/sink.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ type Sink interface {
1313
Flush()
1414
}
1515

16+
// SinkFactory is the interface of objects returned to the user by sink implementations.
17+
// It serves to be able for gone/metric to create new Sink objects which can be called.
18+
// concurrently.
19+
// This allows a sink implementation to avoid using several layers of locks.
1620
type SinkFactory interface {
1721
Sink() Sink
1822
}

metric/sink/statsd/statsd.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,24 @@ type statsdSink struct {
2525
buf []byte
2626
}
2727

28+
// Buffer sets the package size with which writes to the underlying io.Writer (often an UDPConn)
29+
// is done.
2830
func Buffer(size int) Option {
2931
return Option(func(s *statsdSinkFactory) error {
3032
s.max = size
3133
return nil
3234
})
3335
}
3436

37+
// Prefix is prepended with "prefix." to all metric names
3538
func Prefix(pfx string) Option {
3639
return Option(func(s *statsdSinkFactory) error {
3740
s.prefix = pfx + "."
3841
return nil
3942
})
4043
}
4144

45+
// Peer is the address of the statsd UDP server
4246
func Peer(addr string) Option {
4347
return Option(func(s *statsdSinkFactory) error {
4448
conn, err := net.DialTimeout("udp", addr, time.Second)
@@ -49,15 +53,16 @@ func Peer(addr string) Option {
4953
return nil
5054
})
5155
}
52-
56+
57+
// Output sets an general io.Writer as output instead of a UDPConn.
5358
func Output(w io.Writer) Option {
5459
return Option(func(s *statsdSinkFactory) error {
5560
s.out = w
5661
return nil
5762
})
5863
}
5964

60-
// A Sink sending data to a UDP statsd server
65+
// New creasted a SinkFactory which is used to create Sinks sending data to a UDP statsd server
6166
// Provide a UDP address, a prefix and a maximum UDP datagram size.
6267
// 1432 should be a safe size for most nets.
6368
func New(opts ...Option) (sink *statsdSinkFactory, err error) {
@@ -73,6 +78,7 @@ func New(opts ...Option) (sink *statsdSinkFactory, err error) {
7378
return
7479
}
7580

81+
// Sink implmenets the SinkFactory interface.
7682
func (s *statsdSinkFactory) Sink() (metric.Sink) {
7783
newsink := &statsdSink{out: s.out, max: s.max, prefix: s.prefix}
7884
newsink.buf = make([]byte,0,512)

0 commit comments

Comments
 (0)