code: func (s *State) Middleware(c *router.Context, next router.Handler) {
  	state, settings := s.checkSettings(c.Context)
  	if settings.Enabled {
  		started := clock.Now(c.Context)
  		req := c.Request
  		userAgent, ok := req.Header["User-Agent"]
  		if !ok || len(userAgent) == 0 {
  			userAgent = []string{"Unknown"}
  		}
  		ctx := c.Context
  		contentLength := c.Request.ContentLength
  		nrw := newResponseWriter(c.Writer)
  		c.Writer = nrw
  		defer func() {
  			dur := clock.Now(ctx).Sub(started)
  			metric.UpdateServerMetrics(ctx, c.HandlerPath, nrw.Status(), dur,
  				contentLength, nrw.Size(), userAgent[0])
  		}()
  		next(c)
  		s.flushIfNeeded(ctx, req, state, settings)
  	} else {
  		next(c)
  	}
  }
docs: // Middleware is a middleware that collects request metrics and triggers metric
  // flushes.