trpc.group/trpc-go/trpc-go@v1.0.3/admin/README.md (about) 1 English | [中文](README.zh_CN.md) 2 3 # Introduction 4 5 The management command (admin) is the internal management background of the service. It is an additional http service provided by the framework in addition to the normal service port. Through this http interface, instructions can be sent to the service, such as viewing the log level, dynamically setting the log level, etc. The specific command See the list of commands below. 6 7 Admin is generally used to query the internal status information of the service, and users can also define arbitrary commands. 8 9 Admin provides HTTP services to the outside world using the standard RESTful protocol. 10 11 By default, the framework does not enable the admin capability and needs to be configured to start it. When generating the configuration, admin can be configured by default, so that admin can be opened by default. 12 ```yaml 13 server: 14 app: app # The application name of the business, be sure to change it to your own business application name 15 server: server # The process service name, be sure to change it to your own service process name 16 admin: 17 ip: 127.0.0.1 # The IP address of admin, you can also configure the network card NIC 18 port: 11014 # The port of admin, admin will only start when the IP and port are configured here at the same time 19 read_timeout: 3000 # ms. Set the timeout for accepting requests and reading the request information completely to prevent slow clients 20 write_timeout: 60000 # ms. Timeout for processing 21 ``` 22 23 # List of management commands 24 25 The following commands are already built into the framework. Note: the `IP:port` in the commands is the address configured in the admin, not the address configured in the service. 26 27 ## View all management commands 28 29 ```shell 30 curl http://ip:port/cmds 31 ``` 32 Return results 33 34 ```shell 35 { 36 "cmds":[ 37 "/cmds", 38 "/version", 39 "/cmds/loglevel", 40 "/cmds/config" 41 ], 42 "errorcode":0, 43 "message":"" 44 } 45 ``` 46 47 ## View framework version information 48 49 ```shell 50 curl http://ip:port/version 51 ``` 52 Return results 53 54 ```shell 55 { 56 "errorcode": 0, 57 "message": "", 58 "version": "v0.1.0-dev" 59 } 60 ``` 61 62 ## View framework log level 63 64 ```shell 65 curl -XGET http://ip:port/cmds/loglevel?logger=xxx&output=0 66 ``` 67 Note: logger is used to support multiple logs. If not specified, it will use the default log of the framework. Output refers to different outputs under the same logger, with the array index starting at 0. If not specified, it will use the first output. 68 69 Return results 70 71 ```shell 72 { 73 "errorcode":0, 74 "loglevel":"info", 75 "message":"" 76 } 77 ``` 78 79 ## Set framework log level 80 81 (The value is the log level, and the possible values are: trace debug info warn error fatal) 82 83 ```shell 84 curl http://ip:port/cmds/loglevel?logger=xxx -XPUT -d value="debug" 85 ``` 86 Note: The logger is used to support multiple logs. If not specified, the default log of the framework will be used. The 'output' parameter refers to different outputs under the same logger, with the array index starting at 0. If not specified, the first output will be used. 87 88 Note: This sets the internal memory data of the service and will not update the configuration file. It will become invalid after a restart. 89 90 Return results 91 92 ```shell 93 { 94 "errorcode":0, 95 "level":"debug", 96 "message":"", 97 "prelevel":"info" 98 } 99 ``` 100 101 ## View framework configuration file 102 103 ```shell 104 curl http://ip:port/cmds/config 105 ``` 106 Return results 107 108 The 'content' parameter refers to the JSON-formatted content of the configuration file. 109 110 ```shell 111 { 112 "content":{ 113 114 }, 115 "errorcode":0, 116 "message":"" 117 } 118 ``` 119 120 # Customize management commands 121 122 ## Define a function 123 124 First, define your own HTTP interface processing function, which can be defined in any file location. 125 126 ```go 127 // load Trigger loading a local file to update a specific value in memory 128 func load(w http.ResponseWriter, r *http.Request) { 129 reader, err := ioutil.ReadFile("xxx.txt") 130 if err != nil { 131 w.Write([]byte(`{"errorcode":1000, "message":"read file fail"}`)) // Define error codes and error messages by yourself 132 return 133 } 134 135 // Business logic... 136 137 // Return a success error code 138 w.Write([]byte(`{"errorcode":0, "message":"ok"}`)) 139 } 140 ``` 141 142 ## Register a route 143 144 Register admin in the init function or in your own internal function: 145 146 ```go 147 import ( 148 "trpc.group/trpc-go/trpc-go/admin" 149 ) 150 func init() { 151 admin.HandleFunc("/cmds/load", load) // Define the path yourself, usually under /cmds. Be careful not to duplicate, otherwise they will overwrite each other. 152 } 153 ``` 154 155 ## Trigger a command 156 157 Trigger the execution of a custom command 158 159 ```shell 160 curl http://ip:port/cmds/load 161 ``` 162 163 # Pprof performance analysis 164 165 Pprof is a built-in performance analysis tool in Go language, which shares the same port number with the admin service by default. As long as the admin service is enabled, the pprof of the service can be used. 166 167 After configuring admin, there are a few ways to use pprof: 168 169 ## To use pprof on a machine with a configured Go environment and network connectivity to the server 170 171 ```shell 172 go tool pprof http://{$ip}:${port}/debug/pprof/profile?seconds=20 173 ``` 174 175 ## To download pprof files to the local machine and analyze them using local Go tools 176 177 ```shell 178 curl http://${ip}:{$port}/debug/pprof/profile?seconds=20 > profile.out 179 go tool pprof profile.out 180 181 curl http://${ip}:{$port}/debug/pprof/trace?seconds=20 > trace.out 182 go tool trace trace.out 183 ``` 184 185 # Memory management commands debug/pprof/heap 186 187 In addition, trpc-go will automatically remove the pprof route registered on the golang http package DefaultServeMux for you, avoiding the security issues of the golang net/http/pprof package (this is a problem of go itself). 188 189 Therefore, the service built using the trpc-go framework can directly use the pprof command, but the service started with the `http.ListenAndServe("xxx", xxx)` method will not be able to use the pprof command. 190 191 Perform memory analysis on intranet machines with the go command installed: 192 193 ```shell 194 go tool pprof -inuse_space http://xxx:11029/debug/pprof/heap 195 go tool pprof -alloc_space http://xxx:11029/debug/pprof/heap 196 ```