-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples_test.go
154 lines (123 loc) · 2.98 KB
/
examples_test.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
//nolint:testableexamples
package statsd_test
import (
"log"
"runtime"
"time"
"github.com/tecnickcom/statsd"
)
func ping(_ string) {}
func Example() {
c, err := statsd.New() // Connect to the UDP port 8125 by default.
if err != nil {
// If nothing is listening on the target port, an error is returned and
// the returned client does nothing but is still usable. So we can
// just log the error and go on.
log.Print(err)
}
defer c.Close()
// Increment a counter.
c.Increment("foo.counter")
// Gauge something.
c.Gauge("num_goroutine", runtime.NumGoroutine())
// Time something.
t := c.NewTiming()
ping("http://example.com/")
t.Send("homepage.response_time")
// It can also be used as a one-liner to easily time a function.
pingHomepage := func() {
defer c.NewTiming().Send("homepage.response_time")
ping("http://example.com/")
}
pingHomepage()
// Cloning a Client allows using different parameters while still using the
// same connection.
// This is way cheaper and more efficient than using New().
stat := c.Clone(statsd.Prefix("http"), statsd.SampleRate(0.2))
stat.Increment("view") // Increments http.view
}
func ExampleClient_Clone() {
c, err := statsd.New(statsd.Prefix("my_app"))
if err != nil {
log.Print(err)
}
httpStats := c.Clone(statsd.Prefix("http"))
httpStats.Increment("foo.bar") // Increments: my_app.http.foo.bar
}
func ExampleAddress() {
_, err := statsd.New(statsd.Address("192.168.0.5:8126"))
if err != nil {
log.Print(err)
}
}
func ExampleErrorHandler() {
_, err := statsd.New(statsd.ErrorHandler(func(err error) {
log.Print(err)
}))
if err != nil {
log.Print(err)
}
}
func ExampleFlushPeriod() {
_, err := statsd.New(statsd.FlushPeriod(10 * time.Millisecond))
if err != nil {
log.Print(err)
}
}
func ExampleMaxPacketSize() {
_, err := statsd.New(statsd.MaxPacketSize(512))
if err != nil {
log.Print(err)
}
}
func ExampleNetwork() {
// Send metrics using a TCP connection.
_, err := statsd.New(statsd.Network("tcp"))
if err != nil {
log.Print(err)
}
}
func ExampleTagsFormat() {
_, err := statsd.New(statsd.TagsFormat(statsd.InfluxDB))
if err != nil {
log.Print(err)
}
}
func ExampleMute() {
c, err := statsd.New(statsd.Mute(true))
if err != nil {
log.Print(err)
}
c.Increment("foo.bar") // Does nothing.
}
func ExampleSampleRate() {
_, err := statsd.New(statsd.SampleRate(0.2)) // Send metrics 20% of the time.
if err != nil {
log.Print(err)
}
}
func ExamplePrefix() {
c, err := statsd.New(statsd.Prefix("my_app"))
if err != nil {
log.Print(err)
}
c.Increment("foo.bar") // Increments: my_app.foo.bar
}
func ExampleTags() {
_, err := statsd.New(
statsd.TagsFormat(statsd.InfluxDB),
statsd.Tags("region", "us", "app", "my_app"),
)
if err != nil {
log.Print(err)
}
}
func ExampleClient_NewTiming() {
c, err := statsd.New()
if err != nil {
log.Print(err)
}
// Send a timing metric each time the function is run.
defer c.NewTiming().Send("homepage.response_time")
ping("http://example.com/")
}