description
    HTTP Server Timing

    elton可以非常方便的获取各中间件的处理时长,获取统计时长之后,则可方便的写入相关的统计数据或HTTP响应的Server-Timing了。

    如图所示在chrome中network面板所能看得到Server-Timing展示:

    Server Timing - 图1

    1. package main
    2. import (
    3. "github.com/vicanso/elton"
    4. "github.com/vicanso/elton/middleware"
    5. )
    6. func main() {
    7. e := elton.New()
    8. e.EnableTrace = true
    9. e.OnTrace(func(c *elton.Context, traceInfos elton.TraceInfos) {
    10. serverTiming := string(traceInfos.ServerTiming("elton-"))
    11. c.SetHeader(elton.HeaderServerTiming, serverTiming)
    12. })
    13. entry := func(c *elton.Context) (err error) {
    14. c.ID = "random id"
    15. c.NoCache()
    16. return c.Next()
    17. }
    18. e.Use(entry)
    19. // 设置中间件的名称,若不设置从runtime中获取
    20. // 对于公共的中间件,建议指定名称
    21. e.SetFunctionName(entry, "entry")
    22. fn := middleware.NewDefaultResponder()
    23. e.Use(fn)
    24. e.SetFunctionName(fn, "responder")
    25. e.GET("/", func(c *elton.Context) (err error) {
    26. c.Body = &struct {
    27. Name string `json:"name,omitempty"`
    28. Content string `json:"content,omitempty"`
    29. }{
    30. "tree.xie",
    31. "Hello, World!",
    32. }
    33. return
    34. })
    35. err := e.ListenAndServe(":3000")
    36. if err != nil {
    37. panic(err)
    38. }
    39. }
    1. curl -v 'http://127.0.0.1:3000/'
    2. * Trying 127.0.0.1...
    3. * TCP_NODELAY set
    4. * Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
    5. > GET / HTTP/1.1
    6. > Host: 127.0.0.1:3000
    7. > User-Agent: curl/7.54.0
    8. > Accept: */*
    9. >
    10. < HTTP/1.1 200 OK
    11. < Cache-Control: no-cache
    12. < Content-Length: 44
    13. < Content-Type: application/json; charset=utf-8
    14. < Server-Timing: elton-0;dur=0;desc="entry",elton-1;dur=0.03;desc="responder",elton-2;dur=0;desc="main.main.func3"
    15. < Date: Fri, 03 Jan 2020 13:08:50 GMT
    16. <
    17. * Connection #0 to host 127.0.0.1 left intact
    18. {"name":"tree.xie","content":"Hello, World!"}