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