mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-11 16:59:59 +08:00
Compare commits
176 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a8638fc85 | ||
|
|
837a9ffa03 | ||
|
|
d28cfe5f20 | ||
|
|
022c100dc9 | ||
|
|
426b09c356 | ||
|
|
40dc21e4cf | ||
|
|
9b114e3251 | ||
|
|
4c6234f108 | ||
|
|
3cdfcb05f1 | ||
|
|
9f5bfa0088 | ||
|
|
2d42c8fa00 | ||
|
|
10e7922597 | ||
|
|
6e34b55ba7 | ||
|
|
ed15ca04f4 | ||
|
|
295ec27e1b | ||
|
|
d1e702e8a3 | ||
|
|
d1bfb5ef61 | ||
|
|
e43357164c | ||
|
|
cd21c9fa74 | ||
|
|
cdd2fcbbc9 | ||
|
|
8d2db09d45 | ||
|
|
65905b914d | ||
|
|
80e3407be1 | ||
|
|
657d27213a | ||
|
|
8ac18a9422 | ||
|
|
d3ae9cfd49 | ||
|
|
d7f42161fd | ||
|
|
e03229cabe | ||
|
|
8403ed16ae | ||
|
|
d87d203c3b | ||
|
|
3ae6a882a7 | ||
|
|
41c980f00c | ||
|
|
f34d81ca2c | ||
|
|
004ee488a6 | ||
|
|
2e12cd2c99 | ||
|
|
2695c30886 | ||
|
|
c74fb988e0 | ||
|
|
e8a340c1c0 | ||
|
|
06e114e5a3 | ||
|
|
74ad681a66 | ||
|
|
e7bbc09093 | ||
|
|
1eb1450c43 | ||
|
|
9a724fe907 | ||
|
|
30e49f2939 | ||
|
|
a5407479a6 | ||
|
|
7fb5bab26b | ||
|
|
27249e021f | ||
|
|
d809795fec | ||
|
|
c9db9588b7 | ||
|
|
872c50b71a | ||
|
|
7c83155e4f | ||
|
|
358d86b8ae | ||
|
|
f4bb9f5635 | ||
|
|
5c6a3132eb | ||
|
|
2bd95aa007 | ||
|
|
e8376936d5 | ||
|
|
71c0288023 | ||
|
|
9e2f07a842 | ||
|
|
24fd34413f | ||
|
|
3f47251892 | ||
|
|
0b6bc69afa | ||
|
|
5b9bdc8d02 | ||
|
|
ded22e296e | ||
|
|
f0ed2370a3 | ||
|
|
6bf6cfdd01 | ||
|
|
5cc9eb0de4 | ||
|
|
f070d447ef | ||
|
|
f6d9e19ecb | ||
|
|
56807aabf6 | ||
|
|
861dcf2f36 | ||
|
|
c837dc21bb | ||
|
|
96a35ecf1a | ||
|
|
bdec5f2349 | ||
|
|
bc92b57bdb | ||
|
|
d8905b9e9e | ||
|
|
dec6309c55 | ||
|
|
10805577f5 | ||
|
|
a4d8286e36 | ||
|
|
84d2b64e7c | ||
|
|
6476da4a18 | ||
|
|
79eab0ea2f | ||
|
|
3b683fd498 | ||
|
|
d179b342b2 | ||
|
|
58874779e7 | ||
|
|
8829c31c0d | ||
|
|
b42f3fa047 | ||
|
|
9bdadf2381 | ||
|
|
20f665ede8 | ||
|
|
0325d8e92d | ||
|
|
2125977281 | ||
|
|
c26c187e11 | ||
|
|
4ef1859f0b | ||
|
|
407a6cbf9c | ||
|
|
76fc1ef460 | ||
|
|
423955c55f | ||
|
|
db95b3f0e3 | ||
|
|
4bee60eb7f | ||
|
|
7618139dad | ||
|
|
6fd08027ff | ||
|
|
b9e268aae8 | ||
|
|
4c1bb1148b | ||
|
|
50a6bbe6b9 | ||
|
|
dfb3cb510a | ||
|
|
519db812b4 | ||
|
|
3203f8e06b | ||
|
|
b71ac2042a | ||
|
|
d0f9e57022 | ||
|
|
aa68210cde | ||
|
|
280e837c9e | ||
|
|
f669e1226c | ||
|
|
cd15c19250 | ||
|
|
5b35fa17de | ||
|
|
9672298fa8 | ||
|
|
bf3ce16823 | ||
|
|
189721da16 | ||
|
|
a523ab1f93 | ||
|
|
7ea8b636d9 | ||
|
|
b2fea65faa | ||
|
|
a1fe8bf6cd | ||
|
|
67ee9e4391 | ||
|
|
9c1ee50497 | ||
|
|
7c842f22d0 | ||
|
|
14ec29991c | ||
|
|
c7f5aad83a | ||
|
|
e77747cff8 | ||
|
|
f2612db4b1 | ||
|
|
a21ff71373 | ||
|
|
fc04ad7854 | ||
|
|
fbf2eebc42 | ||
|
|
dc43430812 | ||
|
|
c6642bc2e6 | ||
|
|
bdca24dd3b | ||
|
|
00c5734021 | ||
|
|
33f87cf1f0 | ||
|
|
69935c1ba3 | ||
|
|
1fb356f328 | ||
|
|
0b0406f41a | ||
|
|
cc264dcf55 | ||
|
|
e024aebb66 | ||
|
|
f204729482 | ||
|
|
d20cf56a69 | ||
|
|
54d57c7d4b | ||
|
|
28a7c9d38f | ||
|
|
872e75e10d | ||
|
|
af1730079e | ||
|
|
04521e2d24 | ||
|
|
02adcccbf4 | ||
|
|
a74aaf1823 | ||
|
|
1eb2089c69 | ||
|
|
f7f3730e1a | ||
|
|
0ee7654407 | ||
|
|
16cc990fdd | ||
|
|
00061c2e5b | ||
|
|
6793f7a1de | ||
|
|
c8428a7f65 | ||
|
|
a5e1d0d0dc | ||
|
|
8270c7deed | ||
|
|
9f4a882a1b | ||
|
|
cb7b7cb72e | ||
|
|
603c93aa4a | ||
|
|
cb8d9d413a | ||
|
|
ff7443c6a7 | ||
|
|
b812e74d6f | ||
|
|
089cdaa75f | ||
|
|
476026e393 | ||
|
|
75952308f9 | ||
|
|
df0550d6dc | ||
|
|
e481b63b21 | ||
|
|
e47079f0f4 | ||
|
|
9b2a279948 | ||
|
|
db87fd3239 | ||
|
|
598fda0c97 | ||
|
|
b0e335e7b0 | ||
|
|
efdf475da4 | ||
|
|
22a1315136 | ||
|
|
5b22823018 |
@@ -1,4 +1,3 @@
|
||||
comment: false
|
||||
ignore:
|
||||
- "doc"
|
||||
- "example"
|
||||
- "tools"
|
||||
- "tools"
|
||||
40
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
40
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**To Reproduce**
|
||||
Steps to reproduce the behavior, if applicable:
|
||||
|
||||
1. The code is
|
||||
|
||||
```go
|
||||
|
||||
```
|
||||
|
||||
2. The error is
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
**Expected behavior**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Screenshots**
|
||||
If applicable, add screenshots to help explain your problem.
|
||||
|
||||
**Environments (please complete the following information):**
|
||||
- OS: [e.g. Linux]
|
||||
- go-zero version [e.g. 1.2.1]
|
||||
- goctl version [e.g. 1.2.1, optional]
|
||||
|
||||
**More description**
|
||||
Add any other context about the problem here.
|
||||
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
10
.github/ISSUE_TEMPLATE/question.md
vendored
Normal file
10
.github/ISSUE_TEMPLATE/question.md
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
---
|
||||
name: Question
|
||||
about: Ask a question on using go-zero or goctl
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
|
||||
12
.github/workflows/go.yml
vendored
12
.github/workflows/go.yml
vendored
@@ -15,7 +15,7 @@ jobs:
|
||||
- name: Set up Go 1.x
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: ^1.13
|
||||
go-version: ^1.14
|
||||
id: go
|
||||
|
||||
- name: Check out code into the Go module directory
|
||||
@@ -25,10 +25,14 @@ jobs:
|
||||
run: |
|
||||
go get -v -t -d ./...
|
||||
|
||||
- name: Lint
|
||||
run: |
|
||||
go vet -stdmethods=false $(go list ./...)
|
||||
go install mvdan.cc/gofumpt@latest
|
||||
test -z "$(gofumpt -s -l -extra .)" || echo "Please run 'gofumpt -l -w -extra .'"
|
||||
|
||||
- name: Test
|
||||
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
|
||||
|
||||
- name: Codecov
|
||||
uses: codecov/codecov-action@v1.0.6
|
||||
with:
|
||||
token: ${{secrets.CODECOV_TOKEN}}
|
||||
uses: codecov/codecov-action@v2
|
||||
|
||||
19
.github/workflows/issues.yml
vendored
Normal file
19
.github/workflows/issues.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
name: Close inactive issues
|
||||
on:
|
||||
schedule:
|
||||
- cron: "30 1 * * *"
|
||||
|
||||
jobs:
|
||||
close-issues:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/stale@v3
|
||||
with:
|
||||
days-before-issue-stale: 30
|
||||
days-before-issue-close: 14
|
||||
stale-issue-label: "stale"
|
||||
stale-issue-message: "This issue is stale because it has been open for 30 days with no activity."
|
||||
close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale."
|
||||
days-before-pr-stale: -1
|
||||
days-before-pr-close: -1
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
19
.github/workflows/reviewdog.yml
vendored
Normal file
19
.github/workflows/reviewdog.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
name: reviewdog
|
||||
on: [pull_request]
|
||||
jobs:
|
||||
staticcheck:
|
||||
name: runner / staticcheck
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: reviewdog/action-staticcheck@v1
|
||||
with:
|
||||
github_token: ${{ secrets.github_token }}
|
||||
# Change reviewdog reporter if you need [github-pr-check,github-check,github-pr-review].
|
||||
reporter: github-pr-review
|
||||
# Report all results.
|
||||
filter_mode: nofilter
|
||||
# Exit with 1 when it find at least one finding.
|
||||
fail_on_error: true
|
||||
# Set staticcheck flags
|
||||
staticcheck_flags: -checks=inherit,-SA1019,-SA1029,-SA5008
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -10,6 +10,7 @@
|
||||
!*/
|
||||
!api
|
||||
|
||||
# ignore
|
||||
.idea
|
||||
**/.DS_Store
|
||||
**/logs
|
||||
|
||||
@@ -90,7 +90,7 @@ After that, run these local verifications before submitting pull request to pred
|
||||
fail of continuous integration.
|
||||
|
||||
* Format the code with `gofmt`
|
||||
* Run the test with data race enabled `go test -race ./…`
|
||||
* Run the test with data race enabled `go test -race ./...`
|
||||
|
||||
## Code Review
|
||||
|
||||
|
||||
21
ROADMAP.md
21
ROADMAP.md
@@ -5,17 +5,18 @@ Community and contributor involvement is vital for successfully implementing all
|
||||
We hope that the items listed below will inspire further engagement from the community to keep go-zero progressing and shipping exciting and valuable features.
|
||||
|
||||
## 2021 Q2
|
||||
- Support TLS in redis connections
|
||||
- Support service discovery through K8S watch api
|
||||
- Log full sql statements for easier sql problem solving
|
||||
- [x] Support service discovery through K8S client api
|
||||
- [x] Log full sql statements for easier sql problem solving
|
||||
|
||||
## 2021 Q3
|
||||
- Support `goctl mock` command to start a mocking server with given `.api` file
|
||||
- Adapt builtin tracing mechanism to opentracing solutions
|
||||
- Support `goctl model pg` to support PostgreSQL code generation
|
||||
- [x] Support `goctl model pg` to support PostgreSQL code generation
|
||||
- [ ] Support `goctl mock` command to start a mocking server with given `.api` file
|
||||
- [ ] Adapt builtin tracing mechanism to opentracing solutions
|
||||
|
||||
## 2021 Q4
|
||||
- Support `goctl doctor` command to report potential issues for given service
|
||||
- Support `context` in redis related methods for timeout and tracing
|
||||
- Support `context` in sql related methods for timeout and tracing
|
||||
- Support `context` in mongodb related methods for timeout and tracing
|
||||
- [ ] Add `httpx.Client` with governance, like circuit breaker etc.
|
||||
- [ ] Support `goctl doctor` command to report potential issues for given service
|
||||
- [ ] Support `context` in redis related methods for timeout and tracing
|
||||
- [ ] Support `context` in sql related methods for timeout and tracing
|
||||
- [ ] Support `context` in mongodb related methods for timeout and tracing
|
||||
- [ ] Support TLS in redis connections
|
||||
|
||||
@@ -34,7 +34,7 @@ type (
|
||||
expire time.Duration
|
||||
timingWheel *TimingWheel
|
||||
lruCache lru
|
||||
barrier syncx.SharedCalls
|
||||
barrier syncx.SingleFlight
|
||||
unstableExpiry mathx.Unstable
|
||||
stats *cacheStat
|
||||
}
|
||||
@@ -46,7 +46,7 @@ func NewCache(expire time.Duration, opts ...CacheOption) (*Cache, error) {
|
||||
data: make(map[string]interface{}),
|
||||
expire: expire,
|
||||
lruCache: emptyLruCache,
|
||||
barrier: syncx.NewSharedCalls(),
|
||||
barrier: syncx.NewSingleFlight(),
|
||||
unstableExpiry: mathx.NewUnstable(expiryDeviation),
|
||||
}
|
||||
|
||||
|
||||
@@ -106,9 +106,7 @@ func (s *Set) KeysInt() []int {
|
||||
var keys []int
|
||||
|
||||
for key := range s.data {
|
||||
if intKey, ok := key.(int); !ok {
|
||||
continue
|
||||
} else {
|
||||
if intKey, ok := key.(int); ok {
|
||||
keys = append(keys, intKey)
|
||||
}
|
||||
}
|
||||
@@ -121,9 +119,7 @@ func (s *Set) KeysInt64() []int64 {
|
||||
var keys []int64
|
||||
|
||||
for key := range s.data {
|
||||
if intKey, ok := key.(int64); !ok {
|
||||
continue
|
||||
} else {
|
||||
if intKey, ok := key.(int64); ok {
|
||||
keys = append(keys, intKey)
|
||||
}
|
||||
}
|
||||
@@ -136,9 +132,7 @@ func (s *Set) KeysUint() []uint {
|
||||
var keys []uint
|
||||
|
||||
for key := range s.data {
|
||||
if intKey, ok := key.(uint); !ok {
|
||||
continue
|
||||
} else {
|
||||
if intKey, ok := key.(uint); ok {
|
||||
keys = append(keys, intKey)
|
||||
}
|
||||
}
|
||||
@@ -151,9 +145,7 @@ func (s *Set) KeysUint64() []uint64 {
|
||||
var keys []uint64
|
||||
|
||||
for key := range s.data {
|
||||
if intKey, ok := key.(uint64); !ok {
|
||||
continue
|
||||
} else {
|
||||
if intKey, ok := key.(uint64); ok {
|
||||
keys = append(keys, intKey)
|
||||
}
|
||||
}
|
||||
@@ -166,9 +158,7 @@ func (s *Set) KeysStr() []string {
|
||||
var keys []string
|
||||
|
||||
for key := range s.data {
|
||||
if strKey, ok := key.(string); !ok {
|
||||
continue
|
||||
} else {
|
||||
if strKey, ok := key.(string); ok {
|
||||
keys = append(keys, strKey)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,9 +47,11 @@ func TestUnmarshalContextWithMissing(t *testing.T) {
|
||||
Name string `ctx:"name"`
|
||||
Age int `ctx:"age"`
|
||||
}
|
||||
type name string
|
||||
const PersonNameKey name = "name"
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "name", "kevin")
|
||||
ctx = context.WithValue(ctx, PersonNameKey, "kevin")
|
||||
|
||||
var person Person
|
||||
err := For(ctx, &person)
|
||||
|
||||
@@ -9,7 +9,9 @@ import (
|
||||
)
|
||||
|
||||
func TestContextCancel(t *testing.T) {
|
||||
c := context.WithValue(context.Background(), "key", "value")
|
||||
type key string
|
||||
var nameKey key = "name"
|
||||
c := context.WithValue(context.Background(), nameKey, "value")
|
||||
c1, cancel := context.WithCancel(c)
|
||||
o := ValueOnlyFrom(c1)
|
||||
c2, cancel2 := context.WithCancel(o)
|
||||
|
||||
@@ -6,9 +6,10 @@ package internal
|
||||
|
||||
import (
|
||||
context "context"
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
connectivity "google.golang.org/grpc/connectivity"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MocketcdConn is a mock of etcdConn interface
|
||||
|
||||
@@ -5,8 +5,9 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
// MockUpdateListener is a mock of UpdateListener interface
|
||||
|
||||
@@ -9,7 +9,9 @@ type AtomicError struct {
|
||||
|
||||
// Set sets the error.
|
||||
func (ae *AtomicError) Set(err error) {
|
||||
ae.err.Store(err)
|
||||
if err != nil {
|
||||
ae.err.Store(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Load returns the error.
|
||||
|
||||
@@ -17,6 +17,15 @@ func TestAtomicError(t *testing.T) {
|
||||
assert.Equal(t, errDummy, err.Load())
|
||||
}
|
||||
|
||||
func TestAtomicErrorSetNil(t *testing.T) {
|
||||
var (
|
||||
errNil error
|
||||
err AtomicError
|
||||
)
|
||||
err.Set(errNil)
|
||||
assert.Equal(t, errNil, err.Load())
|
||||
}
|
||||
|
||||
func TestAtomicErrorNil(t *testing.T) {
|
||||
var err AtomicError
|
||||
assert.Nil(t, err.Load())
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package fs
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build linux || darwin
|
||||
// +build linux darwin
|
||||
|
||||
package fs
|
||||
|
||||
@@ -395,16 +395,16 @@ func assetEqual(t *testing.T, except, data interface{}) {
|
||||
|
||||
func TestStream_AnyMach(t *testing.T) {
|
||||
assetEqual(t, false, Just(1, 2, 3).AnyMach(func(item interface{}) bool {
|
||||
return 4 == item.(int)
|
||||
return item.(int) == 4
|
||||
}))
|
||||
assetEqual(t, false, Just(1, 2, 3).AnyMach(func(item interface{}) bool {
|
||||
return 0 == item.(int)
|
||||
return item.(int) == 0
|
||||
}))
|
||||
assetEqual(t, true, Just(1, 2, 3).AnyMach(func(item interface{}) bool {
|
||||
return 2 == item.(int)
|
||||
return item.(int) == 2
|
||||
}))
|
||||
assetEqual(t, true, Just(1, 2, 3).AnyMach(func(item interface{}) bool {
|
||||
return 2 == item.(int)
|
||||
return item.(int) == 2
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@ package fx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -30,7 +33,8 @@ func DoWithTimeout(fn func() error, timeout time.Duration, opts ...DoOption) err
|
||||
go func() {
|
||||
defer func() {
|
||||
if p := recover(); p != nil {
|
||||
panicChan <- p
|
||||
// attach call stack to avoid missing in different goroutine
|
||||
panicChan <- fmt.Sprintf("%+v\n\n%s", p, strings.TrimSpace(string(debug.Stack())))
|
||||
}
|
||||
}()
|
||||
done <- fn()
|
||||
|
||||
@@ -20,6 +20,11 @@ func TestMd5(t *testing.T) {
|
||||
assert.Equal(t, md5Digest, actual)
|
||||
}
|
||||
|
||||
func TestMd5Hex(t *testing.T) {
|
||||
actual := Md5Hex([]byte(text))
|
||||
assert.Equal(t, md5Digest, actual)
|
||||
}
|
||||
|
||||
func BenchmarkHashFnv(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
h := fnv.New32()
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestTokenLimit_Rescue(t *testing.T) {
|
||||
rate = 5
|
||||
burst = 10
|
||||
)
|
||||
l := NewTokenLimiter(rate, burst, redis.NewRedis(s.Addr(), redis.NodeType), "tokenlimit")
|
||||
l := NewTokenLimiter(rate, burst, redis.New(s.Addr()), "tokenlimit")
|
||||
s.Close()
|
||||
|
||||
var allowed int
|
||||
|
||||
@@ -31,6 +31,8 @@ var (
|
||||
|
||||
// default to be enabled
|
||||
enabled = syncx.ForAtomicBool(true)
|
||||
// default to be enabled
|
||||
logEnabled = syncx.ForAtomicBool(true)
|
||||
// make it a variable for unit test
|
||||
systemOverloadChecker = func(cpuThreshold int64) bool {
|
||||
return stat.CpuUsage() >= cpuThreshold
|
||||
@@ -80,6 +82,11 @@ func Disable() {
|
||||
enabled.Set(false)
|
||||
}
|
||||
|
||||
// DisableLog disables the stat logs for load shedding.
|
||||
func DisableLog() {
|
||||
logEnabled.Set(false)
|
||||
}
|
||||
|
||||
// NewAdaptiveShedder returns an adaptive shedder.
|
||||
// opts can be used to customize the Shedder.
|
||||
func NewAdaptiveShedder(opts ...ShedderOption) Shedder {
|
||||
|
||||
@@ -25,6 +25,7 @@ func init() {
|
||||
}
|
||||
|
||||
func TestAdaptiveShedder(t *testing.T) {
|
||||
DisableLog()
|
||||
shedder := NewAdaptiveShedder(WithWindow(bucketDuration), WithBuckets(buckets), WithCpuThreshold(100))
|
||||
var wg sync.WaitGroup
|
||||
var drop int64
|
||||
|
||||
@@ -48,6 +48,25 @@ func (s *SheddingStat) IncrementDrop() {
|
||||
atomic.AddInt64(&s.drop, 1)
|
||||
}
|
||||
|
||||
func (s *SheddingStat) loop(c <-chan time.Time) {
|
||||
for range c {
|
||||
st := s.reset()
|
||||
|
||||
if !logEnabled.True() {
|
||||
continue
|
||||
}
|
||||
|
||||
c := stat.CpuUsage()
|
||||
if st.Drop == 0 {
|
||||
logx.Statf("(%s) shedding_stat [1m], cpu: %d, total: %d, pass: %d, drop: %d",
|
||||
s.name, c, st.Total, st.Pass, st.Drop)
|
||||
} else {
|
||||
logx.Statf("(%s) shedding_stat_drop [1m], cpu: %d, total: %d, pass: %d, drop: %d",
|
||||
s.name, c, st.Total, st.Pass, st.Drop)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SheddingStat) reset() snapshot {
|
||||
return snapshot{
|
||||
Total: atomic.SwapInt64(&s.total, 0),
|
||||
@@ -59,15 +78,6 @@ func (s *SheddingStat) reset() snapshot {
|
||||
func (s *SheddingStat) run() {
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
c := stat.CpuUsage()
|
||||
st := s.reset()
|
||||
if st.Drop == 0 {
|
||||
logx.Statf("(%s) shedding_stat [1m], cpu: %d, total: %d, pass: %d, drop: %d",
|
||||
s.name, c, st.Total, st.Pass, st.Drop)
|
||||
} else {
|
||||
logx.Statf("(%s) shedding_stat_drop [1m], cpu: %d, total: %d, pass: %d, drop: %d",
|
||||
s.name, c, st.Total, st.Pass, st.Drop)
|
||||
}
|
||||
}
|
||||
|
||||
s.loop(ticker.C)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package load
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -22,3 +23,32 @@ func TestSheddingStat(t *testing.T) {
|
||||
assert.Equal(t, int64(5), result.Pass)
|
||||
assert.Equal(t, int64(7), result.Drop)
|
||||
}
|
||||
|
||||
func TestLoopTrue(t *testing.T) {
|
||||
ch := make(chan time.Time, 1)
|
||||
ch <- time.Now()
|
||||
close(ch)
|
||||
st := new(SheddingStat)
|
||||
logEnabled.Set(true)
|
||||
st.loop(ch)
|
||||
}
|
||||
|
||||
func TestLoopTrueAndDrop(t *testing.T) {
|
||||
ch := make(chan time.Time, 1)
|
||||
ch <- time.Now()
|
||||
close(ch)
|
||||
st := new(SheddingStat)
|
||||
st.IncrementDrop()
|
||||
logEnabled.Set(true)
|
||||
st.loop(ch)
|
||||
}
|
||||
|
||||
func TestLoopFalseAndDrop(t *testing.T) {
|
||||
ch := make(chan time.Time, 1)
|
||||
ch <- time.Now()
|
||||
close(ch)
|
||||
st := new(SheddingStat)
|
||||
st.IncrementDrop()
|
||||
logEnabled.Set(false)
|
||||
st.loop(ch)
|
||||
}
|
||||
|
||||
@@ -20,49 +20,67 @@ func WithDuration(d time.Duration) Logger {
|
||||
}
|
||||
|
||||
func (l *durationLogger) Error(v ...interface{}) {
|
||||
if shouldLog(ErrorLevel) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Errorf(format string, v ...interface{}) {
|
||||
if shouldLog(ErrorLevel) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Errorv(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Info(v ...interface{}) {
|
||||
if shouldLog(InfoLevel) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, fmt.Sprint(v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Infof(format string, v ...interface{}) {
|
||||
if shouldLog(InfoLevel) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Infov(v interface{}) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Slow(v ...interface{}) {
|
||||
if shouldLog(ErrorLevel) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, fmt.Sprint(v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Slowf(format string, v ...interface{}) {
|
||||
if shouldLog(ErrorLevel) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Slowv(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) WithDuration(duration time.Duration) Logger {
|
||||
l.Duration = timex.ReprOfDuration(duration)
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *durationLogger) write(writer io.Writer, level, content string) {
|
||||
func (l *durationLogger) write(writer io.Writer, level string, val interface{}) {
|
||||
l.Timestamp = getTimestamp()
|
||||
l.Level = level
|
||||
l.Content = content
|
||||
outputJson(writer, logEntry(*l))
|
||||
l.Content = val
|
||||
outputJson(writer, l)
|
||||
}
|
||||
|
||||
@@ -65,12 +65,14 @@ var (
|
||||
timeFormat = "2006-01-02T15:04:05.000Z07"
|
||||
writeConsole bool
|
||||
logLevel uint32
|
||||
infoLog io.WriteCloser
|
||||
errorLog io.WriteCloser
|
||||
severeLog io.WriteCloser
|
||||
slowLog io.WriteCloser
|
||||
statLog io.WriteCloser
|
||||
stackLog io.Writer
|
||||
// use uint32 for atomic operations
|
||||
disableStat uint32
|
||||
infoLog io.WriteCloser
|
||||
errorLog io.WriteCloser
|
||||
severeLog io.WriteCloser
|
||||
slowLog io.WriteCloser
|
||||
statLog io.WriteCloser
|
||||
stackLog io.Writer
|
||||
|
||||
once sync.Once
|
||||
initialized uint32
|
||||
@@ -79,10 +81,10 @@ var (
|
||||
|
||||
type (
|
||||
logEntry struct {
|
||||
Timestamp string `json:"@timestamp"`
|
||||
Level string `json:"level"`
|
||||
Duration string `json:"duration,omitempty"`
|
||||
Content string `json:"content"`
|
||||
Timestamp string `json:"@timestamp"`
|
||||
Level string `json:"level"`
|
||||
Duration string `json:"duration,omitempty"`
|
||||
Content interface{} `json:"content"`
|
||||
}
|
||||
|
||||
logOptions struct {
|
||||
@@ -98,10 +100,13 @@ type (
|
||||
Logger interface {
|
||||
Error(...interface{})
|
||||
Errorf(string, ...interface{})
|
||||
Errorv(interface{})
|
||||
Info(...interface{})
|
||||
Infof(string, ...interface{})
|
||||
Infov(interface{})
|
||||
Slow(...interface{})
|
||||
Slowf(string, ...interface{})
|
||||
Slowv(interface{})
|
||||
WithDuration(time.Duration) Logger
|
||||
}
|
||||
)
|
||||
@@ -133,7 +138,7 @@ func SetUp(c LogConf) error {
|
||||
|
||||
// Alert alerts v in alert level, and the message is written to error log.
|
||||
func Alert(v string) {
|
||||
output(errorLog, levelAlert, v)
|
||||
outputText(errorLog, levelAlert, v)
|
||||
}
|
||||
|
||||
// Close closes the logging.
|
||||
@@ -195,24 +200,29 @@ func Disable() {
|
||||
})
|
||||
}
|
||||
|
||||
// DisableStat disables the stat logs.
|
||||
func DisableStat() {
|
||||
atomic.StoreUint32(&disableStat, 1)
|
||||
}
|
||||
|
||||
// Error writes v into error log.
|
||||
func Error(v ...interface{}) {
|
||||
ErrorCaller(1, v...)
|
||||
}
|
||||
|
||||
// Errorf writes v with format into error log.
|
||||
func Errorf(format string, v ...interface{}) {
|
||||
ErrorCallerf(1, format, v...)
|
||||
}
|
||||
|
||||
// ErrorCaller writes v with context into error log.
|
||||
func ErrorCaller(callDepth int, v ...interface{}) {
|
||||
errorSync(fmt.Sprint(v...), callDepth+callerInnerDepth)
|
||||
errorTextSync(fmt.Sprint(v...), callDepth+callerInnerDepth)
|
||||
}
|
||||
|
||||
// ErrorCallerf writes v with context in format into error log.
|
||||
func ErrorCallerf(callDepth int, format string, v ...interface{}) {
|
||||
errorSync(fmt.Sprintf(format, v...), callDepth+callerInnerDepth)
|
||||
errorTextSync(fmt.Sprintf(format, v...), callDepth+callerInnerDepth)
|
||||
}
|
||||
|
||||
// Errorf writes v with format into error log.
|
||||
func Errorf(format string, v ...interface{}) {
|
||||
ErrorCallerf(1, format, v...)
|
||||
}
|
||||
|
||||
// ErrorStack writes v along with call stack into error log.
|
||||
@@ -227,14 +237,25 @@ func ErrorStackf(format string, v ...interface{}) {
|
||||
stackSync(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Errorv writes v into error log with json content.
|
||||
// No call stack attached, because not elegant to pack the messages.
|
||||
func Errorv(v interface{}) {
|
||||
errorAnySync(v)
|
||||
}
|
||||
|
||||
// Info writes v into access log.
|
||||
func Info(v ...interface{}) {
|
||||
infoSync(fmt.Sprint(v...))
|
||||
infoTextSync(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Infof writes v with format into access log.
|
||||
func Infof(format string, v ...interface{}) {
|
||||
infoSync(fmt.Sprintf(format, v...))
|
||||
infoTextSync(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Infov writes v into access log with json content.
|
||||
func Infov(v interface{}) {
|
||||
infoAnySync(v)
|
||||
}
|
||||
|
||||
// Must checks if err is nil, otherwise logs the err and exits.
|
||||
@@ -242,7 +263,7 @@ func Must(err error) {
|
||||
if err != nil {
|
||||
msg := formatWithCaller(err.Error(), 3)
|
||||
log.Print(msg)
|
||||
output(severeLog, levelFatal, msg)
|
||||
outputText(severeLog, levelFatal, msg)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -264,12 +285,17 @@ func Severef(format string, v ...interface{}) {
|
||||
|
||||
// Slow writes v into slow log.
|
||||
func Slow(v ...interface{}) {
|
||||
slowSync(fmt.Sprint(v...))
|
||||
slowTextSync(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Slowf writes v with format into slow log.
|
||||
func Slowf(format string, v ...interface{}) {
|
||||
slowSync(fmt.Sprintf(format, v...))
|
||||
slowTextSync(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Slowv writes v into slow log with json content.
|
||||
func Slowv(v interface{}) {
|
||||
slowAnySync(v)
|
||||
}
|
||||
|
||||
// Stat writes v into stat log.
|
||||
@@ -312,8 +338,14 @@ func createOutput(path string) (io.WriteCloser, error) {
|
||||
options.gzipEnabled), options.gzipEnabled)
|
||||
}
|
||||
|
||||
func errorSync(msg string, callDepth int) {
|
||||
if shouldLog(ErrorLevel) {
|
||||
func errorAnySync(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
outputAny(errorLog, levelError, v)
|
||||
}
|
||||
}
|
||||
|
||||
func errorTextSync(msg string, callDepth int) {
|
||||
if shallLog(ErrorLevel) {
|
||||
outputError(errorLog, msg, callDepth)
|
||||
}
|
||||
}
|
||||
@@ -362,13 +394,28 @@ func handleOptions(opts []LogOption) {
|
||||
}
|
||||
}
|
||||
|
||||
func infoSync(msg string) {
|
||||
if shouldLog(InfoLevel) {
|
||||
output(infoLog, levelInfo, msg)
|
||||
func infoAnySync(val interface{}) {
|
||||
if shallLog(InfoLevel) {
|
||||
outputAny(infoLog, levelInfo, val)
|
||||
}
|
||||
}
|
||||
|
||||
func output(writer io.Writer, level, msg string) {
|
||||
func infoTextSync(msg string) {
|
||||
if shallLog(InfoLevel) {
|
||||
outputText(infoLog, levelInfo, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func outputAny(writer io.Writer, level string, val interface{}) {
|
||||
info := logEntry{
|
||||
Timestamp: getTimestamp(),
|
||||
Level: level,
|
||||
Content: val,
|
||||
}
|
||||
outputJson(writer, info)
|
||||
}
|
||||
|
||||
func outputText(writer io.Writer, level, msg string) {
|
||||
info := logEntry{
|
||||
Timestamp: getTimestamp(),
|
||||
Level: level,
|
||||
@@ -379,7 +426,7 @@ func output(writer io.Writer, level, msg string) {
|
||||
|
||||
func outputError(writer io.Writer, msg string, callDepth int) {
|
||||
content := formatWithCaller(msg, callDepth)
|
||||
output(writer, levelError, content)
|
||||
outputText(writer, levelError, content)
|
||||
}
|
||||
|
||||
func outputJson(writer io.Writer, info interface{}) {
|
||||
@@ -481,30 +528,40 @@ func setupWithVolume(c LogConf) error {
|
||||
}
|
||||
|
||||
func severeSync(msg string) {
|
||||
if shouldLog(SevereLevel) {
|
||||
output(severeLog, levelSevere, fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
||||
if shallLog(SevereLevel) {
|
||||
outputText(severeLog, levelSevere, fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
||||
}
|
||||
}
|
||||
|
||||
func shouldLog(level uint32) bool {
|
||||
func shallLog(level uint32) bool {
|
||||
return atomic.LoadUint32(&logLevel) <= level
|
||||
}
|
||||
|
||||
func slowSync(msg string) {
|
||||
if shouldLog(ErrorLevel) {
|
||||
output(slowLog, levelSlow, msg)
|
||||
func shallLogStat() bool {
|
||||
return atomic.LoadUint32(&disableStat) == 0
|
||||
}
|
||||
|
||||
func slowAnySync(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
outputAny(slowLog, levelSlow, v)
|
||||
}
|
||||
}
|
||||
|
||||
func slowTextSync(msg string) {
|
||||
if shallLog(ErrorLevel) {
|
||||
outputText(slowLog, levelSlow, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func stackSync(msg string) {
|
||||
if shouldLog(ErrorLevel) {
|
||||
output(stackLog, levelError, fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
||||
if shallLog(ErrorLevel) {
|
||||
outputText(stackLog, levelError, fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
||||
}
|
||||
}
|
||||
|
||||
func statSync(msg string) {
|
||||
if shouldLog(InfoLevel) {
|
||||
output(statLog, levelStat, msg)
|
||||
if shallLogStat() && shallLog(InfoLevel) {
|
||||
outputText(statLog, levelStat, msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -92,6 +92,30 @@ func TestStructedLogAlert(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogError(t *testing.T) {
|
||||
doTestStructedLog(t, levelError, func(writer io.WriteCloser) {
|
||||
errorLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
Error(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogErrorf(t *testing.T) {
|
||||
doTestStructedLog(t, levelError, func(writer io.WriteCloser) {
|
||||
errorLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
Errorf("%s", fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogErrorv(t *testing.T) {
|
||||
doTestStructedLog(t, levelError, func(writer io.WriteCloser) {
|
||||
errorLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
Errorv(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogInfo(t *testing.T) {
|
||||
doTestStructedLog(t, levelInfo, func(writer io.WriteCloser) {
|
||||
infoLog = writer
|
||||
@@ -100,6 +124,22 @@ func TestStructedLogInfo(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogInfof(t *testing.T) {
|
||||
doTestStructedLog(t, levelInfo, func(writer io.WriteCloser) {
|
||||
infoLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
Infof("%s", fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogInfov(t *testing.T) {
|
||||
doTestStructedLog(t, levelInfo, func(writer io.WriteCloser) {
|
||||
infoLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
Infov(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogSlow(t *testing.T) {
|
||||
doTestStructedLog(t, levelSlow, func(writer io.WriteCloser) {
|
||||
slowLog = writer
|
||||
@@ -116,6 +156,14 @@ func TestStructedLogSlowf(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogSlowv(t *testing.T) {
|
||||
doTestStructedLog(t, levelSlow, func(writer io.WriteCloser) {
|
||||
slowLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
Slowv(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogStat(t *testing.T) {
|
||||
doTestStructedLog(t, levelStat, func(writer io.WriteCloser) {
|
||||
statLog = writer
|
||||
@@ -246,6 +294,17 @@ func TestDisable(t *testing.T) {
|
||||
assert.Nil(t, Close())
|
||||
}
|
||||
|
||||
func TestDisableStat(t *testing.T) {
|
||||
DisableStat()
|
||||
|
||||
const message = "hello there"
|
||||
writer := new(mockWriter)
|
||||
statLog = writer
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
Stat(message)
|
||||
assert.Equal(t, 0, writer.builder.Len())
|
||||
}
|
||||
|
||||
func TestWithGzip(t *testing.T) {
|
||||
fn := WithGzip()
|
||||
var opt logOptions
|
||||
@@ -357,7 +416,9 @@ func doTestStructedLog(t *testing.T, level string, setup func(writer io.WriteClo
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, level, entry.Level)
|
||||
assert.True(t, strings.Contains(entry.Content, message))
|
||||
val, ok := entry.Content.(string)
|
||||
assert.True(t, ok)
|
||||
assert.True(t, strings.Contains(val, message))
|
||||
}
|
||||
|
||||
func testSetLevelTwiceWithMode(t *testing.T, mode string) {
|
||||
|
||||
@@ -47,7 +47,6 @@ type (
|
||||
done chan lang.PlaceholderType
|
||||
rule RotateRule
|
||||
compress bool
|
||||
keepDays int
|
||||
// can't use threading.RoutineGroup because of cycle import
|
||||
waitGroup sync.WaitGroup
|
||||
closeOnce sync.Once
|
||||
|
||||
@@ -3,6 +3,7 @@ package logx
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -97,7 +98,13 @@ func TestRotateLoggerRotate(t *testing.T) {
|
||||
}()
|
||||
}
|
||||
err = logger.rotate()
|
||||
assert.Nil(t, err)
|
||||
switch v := err.(type) {
|
||||
case *os.LinkError:
|
||||
// avoid rename error on docker container
|
||||
assert.Equal(t, syscall.EXDEV, v.Err)
|
||||
default:
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRotateLoggerWrite(t *testing.T) {
|
||||
|
||||
@@ -44,5 +44,5 @@ func captureOutput(f func()) string {
|
||||
func getContent(jsonStr string) string {
|
||||
var entry logEntry
|
||||
json.Unmarshal([]byte(jsonStr), &entry)
|
||||
return entry.Content
|
||||
return entry.Content.(string)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/timex"
|
||||
"github.com/tal-tech/go-zero/core/trace/tracespec"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type traceLogger struct {
|
||||
@@ -18,50 +18,68 @@ type traceLogger struct {
|
||||
}
|
||||
|
||||
func (l *traceLogger) Error(v ...interface{}) {
|
||||
if shouldLog(ErrorLevel) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Errorf(format string, v ...interface{}) {
|
||||
if shouldLog(ErrorLevel) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Errorv(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Info(v ...interface{}) {
|
||||
if shouldLog(InfoLevel) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, fmt.Sprint(v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Infof(format string, v ...interface{}) {
|
||||
if shouldLog(InfoLevel) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Infov(v interface{}) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Slow(v ...interface{}) {
|
||||
if shouldLog(ErrorLevel) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, fmt.Sprint(v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Slowf(format string, v ...interface{}) {
|
||||
if shouldLog(ErrorLevel) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Slowv(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) WithDuration(duration time.Duration) Logger {
|
||||
l.Duration = timex.ReprOfDuration(duration)
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *traceLogger) write(writer io.Writer, level, content string) {
|
||||
func (l *traceLogger) write(writer io.Writer, level string, val interface{}) {
|
||||
l.Timestamp = getTimestamp()
|
||||
l.Level = level
|
||||
l.Content = content
|
||||
l.Content = val
|
||||
l.Trace = traceIdFromContext(l.ctx)
|
||||
l.Span = spanIdFromContext(l.ctx)
|
||||
outputJson(writer, l)
|
||||
@@ -75,19 +93,19 @@ func WithContext(ctx context.Context) Logger {
|
||||
}
|
||||
|
||||
func spanIdFromContext(ctx context.Context) string {
|
||||
t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
|
||||
if !ok {
|
||||
return ""
|
||||
spanCtx := trace.SpanContextFromContext(ctx)
|
||||
if spanCtx.HasSpanID() {
|
||||
return spanCtx.SpanID().String()
|
||||
}
|
||||
|
||||
return t.SpanId()
|
||||
return ""
|
||||
}
|
||||
|
||||
func traceIdFromContext(ctx context.Context) string {
|
||||
t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
|
||||
if !ok {
|
||||
return ""
|
||||
spanCtx := trace.SpanContextFromContext(ctx)
|
||||
if spanCtx.HasTraceID() {
|
||||
return spanCtx.TraceID().String()
|
||||
}
|
||||
|
||||
return t.TraceId()
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -9,71 +9,90 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tal-tech/go-zero/core/trace/tracespec"
|
||||
"go.opentelemetry.io/otel"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
const (
|
||||
mockTraceID = "mock-trace-id"
|
||||
mockSpanID = "mock-span-id"
|
||||
traceKey = "trace"
|
||||
spanKey = "span"
|
||||
)
|
||||
|
||||
var mock tracespec.Trace = new(mockTrace)
|
||||
|
||||
func TestTraceLog(t *testing.T) {
|
||||
var buf mockWriter
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||
WithContext(ctx).(*traceLogger).write(&buf, levelInfo, testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), mockTraceID))
|
||||
assert.True(t, strings.Contains(buf.String(), mockSpanID))
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
}
|
||||
|
||||
func TestTraceError(t *testing.T) {
|
||||
var buf mockWriter
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
errorLog = newLogWriter(log.New(&buf, "", flags))
|
||||
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||
l := WithContext(ctx).(*traceLogger)
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Error(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), mockTraceID))
|
||||
assert.True(t, strings.Contains(buf.String(), mockSpanID))
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Errorf(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), mockTraceID))
|
||||
assert.True(t, strings.Contains(buf.String(), mockSpanID))
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
}
|
||||
|
||||
func TestTraceInfo(t *testing.T) {
|
||||
var buf mockWriter
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
infoLog = newLogWriter(log.New(&buf, "", flags))
|
||||
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||
l := WithContext(ctx).(*traceLogger)
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Info(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), mockTraceID))
|
||||
assert.True(t, strings.Contains(buf.String(), mockSpanID))
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Infof(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), mockTraceID))
|
||||
assert.True(t, strings.Contains(buf.String(), mockSpanID))
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
}
|
||||
|
||||
func TestTraceSlow(t *testing.T) {
|
||||
var buf mockWriter
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
slowLog = newLogWriter(log.New(&buf, "", flags))
|
||||
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||
l := WithContext(ctx).(*traceLogger)
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Slow(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), mockTraceID))
|
||||
assert.True(t, strings.Contains(buf.String(), mockSpanID))
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Slowf(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), mockTraceID))
|
||||
assert.True(t, strings.Contains(buf.String(), mockSpanID))
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
}
|
||||
|
||||
func TestTraceWithoutContext(t *testing.T) {
|
||||
@@ -83,34 +102,10 @@ func TestTraceWithoutContext(t *testing.T) {
|
||||
l := WithContext(context.Background()).(*traceLogger)
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Info(testlog)
|
||||
assert.False(t, strings.Contains(buf.String(), mockTraceID))
|
||||
assert.False(t, strings.Contains(buf.String(), mockSpanID))
|
||||
assert.False(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.False(t, strings.Contains(buf.String(), spanKey))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Infof(testlog)
|
||||
assert.False(t, strings.Contains(buf.String(), mockTraceID))
|
||||
assert.False(t, strings.Contains(buf.String(), mockSpanID))
|
||||
}
|
||||
|
||||
type mockTrace struct{}
|
||||
|
||||
func (t mockTrace) TraceId() string {
|
||||
return mockTraceID
|
||||
}
|
||||
|
||||
func (t mockTrace) SpanId() string {
|
||||
return mockSpanID
|
||||
}
|
||||
|
||||
func (t mockTrace) Finish() {
|
||||
}
|
||||
|
||||
func (t mockTrace) Fork(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (t mockTrace) Follow(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (t mockTrace) Visit(fn func(key, val string) bool) {
|
||||
assert.False(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.False(t, strings.Contains(buf.String(), spanKey))
|
||||
}
|
||||
|
||||
@@ -43,7 +43,8 @@ type (
|
||||
UnmarshalOption func(*unmarshalOptions)
|
||||
|
||||
unmarshalOptions struct {
|
||||
fromString bool
|
||||
fromString bool
|
||||
canonicalKey func(key string) string
|
||||
}
|
||||
|
||||
keyCache map[string][]string
|
||||
@@ -229,7 +230,7 @@ func (u *Unmarshaler) processFieldPrimitive(field reflect.StructField, value ref
|
||||
default:
|
||||
switch v := mapValue.(type) {
|
||||
case json.Number:
|
||||
return u.processFieldPrimitiveWithJsonNumber(field, value, v, opts, fullName)
|
||||
return u.processFieldPrimitiveWithJSONNumber(field, value, v, opts, fullName)
|
||||
default:
|
||||
if typeKind == valueKind {
|
||||
if err := validateValueInOptions(opts.options(), mapValue); err != nil {
|
||||
@@ -244,7 +245,7 @@ func (u *Unmarshaler) processFieldPrimitive(field reflect.StructField, value ref
|
||||
return newTypeMismatchError(fullName)
|
||||
}
|
||||
|
||||
func (u *Unmarshaler) processFieldPrimitiveWithJsonNumber(field reflect.StructField, value reflect.Value,
|
||||
func (u *Unmarshaler) processFieldPrimitiveWithJSONNumber(field reflect.StructField, value reflect.Value,
|
||||
v json.Number, opts *fieldOptionsWithContext, fullName string) error {
|
||||
fieldType := field.Type
|
||||
fieldKind := fieldType.Kind()
|
||||
@@ -323,7 +324,11 @@ func (u *Unmarshaler) processNamedField(field reflect.StructField, value reflect
|
||||
}
|
||||
|
||||
fullName = join(fullName, key)
|
||||
mapValue, hasValue := getValue(m, key)
|
||||
canonicalKey := key
|
||||
if u.opts.canonicalKey != nil {
|
||||
canonicalKey = u.opts.canonicalKey(key)
|
||||
}
|
||||
mapValue, hasValue := getValue(m, canonicalKey)
|
||||
if hasValue {
|
||||
return u.processNamedFieldWithValue(field, value, mapValue, key, opts, fullName)
|
||||
}
|
||||
@@ -513,14 +518,14 @@ func (u *Unmarshaler) fillSliceValue(slice reflect.Value, index int, baseKind re
|
||||
target.Set(reflect.ValueOf(value))
|
||||
ithVal.Set(target.Addr())
|
||||
return nil
|
||||
} else {
|
||||
if ithVal.Kind() != reflect.TypeOf(value).Kind() {
|
||||
return errTypeMismatch
|
||||
}
|
||||
|
||||
ithVal.Set(reflect.ValueOf(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
if ithVal.Kind() != reflect.TypeOf(value).Kind() {
|
||||
return errTypeMismatch
|
||||
}
|
||||
|
||||
ithVal.Set(reflect.ValueOf(value))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -621,6 +626,13 @@ func WithStringValues() UnmarshalOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithCanonicalKeyFunc customizes a Unmarshaler with Canonical Key func
|
||||
func WithCanonicalKeyFunc(f func(string) string) UnmarshalOption {
|
||||
return func(opt *unmarshalOptions) {
|
||||
opt.canonicalKey = f
|
||||
}
|
||||
}
|
||||
|
||||
func fillDurationValue(fieldKind reflect.Kind, value reflect.Value, dur string) error {
|
||||
d, err := time.ParseDuration(dur)
|
||||
if err != nil {
|
||||
|
||||
@@ -112,6 +112,12 @@ func MapReduceWithSource(source <-chan interface{}, mapper MapperFunc, reducer R
|
||||
opts ...Option) (interface{}, error) {
|
||||
options := buildOptions(opts...)
|
||||
output := make(chan interface{})
|
||||
defer func() {
|
||||
for range output {
|
||||
panic("more than one element written in reducer")
|
||||
}
|
||||
}()
|
||||
|
||||
collector := make(chan interface{}, options.workers)
|
||||
done := syncx.NewDoneChan()
|
||||
writer := newGuardedWriter(output, done.Done())
|
||||
|
||||
@@ -202,6 +202,22 @@ func TestMapReduce(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapReduceWithReduerWriteMoreThanOnce(t *testing.T) {
|
||||
assert.Panics(t, func() {
|
||||
MapReduce(func(source chan<- interface{}) {
|
||||
for i := 0; i < 10; i++ {
|
||||
source <- i
|
||||
}
|
||||
}, func(item interface{}, writer Writer, cancel func(error)) {
|
||||
writer.Write(item)
|
||||
}, func(pipe <-chan interface{}, writer Writer, cancel func(error)) {
|
||||
drain(pipe)
|
||||
writer.Write("one")
|
||||
writer.Write("two")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestMapReduceVoid(t *testing.T) {
|
||||
var value uint32
|
||||
tests := []struct {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package proc
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build linux || darwin
|
||||
// +build linux darwin
|
||||
|
||||
package proc
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package proc
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build linux || darwin
|
||||
// +build linux darwin
|
||||
|
||||
package proc
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package proc
|
||||
@@ -14,5 +15,5 @@ func AddWrapUpListener(fn func()) func() {
|
||||
return fn
|
||||
}
|
||||
|
||||
func SetTimeoutToForceQuit(duration time.Duration) {
|
||||
func SetTimeToForceQuit(duration time.Duration) {
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build linux || darwin
|
||||
// +build linux darwin
|
||||
|
||||
package proc
|
||||
|
||||
10
core/proc/signals+polyfill.go
Normal file
10
core/proc/signals+polyfill.go
Normal file
@@ -0,0 +1,10 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package proc
|
||||
|
||||
import "context"
|
||||
|
||||
func Done() <-chan struct{} {
|
||||
return context.Background().Done()
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build linux || darwin
|
||||
// +build linux darwin
|
||||
|
||||
package proc
|
||||
@@ -12,6 +13,8 @@ import (
|
||||
|
||||
const timeFormat = "0102150405"
|
||||
|
||||
var done = make(chan struct{})
|
||||
|
||||
func init() {
|
||||
go func() {
|
||||
var profiler Stopper
|
||||
@@ -33,6 +36,13 @@ func init() {
|
||||
profiler = nil
|
||||
}
|
||||
case syscall.SIGTERM:
|
||||
select {
|
||||
case <-done:
|
||||
// already closed
|
||||
default:
|
||||
close(done)
|
||||
}
|
||||
|
||||
gracefulStop(signals)
|
||||
default:
|
||||
logx.Error("Got unregistered signal:", v)
|
||||
@@ -40,3 +50,8 @@ func init() {
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Done returns the channel that notifies the process quitting.
|
||||
func Done() <-chan struct{} {
|
||||
return done
|
||||
}
|
||||
|
||||
16
core/proc/signals_test.go
Normal file
16
core/proc/signals_test.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package proc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDone(t *testing.T) {
|
||||
select {
|
||||
case <-Done():
|
||||
assert.Fail(t, "should run")
|
||||
default:
|
||||
}
|
||||
assert.NotNil(t, Done())
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build debug
|
||||
// +build debug
|
||||
|
||||
package search
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"github.com/tal-tech/go-zero/core/prometheus"
|
||||
"github.com/tal-tech/go-zero/core/stat"
|
||||
"github.com/tal-tech/go-zero/core/trace"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -29,6 +30,7 @@ type ServiceConf struct {
|
||||
Mode string `json:",default=pro,options=dev|test|rt|pre|pro"`
|
||||
MetricsUrl string `json:",optional"`
|
||||
Prometheus prometheus.Config `json:",optional"`
|
||||
Telemetry trace.Config `json:",optional"`
|
||||
}
|
||||
|
||||
// MustSetUp sets up the service, exits on error.
|
||||
@@ -49,6 +51,12 @@ func (sc ServiceConf) SetUp() error {
|
||||
|
||||
sc.initMode()
|
||||
prometheus.StartAgent(sc.Prometheus)
|
||||
|
||||
if len(sc.Telemetry.Name) == 0 {
|
||||
sc.Telemetry.Name = sc.Name
|
||||
}
|
||||
trace.StartAgent(sc.Telemetry)
|
||||
|
||||
if len(sc.MetricsUrl) > 0 {
|
||||
stat.SetReportWriter(stat.NewRemoteWriter(sc.MetricsUrl))
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build !linux
|
||||
// +build !linux
|
||||
|
||||
package stat
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package stat
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package stat
|
||||
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
)
|
||||
|
||||
func TestRefreshCpu(t *testing.T) {
|
||||
assert.True(t, RefreshCpu() >= 0)
|
||||
assert.NotPanics(t, func() {
|
||||
RefreshCpu()
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkRefreshCpu(b *testing.B) {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build !linux
|
||||
// +build !linux
|
||||
|
||||
package internal
|
||||
|
||||
@@ -38,7 +38,9 @@ func init() {
|
||||
atomic.StoreInt64(&cpuUsage, usage)
|
||||
})
|
||||
case <-allTicker.C:
|
||||
printUsage()
|
||||
if logEnabled.True() {
|
||||
printUsage()
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
2
core/stores/cache/cache.go
vendored
2
core/stores/cache/cache.go
vendored
@@ -29,7 +29,7 @@ type (
|
||||
)
|
||||
|
||||
// New returns a Cache.
|
||||
func New(c ClusterConf, barrier syncx.SharedCalls, st *Stat, errNotFound error,
|
||||
func New(c ClusterConf, barrier syncx.SingleFlight, st *Stat, errNotFound error,
|
||||
opts ...Option) Cache {
|
||||
if len(c) == 0 || TotalWeights(c) <= 0 {
|
||||
log.Fatal("no cache nodes")
|
||||
|
||||
6
core/stores/cache/cache_test.go
vendored
6
core/stores/cache/cache_test.go
vendored
@@ -23,6 +23,7 @@ type mockedNode struct {
|
||||
|
||||
func (mc *mockedNode) Del(keys ...string) error {
|
||||
var be errorx.BatchError
|
||||
|
||||
for _, key := range keys {
|
||||
if _, ok := mc.vals[key]; !ok {
|
||||
be.Add(mc.errNotFound)
|
||||
@@ -30,6 +31,7 @@ func (mc *mockedNode) Del(keys ...string) error {
|
||||
delete(mc.vals, key)
|
||||
}
|
||||
}
|
||||
|
||||
return be.Err()
|
||||
}
|
||||
|
||||
@@ -102,7 +104,7 @@ func TestCache_SetDel(t *testing.T) {
|
||||
Weight: 100,
|
||||
},
|
||||
}
|
||||
c := New(conf, syncx.NewSharedCalls(), NewStat("mock"), errPlaceholder)
|
||||
c := New(conf, syncx.NewSingleFlight(), NewStat("mock"), errPlaceholder)
|
||||
for i := 0; i < total; i++ {
|
||||
if i%2 == 0 {
|
||||
assert.Nil(t, c.Set(fmt.Sprintf("key/%d", i), i))
|
||||
@@ -140,7 +142,7 @@ func TestCache_OneNode(t *testing.T) {
|
||||
Weight: 100,
|
||||
},
|
||||
}
|
||||
c := New(conf, syncx.NewSharedCalls(), NewStat("mock"), errPlaceholder)
|
||||
c := New(conf, syncx.NewSingleFlight(), NewStat("mock"), errPlaceholder)
|
||||
for i := 0; i < total; i++ {
|
||||
if i%2 == 0 {
|
||||
assert.Nil(t, c.Set(fmt.Sprintf("key/%d", i), i))
|
||||
|
||||
19
core/stores/cache/cachenode.go
vendored
19
core/stores/cache/cachenode.go
vendored
@@ -29,7 +29,7 @@ type cacheNode struct {
|
||||
rds *redis.Redis
|
||||
expiry time.Duration
|
||||
notFoundExpiry time.Duration
|
||||
barrier syncx.SharedCalls
|
||||
barrier syncx.SingleFlight
|
||||
r *rand.Rand
|
||||
lock *sync.Mutex
|
||||
unstableExpiry mathx.Unstable
|
||||
@@ -43,7 +43,7 @@ type cacheNode struct {
|
||||
// st is used to stat the cache.
|
||||
// errNotFound defines the error that returned on cache not found.
|
||||
// opts are the options that customize the cacheNode.
|
||||
func NewNode(rds *redis.Redis, barrier syncx.SharedCalls, st *Stat,
|
||||
func NewNode(rds *redis.Redis, barrier syncx.SingleFlight, st *Stat,
|
||||
errNotFound error, opts ...Option) Cache {
|
||||
o := newOptions(opts...)
|
||||
return cacheNode{
|
||||
@@ -65,9 +65,18 @@ func (c cacheNode) Del(keys ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := c.rds.Del(keys...); err != nil {
|
||||
logx.Errorf("failed to clear cache with keys: %q, error: %v", formatKeys(keys), err)
|
||||
c.asyncRetryDelCache(keys...)
|
||||
if len(keys) > 1 && c.rds.Type == redis.ClusterType {
|
||||
for _, key := range keys {
|
||||
if _, err := c.rds.Del(key); err != nil {
|
||||
logx.Errorf("failed to clear cache with key: %q, error: %v", key, err)
|
||||
c.asyncRetryDelCache(key)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if _, err := c.rds.Del(keys...); err != nil {
|
||||
logx.Errorf("failed to clear cache with keys: %q, error: %v", formatKeys(keys), err)
|
||||
c.asyncRetryDelCache(keys...)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
30
core/stores/cache/cachenode_test.go
vendored
30
core/stores/cache/cachenode_test.go
vendored
@@ -29,6 +29,7 @@ func init() {
|
||||
func TestCacheNode_DelCache(t *testing.T) {
|
||||
store, clean, err := redistest.CreateRedis()
|
||||
assert.Nil(t, err)
|
||||
store.Type = redis.ClusterType
|
||||
defer clean()
|
||||
|
||||
cn := cacheNode{
|
||||
@@ -49,13 +50,30 @@ func TestCacheNode_DelCache(t *testing.T) {
|
||||
assert.Nil(t, cn.Del("first", "second"))
|
||||
}
|
||||
|
||||
func TestCacheNode_DelCacheWithErrors(t *testing.T) {
|
||||
store, clean, err := redistest.CreateRedis()
|
||||
assert.Nil(t, err)
|
||||
store.Type = redis.ClusterType
|
||||
clean()
|
||||
|
||||
cn := cacheNode{
|
||||
rds: store,
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
lock: new(sync.Mutex),
|
||||
unstableExpiry: mathx.NewUnstable(expiryDeviation),
|
||||
stat: NewStat("any"),
|
||||
errNotFound: errTestNotFound,
|
||||
}
|
||||
assert.Nil(t, cn.Del("third", "fourth"))
|
||||
}
|
||||
|
||||
func TestCacheNode_InvalidCache(t *testing.T) {
|
||||
s, err := miniredis.Run()
|
||||
assert.Nil(t, err)
|
||||
defer s.Close()
|
||||
|
||||
cn := cacheNode{
|
||||
rds: redis.NewRedis(s.Addr(), redis.NodeType),
|
||||
rds: redis.New(s.Addr()),
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
lock: new(sync.Mutex),
|
||||
unstableExpiry: mathx.NewUnstable(expiryDeviation),
|
||||
@@ -78,7 +96,7 @@ func TestCacheNode_Take(t *testing.T) {
|
||||
cn := cacheNode{
|
||||
rds: store,
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
barrier: syncx.NewSharedCalls(),
|
||||
barrier: syncx.NewSingleFlight(),
|
||||
lock: new(sync.Mutex),
|
||||
unstableExpiry: mathx.NewUnstable(expiryDeviation),
|
||||
stat: NewStat("any"),
|
||||
@@ -105,7 +123,7 @@ func TestCacheNode_TakeNotFound(t *testing.T) {
|
||||
cn := cacheNode{
|
||||
rds: store,
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
barrier: syncx.NewSharedCalls(),
|
||||
barrier: syncx.NewSingleFlight(),
|
||||
lock: new(sync.Mutex),
|
||||
unstableExpiry: mathx.NewUnstable(expiryDeviation),
|
||||
stat: NewStat("any"),
|
||||
@@ -144,7 +162,7 @@ func TestCacheNode_TakeWithExpire(t *testing.T) {
|
||||
cn := cacheNode{
|
||||
rds: store,
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
barrier: syncx.NewSharedCalls(),
|
||||
barrier: syncx.NewSingleFlight(),
|
||||
lock: new(sync.Mutex),
|
||||
unstableExpiry: mathx.NewUnstable(expiryDeviation),
|
||||
stat: NewStat("any"),
|
||||
@@ -171,7 +189,7 @@ func TestCacheNode_String(t *testing.T) {
|
||||
cn := cacheNode{
|
||||
rds: store,
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
barrier: syncx.NewSharedCalls(),
|
||||
barrier: syncx.NewSingleFlight(),
|
||||
lock: new(sync.Mutex),
|
||||
unstableExpiry: mathx.NewUnstable(expiryDeviation),
|
||||
stat: NewStat("any"),
|
||||
@@ -188,7 +206,7 @@ func TestCacheValueWithBigInt(t *testing.T) {
|
||||
cn := cacheNode{
|
||||
rds: store,
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
barrier: syncx.NewSharedCalls(),
|
||||
barrier: syncx.NewSingleFlight(),
|
||||
lock: new(sync.Mutex),
|
||||
unstableExpiry: mathx.NewUnstable(expiryDeviation),
|
||||
stat: NewStat("any"),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package clickhouse
|
||||
|
||||
import (
|
||||
// imports the driver.
|
||||
// imports the driver, don't remove this comment, golint requires.
|
||||
_ "github.com/ClickHouse/clickhouse-go"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
mgo "github.com/globalsign/mgo"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockMgoCollection is a mock of MgoCollection interface
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
bson "github.com/globalsign/mgo/bson"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockIter is a mock of Iter interface
|
||||
|
||||
@@ -11,8 +11,8 @@ var (
|
||||
// ErrNotFound is an alias of mgo.ErrNotFound.
|
||||
ErrNotFound = mgo.ErrNotFound
|
||||
|
||||
// can't use one SharedCalls per conn, because multiple conns may share the same cache key.
|
||||
sharedCalls = syncx.NewSharedCalls()
|
||||
// can't use one SingleFlight per conn, because multiple conns may share the same cache key.
|
||||
sharedCalls = syncx.NewSingleFlight()
|
||||
stats = cache.NewStat("mongoc")
|
||||
)
|
||||
|
||||
|
||||
@@ -120,7 +120,7 @@ func TestStatCacheFails(t *testing.T) {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
defer log.SetOutput(os.Stdout)
|
||||
|
||||
r := redis.NewRedis("localhost:59999", redis.NodeType)
|
||||
r := redis.New("localhost:59999")
|
||||
cach := cache.NewNode(r, sharedCalls, stats, mgo.ErrNotFound)
|
||||
c := newCollection(dummyConn{}, cach)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
// imports the driver.
|
||||
// imports the driver, don't remove this comment, golint requires.
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
@@ -71,6 +71,8 @@ type (
|
||||
IntCmd = red.IntCmd
|
||||
// FloatCmd is an alias of redis.FloatCmd.
|
||||
FloatCmd = red.FloatCmd
|
||||
// StringCmd is an alias of redis.StringCmd.
|
||||
StringCmd = red.StringCmd
|
||||
)
|
||||
|
||||
// New returns a Redis with given options.
|
||||
@@ -88,6 +90,7 @@ func New(addr string, opts ...Option) *Redis {
|
||||
return r
|
||||
}
|
||||
|
||||
// Deprecated: use New instead, will be removed in v2.
|
||||
// NewRedis returns a Redis.
|
||||
func NewRedis(redisAddr, redisType string, redisPass ...string) *Redis {
|
||||
var opts []Option
|
||||
|
||||
@@ -963,7 +963,7 @@ func TestRedis_Pipelined(t *testing.T) {
|
||||
func TestRedisString(t *testing.T) {
|
||||
runOnRedis(t, func(client *Redis) {
|
||||
client.Ping()
|
||||
_, err := getRedis(NewRedis(client.Addr, ClusterType))
|
||||
_, err := getRedis(New(client.Addr, Cluster()))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, client.Addr, client.String())
|
||||
assert.NotNil(t, New(client.Addr, badType()).Ping())
|
||||
@@ -1075,7 +1075,7 @@ func TestRedisGeo(t *testing.T) {
|
||||
|
||||
func TestRedis_WithPass(t *testing.T) {
|
||||
runOnRedis(t, func(client *Redis) {
|
||||
err := NewRedis(client.Addr, NodeType, "any").Ping()
|
||||
err := New(client.Addr, WithPass("any")).Ping()
|
||||
assert.NotNil(t, err)
|
||||
})
|
||||
}
|
||||
@@ -1095,7 +1095,7 @@ func runOnRedis(t *testing.T, fn func(client *Redis)) {
|
||||
client.Close()
|
||||
}
|
||||
}()
|
||||
fn(NewRedis(s.Addr(), NodeType))
|
||||
fn(New(s.Addr()))
|
||||
}
|
||||
|
||||
func runOnRedisTLS(t *testing.T, fn func(client *Redis)) {
|
||||
|
||||
@@ -10,10 +10,10 @@ import (
|
||||
func TestBlockingNode(t *testing.T) {
|
||||
r, err := miniredis.Run()
|
||||
assert.Nil(t, err)
|
||||
node, err := CreateBlockingNode(NewRedis(r.Addr(), NodeType))
|
||||
node, err := CreateBlockingNode(New(r.Addr()))
|
||||
assert.Nil(t, err)
|
||||
node.Close()
|
||||
node, err = CreateBlockingNode(NewRedis(r.Addr(), ClusterType))
|
||||
node, err = CreateBlockingNode(New(r.Addr(), Cluster()))
|
||||
assert.Nil(t, err)
|
||||
node.Close()
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ func CreateRedis() (r *redis.Redis, clean func(), err error) {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return redis.NewRedis(mr.Addr(), redis.NodeType), func() {
|
||||
return redis.New(mr.Addr()), func() {
|
||||
ch := make(chan lang.PlaceholderType)
|
||||
go func() {
|
||||
mr.Close()
|
||||
|
||||
@@ -17,8 +17,8 @@ var (
|
||||
// ErrNotFound is an alias of sqlx.ErrNotFound.
|
||||
ErrNotFound = sqlx.ErrNotFound
|
||||
|
||||
// can't use one SharedCalls per conn, because multiple conns may share the same cache key.
|
||||
exclusiveCalls = syncx.NewSharedCalls()
|
||||
// can't use one SingleFlight per conn, because multiple conns may share the same cache key.
|
||||
exclusiveCalls = syncx.NewSingleFlight()
|
||||
stats = cache.NewStat("sqlc")
|
||||
)
|
||||
|
||||
|
||||
@@ -286,7 +286,7 @@ func TestStatCacheFails(t *testing.T) {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
defer log.SetOutput(os.Stdout)
|
||||
|
||||
r := redis.NewRedis("localhost:59999", redis.NodeType)
|
||||
r := redis.New("localhost:59999")
|
||||
c := NewNodeConn(dummySqlConn{}, r, cache.WithExpiry(time.Second*10))
|
||||
|
||||
for i := 0; i < 20; i++ {
|
||||
@@ -485,7 +485,7 @@ func TestCachedConnExecDropCache(t *testing.T) {
|
||||
value = "any"
|
||||
)
|
||||
var conn trackedConn
|
||||
c := NewNodeConn(&conn, redis.NewRedis(r.Addr(), redis.NodeType), cache.WithExpiry(time.Second*30))
|
||||
c := NewNodeConn(&conn, redis.New(r.Addr()), cache.WithExpiry(time.Second*30))
|
||||
assert.Nil(t, c.SetCache(key, value))
|
||||
_, err = c.Exec(func(conn sqlx.SqlConn) (result sql.Result, e error) {
|
||||
return conn.Exec("delete from user_table where id='kevin'")
|
||||
@@ -503,7 +503,7 @@ func TestCachedConnExecDropCache(t *testing.T) {
|
||||
func TestCachedConnExecDropCacheFailed(t *testing.T) {
|
||||
const key = "user"
|
||||
var conn trackedConn
|
||||
r := redis.NewRedis("anyredis:8888", redis.NodeType)
|
||||
r := redis.New("anyredis:8888")
|
||||
c := NewNodeConn(&conn, r, cache.WithExpiry(time.Second*10))
|
||||
_, err := c.Exec(func(conn sqlx.SqlConn) (result sql.Result, e error) {
|
||||
return conn.Exec("delete from user_table where id='kevin'")
|
||||
@@ -600,6 +600,10 @@ func (d dummySqlConn) QueryRowsPartial(v interface{}, query string, args ...inte
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d dummySqlConn) RawDB() (*sql.DB, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (d dummySqlConn) Transact(func(session sqlx.Session) error) error {
|
||||
return nil
|
||||
}
|
||||
@@ -621,6 +625,10 @@ func (c *trackedConn) QueryRows(v interface{}, query string, args ...interface{}
|
||||
return c.dummySqlConn.QueryRows(v, query, args...)
|
||||
}
|
||||
|
||||
func (c *trackedConn) RawDB() (*sql.DB, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *trackedConn) Transact(fn func(session sqlx.Session) error) error {
|
||||
c.transactValue = true
|
||||
return c.dummySqlConn.Transact(fn)
|
||||
|
||||
@@ -43,6 +43,10 @@ func (c *mockedConn) QueryRowsPartial(v interface{}, query string, args ...inter
|
||||
panic("should not called")
|
||||
}
|
||||
|
||||
func (c *mockedConn) RawDB() (*sql.DB, error) {
|
||||
panic("should not called")
|
||||
}
|
||||
|
||||
func (c *mockedConn) Transact(func(session Session) error) error {
|
||||
panic("should not called")
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/breaker"
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
)
|
||||
|
||||
// ErrNotFound is an alias of sql.ErrNoRows
|
||||
@@ -23,6 +24,8 @@ type (
|
||||
// SqlConn only stands for raw connections, so Transact method can be called.
|
||||
SqlConn interface {
|
||||
Session
|
||||
// RawDB is for other ORM to operate with, use it with caution.
|
||||
RawDB() (*sql.DB, error)
|
||||
Transact(func(session Session) error) error
|
||||
}
|
||||
|
||||
@@ -43,13 +46,15 @@ type (
|
||||
// Because CORBA doesn't support PREPARE, so we need to combine the
|
||||
// query arguments into one string and do underlying query without arguments
|
||||
commonSqlConn struct {
|
||||
driverName string
|
||||
datasource string
|
||||
beginTx beginnable
|
||||
brk breaker.Breaker
|
||||
accept func(error) bool
|
||||
connProv connProvider
|
||||
onError func(error)
|
||||
beginTx beginnable
|
||||
brk breaker.Breaker
|
||||
accept func(error) bool
|
||||
}
|
||||
|
||||
connProvider func() (*sql.DB, error)
|
||||
|
||||
sessionConn interface {
|
||||
Exec(query string, args ...interface{}) (sql.Result, error)
|
||||
Query(query string, args ...interface{}) (*sql.Rows, error)
|
||||
@@ -69,10 +74,34 @@ type (
|
||||
// NewSqlConn returns a SqlConn with given driver name and datasource.
|
||||
func NewSqlConn(driverName, datasource string, opts ...SqlOption) SqlConn {
|
||||
conn := &commonSqlConn{
|
||||
driverName: driverName,
|
||||
datasource: datasource,
|
||||
beginTx: begin,
|
||||
brk: breaker.NewBreaker(),
|
||||
connProv: func() (*sql.DB, error) {
|
||||
return getSqlConn(driverName, datasource)
|
||||
},
|
||||
onError: func(err error) {
|
||||
logInstanceError(datasource, err)
|
||||
},
|
||||
beginTx: begin,
|
||||
brk: breaker.NewBreaker(),
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(conn)
|
||||
}
|
||||
|
||||
return conn
|
||||
}
|
||||
|
||||
// NewSqlConnFromDB returns a SqlConn with the given sql.DB.
|
||||
// Use it with caution, it's provided for other ORM to interact with.
|
||||
func NewSqlConnFromDB(db *sql.DB, opts ...SqlOption) SqlConn {
|
||||
conn := &commonSqlConn{
|
||||
connProv: func() (*sql.DB, error) {
|
||||
return db, nil
|
||||
},
|
||||
onError: func(err error) {
|
||||
logx.Errorf("Error on getting sql instance: %v", err)
|
||||
},
|
||||
beginTx: begin,
|
||||
brk: breaker.NewBreaker(),
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(conn)
|
||||
@@ -84,9 +113,9 @@ func NewSqlConn(driverName, datasource string, opts ...SqlOption) SqlConn {
|
||||
func (db *commonSqlConn) Exec(q string, args ...interface{}) (result sql.Result, err error) {
|
||||
err = db.brk.DoWithAcceptable(func() error {
|
||||
var conn *sql.DB
|
||||
conn, err = getSqlConn(db.driverName, db.datasource)
|
||||
conn, err = db.connProv()
|
||||
if err != nil {
|
||||
logInstanceError(db.datasource, err)
|
||||
db.onError(err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -100,9 +129,9 @@ func (db *commonSqlConn) Exec(q string, args ...interface{}) (result sql.Result,
|
||||
func (db *commonSqlConn) Prepare(query string) (stmt StmtSession, err error) {
|
||||
err = db.brk.DoWithAcceptable(func() error {
|
||||
var conn *sql.DB
|
||||
conn, err = getSqlConn(db.driverName, db.datasource)
|
||||
conn, err = db.connProv()
|
||||
if err != nil {
|
||||
logInstanceError(db.datasource, err)
|
||||
db.onError(err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -145,6 +174,10 @@ func (db *commonSqlConn) QueryRowsPartial(v interface{}, q string, args ...inter
|
||||
}, q, args...)
|
||||
}
|
||||
|
||||
func (db *commonSqlConn) RawDB() (*sql.DB, error) {
|
||||
return db.connProv()
|
||||
}
|
||||
|
||||
func (db *commonSqlConn) Transact(fn func(Session) error) error {
|
||||
return db.brk.DoWithAcceptable(func() error {
|
||||
return transact(db, db.beginTx, fn)
|
||||
@@ -163,9 +196,9 @@ func (db *commonSqlConn) acceptable(err error) bool {
|
||||
func (db *commonSqlConn) queryRows(scanner func(*sql.Rows) error, q string, args ...interface{}) error {
|
||||
var qerr error
|
||||
return db.brk.DoWithAcceptable(func() error {
|
||||
conn, err := getSqlConn(db.driverName, db.datasource)
|
||||
conn, err := db.connProv()
|
||||
if err != nil {
|
||||
logInstanceError(db.datasource, err)
|
||||
db.onError(err)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -21,12 +21,15 @@ func TestSqlConn(t *testing.T) {
|
||||
mock.ExpectExec("any")
|
||||
mock.ExpectQuery("any").WillReturnRows(sqlmock.NewRows([]string{"foo"}))
|
||||
conn := NewMysql(mockedDatasource)
|
||||
db, err := conn.RawDB()
|
||||
assert.Nil(t, err)
|
||||
rawConn := NewSqlConnFromDB(db, withMysqlAcceptable())
|
||||
badConn := NewMysql("badsql")
|
||||
_, err := conn.Exec("any", "value")
|
||||
_, err = conn.Exec("any", "value")
|
||||
assert.NotNil(t, err)
|
||||
_, err = badConn.Exec("any", "value")
|
||||
assert.NotNil(t, err)
|
||||
_, err = conn.Prepare("any")
|
||||
_, err = rawConn.Prepare("any")
|
||||
assert.NotNil(t, err)
|
||||
_, err = badConn.Prepare("any")
|
||||
assert.NotNil(t, err)
|
||||
|
||||
@@ -30,7 +30,8 @@ func (t txSession) Prepare(q string) (StmtSession, error) {
|
||||
}
|
||||
|
||||
return statement{
|
||||
stmt: stmt,
|
||||
query: q,
|
||||
stmt: stmt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -70,9 +71,9 @@ func begin(db *sql.DB) (trans, error) {
|
||||
}
|
||||
|
||||
func transact(db *commonSqlConn, b beginnable, fn func(Session) error) (err error) {
|
||||
conn, err := getSqlConn(db.driverName, db.datasource)
|
||||
conn, err := db.connProv()
|
||||
if err != nil {
|
||||
logInstanceError(db.datasource, err)
|
||||
db.onError(err)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,24 @@ func Filter(s string, filter func(r rune) bool) string {
|
||||
return string(chars[:n])
|
||||
}
|
||||
|
||||
// FirstN returns first n runes from s.
|
||||
func FirstN(s string, n int, ellipsis ...string) string {
|
||||
var i int
|
||||
|
||||
for j := range s {
|
||||
if i == n {
|
||||
ret := s[:j]
|
||||
for _, each := range ellipsis {
|
||||
ret += each
|
||||
}
|
||||
return ret
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// HasEmpty checks if there are empty strings in args.
|
||||
func HasEmpty(args ...string) bool {
|
||||
for _, arg := range args {
|
||||
|
||||
@@ -92,6 +92,61 @@ func TestFilter(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFirstN(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
n int
|
||||
ellipsis string
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
name: "english string",
|
||||
input: "anything that we use",
|
||||
n: 8,
|
||||
expect: "anything",
|
||||
},
|
||||
{
|
||||
name: "english string with ellipsis",
|
||||
input: "anything that we use",
|
||||
n: 8,
|
||||
ellipsis: "...",
|
||||
expect: "anything...",
|
||||
},
|
||||
{
|
||||
name: "english string more",
|
||||
input: "anything that we use",
|
||||
n: 80,
|
||||
expect: "anything that we use",
|
||||
},
|
||||
{
|
||||
name: "chinese string",
|
||||
input: "我是中国人",
|
||||
n: 2,
|
||||
expect: "我是",
|
||||
},
|
||||
{
|
||||
name: "chinese string with ellipsis",
|
||||
input: "我是中国人",
|
||||
n: 2,
|
||||
ellipsis: "...",
|
||||
expect: "我是...",
|
||||
},
|
||||
{
|
||||
name: "chinese string",
|
||||
input: "我是中国人",
|
||||
n: 10,
|
||||
expect: "我是中国人",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
assert.Equal(t, test.expect, FirstN(test.input, test.n, test.ellipsis))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemove(t *testing.T) {
|
||||
cases := []struct {
|
||||
input []string
|
||||
|
||||
@@ -19,10 +19,8 @@ func TestDoneChanDone(t *testing.T) {
|
||||
|
||||
waitGroup.Add(1)
|
||||
go func() {
|
||||
select {
|
||||
case <-doneChan.Done():
|
||||
waitGroup.Done()
|
||||
}
|
||||
<-doneChan.Done()
|
||||
waitGroup.Done()
|
||||
}()
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
|
||||
@@ -17,10 +17,11 @@ func TestPoolGet(t *testing.T) {
|
||||
ch := make(chan lang.PlaceholderType)
|
||||
|
||||
for i := 0; i < limit; i++ {
|
||||
var fail AtomicBool
|
||||
go func() {
|
||||
v := stack.Get()
|
||||
if v.(int) != 1 {
|
||||
t.Fatal("unmatch value")
|
||||
fail.Set(true)
|
||||
}
|
||||
ch <- lang.Placeholder
|
||||
}()
|
||||
@@ -30,6 +31,10 @@ func TestPoolGet(t *testing.T) {
|
||||
case <-time.After(time.Second):
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
if fail.True() {
|
||||
t.Fatal("unmatch value")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,20 +9,21 @@ import (
|
||||
|
||||
// A ResourceManager is a manager that used to manage resources.
|
||||
type ResourceManager struct {
|
||||
resources map[string]io.Closer
|
||||
sharedCalls SharedCalls
|
||||
lock sync.RWMutex
|
||||
resources map[string]io.Closer
|
||||
singleFlight SingleFlight
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
// NewResourceManager returns a ResourceManager.
|
||||
func NewResourceManager() *ResourceManager {
|
||||
return &ResourceManager{
|
||||
resources: make(map[string]io.Closer),
|
||||
sharedCalls: NewSharedCalls(),
|
||||
resources: make(map[string]io.Closer),
|
||||
singleFlight: NewSingleFlight(),
|
||||
}
|
||||
}
|
||||
|
||||
// Close closes the manager.
|
||||
// Don't use the ResourceManager after Close() called.
|
||||
func (manager *ResourceManager) Close() error {
|
||||
manager.lock.Lock()
|
||||
defer manager.lock.Unlock()
|
||||
@@ -34,12 +35,15 @@ func (manager *ResourceManager) Close() error {
|
||||
}
|
||||
}
|
||||
|
||||
// release resources to avoid using it later
|
||||
manager.resources = nil
|
||||
|
||||
return be.Err()
|
||||
}
|
||||
|
||||
// GetResource returns the resource associated with given key.
|
||||
func (manager *ResourceManager) GetResource(key string, create func() (io.Closer, error)) (io.Closer, error) {
|
||||
val, err := manager.sharedCalls.Do(key, func() (interface{}, error) {
|
||||
val, err := manager.singleFlight.Do(key, func() (interface{}, error) {
|
||||
manager.lock.RLock()
|
||||
resource, ok := manager.resources[key]
|
||||
manager.lock.RUnlock()
|
||||
|
||||
@@ -44,3 +44,31 @@ func TestResourceManager_GetResourceError(t *testing.T) {
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceManager_Close(t *testing.T) {
|
||||
manager := NewResourceManager()
|
||||
for i := 0; i < 10; i++ {
|
||||
_, err := manager.GetResource("key", func() (io.Closer, error) {
|
||||
return nil, errors.New("fail")
|
||||
})
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
if assert.NoError(t, manager.Close()) {
|
||||
assert.Equal(t, 0, len(manager.resources))
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceManager_UseAfterClose(t *testing.T) {
|
||||
manager := NewResourceManager()
|
||||
_, err := manager.GetResource("key", func() (io.Closer, error) {
|
||||
return nil, errors.New("fail")
|
||||
})
|
||||
assert.NotNil(t, err)
|
||||
if assert.NoError(t, manager.Close()) {
|
||||
_, err = manager.GetResource("key", func() (io.Closer, error) {
|
||||
return nil, errors.New("fail")
|
||||
})
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,17 @@ package syncx
|
||||
import "sync"
|
||||
|
||||
type (
|
||||
// SharedCalls lets the concurrent calls with the same key to share the call result.
|
||||
// SharedCalls is an alias of SingleFlight.
|
||||
// Deprecated: use SingleFlight.
|
||||
SharedCalls = SingleFlight
|
||||
|
||||
// SingleFlight lets the concurrent calls with the same key to share the call result.
|
||||
// For example, A called F, before it's done, B called F. Then B would not execute F,
|
||||
// and shared the result returned by F which called by A.
|
||||
// The calls with the same key are dependent, concurrent calls share the returned values.
|
||||
// A ------->calls F with key<------------------->returns val
|
||||
// B --------------------->calls F with key------>returns val
|
||||
SharedCalls interface {
|
||||
SingleFlight interface {
|
||||
Do(key string, fn func() (interface{}, error)) (interface{}, error)
|
||||
DoEx(key string, fn func() (interface{}, error)) (interface{}, bool, error)
|
||||
}
|
||||
@@ -20,20 +24,26 @@ type (
|
||||
err error
|
||||
}
|
||||
|
||||
sharedGroup struct {
|
||||
flightGroup struct {
|
||||
calls map[string]*call
|
||||
lock sync.Mutex
|
||||
}
|
||||
)
|
||||
|
||||
// NewSharedCalls returns a SharedCalls.
|
||||
func NewSharedCalls() SharedCalls {
|
||||
return &sharedGroup{
|
||||
// NewSingleFlight returns a SingleFlight.
|
||||
func NewSingleFlight() SingleFlight {
|
||||
return &flightGroup{
|
||||
calls: make(map[string]*call),
|
||||
}
|
||||
}
|
||||
|
||||
func (g *sharedGroup) Do(key string, fn func() (interface{}, error)) (interface{}, error) {
|
||||
// NewSharedCalls returns a SingleFlight.
|
||||
// Deprecated: use NewSingleFlight.
|
||||
func NewSharedCalls() SingleFlight {
|
||||
return NewSingleFlight()
|
||||
}
|
||||
|
||||
func (g *flightGroup) Do(key string, fn func() (interface{}, error)) (interface{}, error) {
|
||||
c, done := g.createCall(key)
|
||||
if done {
|
||||
return c.val, c.err
|
||||
@@ -43,7 +53,7 @@ func (g *sharedGroup) Do(key string, fn func() (interface{}, error)) (interface{
|
||||
return c.val, c.err
|
||||
}
|
||||
|
||||
func (g *sharedGroup) DoEx(key string, fn func() (interface{}, error)) (val interface{}, fresh bool, err error) {
|
||||
func (g *flightGroup) DoEx(key string, fn func() (interface{}, error)) (val interface{}, fresh bool, err error) {
|
||||
c, done := g.createCall(key)
|
||||
if done {
|
||||
return c.val, false, c.err
|
||||
@@ -53,7 +63,7 @@ func (g *sharedGroup) DoEx(key string, fn func() (interface{}, error)) (val inte
|
||||
return c.val, true, c.err
|
||||
}
|
||||
|
||||
func (g *sharedGroup) createCall(key string) (c *call, done bool) {
|
||||
func (g *flightGroup) createCall(key string) (c *call, done bool) {
|
||||
g.lock.Lock()
|
||||
if c, ok := g.calls[key]; ok {
|
||||
g.lock.Unlock()
|
||||
@@ -69,7 +79,7 @@ func (g *sharedGroup) createCall(key string) (c *call, done bool) {
|
||||
return c, false
|
||||
}
|
||||
|
||||
func (g *sharedGroup) makeCall(c *call, key string, fn func() (interface{}, error)) {
|
||||
func (g *flightGroup) makeCall(c *call, key string, fn func() (interface{}, error)) {
|
||||
defer func() {
|
||||
g.lock.Lock()
|
||||
delete(g.calls, key)
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func TestExclusiveCallDo(t *testing.T) {
|
||||
g := NewSharedCalls()
|
||||
g := NewSingleFlight()
|
||||
v, err := g.Do("key", func() (interface{}, error) {
|
||||
return "bar", nil
|
||||
})
|
||||
@@ -23,7 +23,7 @@ func TestExclusiveCallDo(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExclusiveCallDoErr(t *testing.T) {
|
||||
g := NewSharedCalls()
|
||||
g := NewSingleFlight()
|
||||
someErr := errors.New("some error")
|
||||
v, err := g.Do("key", func() (interface{}, error) {
|
||||
return nil, someErr
|
||||
@@ -37,7 +37,7 @@ func TestExclusiveCallDoErr(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExclusiveCallDoDupSuppress(t *testing.T) {
|
||||
g := NewSharedCalls()
|
||||
g := NewSingleFlight()
|
||||
c := make(chan string)
|
||||
var calls int32
|
||||
fn := func() (interface{}, error) {
|
||||
@@ -69,7 +69,7 @@ func TestExclusiveCallDoDupSuppress(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExclusiveCallDoDiffDupSuppress(t *testing.T) {
|
||||
g := NewSharedCalls()
|
||||
g := NewSingleFlight()
|
||||
broadcast := make(chan struct{})
|
||||
var calls int32
|
||||
tests := []string{"e", "a", "e", "a", "b", "c", "b", "a", "c", "d", "b", "c", "d"}
|
||||
@@ -102,7 +102,7 @@ func TestExclusiveCallDoDiffDupSuppress(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExclusiveCallDoExDupSuppress(t *testing.T) {
|
||||
g := NewSharedCalls()
|
||||
g := NewSingleFlight()
|
||||
c := make(chan string)
|
||||
var calls int32
|
||||
fn := func() (interface{}, error) {
|
||||
@@ -1,11 +1,14 @@
|
||||
package syncx
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tal-tech/go-zero/core/lang"
|
||||
)
|
||||
|
||||
func TestTryLock(t *testing.T) {
|
||||
@@ -30,8 +33,6 @@ func TestSpinLockRace(t *testing.T) {
|
||||
var wait sync.WaitGroup
|
||||
wait.Add(1)
|
||||
go func() {
|
||||
lock.Lock()
|
||||
lock.Unlock()
|
||||
wait.Done()
|
||||
}()
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
@@ -39,3 +40,31 @@ func TestSpinLockRace(t *testing.T) {
|
||||
wait.Wait()
|
||||
assert.True(t, lock.TryLock())
|
||||
}
|
||||
|
||||
func TestSpinLock_TryLock(t *testing.T) {
|
||||
var lock SpinLock
|
||||
var count int32
|
||||
var wait sync.WaitGroup
|
||||
wait.Add(2)
|
||||
sig := make(chan lang.PlaceholderType)
|
||||
|
||||
go func() {
|
||||
lock.TryLock()
|
||||
sig <- lang.Placeholder
|
||||
atomic.AddInt32(&count, 1)
|
||||
runtime.Gosched()
|
||||
lock.Unlock()
|
||||
wait.Done()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
<-sig
|
||||
lock.Lock()
|
||||
atomic.AddInt32(&count, 1)
|
||||
lock.Unlock()
|
||||
wait.Done()
|
||||
}()
|
||||
|
||||
wait.Wait()
|
||||
assert.Equal(t, int32(2), atomic.LoadInt32(&count))
|
||||
}
|
||||
|
||||
69
core/trace/agent.go
Normal file
69
core/trace/agent.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/exporters/jaeger"
|
||||
"go.opentelemetry.io/otel/exporters/zipkin"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||
)
|
||||
|
||||
const (
|
||||
kindJaeger = "jaeger"
|
||||
kindZipkin = "zipkin"
|
||||
)
|
||||
|
||||
var once sync.Once
|
||||
|
||||
// StartAgent starts a opentelemetry agent.
|
||||
func StartAgent(c Config) {
|
||||
once.Do(func() {
|
||||
startAgent(c)
|
||||
})
|
||||
}
|
||||
|
||||
func createExporter(c Config) (sdktrace.SpanExporter, error) {
|
||||
// Just support jaeger now, more for later
|
||||
switch c.Batcher {
|
||||
case kindJaeger:
|
||||
return jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(c.Endpoint)))
|
||||
case kindZipkin:
|
||||
return zipkin.New(c.Endpoint)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown exporter: %s", c.Batcher)
|
||||
}
|
||||
}
|
||||
|
||||
func startAgent(c Config) {
|
||||
opts := []sdktrace.TracerProviderOption{
|
||||
// Set the sampling rate based on the parent span to 100%
|
||||
sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(c.Sampler))),
|
||||
// Record information about this application in an Resource.
|
||||
sdktrace.WithResource(resource.NewSchemaless(semconv.ServiceNameKey.String(c.Name))),
|
||||
}
|
||||
|
||||
if len(c.Endpoint) > 0 {
|
||||
exp, err := createExporter(c)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Always be sure to batch in production.
|
||||
opts = append(opts, sdktrace.WithBatcher(exp))
|
||||
}
|
||||
|
||||
tp := sdktrace.NewTracerProvider(opts...)
|
||||
otel.SetTracerProvider(tp)
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||
propagation.TraceContext{}, propagation.Baggage{}))
|
||||
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) {
|
||||
logx.Errorf("[otel] error: %v", err)
|
||||
}))
|
||||
}
|
||||
40
core/trace/attributes.go
Normal file
40
core/trace/attributes.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||
gcodes "google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
const (
|
||||
// GRPCStatusCodeKey is convention for numeric status code of a gRPC request.
|
||||
GRPCStatusCodeKey = attribute.Key("rpc.grpc.status_code")
|
||||
// RPCNameKey is the name of message transmitted or received.
|
||||
RPCNameKey = attribute.Key("name")
|
||||
// RPCMessageTypeKey is the type of message transmitted or received.
|
||||
RPCMessageTypeKey = attribute.Key("message.type")
|
||||
// RPCMessageIDKey is the identifier of message transmitted or received.
|
||||
RPCMessageIDKey = attribute.Key("message.id")
|
||||
// RPCMessageCompressedSizeKey is the compressed size of the message transmitted or received in bytes.
|
||||
RPCMessageCompressedSizeKey = attribute.Key("message.compressed_size")
|
||||
// RPCMessageUncompressedSizeKey is the uncompressed size of the message
|
||||
// transmitted or received in bytes.
|
||||
RPCMessageUncompressedSizeKey = attribute.Key("message.uncompressed_size")
|
||||
)
|
||||
|
||||
// Semantic conventions for common RPC attributes.
|
||||
var (
|
||||
// RPCSystemGRPC is the semantic convention for gRPC as the remoting system.
|
||||
RPCSystemGRPC = semconv.RPCSystemKey.String("grpc")
|
||||
// RPCNameMessage is the semantic convention for a message named message.
|
||||
RPCNameMessage = RPCNameKey.String("message")
|
||||
// RPCMessageTypeSent is the semantic conventions for sent RPC message types.
|
||||
RPCMessageTypeSent = RPCMessageTypeKey.String("SENT")
|
||||
// RPCMessageTypeReceived is the semantic conventions for the received RPC message types.
|
||||
RPCMessageTypeReceived = RPCMessageTypeKey.String("RECEIVED")
|
||||
)
|
||||
|
||||
// StatusCodeAttr returns a attribute.KeyValue that represents the give c.
|
||||
func StatusCodeAttr(c gcodes.Code) attribute.KeyValue {
|
||||
return GRPCStatusCodeKey.Int64(int64(c))
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrInvalidCarrier indicates an error that the carrier is invalid.
|
||||
var ErrInvalidCarrier = errors.New("invalid carrier")
|
||||
|
||||
type (
|
||||
// Carrier interface wraps the Get and Set method.
|
||||
Carrier interface {
|
||||
Get(key string) string
|
||||
Set(key, value string)
|
||||
}
|
||||
|
||||
httpCarrier http.Header
|
||||
// grpc metadata takes keys as case insensitive
|
||||
grpcCarrier map[string][]string
|
||||
)
|
||||
|
||||
func (h httpCarrier) Get(key string) string {
|
||||
return http.Header(h).Get(key)
|
||||
}
|
||||
|
||||
func (h httpCarrier) Set(key, val string) {
|
||||
http.Header(h).Set(key, val)
|
||||
}
|
||||
|
||||
func (g grpcCarrier) Get(key string) string {
|
||||
if vals, ok := g[strings.ToLower(key)]; ok && len(vals) > 0 {
|
||||
return vals[0]
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (g grpcCarrier) Set(key, val string) {
|
||||
key = strings.ToLower(key)
|
||||
g[key] = append(g[key], val)
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
func TestHttpCarrier(t *testing.T) {
|
||||
tests := []map[string]string{
|
||||
{},
|
||||
{
|
||||
"first": "a",
|
||||
"second": "b",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(stringx.RandId(), func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
carrier := httpCarrier(req.Header)
|
||||
for k, v := range test {
|
||||
carrier.Set(k, v)
|
||||
}
|
||||
for k, v := range test {
|
||||
assert.Equal(t, v, carrier.Get(k))
|
||||
}
|
||||
assert.Equal(t, "", carrier.Get("none"))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrpcCarrier(t *testing.T) {
|
||||
tests := []map[string]string{
|
||||
{},
|
||||
{
|
||||
"first": "a",
|
||||
"second": "b",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(stringx.RandId(), func(t *testing.T) {
|
||||
m := make(map[string][]string)
|
||||
carrier := grpcCarrier(m)
|
||||
for k, v := range test {
|
||||
carrier.Set(k, v)
|
||||
}
|
||||
for k, v := range test {
|
||||
assert.Equal(t, v, carrier.Get(k))
|
||||
}
|
||||
assert.Equal(t, "", carrier.Get("none"))
|
||||
})
|
||||
}
|
||||
}
|
||||
12
core/trace/config.go
Normal file
12
core/trace/config.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package trace
|
||||
|
||||
// TraceName represents the tracing name.
|
||||
const TraceName = "go-zero"
|
||||
|
||||
// A Config is a opentelemetry config.
|
||||
type Config struct {
|
||||
Name string `json:",optional"`
|
||||
Endpoint string `json:",optional"`
|
||||
Sampler float64 `json:",default=1.0"`
|
||||
Batcher string `json:",default=jaeger,options=jaeger|zipkin"`
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package trace
|
||||
|
||||
const (
|
||||
traceIdKey = "X-Trace-ID"
|
||||
spanIdKey = "X-Span-ID"
|
||||
)
|
||||
38
core/trace/message.go
Normal file
38
core/trace/message.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
const messageEvent = "message"
|
||||
|
||||
var (
|
||||
// MessageSent is the type of sent messages.
|
||||
MessageSent = messageType(RPCMessageTypeSent)
|
||||
// MessageReceived is the type of received messages.
|
||||
MessageReceived = messageType(RPCMessageTypeReceived)
|
||||
)
|
||||
|
||||
type messageType attribute.KeyValue
|
||||
|
||||
// Event adds an event of the messageType to the span associated with the
|
||||
// passed context with id and size (if message is a proto message).
|
||||
func (m messageType) Event(ctx context.Context, id int, message interface{}) {
|
||||
span := trace.SpanFromContext(ctx)
|
||||
if p, ok := message.(proto.Message); ok {
|
||||
span.AddEvent(messageEvent, trace.WithAttributes(
|
||||
attribute.KeyValue(m),
|
||||
RPCMessageIDKey.Int(id),
|
||||
RPCMessageUncompressedSizeKey.Int(proto.Size(p)),
|
||||
))
|
||||
} else {
|
||||
span.AddEvent(messageEvent, trace.WithAttributes(
|
||||
attribute.KeyValue(m),
|
||||
RPCMessageIDKey.Int(id),
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/trace/tracespec"
|
||||
)
|
||||
|
||||
var emptyNoopSpan = noopSpan{}
|
||||
|
||||
type noopSpan struct{}
|
||||
|
||||
func (s noopSpan) Finish() {
|
||||
}
|
||||
|
||||
func (s noopSpan) Follow(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
|
||||
return ctx, emptyNoopSpan
|
||||
}
|
||||
|
||||
func (s noopSpan) Fork(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
|
||||
return ctx, emptyNoopSpan
|
||||
}
|
||||
|
||||
func (s noopSpan) SpanId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (s noopSpan) TraceId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (s noopSpan) Visit(fn func(key, val string) bool) {
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNoopSpan_Fork(t *testing.T) {
|
||||
ctx, span := emptyNoopSpan.Fork(context.Background(), "", "")
|
||||
assert.Equal(t, emptyNoopSpan, span)
|
||||
assert.Equal(t, context.Background(), ctx)
|
||||
}
|
||||
|
||||
func TestNoopSpan_Follow(t *testing.T) {
|
||||
ctx, span := emptyNoopSpan.Follow(context.Background(), "", "")
|
||||
assert.Equal(t, emptyNoopSpan, span)
|
||||
assert.Equal(t, context.Background(), ctx)
|
||||
}
|
||||
|
||||
func TestNoopSpan(t *testing.T) {
|
||||
emptyNoopSpan.Visit(func(key, val string) bool {
|
||||
assert.Fail(t, "should not go here")
|
||||
return true
|
||||
})
|
||||
|
||||
ctx, span := emptyNoopSpan.Follow(context.Background(), "", "")
|
||||
assert.Equal(t, context.Background(), ctx)
|
||||
assert.Equal(t, "", span.TraceId())
|
||||
assert.Equal(t, "", span.SpanId())
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
const (
|
||||
// HttpFormat means http carrier format.
|
||||
HttpFormat = iota
|
||||
// GrpcFormat means grpc carrier format.
|
||||
GrpcFormat
|
||||
)
|
||||
|
||||
var (
|
||||
emptyHttpPropagator httpPropagator
|
||||
emptyGrpcPropagator grpcPropagator
|
||||
)
|
||||
|
||||
type (
|
||||
// Propagator interface wraps the Extract and Inject methods.
|
||||
Propagator interface {
|
||||
Extract(carrier interface{}) (Carrier, error)
|
||||
Inject(carrier interface{}) (Carrier, error)
|
||||
}
|
||||
|
||||
httpPropagator struct{}
|
||||
grpcPropagator struct{}
|
||||
)
|
||||
|
||||
func (h httpPropagator) Extract(carrier interface{}) (Carrier, error) {
|
||||
if c, ok := carrier.(http.Header); ok {
|
||||
return httpCarrier(c), nil
|
||||
}
|
||||
|
||||
return nil, ErrInvalidCarrier
|
||||
}
|
||||
|
||||
func (h httpPropagator) Inject(carrier interface{}) (Carrier, error) {
|
||||
if c, ok := carrier.(http.Header); ok {
|
||||
return httpCarrier(c), nil
|
||||
}
|
||||
|
||||
return nil, ErrInvalidCarrier
|
||||
}
|
||||
|
||||
func (g grpcPropagator) Extract(carrier interface{}) (Carrier, error) {
|
||||
if c, ok := carrier.(metadata.MD); ok {
|
||||
return grpcCarrier(c), nil
|
||||
}
|
||||
|
||||
return nil, ErrInvalidCarrier
|
||||
}
|
||||
|
||||
func (g grpcPropagator) Inject(carrier interface{}) (Carrier, error) {
|
||||
if c, ok := carrier.(metadata.MD); ok {
|
||||
return grpcCarrier(c), nil
|
||||
}
|
||||
|
||||
return nil, ErrInvalidCarrier
|
||||
}
|
||||
|
||||
// Extract extracts tracing information from carrier with given format.
|
||||
func Extract(format, carrier interface{}) (Carrier, error) {
|
||||
switch v := format.(type) {
|
||||
case int:
|
||||
if v == HttpFormat {
|
||||
return emptyHttpPropagator.Extract(carrier)
|
||||
} else if v == GrpcFormat {
|
||||
return emptyGrpcPropagator.Extract(carrier)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, ErrInvalidCarrier
|
||||
}
|
||||
|
||||
// Inject injects tracing information into carrier with given format.
|
||||
func Inject(format, carrier interface{}) (Carrier, error) {
|
||||
switch v := format.(type) {
|
||||
case int:
|
||||
if v == HttpFormat {
|
||||
return emptyHttpPropagator.Inject(carrier)
|
||||
} else if v == GrpcFormat {
|
||||
return emptyGrpcPropagator.Inject(carrier)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, ErrInvalidCarrier
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
func TestHttpPropagator_Extract(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req.Header.Set(traceIdKey, "trace")
|
||||
req.Header.Set(spanIdKey, "span")
|
||||
carrier, err := Extract(HttpFormat, req.Header)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "trace", carrier.Get(traceIdKey))
|
||||
assert.Equal(t, "span", carrier.Get(spanIdKey))
|
||||
|
||||
_, err = Extract(HttpFormat, req)
|
||||
assert.Equal(t, ErrInvalidCarrier, err)
|
||||
}
|
||||
|
||||
func TestHttpPropagator_Inject(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req.Header.Set(traceIdKey, "trace")
|
||||
req.Header.Set(spanIdKey, "span")
|
||||
carrier, err := Inject(HttpFormat, req.Header)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "trace", carrier.Get(traceIdKey))
|
||||
assert.Equal(t, "span", carrier.Get(spanIdKey))
|
||||
|
||||
_, err = Inject(HttpFormat, req)
|
||||
assert.Equal(t, ErrInvalidCarrier, err)
|
||||
}
|
||||
|
||||
func TestGrpcPropagator_Extract(t *testing.T) {
|
||||
md := metadata.New(map[string]string{
|
||||
traceIdKey: "trace",
|
||||
spanIdKey: "span",
|
||||
})
|
||||
carrier, err := Extract(GrpcFormat, md)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "trace", carrier.Get(traceIdKey))
|
||||
assert.Equal(t, "span", carrier.Get(spanIdKey))
|
||||
|
||||
_, err = Extract(GrpcFormat, 1)
|
||||
assert.Equal(t, ErrInvalidCarrier, err)
|
||||
_, err = Extract(nil, 1)
|
||||
assert.Equal(t, ErrInvalidCarrier, err)
|
||||
}
|
||||
|
||||
func TestGrpcPropagator_Inject(t *testing.T) {
|
||||
md := metadata.New(map[string]string{
|
||||
traceIdKey: "trace",
|
||||
spanIdKey: "span",
|
||||
})
|
||||
carrier, err := Inject(GrpcFormat, md)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "trace", carrier.Get(traceIdKey))
|
||||
assert.Equal(t, "span", carrier.Get(spanIdKey))
|
||||
|
||||
_, err = Inject(GrpcFormat, 1)
|
||||
assert.Equal(t, ErrInvalidCarrier, err)
|
||||
_, err = Inject(nil, 1)
|
||||
assert.Equal(t, ErrInvalidCarrier, err)
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/core/timex"
|
||||
"github.com/tal-tech/go-zero/core/trace/tracespec"
|
||||
)
|
||||
|
||||
const (
|
||||
initSpanId = "0"
|
||||
clientFlag = "client"
|
||||
serverFlag = "server"
|
||||
spanSepRune = '.'
|
||||
)
|
||||
|
||||
var spanSep = string([]byte{spanSepRune})
|
||||
|
||||
// A Span is a calling span that connects caller and callee.
|
||||
type Span struct {
|
||||
ctx spanContext
|
||||
serviceName string
|
||||
operationName string
|
||||
startTime time.Time
|
||||
flag string
|
||||
children int
|
||||
}
|
||||
|
||||
func newServerSpan(carrier Carrier, serviceName, operationName string) tracespec.Trace {
|
||||
traceId := stringx.TakeWithPriority(func() string {
|
||||
if carrier != nil {
|
||||
return carrier.Get(traceIdKey)
|
||||
}
|
||||
return ""
|
||||
}, stringx.RandId)
|
||||
spanId := stringx.TakeWithPriority(func() string {
|
||||
if carrier != nil {
|
||||
return carrier.Get(spanIdKey)
|
||||
}
|
||||
return ""
|
||||
}, func() string {
|
||||
return initSpanId
|
||||
})
|
||||
|
||||
return &Span{
|
||||
ctx: spanContext{
|
||||
traceId: traceId,
|
||||
spanId: spanId,
|
||||
},
|
||||
serviceName: serviceName,
|
||||
operationName: operationName,
|
||||
startTime: timex.Time(),
|
||||
flag: serverFlag,
|
||||
}
|
||||
}
|
||||
|
||||
// Finish finishes the calling span.
|
||||
func (s *Span) Finish() {
|
||||
}
|
||||
|
||||
// Follow follows the tracing service and operation names in context.
|
||||
func (s *Span) Follow(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
|
||||
span := &Span{
|
||||
ctx: spanContext{
|
||||
traceId: s.ctx.traceId,
|
||||
spanId: s.followSpanId(),
|
||||
},
|
||||
serviceName: serviceName,
|
||||
operationName: operationName,
|
||||
startTime: timex.Time(),
|
||||
flag: s.flag,
|
||||
}
|
||||
return context.WithValue(ctx, tracespec.TracingKey, span), span
|
||||
}
|
||||
|
||||
// Fork forks the tracing service and operation names in context.
|
||||
func (s *Span) Fork(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
|
||||
span := &Span{
|
||||
ctx: spanContext{
|
||||
traceId: s.ctx.traceId,
|
||||
spanId: s.forkSpanId(),
|
||||
},
|
||||
serviceName: serviceName,
|
||||
operationName: operationName,
|
||||
startTime: timex.Time(),
|
||||
flag: clientFlag,
|
||||
}
|
||||
return context.WithValue(ctx, tracespec.TracingKey, span), span
|
||||
}
|
||||
|
||||
// SpanId returns the span id.
|
||||
func (s *Span) SpanId() string {
|
||||
return s.ctx.SpanId()
|
||||
}
|
||||
|
||||
// TraceId returns the trace id.
|
||||
func (s *Span) TraceId() string {
|
||||
return s.ctx.TraceId()
|
||||
}
|
||||
|
||||
// Visit visits the span using fn.
|
||||
func (s *Span) Visit(fn func(key, val string) bool) {
|
||||
s.ctx.Visit(fn)
|
||||
}
|
||||
|
||||
func (s *Span) forkSpanId() string {
|
||||
s.children++
|
||||
return fmt.Sprintf("%s.%d", s.ctx.spanId, s.children)
|
||||
}
|
||||
|
||||
func (s *Span) followSpanId() string {
|
||||
fields := strings.FieldsFunc(s.ctx.spanId, func(r rune) bool {
|
||||
return r == spanSepRune
|
||||
})
|
||||
if len(fields) == 0 {
|
||||
return s.ctx.spanId
|
||||
}
|
||||
|
||||
last := fields[len(fields)-1]
|
||||
val, err := strconv.Atoi(last)
|
||||
if err != nil {
|
||||
return s.ctx.spanId
|
||||
}
|
||||
|
||||
last = strconv.Itoa(val + 1)
|
||||
fields[len(fields)-1] = last
|
||||
|
||||
return strings.Join(fields, spanSep)
|
||||
}
|
||||
|
||||
// StartClientSpan starts the client span with given context, service and operation names.
|
||||
func StartClientSpan(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
|
||||
if span, ok := ctx.Value(tracespec.TracingKey).(*Span); ok {
|
||||
return span.Fork(ctx, serviceName, operationName)
|
||||
}
|
||||
|
||||
return ctx, emptyNoopSpan
|
||||
}
|
||||
|
||||
// StartServerSpan starts the server span with given context, carrier, service and operation names.
|
||||
func StartServerSpan(ctx context.Context, carrier Carrier, serviceName, operationName string) (
|
||||
context.Context, tracespec.Trace) {
|
||||
span := newServerSpan(carrier, serviceName, operationName)
|
||||
return context.WithValue(ctx, tracespec.TracingKey, span), span
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/core/trace/tracespec"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
func TestClientSpan(t *testing.T) {
|
||||
span := newServerSpan(nil, "service", "operation")
|
||||
ctx := context.WithValue(context.Background(), tracespec.TracingKey, span)
|
||||
ctx, span = StartClientSpan(ctx, "entrance", "operation")
|
||||
defer span.Finish()
|
||||
assert.Equal(t, span, ctx.Value(tracespec.TracingKey))
|
||||
|
||||
const serviceName = "authorization"
|
||||
const operationName = "verification"
|
||||
ctx, childSpan := span.Fork(ctx, serviceName, operationName)
|
||||
defer childSpan.Finish()
|
||||
|
||||
assert.Equal(t, childSpan, ctx.Value(tracespec.TracingKey))
|
||||
assert.Equal(t, getSpan(span).TraceId(), getSpan(childSpan).TraceId())
|
||||
assert.Equal(t, "0.1.1", getSpan(childSpan).SpanId())
|
||||
assert.Equal(t, serviceName, childSpan.(*Span).serviceName)
|
||||
assert.Equal(t, operationName, childSpan.(*Span).operationName)
|
||||
assert.Equal(t, clientFlag, childSpan.(*Span).flag)
|
||||
}
|
||||
|
||||
func TestClientSpan_WithoutTrace(t *testing.T) {
|
||||
ctx, span := StartClientSpan(context.Background(), "entrance", "operation")
|
||||
defer span.Finish()
|
||||
assert.Equal(t, emptyNoopSpan, span)
|
||||
assert.Equal(t, context.Background(), ctx)
|
||||
}
|
||||
|
||||
func TestServerSpan(t *testing.T) {
|
||||
ctx, span := StartServerSpan(context.Background(), nil, "service", "operation")
|
||||
defer span.Finish()
|
||||
assert.Equal(t, span, ctx.Value(tracespec.TracingKey))
|
||||
|
||||
const serviceName = "authorization"
|
||||
const operationName = "verification"
|
||||
ctx, childSpan := span.Fork(ctx, serviceName, operationName)
|
||||
defer childSpan.Finish()
|
||||
|
||||
assert.Equal(t, childSpan, ctx.Value(tracespec.TracingKey))
|
||||
assert.Equal(t, getSpan(span).TraceId(), getSpan(childSpan).TraceId())
|
||||
assert.Equal(t, "0.1", getSpan(childSpan).SpanId())
|
||||
assert.Equal(t, serviceName, childSpan.(*Span).serviceName)
|
||||
assert.Equal(t, operationName, childSpan.(*Span).operationName)
|
||||
assert.Equal(t, clientFlag, childSpan.(*Span).flag)
|
||||
}
|
||||
|
||||
func TestServerSpan_WithCarrier(t *testing.T) {
|
||||
md := metadata.New(map[string]string{
|
||||
traceIdKey: "a",
|
||||
spanIdKey: "0.1",
|
||||
})
|
||||
ctx, span := StartServerSpan(context.Background(), grpcCarrier(md), "service", "operation")
|
||||
defer span.Finish()
|
||||
assert.Equal(t, span, ctx.Value(tracespec.TracingKey))
|
||||
|
||||
const serviceName = "authorization"
|
||||
const operationName = "verification"
|
||||
ctx, childSpan := span.Fork(ctx, serviceName, operationName)
|
||||
defer childSpan.Finish()
|
||||
|
||||
assert.Equal(t, childSpan, ctx.Value(tracespec.TracingKey))
|
||||
assert.Equal(t, getSpan(span).TraceId(), getSpan(childSpan).TraceId())
|
||||
assert.Equal(t, "0.1.1", getSpan(childSpan).SpanId())
|
||||
assert.Equal(t, serviceName, childSpan.(*Span).serviceName)
|
||||
assert.Equal(t, operationName, childSpan.(*Span).operationName)
|
||||
assert.Equal(t, clientFlag, childSpan.(*Span).flag)
|
||||
}
|
||||
|
||||
func TestSpan_Follow(t *testing.T) {
|
||||
tests := []struct {
|
||||
span string
|
||||
expectSpan string
|
||||
}{
|
||||
{
|
||||
"0.1",
|
||||
"0.2",
|
||||
},
|
||||
{
|
||||
"0",
|
||||
"1",
|
||||
},
|
||||
{
|
||||
"a",
|
||||
"a",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(stringx.RandId(), func(t *testing.T) {
|
||||
md := metadata.New(map[string]string{
|
||||
traceIdKey: "a",
|
||||
spanIdKey: test.span,
|
||||
})
|
||||
ctx, span := StartServerSpan(context.Background(), grpcCarrier(md),
|
||||
"service", "operation")
|
||||
defer span.Finish()
|
||||
assert.Equal(t, span, ctx.Value(tracespec.TracingKey))
|
||||
|
||||
const serviceName = "authorization"
|
||||
const operationName = "verification"
|
||||
ctx, childSpan := span.Follow(ctx, serviceName, operationName)
|
||||
defer childSpan.Finish()
|
||||
|
||||
assert.Equal(t, childSpan, ctx.Value(tracespec.TracingKey))
|
||||
assert.Equal(t, getSpan(span).TraceId(), getSpan(childSpan).TraceId())
|
||||
assert.Equal(t, test.expectSpan, getSpan(childSpan).SpanId())
|
||||
assert.Equal(t, serviceName, childSpan.(*Span).serviceName)
|
||||
assert.Equal(t, operationName, childSpan.(*Span).operationName)
|
||||
assert.Equal(t, span.(*Span).flag, childSpan.(*Span).flag)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSpan_Visit(t *testing.T) {
|
||||
var run bool
|
||||
span := newServerSpan(nil, "service", "operation")
|
||||
span.Visit(func(key, val string) bool {
|
||||
assert.True(t, len(key) > 0)
|
||||
assert.True(t, len(val) > 0)
|
||||
run = true
|
||||
return true
|
||||
})
|
||||
assert.True(t, run)
|
||||
}
|
||||
|
||||
func getSpan(span tracespec.Trace) tracespec.Trace {
|
||||
return span.(*Span)
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package trace
|
||||
|
||||
type spanContext struct {
|
||||
traceId string
|
||||
spanId string
|
||||
}
|
||||
|
||||
func (sc spanContext) TraceId() string {
|
||||
return sc.traceId
|
||||
}
|
||||
|
||||
func (sc spanContext) SpanId() string {
|
||||
return sc.spanId
|
||||
}
|
||||
|
||||
func (sc spanContext) Visit(fn func(key, val string) bool) {
|
||||
fn(traceIdKey, sc.traceId)
|
||||
fn(spanIdKey, sc.spanId)
|
||||
}
|
||||
56
core/trace/tracer.go
Normal file
56
core/trace/tracer.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/baggage"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
sdktrace "go.opentelemetry.io/otel/trace"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
// assert that metadataSupplier implements the TextMapCarrier interface
|
||||
var _ propagation.TextMapCarrier = new(metadataSupplier)
|
||||
|
||||
type metadataSupplier struct {
|
||||
metadata *metadata.MD
|
||||
}
|
||||
|
||||
func (s *metadataSupplier) Get(key string) string {
|
||||
values := s.metadata.Get(key)
|
||||
if len(values) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return values[0]
|
||||
}
|
||||
|
||||
func (s *metadataSupplier) Set(key, value string) {
|
||||
s.metadata.Set(key, value)
|
||||
}
|
||||
|
||||
func (s *metadataSupplier) Keys() []string {
|
||||
out := make([]string, 0, len(*s.metadata))
|
||||
for key := range *s.metadata {
|
||||
out = append(out, key)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// Inject injects the metadata into ctx.
|
||||
func Inject(ctx context.Context, p propagation.TextMapPropagator, metadata *metadata.MD) {
|
||||
p.Inject(ctx, &metadataSupplier{
|
||||
metadata: metadata,
|
||||
})
|
||||
}
|
||||
|
||||
// Extract extracts the metadata from ctx.
|
||||
func Extract(ctx context.Context, p propagation.TextMapPropagator, metadata *metadata.MD) (
|
||||
baggage.Baggage, sdktrace.SpanContext) {
|
||||
ctx = p.Extract(ctx, &metadataSupplier{
|
||||
metadata: metadata,
|
||||
})
|
||||
|
||||
return baggage.FromContext(ctx), sdktrace.SpanContextFromContext(ctx)
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user