Skip to content

Commit

Permalink
implements tracing id
Browse files Browse the repository at this point in the history
  • Loading branch information
BugRoger committed Dec 13, 2017
1 parent 0327395 commit 53fac3a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/api/handlers/get_openstack_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (d *getOpenstackMetadata) Handle(params operations.GetOpenstackMetadataPara
},
}

client, err := scoped.NewClient(authOptions, d.Logger)
client, err := scoped.NewClient(authOptions, getTracingLogger(params.HTTPRequest))
if err != nil {
return NewErrorResponse(&operations.GetOpenstackMetadataDefault{}, 500, err.Error())
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/api/handlers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package handlers

import (
"fmt"
"net/http"
"strings"

kitlog "github.com/go-kit/kit/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"

Expand Down Expand Up @@ -52,3 +54,11 @@ func klusterFromCRD(k *v1.Kluster) *models.Kluster {
Status: k.Status,
}
}

func getTracingLogger(request *http.Request) kitlog.Logger {
logger, ok := request.Context().Value("logger").(kitlog.Logger)
if !ok {
logger = kitlog.NewNopLogger()
}
return logger
}
6 changes: 5 additions & 1 deletion pkg/api/rest/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func setupGlobalMiddleware(handler http.Handler, rt *apipkg.Runtime) http.Handle
MaxAge: 600,
}).Handler

requestIDHandler := func(next http.Handler) http.Handler {
return logutil.RequestIDHandler(next)
}

loggingHandler := func(next http.Handler) http.Handler {
return logutil.LoggingHandler(rt.Logger, next)
}
Expand All @@ -99,5 +103,5 @@ func setupGlobalMiddleware(handler http.Handler, rt *apipkg.Runtime) http.Handle
})
}

return alice.New(loggingHandler, handlers.RootHandler, redocHandler, staticHandler, corsHandler).Then(handler)
return alice.New(requestIDHandler, loggingHandler, handlers.RootHandler, redocHandler, staticHandler, corsHandler).Then(handler)
}
5 changes: 5 additions & 0 deletions pkg/util/log/gophercloud.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package log

import (
"fmt"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -43,6 +44,10 @@ func (lrt *loggingRoundTripper) RoundTrip(request *http.Request) (response *http
"openstack_id", strings.Join(requestIds(response), ","))
}

if id := request.Context().Value(KubernikusRequestID); id != nil {
keyvals = append(keyvals, "id", fmt.Sprintf("%s", id))
}

keyvals = append(keyvals,
"took", time.Since(begin),
"v", 2,
Expand Down
39 changes: 36 additions & 3 deletions pkg/util/log/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,58 @@ package log

import (
"bufio"
"context"
"fmt"
"net"
"net/http"
"strings"
"time"

kitlog "github.com/go-kit/kit/log"
uuid "github.com/satori/go.uuid"
)

type key int

const (
KubernikusRequestID key = 0
)

func RequestIDHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, request *http.Request) {
if id := request.Context().Value(KubernikusRequestID); id == nil {
request = request.WithContext(context.WithValue(request.Context(), KubernikusRequestID, uuid.NewV4()))
}
next.ServeHTTP(rw, request)
})
}

func LoggingHandler(logger kitlog.Logger, next http.Handler) http.Handler {
logger = kitlog.With(logger, "api", "ingress")
ingress_logger := kitlog.With(logger, "api", "ingress")
return http.HandlerFunc(func(rw http.ResponseWriter, request *http.Request) {
wrapper := makeWrapper(rw)

id := ""
if reqId := request.Context().Value(KubernikusRequestID); reqId != nil {
id = fmt.Sprintf("%s", reqId)
logger = kitlog.With(logger, "id", id)
}
request = request.WithContext(context.WithValue(request.Context(), "logger", logger))

defer func(begin time.Time) {
log(logger, request,
var keyvals = make([]interface{}, 0, 4)

keyvals = append(keyvals,
"status", wrapper.Status(),
"size", wrapper.Size(),
"took", time.Since(begin))
"took", time.Since(begin),
)

if id != "" {
keyvals = append(keyvals, "id", id)
}

log(ingress_logger, request, keyvals...)
}(time.Now())

next.ServeHTTP(wrapper, request)
Expand Down

0 comments on commit 53fac3a

Please sign in to comment.