-
Notifications
You must be signed in to change notification settings - Fork 0
/
httprouter_server.go
81 lines (67 loc) · 2.06 KB
/
httprouter_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"os"
"time"
"github.com/julienschmidt/httprouter"
"github.com/pinpoint-apm/pinpoint-go-agent"
"github.com/pinpoint-apm/pinpoint-go-agent/plugin/httprouter"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
tracer := pinpoint.FromContext(r.Context())
func() {
defer tracer.NewSpanEvent("func_1").EndSpanEvent()
func() {
defer tracer.NewSpanEvent("func_2").EndSpanEvent()
}()
time.Sleep(1 * time.Second)
}()
fmt.Fprint(w, "Welcome!\n")
}
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
sleep()
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}
func blog(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
sleep()
fmt.Fprintf(w, "blog: %s, %s\n", ps.ByName("category"), ps.ByName("post"))
}
func files(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
sleep()
fmt.Fprintf(w, "files: %s\n", ps.ByName("filepath"))
}
func sleep() {
seed := rand.NewSource(time.Now().UnixNano())
random := rand.New(seed).Intn(10000)
time.Sleep(time.Duration(random+1) * time.Millisecond)
}
func main() {
opts := []pinpoint.ConfigOption{
pinpoint.WithAppName("GoHttpRouterTest"),
pinpoint.WithHttpUrlStatEnable(true),
pinpoint.WithConfigFile(os.Getenv("HOME") + "/tmp/pinpoint-config.yaml"),
}
cfg, _ := pinpoint.NewConfig(opts...)
agent, err := pinpoint.NewAgent(cfg)
if err != nil {
log.Fatalf("pinpoint agent start fail: %v", err)
}
defer agent.Shutdown()
//router := httprouter.New()
//router.GET("/", pphttprouter.WrapHandle(Index))
//router.GET("/hello/:name", Hello)
router := pphttprouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)
router.GET("/blog/:category/:post", blog)
router.GET("/files/*filepath", files)
router.HandlerFunc("GET", "/user/:name/age/:old", func(w http.ResponseWriter, r *http.Request) {
sleep()
ps := httprouter.ParamsFromContext(r.Context())
fmt.Fprintf(w, "user: %s, %s\n", ps.ByName("name"), ps.ByName("old"))
})
log.Fatal(http.ListenAndServe(":8000", router))
}