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
key := getWord()
url := fmt.Sprintf("http://irrelevant/%s", key)
req, _ := http.NewRequest("GET", url, nil)
rec := httptest.NewRecorder()
dst.ServeHTTP(rec, req)
case f
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
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: J2EE下使用AJAX(二) Prototype封装
需提前下载prototype.js并置于自己的项目中,本例实现的功能与上例类似,Prototype对XMLHttpRequest进行了封装,我们可以使用较为简单的方式发送请求。 2.1 Servlet 实际上与上例的Servlet完全一样。内容如下: pack…