trc/trc.go

41 lines
845 B
Go

package trc
import (
"net/http"
"git.bit5.ru/backend/errors"
"github.com/google/uuid"
"github.com/go-logr/logr"
)
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
}
//-----------------------------------------------------------------------------
func NewTraceLogger(logger logr.Logger, traceId TraceId) logr.Logger {
return logger.WithValues("trace_id", traceId)
}