Apply uncommitted changes from ptah

This commit is contained in:
Andrew Morgan 2018-07-23 16:31:12 +01:00
parent 2c021484b7
commit 9606dd8ca1
2 changed files with 27 additions and 0 deletions

View file

@ -13,6 +13,20 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
) )
type statusCodeResponseWriter struct {
http.ResponseWriter
statusCode int
}
func NewStatusCodeResponseWriter(w http.ResponseWriter) *statusCodeResponseWriter {
return &statusCodeResponseWriter{w, http.StatusOK}
}
func (lrw *statusCodeResponseWriter) WriteHeader(code int) {
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)
}
// MakeAuthAPI turns a util.JSONRequestHandler function into an http.Handler which authenticates the request. // MakeAuthAPI turns a util.JSONRequestHandler function into an http.Handler which authenticates the request.
func MakeAuthAPI( func MakeAuthAPI(
tracer opentracing.Tracer, metricsName string, data auth.Data, tracer opentracing.Tracer, metricsName string, data auth.Data,
@ -24,6 +38,11 @@ func MakeAuthAPI(
return *err return *err
} }
// Add user information to opentracing span
span := opentracing.SpanFromContext(req.Context())
span.SetTag("matrix.user", device.UserID)
span.SetTag("matrix.device", device.ID)
return f(req, device) return f(req, device)
} }
return MakeExternalAPI(tracer, metricsName, h) return MakeExternalAPI(tracer, metricsName, h)
@ -36,8 +55,15 @@ func MakeExternalAPI(tracer opentracing.Tracer, metricsName string, f func(*http
withSpan := func(w http.ResponseWriter, req *http.Request) { withSpan := func(w http.ResponseWriter, req *http.Request) {
span := tracer.StartSpan(metricsName) span := tracer.StartSpan(metricsName)
defer span.Finish() defer span.Finish()
ext.HTTPUrl.Set(span, req.URL.String())
ext.HTTPMethod.Set(span, req.Method)
req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span)) req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span))
rw := NewStatusCodeResponseWriter(w)
h.ServeHTTP(w, req) h.ServeHTTP(w, req)
ext.HTTPStatusCode.Set(span, uint16(rw.statusCode))
} }
return http.HandlerFunc(withSpan) return http.HandlerFunc(withSpan)

View file

@ -61,6 +61,7 @@ func Setup(
// {keyID} argument and always return a response containing all of the keys. // {keyID} argument and always return a response containing all of the keys.
v2keysmux.Handle("/server/{keyID}", localKeys).Methods(http.MethodGet) v2keysmux.Handle("/server/{keyID}", localKeys).Methods(http.MethodGet)
v2keysmux.Handle("/server/", localKeys).Methods(http.MethodGet) v2keysmux.Handle("/server/", localKeys).Methods(http.MethodGet)
v2keysmux.Handle("/server", localKeys).Methods(http.MethodGet)
v1fedmux.Handle("/send/{txnID}/", common.MakeFedAPI( v1fedmux.Handle("/send/{txnID}/", common.MakeFedAPI(
tracer, "federation_send", cfg.Matrix.ServerName, keys, tracer, "federation_send", cfg.Matrix.ServerName, keys,