Skip to content

CaptureMetrics reports 200 after a zero-byte ReadFrom #42

Open

Description

@jgehrcke

With httpsnoop v1.0.4, this handler sends HTTP 502 to the client, but CaptureMetrics reports status code 200:

  http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      _, err := io.Copy(w, iotest.ErrReader(errors.New("upstream failed")))
      if err != nil {
          http.Error(w, err.Error(), http.StatusBadGateway)
      }
  })

(Wrap it with CaptureMetrics and serve through a net/http server for a repro)

The ReadFrom hook sets headerWritten=true even when zero bytes were copied:

ReadFrom: func(next ReadFromFunc) ReadFromFunc {
return func(src io.Reader) (int64, error) {
n, err := next(src)
headerWritten = true
m.Written += n
return n, err
}

Consequently, e.g. a subsequent 5xx response emission or a 204 response emission would be tracked with a status code of 200 (which is the initial value, see here).

As a solution, we could set headerWritten only when n > 0,

We probably want regression tests for two cases, for example:

  • source returns an error before producing any bytes; the handler then sends 502, the client receives it, and httpsnoop captures the 502
  • source reaches EOF without producing any bytes; the handler then sends 204, the client receives it, and httpsnoop captures the 204

Related: #32, #38

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions