trc/trc.go

43 lines
898 B
Go

package trc
import (
"fmt"
"net/http"
"git.bit5.ru/backend/colog"
"git.bit5.ru/backend/errors"
"github.com/google/uuid"
)
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 *colog.CoLog, traceId TraceId) *colog.CoLog {
prefix := fmt.Sprintf("[trace_id:%s]", traceId)
return logger.Clone().AddPrefix(prefix)
}