Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Meituan-Dianping/cat-go
https://github.com/Meituan-Dianping/cat-go
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/Meituan-Dianping/cat-go
- Owner: Meituan-Dianping
- License: apache-2.0
- Created: 2019-01-16T06:09:30.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-04-03T03:30:59.000Z (over 1 year ago)
- Last Synced: 2024-06-19T21:10:25.235Z (3 months ago)
- Language: Go
- Size: 60.5 KB
- Stars: 52
- Watchers: 5
- Forks: 26
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome - Meituan-Dianping/cat-go - (Go)
README
# Cat client for golang (v2)
`gocat.v2` is the client sdk for the [CAT(Central Application Tracking)](https://github.com/dianping/cat) for the Go programming language.
See the [**DIFFERENCES**](DIFFERENCES.md) between [gocat](https://github.com/dianping/cat/tree/master/lib) and `gocat.v2`
The sdk requires a minimum version of Go 1.8
The current latest version is 2.0.0
Please check [**CHANGELOG**](./CHANGELOG.md) for information about our latest updates to the sdk.
## Installation
You can simply get started with our sdk by using the following command:
```
go get github.com/Meituan-Dianping/cat-go
```## Quickstart
This example shows how you can send tracking data to CAT using `gocat.v2`.
```go
package mainimport (
"errors"
"math/rand"
"sync"
"time""github.com/Meituan-Dianping/cat-go/cat"
)const TestType = "foo"
var wg = sync.WaitGroup{}
func init() {
cat.DebugOn()
cat.Init("gocat.v2")
}// send transaction
func case1() {
t := cat.NewTransaction(TestType, "test")
defer t.Complete()if rand.Int31n(100) == 0 {
t.SetStatus(cat.FAIL)
}t.AddData("foo", "bar")
t.NewEvent(TestType, "event-1")
t.Complete()if rand.Int31n(100) == 0 {
t.LogEvent(TestType, "event-2", cat.FAIL)
} else {
t.LogEvent(TestType, "event-2")
}
t.LogEvent(TestType, "event-3", cat.SUCCESS, "k=v")t.SetDurationStart(time.Now().Add(-5 * time.Second))
t.SetTime(time.Now().Add(-5 * time.Second))
t.SetDuration(time.Millisecond * 500)
}// send completed transaction with duration
func case2() {
cat.NewCompletedTransactionWithDuration(TestType, "completed", time.Second*24)
cat.NewCompletedTransactionWithDuration(TestType, "completed-over-60s", time.Second*65)
}// send event
func case3() {
// way 1
e := cat.NewEvent(TestType, "event-4")
e.Complete()
// way 2if rand.Int31n(100) == 0 {
cat.LogEvent(TestType, "event-5", cat.FAIL)
} else {
cat.LogEvent(TestType, "event-5")
}
cat.LogEvent(TestType, "event-6", cat.SUCCESS, "foobar")
}// send error with backtrace
func case4() {
if rand.Int31n(100) == 0 {
err := errors.New("error")
cat.LogError(err)
}
}// send metric
func case5() {
cat.LogMetricForCount("metric-1")
cat.LogMetricForCount("metric-2", 3)
cat.LogMetricForDuration("metric-3", 150*time.Millisecond)
cat.NewMetricHelper("metric-4").Count(7)
cat.NewMetricHelper("metric-5").Duration(time.Second)
}func run(f func()) {
defer wg.Done()for i := 0; i < 100000000; i++ {
f()
time.Sleep(time.Microsecond * 100)
}
}func start(f func()) {
wg.Add(1)
go run(f)
}func main() {
start(case1)
start(case2)
start(case3)
start(case4)
start(case5)wg.Wait()
cat.Shutdown()
}```
## License
This SDK is distributed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0),