trc golang 进程请求追踪包
trc 是一个方便的golang 进程请求追踪框架,可以提供方便的请求追踪查看
参考使用
代码来说官方示例,完整的可以查看官方
package main
import (
"context"
"fmt"
"log"
"math/rand"
"net/http"
"net/http/httptest"
_ "net/http/pprof"
"strings"
"time"
"github.com/peterbourgon/trc/eztrc"
)
func main() {
// Create a `kv` service in memory.
kv := NewKV(NewStore())
// Serve the `kv` API over HTTP.
var apiHandler http.Handler
{
apiHandler = kv // `kv` implements http.Handler
apiHandler = eztrc.Middleware(apiCategory)(apiHandler) // create a trace for each API request
}
// Generate random get/set/del requests to the API handler.
go func() {
load(context.Background(), apiHandler)
}()
// Serve the trace API over HTTP.
var tracesHandler http.Handler
{
tracesHandler = eztrc.Handler() // this serves the singleton eztrc.Collector
tracesHandler = eztrc.Middleware(func(*http.Request) string { return "traces" })(tracesHandler) // create a trace for each trace request
}
// Here's how you would change the number of traces per category.
eztrc.Collector().Resize(context.Background(), 100)
// Create a single serve mux for both API endpoints.
mux := http.NewServeMux()
mux.Handle("/api", http.StripPrefix("/api", apiHandler)) // technically unnecessary as it's not used by the loader
mux.Handle("/traces", http.StripPrefix("/traces", tracesHandler))
// Run the server.
server := &http.Server{Addr: "localhost:8080", Handler: mux}
log.Printf("http://localhost:8080/traces")
log.Fatal(server.ListenAndServe())
}
func load(ctx context.Context, dst http.Handler) {
for ctx.Err() == nil {
f := rand.Float64()
switch {
case f < 0.6:
key := getWord()
url := fmt.Sprintf("http://irrelevant/%s", key)
req, _ := http.NewRequest("GET", url, nil)
rec := httptest.NewRecorder()
dst.ServeHTTP(rec, req)
case f < 0.9:
key := getWord()
val := getWord()
url := fmt.Sprintf("http://irrelevant/%s", key)
req, _ := http.NewRequest("PUT", url, strings.NewReader(val))
rec := httptest.NewRecorder()
dst.ServeHTTP(rec, req)
default:
key := getWord()
url := fmt.Sprintf("http://irrelevant/%s", key)
req, _ := http.NewRequest("DELETE", url, nil)
rec := httptest.NewRecorder()
dst.ServeHTTP(rec, req)
}
time.Sleep(5 * time.Millisecond)
}
}
效果
参考资料
https://github.com/peterbourgon/trc
https://github.com/peterbourgon/trc/tree/main/_examples/trc-basic
https://pkg.go.dev/golang.org/x/net/trace