mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-03 08:10:28 +00:00
implement trace_id
This commit is contained in:
parent
8a3d16860c
commit
1148690440
3 changed files with 22 additions and 21 deletions
|
@ -20,7 +20,7 @@ func Errors(log zerolog.Logger) server.Middleware {
|
|||
var code int
|
||||
|
||||
log.Err(err).
|
||||
Str("trace_id", "TODO").
|
||||
Str("trace_id", server.GetTraceID(r.Context())).
|
||||
Msg("ERROR occurred")
|
||||
|
||||
switch {
|
||||
|
|
|
@ -8,12 +8,12 @@ import (
|
|||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type StatusRecorder struct {
|
||||
type statusRecorder struct {
|
||||
http.ResponseWriter
|
||||
Status int
|
||||
}
|
||||
|
||||
func (r *StatusRecorder) WriteHeader(status int) {
|
||||
func (r *statusRecorder) WriteHeader(status int) {
|
||||
r.Status = status
|
||||
r.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
@ -21,20 +21,21 @@ func (r *StatusRecorder) WriteHeader(status int) {
|
|||
func Logger(log zerolog.Logger) server.Middleware {
|
||||
return func(next server.Handler) server.Handler {
|
||||
return server.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
traceId := server.GetTraceID(r.Context())
|
||||
|
||||
log.Info().
|
||||
Str("trace_id", "TODO").
|
||||
Str("trace_id", traceId).
|
||||
Str("method", r.Method).
|
||||
Str("path", r.URL.Path).
|
||||
Str("remove_address", r.RemoteAddr).
|
||||
Msg("request started")
|
||||
|
||||
record := &StatusRecorder{ResponseWriter: w, Status: http.StatusOK}
|
||||
record := &statusRecorder{ResponseWriter: w, Status: http.StatusOK}
|
||||
|
||||
err := next.ServeHTTP(record, r)
|
||||
|
||||
log.Info().
|
||||
Str("trave_id", "TODO").
|
||||
Str("trace_id", traceId).
|
||||
Str("method", r.Method).
|
||||
Str("url", r.URL.Path).
|
||||
Str("remote_address", r.RemoteAddr).
|
||||
|
@ -42,7 +43,6 @@ func Logger(log zerolog.Logger) server.Middleware {
|
|||
Msg("request completed")
|
||||
|
||||
return err
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -70,19 +70,15 @@ func SugarLogger(log zerolog.Logger) server.Middleware {
|
|||
return func(next server.Handler) server.Handler {
|
||||
return server.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
record := &StatusRecorder{ResponseWriter: w, Status: http.StatusOK}
|
||||
record := &statusRecorder{ResponseWriter: w, Status: http.StatusOK}
|
||||
|
||||
err := next.ServeHTTP(record, r) // Blocks until the next handler returns.
|
||||
|
||||
scheme := "http"
|
||||
if r.TLS != nil {
|
||||
scheme = "https"
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s://%s%s %s", scheme, r.Host, r.RequestURI, r.Proto)
|
||||
url := fmt.Sprintf("%s %s", r.RequestURI, r.Proto)
|
||||
|
||||
log.Info().
|
||||
Msgf("%s %s %s",
|
||||
Str("trace_id", server.GetTraceID(r.Context())).
|
||||
Msgf("%s %s %s",
|
||||
bold(fmtCode(record.Status)),
|
||||
bold(orange(""+r.Method+"")),
|
||||
aqua(url),
|
||||
|
|
|
@ -3,6 +3,8 @@ package server
|
|||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type vkey int
|
||||
|
@ -16,6 +18,14 @@ type Values struct {
|
|||
TraceID string
|
||||
}
|
||||
|
||||
func GetTraceID(ctx context.Context) string {
|
||||
v, ok := ctx.Value(key).(Values)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return v.TraceID
|
||||
}
|
||||
|
||||
func (s *Server) toHttpHandler(handler Handler, mw ...Middleware) http.HandlerFunc {
|
||||
handler = wrapMiddleware(mw, handler)
|
||||
|
||||
|
@ -26,8 +36,7 @@ func (s *Server) toHttpHandler(handler Handler, mw ...Middleware) http.HandlerFu
|
|||
|
||||
// Add the trace ID to the context
|
||||
ctx = context.WithValue(ctx, key, Values{
|
||||
// TODO: Initialize a new trace ID
|
||||
TraceID: "00000000-0000-0000-0000-000000000000",
|
||||
TraceID: uuid.NewString(),
|
||||
})
|
||||
|
||||
err := handler.ServeHTTP(w, r.WithContext(ctx))
|
||||
|
@ -61,10 +70,6 @@ func (s *Server) handle(method, pattern string, handler Handler, mw ...Middlewar
|
|||
}
|
||||
}
|
||||
|
||||
func (s *Server) Handler(pattern string, handler Handler, mw ...Middleware) {
|
||||
s.mux.Handle(pattern, s.toHttpHandler(handler, mw...))
|
||||
}
|
||||
|
||||
func (s *Server) Get(pattern string, handler Handler, mw ...Middleware) {
|
||||
s.handle(http.MethodGet, pattern, handler, mw...)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue