07-15
今日已办
OpenTelemetry-SDK
Trace
TraceProvider
// TracerProvider 是它提供的 Tracers 中所有 Span 的收集目的地,它代表了一个独特的遥测收集管道。该管道的定义方式(即如何收集、处理这些 Span 以及将其导出到何处)取决于其实现
type TracerProvider interface {
// 根据名称、选项返回唯一Tracer
// TracerOption
// WithInstrumentationVersion sets the instrumentation version.
// WithInstrumentationAttributes sets the instrumentation attributes.
// WithSchemaURL sets the schema URL for the Tracer.
Tracer(name string, options ...TracerOption) Tracer
}
// This is short for GetTracerProvider().Tracer(name, opts...)
func Tracer(name string, opts ...trace.TracerOption) trace.Tracer {
return GetTracerProvider().Tracer(name, opts...)
}
// GetTracerProvider returns the registered global trace provider.
func GetTracerProvider() trace.TracerProvider {
return global.TracerProvider()
}
// SetTracerProvider registers `tp` as the global trace provider.
func SetTracerProvider(tp trace.TracerProvider) {
global.SetTracerProvider(tp)
}
Tracer
// Tracer is the creator of Spans.
type Tracer interface {
// Start
// 1.创建span和包含该span的context
// 2.如果ctx包含span,则新span为子span,否则为根span;可以通过WithNewRoot()覆盖使得该span为根span
// 3.WithAttributes() 提供所有已知的 Span 属性,采样器只能访问创建 Span 时提供的属性
// 4.任何创建的 Span 也必须结束
// SpanStartOption
// WithLinks adds links to a Span.
// WithNewRoot specifies that the Span should be treated as a root Span.
// WithSpanKind sets the SpanKind of a Span.
Start(ctx context.Context, spanName string, opts ...SpanStartOption) (context.Context, Span)
}
Span
// Span is the individual component of a trace.
// 表示追踪的单个命名和定时操作的工作流。Tracer用于创建Span
type Span interface {
// End completes the Span.
// WithStackTrace sets the flag to capture the error with stack trace (e.g. true, false).
End(options ...SpanEndOption)
// AddEvent adds an event with the provided name and options.
AddEvent(name string, options ...EventOption)
// IsRecording returns the recording state of the Span.
// true : span is active & events can be recorded
IsRecording() bool
// RecordError will record err as an exception span event for this span.
RecordError(err error, options ...EventOption)
// SpanContext returns the SpanContext of the Span. 即时结束也可以获取
SpanContext() SpanContext
// SetStatus sets the status of the Span in the form of a code and a
// description, provided the status hasn't already been set to a higher
// value before (OK > Error > Unset). 错误才有描述
SetStatus(code codes.Code, description string)
// SetName sets the Span name.
SetName(name string)
// SetAttributes sets kv as attributes of the Span. overwritten
SetAttributes(kv ...attribute.KeyValue)
// TracerProvider returns a TracerProvider that can be used to generate
// additional Spans on the same telemet