func (s *server) traceHandler(h func(http.ResponseWriter, *http.Request)) http.Handler {
	return s.tc.HTTPHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ww := &proxyResponseWriter{w: w}
		span := trace.FromContext(r.Context())
		defer func() {
			code := ww.code
			if code == 0 {
				code = http.StatusOK
			}
			span.SetLabel("http/resp/status_code", fmt.Sprint(code))
			span.SetLabel("http/response/content_length", fmt.Sprint(ww.length))
			span.SetLabel("http/req/id", span.TraceID())
			span.SetLabel("app/version", version.Version())
			span.Finish()
		}()
		ww.Header().Set("X-Cloud-Trace-Context", span.TraceID())
		ww.Header().Set("X-App-Version", version.Version())
		h(ww, r)
	}))
}
traceHandler wraps the HTTP handler with tracing that automatically finishes
the span. It adds additional fields to the trace span about the response and
adds correlation header to the headers.
package main
import (
	"fmt"
	"net/http"
	"time"
	"cloud.google.com/go/trace"
	"github.com/ahmetb/coffeelog/version"
	"github.com/sirupsen/logrus"
)
type proxyResponseWriter struct {
	w      http.ResponseWriter
	code   int
	length int
}
func (p *proxyResponseWriter) Header() http.Header { return p.w.Header() }
func (p *proxyResponseWriter) WriteHeader(code int) { p.code = code; p.w.WriteHeader(code) }
func (p *proxyResponseWriter) Write(b []byte) (int, error) {
	n, err := p.w.Write(b)
	p.length += n
	return n, err
}