Skip to content

Commit 406f8f8

Browse files
debugstats: add package example, and test
1 parent cff29ae commit 406f8f8

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,15 @@ func main() {
181181
Use the `debugstats` package to print all stats to the console.
182182

183183
```go
184-
handler := debugstats.Client{Dst: os.Stdout}
184+
handler := &debugstats.Client{Dst: os.Stdout}
185+
// to use as a standalone engine:
185186
engine := stats.NewEngine("engine-name", handler)
186187
engine.Incr("server.start")
188+
// or, register on the default stats engine:
189+
stats.Register(handler)
190+
191+
// Sample output:
192+
// server.start:1|c
187193
```
188194

189195
You can use the `Grep` property to filter the printed metrics for only ones you
@@ -203,7 +209,7 @@ Monitoring
203209
> `go_version` in v4.6.0.
204210
205211
The
206-
[github.com/segmentio/stats/procstats](https://godoc.org/github.com/segmentio/stats/procstats)
212+
[github.com/segmentio/stats/v5/procstats](https://godoc.org/github.com/segmentio/stats/v5/procstats)
207213
package exposes an API for creating a statistics collector on local processes.
208214
Statistics are collected for the current process and metrics including Goroutine
209215
count and memory usage are reported.
@@ -249,7 +255,7 @@ func main() {
249255

250256
### HTTP Servers
251257

252-
The [github.com/segmentio/stats/httpstats](https://godoc.org/github.com/segmentio/stats/httpstats)
258+
The [github.com/segmentio/stats/v5/httpstats](https://godoc.org/github.com/segmentio/stats/v5/httpstats)
253259
package exposes a decorator of `http.Handler` that automatically adds metric
254260
collection to a HTTP handler, reporting things like request processing time,
255261
error counters, header and body sizes...
@@ -284,7 +290,7 @@ func main() {
284290

285291
### HTTP Clients
286292

287-
The [github.com/segmentio/stats/httpstats](https://godoc.org/github.com/segmentio/stats/httpstats)
293+
The [github.com/segmentio/stats/v5/httpstats](https://godoc.org/github.com/segmentio/stats/v5/httpstats)
288294
package exposes a decorator of `http.RoundTripper` which collects and reports
289295
metrics for client requests the same way it's done on the server side.
290296

debugstats/debugstats.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,50 @@
1-
// Package debugstats simplifies metric troubleshooting by sending metrics to
2-
// any io.Writer.
1+
// Package debugstats is a very small helper that makes it easy to **see** the
2+
// raw metrics produced by github.com/segmentio/stats/v5 while you are debugging
3+
// or developing a new instrumentation strategy.
34
//
4-
// By default, metrics will be printed to os.Stdout. Use the Dst and Grep fields
5-
// to customize the output as appropriate.
5+
// - It implements the stats.Handler interface.
6+
// - Every metric that reaches the handler is written as a single line in a
7+
// StatsD-like format (metric name, value, type, tags) followed by '\n'.
8+
// - A time-stamp (RFC-3339) is prepended so that the stream can later be
9+
// correlated with logs if desired.
10+
//
11+
// # Destination
12+
//
13+
// By default the lines are written to os.Stdout, but any io.Writer can be
14+
// supplied through the Client’s Dst field:
15+
//
16+
// var buf bytes.Buffer
17+
// stats.Register(&debugstats.Client{Dst: &buf}) // write into a buffer
18+
//
19+
// # Grep-like filtering
20+
//
21+
// When you are only interested in a subset of your metrics you can pass a
22+
// regular expression via the Grep field. Only the lines whose *full textual
23+
// representation* match the regexp are emitted:
24+
//
25+
// stats.Register(&debugstats.Client{
26+
// Grep: regexp.MustCompile(`^my_service\.http_requests_total`),
27+
// })
28+
//
29+
// Quick example
30+
//
31+
// func main() {
32+
// stats.Register(&debugstats.Client{}) // defaults to stdout
33+
//
34+
// stats.Set("active_users", 42)
35+
// stats.Observe("compression_ratio", 0.28,
36+
// stats.T("codec", "zstd"),
37+
// stats.T("bucket", "10-100 kB"),
38+
// )
39+
//
40+
// // Flush whenever you need a consistent snapshot:
41+
// stats.Flush()
42+
// }
43+
//
44+
// Typical output (wrapped for readability):
45+
//
46+
// 2024-04-18T09:45:00Z active_users:42|g
47+
// 2024-04-18T09:45:00Z compression_ratio:0.28|d|#bucket:10-100 kB,codec:zstd
648
package debugstats
749

850
import (

debugstats/example_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package debugstats_test
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/segmentio/stats/v5"
7+
"github.com/segmentio/stats/v5/debugstats"
8+
)
9+
10+
// ExampleClient demonstrates how to register a debugstats.Client so that all
11+
// produced metrics are echoed to stdout. The example purposefully omits an
12+
// output check because the first column is a time-stamp whose value varies.
13+
func ExampleClient() {
14+
// Only show metrics whose name contains "foo".
15+
stats.Register(&debugstats.Client{
16+
Grep: regexp.MustCompile(`foo`),
17+
})
18+
19+
stats.Set("foo_active_users", 123)
20+
stats.Observe("bar_compression_ratio", 0.37) // <- this one is filtered out
21+
22+
// Flush to make sure the handler has processed everything before the
23+
// program exits in short-lived examples or CLI tools.
24+
stats.Flush()
25+
}

0 commit comments

Comments
 (0)