trc/trc.go

41 lines
845 B
Go
Raw Normal View History

2022-10-26 10:50:30 +03:00
package trc
import (
"net/http"
"git.bit5.ru/backend/errors"
"github.com/google/uuid"
2022-11-03 16:53:58 +03:00
"github.com/go-logr/logr"
2022-10-26 10:50:30 +03:00
)
type TraceId string
func NewTraceId() TraceId {
return TraceId(uuid.New().String())
}
type SpanId string
func NewSpanId() SpanId {
return SpanId(uuid.New().String())
}
//-----------------------------------------------------------------------------
const TraceIdHeaderName = "trace-id"
func RequestTraceId(req *http.Request) (TraceId, error) {
traceIdStr := req.Header.Get(TraceIdHeaderName)
if len(traceIdStr) == 0 {
return "", errors.New("Trace id must be specified.")
}
return TraceId(traceIdStr), nil
}
//-----------------------------------------------------------------------------
2022-11-03 16:53:58 +03:00
func NewTraceLogger(logger logr.Logger, traceId TraceId) logr.Logger {
return logger.WithValues("trace_id", traceId)
2022-10-26 10:50:30 +03:00
}