Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc: add caller info to client and wrap innerClient #8704

Merged
merged 31 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
14e8372
grpc: add caller info and wrap innerClient
okJiang Oct 15, 2024
55a3a7e
Merge branch 'master' of github.com:tikv/pd into introduce-callerid-f…
okJiang Oct 18, 2024
806cbae
modify go.mod
okJiang Oct 18, 2024
39cc31e
update go.mod
okJiang Oct 18, 2024
3cef11f
fix static
okJiang Oct 18, 2024
1a0f01a
Merge branch 'master' of github.com:tikv/pd into introduce-callerid-f…
okJiang Oct 21, 2024
f3e68bc
Merge branch 'master' of https://github.com/tikv/pd into introduce-ca…
okJiang Oct 22, 2024
9c46707
Merge branch 'master' of github.com:tikv/pd into introduce-callerid-f…
okJiang Nov 6, 2024
10a2cd5
fix linter
okJiang Nov 6, 2024
5443eb0
add ut
okJiang Nov 6, 2024
ff42837
fix lint
okJiang Nov 6, 2024
34dc7bd
fix ut
okJiang Nov 6, 2024
51d33d0
fix lint
okJiang Nov 6, 2024
b0a6106
fix comment
okJiang Nov 8, 2024
7e5abe1
fix lint
okJiang Nov 11, 2024
879d3db
use runtime to acquire callerID and callerComponent
okJiang Nov 12, 2024
c959e23
Merge branch 'master' of https://github.com/tikv/pd into introduce-ca…
okJiang Nov 12, 2024
fcf5ba2
fix lint
okJiang Nov 12, 2024
ec50c2f
fix ut
okJiang Nov 12, 2024
ddec23f
add lisence
okJiang Nov 12, 2024
550c343
add more comments
okJiang Nov 13, 2024
9e3af20
Merge branch 'introduce-callerid-for-grpc' of https://github.com/okJi…
okJiang Nov 14, 2024
971f5c7
make tidy
okJiang Nov 14, 2024
2a1fc44
Merge branch 'master' of https://github.com/tikv/pd into introduce-ca…
okJiang Nov 15, 2024
1c5d544
add a comment
okJiang Nov 15, 2024
10ff91d
remove WithCallerID
okJiang Nov 15, 2024
de83c86
fix comment
okJiang Nov 19, 2024
7de9e86
Merge branch 'master' of github.com:tikv/pd into introduce-callerid-f…
okJiang Nov 19, 2024
6435a6f
fix comment: use global caller.ID
okJiang Nov 20, 2024
70add24
upgrade kvproto
okJiang Nov 20, 2024
dadf547
Merge branch 'master' into introduce-callerid-for-grpc
ti-chi-bot[bot] Nov 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions client/caller/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package caller

type (
// Caller ID can be understood as a binary file; it is a process.
ID string
// Caller component refers to the components within the process.
Component string
)

const (
// TestID is used for test.
TestID ID = "test"
// TestComponent is used for test.
TestComponent Component = "test"
)
47 changes: 47 additions & 0 deletions client/caller/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2024 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package caller

import (
"os"
"path/filepath"
"runtime"
"strings"
)

// GetCallerID returns the name of the currently running process
func GetCallerID() ID {
return ID(filepath.Base(os.Args[0]))
}

// GetComponent returns the package path of the calling function
// The argument upperLayer specifies the number of stack frames to ascend.
// NOTE: This function is time-consuming and please do not use it in high qps scenarios.
func GetComponent(upperLayer int) Component {
// Get the program counter for the calling function
pc, _, _, ok := runtime.Caller(upperLayer + 1)
if !ok {
return "unknown"
}

// Retrieve the full function name, including the package path
fullFuncName := runtime.FuncForPC(pc).Name()

// Separates the package and function
lastSlash := strings.LastIndex(fullFuncName, ".")

// Extract the package name
return Component(fullFuncName[:lastSlash])
}
36 changes: 36 additions & 0 deletions client/caller/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package caller

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestGetComponent(t *testing.T) {
re := require.New(t)

re.Equal(Component("github.com/tikv/pd/client/caller"), GetComponent(0))
re.Equal(Component("testing"), GetComponent(1))
re.Equal(Component("runtime"), GetComponent(2))
re.Equal(Component("unknown"), GetComponent(3))
}

func TestGetCallerID(t *testing.T) {
re := require.New(t)

re.Equal(ID("caller.test"), GetCallerID())
}
Loading