Exemplo n.º 1
0
func (h *healthcheck) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	proxyWriter, ok := rw.(utils.ProxyResponseWriter)
	var accessLogRecord *schema.AccessLogRecord
	if ok {
		alr := proxyWriter.Context().Value("AccessLogRecord")
		if alr == nil {
			h.logger.Error("AccessLogRecord not set on context", errors.New("failed-to-access-log-record"))
		}
		accessLogRecord = alr.(*schema.AccessLogRecord)
	}
	if h.userAgent == "" || r.Header.Get("User-Agent") == h.userAgent {
		rw.Header().Set("Cache-Control", "private, max-age=0")
		rw.Header().Set("Expires", "0")

		draining := atomic.LoadInt32(h.heartbeatOK) == 0
		if !draining {
			rw.WriteHeader(http.StatusOK)
			rw.Write([]byte("ok\n"))
			r.Close = true
			if ok {
				accessLogRecord.StatusCode = http.StatusOK
			}
		} else {
			rw.WriteHeader(http.StatusServiceUnavailable)
			r.Close = true
			if ok {
				accessLogRecord.StatusCode = http.StatusServiceUnavailable
			}
		}
		return
	}
	next(rw, r)
}
				`app_id:"FakeApplicationId" ` +
				`app_index:"3"` +
				"\n"

			Expect(record.LogMessage()).To(Equal(recordString))
		})

		Context("with values missing", func() {
			BeforeEach(func() {
				record.Request.Header = http.Header{}
				record.RouteEndpoint = &route.Endpoint{
					ApplicationId:        "FakeApplicationId",
					PrivateInstanceIndex: "",
				}
				record.BodyBytesSent = 0
				record.StatusCode = 0
				record.FinishedAt = time.Time{}
				record.RequestBytesReceived = 0
			})
			It("makes a record", func() {
				recordString := "FakeRequestHost - " +
					"[2000-01-01T00:00:00.000+0000] " +
					`"FakeRequestMethod http://example.com/request FakeRequestProto" ` +
					`"-" ` +
					"0 " +
					"0 " +
					`"-" ` +
					`"-" ` +
					`"FakeRemoteAddr" ` +
					`"-" ` +
					`x_forwarded_for:"-" ` +