github.com/uber-go/tally/v4@v4.1.17/statsd/example/statsd_main.go (about) 1 // Copyright (c) 2021 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package main 22 23 import ( 24 "log" 25 "math/rand" 26 "time" 27 28 "github.com/cactus/go-statsd-client/v5/statsd" 29 tally "github.com/uber-go/tally/v4" 30 statsdreporter "github.com/uber-go/tally/v4/statsd" 31 ) 32 33 // To view statsd emitted metrics locally you can use 34 // netcat with "nc 8125 -l -u" 35 func main() { 36 statter, err := statsd.NewClientWithConfig(&statsd.ClientConfig{ 37 Address: "127.0.0.1:8125", 38 Prefix: "stats", 39 UseBuffered: true, 40 FlushInterval: 100 * time.Millisecond, 41 FlushBytes: 1440, 42 }) 43 if err != nil { 44 log.Fatalf("could not create statsd client: %v", err) 45 } 46 47 opts := statsdreporter.Options{} 48 r := statsdreporter.NewReporter(statter, opts) 49 50 scope, closer := tally.NewRootScope(tally.ScopeOptions{ 51 Prefix: "my-service", 52 Tags: map[string]string{}, 53 Reporter: r, 54 }, 1*time.Second) 55 defer closer.Close() 56 57 counter := scope.Counter("test-counter") 58 59 gauge := scope.Gauge("test-gauge") 60 61 timer := scope.Timer("test-timer") 62 63 histogram := scope.Histogram("test-histogram", tally.DefaultBuckets) 64 65 go func() { 66 for { 67 counter.Inc(1) 68 time.Sleep(time.Second) 69 } 70 }() 71 72 go func() { 73 for { 74 gauge.Update(rand.Float64() * 1000) 75 time.Sleep(time.Second) 76 } 77 }() 78 79 go func() { 80 for { 81 tsw := timer.Start() 82 hsw := histogram.Start() 83 time.Sleep(time.Duration(rand.Float64() * float64(time.Second))) 84 tsw.Stop() 85 hsw.Stop() 86 } 87 }() 88 89 select {} 90 }