Skip to content

Commit 0da1b11

Browse files
grpc: add caller info to client and wrap innerClient (#8704)
ref #8593 Signed-off-by: okJiang <[email protected]> Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
1 parent 41ec8dc commit 0da1b11

29 files changed

+672
-388
lines changed

Diff for: client/caller/caller.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2024 TiKV Project Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package caller
16+
17+
import (
18+
"os"
19+
"path/filepath"
20+
"runtime"
21+
"strings"
22+
)
23+
24+
type (
25+
// Caller ID can be understood as a binary file; it is a process.
26+
ID string
27+
// Caller component refers to the components within the process.
28+
Component string
29+
)
30+
31+
const (
32+
// TestID is used for test.
33+
TestID ID = "test"
34+
// TestComponent is used for test.
35+
TestComponent Component = "test"
36+
)
37+
38+
var processName ID
39+
40+
func init() {
41+
processName = ID(filepath.Base(os.Args[0]))
42+
}
43+
44+
// GetCallerID returns the name of the currently running process
45+
func GetCallerID() ID {
46+
return processName
47+
}
48+
49+
// GetComponent returns the package path of the calling function
50+
// The argument upperLayer specifies the number of stack frames to ascend.
51+
// NOTE: This function is time-consuming and please do not use it in high qps scenarios.
52+
func GetComponent(upperLayer int) Component {
53+
// Get the program counter for the calling function
54+
pc, _, _, ok := runtime.Caller(upperLayer + 1)
55+
if !ok {
56+
return "unknown"
57+
}
58+
59+
// Retrieve the full function name, including the package path
60+
fullFuncName := runtime.FuncForPC(pc).Name()
61+
62+
// Separates the package and function
63+
lastSlash := strings.LastIndex(fullFuncName, ".")
64+
65+
// Extract the package name
66+
return Component(fullFuncName[:lastSlash])
67+
}

Diff for: client/caller/caller_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2024 TiKV Project Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package caller
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/require"
21+
)
22+
23+
func TestGetComponent(t *testing.T) {
24+
re := require.New(t)
25+
26+
re.Equal(Component("github.com/tikv/pd/client/caller"), GetComponent(0))
27+
re.Equal(Component("testing"), GetComponent(1))
28+
re.Equal(Component("runtime"), GetComponent(2))
29+
re.Equal(Component("unknown"), GetComponent(3))
30+
}
31+
32+
func TestGetCallerID(t *testing.T) {
33+
re := require.New(t)
34+
35+
re.Equal(ID("caller.test"), GetCallerID())
36+
}

0 commit comments

Comments
 (0)