Description
I've created this issue according to https://github.com/golang/proposal .
This is an issue to discuss a proposal to add goroutine names for debugging purposes only. The name will appear only in stack dumps. I do not propose any new mechanism that would allow creation of a thread-local storage.
In essence, I propose adding several new functions to the runtime package:
- SetGoroutineName(name string) - set the current goroutine name to the specified value. The name can be up to runtime.MaxGoroutineNameLen (exact value TBD, I expect it to be around 40).
- SetEnableGoroutineName(bool) - enable goroutine name output in backtraces. By default goroutines are not enabled.
- IsGoroutineNameEnabled() bool - returns the status of name output enablement.
Sidenote: I don't like the name "name", as Go routines are anonymous. Perhaps I should rename it to "Trace ID" or something like it?
The goroutine name will be silently inherited by any goroutines spawned by the parent process.
Here's an example of use:
package main
import (
"runtime"
"net/http"
)
func main() {
runtime.SetEnableGoroutineName(true)
http.HandleFunc("/", HelloServer)
http.ListenAndServe(":8080", nil)
}
func HelloServer(w http.ResponseWriter, r *http.Request) {
runtime.SetGoroutineName(r.Header.Get("Request-Id"))
panic("An error happened")
}
Run it:
cyberax@CybMac:~/aurora/go/bin$ curl -H "Request-Id: req-id-123123" http://localhost:8080/
curl: (52) Empty reply from server
With the server output:
cyberax@CybMac:~/aurora/go$ ./bin/go run test.go
2019/10/25 21:34:28 http: panic serving [::1]:61981: An error happened
goroutine 6 (req-id-123123) [running]:
net/http.(*conn).serve.func1(0xc00009cb40)
/Users/cyberax/aurora/go/src/net/http/server.go:1772 +0x139
panic(0x127aa00, 0x133df70)
....
I have created a proof-of-concept implementation: https://github.com/Cyberax/go/commit/ee50b1e771a3afdb760f56b6d1b1603772e3b3f0 and for the Delve debugger: https://github.com/Cyberax/delve/commit/c69a60eba91723425619654dce55236376d3929d