You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/pkg/net/trace/span.go

138 lines
2.9 KiB

6 years ago
package trace
import (
"fmt"
"time"
protogen "github.com/bilibili/kratos/pkg/net/trace/proto"
6 years ago
)
const (
_maxChilds = 1024
_maxTags = 128
_maxLogs = 256
)
6 years ago
var _ Trace = &Span{}
6 years ago
6 years ago
// Span is a trace span.
type Span struct {
6 years ago
dapper *dapper
context spanContext
operationName string
startTime time.Time
duration time.Duration
tags []Tag
logs []*protogen.Log
childs int
}
6 years ago
func (s *Span) Name() string {
return s.operationName
}
func (s *Span) StartTime() time.Time {
return s.startTime
}
func (s *Span) Duration() time.Duration {
return s.duration
}
func (s *Span) TraceID() string {
return s.context.String()
}
6 years ago
func (s *Span) Context() spanContext {
return s.context
6 years ago
}
func (s *Span) Tags() []Tag {
return s.tags
}
6 years ago
func (s *Span) Logs() []*protogen.Log {
return s.logs
}
6 years ago
func (s *Span) Fork(serviceName, operationName string) Trace {
6 years ago
if s.childs > _maxChilds {
// if child span more than max childs set return noopspan
return noopspan{}
}
s.childs++
// 为了兼容临时为 New 的 Span 设置 span.kind
return s.dapper.newSpanWithContext(operationName, s.context).SetTag(TagString(TagSpanKind, "client"))
}
6 years ago
func (s *Span) Follow(serviceName, operationName string) Trace {
6 years ago
return s.Fork(serviceName, operationName).SetTag(TagString(TagSpanKind, "producer"))
}
6 years ago
func (s *Span) Finish(perr *error) {
6 years ago
s.duration = time.Since(s.startTime)
if perr != nil && *perr != nil {
err := *perr
s.SetTag(TagBool(TagError, true))
s.SetLog(Log(LogMessage, err.Error()))
if err, ok := err.(stackTracer); ok {
s.SetLog(Log(LogStack, fmt.Sprintf("%+v", err.StackTrace())))
}
}
s.dapper.report(s)
}
6 years ago
func (s *Span) SetTag(tags ...Tag) Trace {
6 years ago
if !s.context.isSampled() && !s.context.isDebug() {
return s
}
if len(s.tags) < _maxTags {
s.tags = append(s.tags, tags...)
}
if len(s.tags) == _maxTags {
s.tags = append(s.tags, Tag{Key: "trace.error", Value: "too many tags"})
}
return s
}
// LogFields is an efficient and type-checked way to record key:value
// NOTE current unsupport
6 years ago
func (s *Span) SetLog(logs ...LogField) Trace {
6 years ago
if !s.context.isSampled() && !s.context.isDebug() {
return s
}
if len(s.logs) < _maxLogs {
s.setLog(logs...)
}
if len(s.logs) == _maxLogs {
s.setLog(LogField{Key: "trace.error", Value: "too many logs"})
}
return s
}
6 years ago
func (s *Span) setLog(logs ...LogField) Trace {
6 years ago
protoLog := &protogen.Log{
Timestamp: time.Now().UnixNano(),
Fields: make([]*protogen.Field, len(logs)),
}
for i := range logs {
protoLog.Fields[i] = &protogen.Field{Key: logs[i].Key, Value: []byte(logs[i].Value)}
}
s.logs = append(s.logs, protoLog)
return s
}
// Visit visits the k-v pair in trace, calling fn for each.
6 years ago
func (s *Span) Visit(fn func(k, v string)) {
6 years ago
fn(KratosTraceID, s.context.String())
}
// SetTitle reset trace title
6 years ago
func (s *Span) SetTitle(operationName string) {
6 years ago
s.operationName = operationName
}
6 years ago
func (s *Span) String() string {
6 years ago
return s.context.String()
}