-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
66 lines (57 loc) · 1.63 KB
/
store_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
package example
import (
"context"
"github.com/kirk91/stats"
"github.com/kirk91/stats/sink/statsd"
)
const (
appid = "samaritan"
region = "aisa-ch"
zone = "hz"
)
func ExampleStore() {
store := stats.NewStore(stats.NewStoreOption())
// NOTE: If the underlying sink supports tag feature, you could
// add it depends on demand. Otherwise, you should ignore the
// following section.
defaultTags := map[string]string{
"region": region,
"zone": zone,
}
tagExtractStrategies := []stats.TagExtractStrategy{
{
// service.(<service_name>.)*
Name: "service_name",
Regex: "^service\\.((.*?)\\.)",
},
{
// service.[<service_name>.]redis.(<redis_cmd>.)<base_stat>
Name: "redis_cmd",
Regex: "^service(?:\\.).*?\\.redis\\.((.*?)\\.)",
},
}
tagOption := stats.NewTagOption().WithDefaultTags(defaultTags).
WithTagExtractStrategies(tagExtractStrategies...)
store.SetTagOption(tagOption)
// add statsd sink
statsdAddr := "127.0.0.1:8125"
statsdSink := statsd.New(statsdAddr, appid)
store.AddSink(statsdSink)
// start the background routine which flushes the metrics periodically.
ctx, cancel := context.WithCancel(context.TODO())
loopDone := make(chan struct{})
go func() {
store.FlushingLoop(ctx)
close(loopDone)
}()
defer func() {
cancel()
<-loopDone
}()
// create a stats group, the metrics which under it will sharing the same prefix.
scope := store.CreateScope("listener")
scope.Counter("conn_create").Inc() // listener.conn_create
scope.Gauge("conn_active").Inc() // listener.conn_active
scope.Histogram("conn_length_sec").Record(1) // listener.conn_length_sec
//Output:
}