go-server-timing

Go (golang) library for creating and consuming HTTP Server-Timing headers

  • 所有者: mitchellh/go-server-timing
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

HTTP Server-Timing for Go

Godoc

This is a library including middleware for using
HTTP Server-Timing with Go. This header
allows a server to send timing information from the backend, such as database
access time, file reads, etc. The timing information can be then be inspected
in the standard browser developer tools:

Server Timing Example

Features

  • Middleware for injecting the server timing struct into the request Context
    and writing the Server-Timing header.

  • Concurrency-safe structures for easily recording timings of multiple
    concurrency tasks.

  • Parse Server-Timing headers as a client.

  • Note: No browser properly supports sending the Server-Timing header as
    an HTTP Trailer so
    the Middleware only supports a normal header currently.

Browser Support

Browser support is required to view server timings easily. Because server
timings are sent as an HTTP header, there is no negative impact to sending
the header to unsupported browsers.

  • Chrome 65 or higher is required to properly display server timings
    in the devtools.

  • Firefox is pending with an open bug report (ID 1403051)

  • IE, Opera, and others are unknown at this time.

Usage

Example usage is shown below. A fully runnable example is available in
the example/ directory.

func main() {
	// Our handler. In a real application this might be your root router,
	// or some subset of your router. Wrapping this ensures that all routes
	// handled by this handler have access to the server timing header struct.
	var h http.Handler = http.HandlerFunc(handler)

	// Wrap our handler with the server timing middleware
	h = servertiming.Middleware(h, nil)

	// Start!
	http.ListenAndServe(":8080", h)
}

func handler(w http.ResponseWriter, r *http.Request) {
	// Get our timing header builder from the context
	timing := servertiming.FromContext(r.Context())

	// Imagine your handler performs some tasks in a goroutine, such as
	// accessing some remote service. timing is concurrency safe so we can
	// record how long that takes. Let's simulate making 5 concurrent requests
	// to various servicse.
	var wg sync.WaitGroup
	for i := 0; i < 5; i++ {
		wg.Add(1)
		name := fmt.Sprintf("service-%d", i)
		go func(name string) {
			// This creats a new metric and starts the timer. The Stop is
			// deferred so when the function exits it'll record the duration.
			defer timing.NewMetric(name).Start().Stop()
			time.Sleep(random(25, 75))
			wg.Done()
		}(name)
	}

	// Imagine this is just some blocking code in your main handler such
	// as a SQL query. Let's record that.
	m := timing.NewMetric("sql").WithDesc("SQL query").Start()
	time.Sleep(random(20, 50))
	m.Stop()

	// Wait for the goroutine to end
	wg.Wait()

	// You could continue recording more metrics, but let's just return now
	w.WriteHeader(200)
	w.Write([]byte("Done. Check your browser inspector timing details."))
}

func random(min, max int) time.Duration {
	return (time.Duration(rand.Intn(max-min) + min)) * time.Millisecond
}

主要指标

概览
名称与所有者mitchellh/go-server-timing
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2018-02-12 03:56:02
推送于2023-12-08 11:37:45
最后一次提交2020-11-07 21:50:52
发布数2
最新版本名称v1.0.1 (发布于 )
第一版名称v1.0.0 (发布于 )
用户参与
星数857
关注者数17
派生数36
提交数43
已启用问题?
问题数12
打开的问题数7
拉请求数10
打开的拉请求数2
关闭的拉请求数3
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?