From 9606dd8ca126e16c7fbae7f0018dd909e882f6d4 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 23 Jul 2018 16:31:12 +0100 Subject: [PATCH] Apply uncommitted changes from ptah --- .../matrix-org/dendrite/common/httpapi.go | 26 +++++++++++++++++++ .../dendrite/federationapi/routing/routing.go | 1 + 2 files changed, 27 insertions(+) diff --git a/src/github.com/matrix-org/dendrite/common/httpapi.go b/src/github.com/matrix-org/dendrite/common/httpapi.go index 46ff62df..2c2637df 100644 --- a/src/github.com/matrix-org/dendrite/common/httpapi.go +++ b/src/github.com/matrix-org/dendrite/common/httpapi.go @@ -13,6 +13,20 @@ import ( "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. func MakeAuthAPI( tracer opentracing.Tracer, metricsName string, data auth.Data, @@ -24,6 +38,11 @@ func MakeAuthAPI( 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 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) { span := tracer.StartSpan(metricsName) defer span.Finish() + + ext.HTTPUrl.Set(span, req.URL.String()) + ext.HTTPMethod.Set(span, req.Method) + req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span)) + rw := NewStatusCodeResponseWriter(w) + h.ServeHTTP(w, req) + ext.HTTPStatusCode.Set(span, uint16(rw.statusCode)) } return http.HandlerFunc(withSpan) diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go index 121d4afe..da72b8d9 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go @@ -61,6 +61,7 @@ func Setup( // {keyID} argument and always return a response containing all of the keys. v2keysmux.Handle("/server/{keyID}", localKeys).Methods(http.MethodGet) v2keysmux.Handle("/server/", localKeys).Methods(http.MethodGet) + v2keysmux.Handle("/server", localKeys).Methods(http.MethodGet) v1fedmux.Handle("/send/{txnID}/", common.MakeFedAPI( tracer, "federation_send", cfg.Matrix.ServerName, keys,