github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/catgo/cat-go/README.md (about) 1 # Cat client for golang (v2) 2 3 `gocat.v2` is the client sdk for the [CAT(Central Application Tracking)](https://github.com/dianping/cat) for the Go programming language. 4 5 See the [**DIFFERENCES**](DIFFERENCES.md) between [gocat](https://github.com/dianping/cat/tree/master/lib) and `gocat.v2` 6 7 The sdk requires a minimum version of Go 1.8 8 9 The current latest version is 2.0.0 10 11 Please check [**CHANGELOG**](./CHANGELOG.md) for information about our latest updates to the sdk. 12 13 ## Installation 14 15 You can simply get started with our sdk by using the following command: 16 ``` 17 go get github.com/Meituan-Dianping/cat-go 18 ``` 19 20 ## Quickstart 21 22 This example shows how you can send tracking data to CAT using `gocat.v2`. 23 24 ```go 25 package main 26 27 import ( 28 "errors" 29 "math/rand" 30 "sync" 31 "time" 32 33 "github.com/Meituan-Dianping/cat-go/cat" 34 ) 35 36 const TestType = "foo" 37 38 var wg = sync.WaitGroup{} 39 40 func init() { 41 cat.DebugOn() 42 cat.Init("gocat.v2") 43 } 44 45 // send transaction 46 func case1() { 47 t := cat.NewTransaction(TestType, "test") 48 defer t.Complete() 49 50 if rand.Int31n(100) == 0 { 51 t.SetStatus(cat.FAIL) 52 } 53 54 t.AddData("foo", "bar") 55 56 t.NewEvent(TestType, "event-1") 57 t.Complete() 58 59 if rand.Int31n(100) == 0 { 60 t.LogEvent(TestType, "event-2", cat.FAIL) 61 } else { 62 t.LogEvent(TestType, "event-2") 63 } 64 t.LogEvent(TestType, "event-3", cat.SUCCESS, "k=v") 65 66 t.SetDurationStart(time.Now().Add(-5 * time.Second)) 67 t.SetTime(time.Now().Add(-5 * time.Second)) 68 t.SetDuration(time.Millisecond * 500) 69 } 70 71 // send completed transaction with duration 72 func case2() { 73 cat.NewCompletedTransactionWithDuration(TestType, "completed", time.Second*24) 74 cat.NewCompletedTransactionWithDuration(TestType, "completed-over-60s", time.Second*65) 75 } 76 77 // send event 78 func case3() { 79 // way 1 80 e := cat.NewEvent(TestType, "event-4") 81 e.Complete() 82 // way 2 83 84 if rand.Int31n(100) == 0 { 85 cat.LogEvent(TestType, "event-5", cat.FAIL) 86 } else { 87 cat.LogEvent(TestType, "event-5") 88 } 89 cat.LogEvent(TestType, "event-6", cat.SUCCESS, "foobar") 90 } 91 92 // send error with backtrace 93 func case4() { 94 if rand.Int31n(100) == 0 { 95 err := errors.New("error") 96 cat.LogError(err) 97 } 98 } 99 100 // send metric 101 func case5() { 102 cat.LogMetricForCount("metric-1") 103 cat.LogMetricForCount("metric-2", 3) 104 cat.LogMetricForDuration("metric-3", 150*time.Millisecond) 105 cat.NewMetricHelper("metric-4").Count(7) 106 cat.NewMetricHelper("metric-5").Duration(time.Second) 107 } 108 109 func run(f func()) { 110 defer wg.Done() 111 112 for i := 0; i < 100000000; i++ { 113 f() 114 time.Sleep(time.Microsecond * 100) 115 } 116 } 117 118 func start(f func()) { 119 wg.Add(1) 120 go run(f) 121 } 122 123 func main() { 124 start(case1) 125 start(case2) 126 start(case3) 127 start(case4) 128 start(case5) 129 130 wg.Wait() 131 132 cat.Shutdown() 133 } 134 135 ``` 136 137 ## License 138 139 This SDK is distributed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0),