otel
func GetTextMapPropagator
1
|
func GetTextMapPropagator() propagation.TextMapPropagator
|
GetTextMapPropagator返回全局TextMapPropagator。如果未设置任何内容,则返回No-Op TextMapPropagator。
func GetTracerProvider
1
|
func GetTracerProvider() trace.TracerProvider
|
GetTracerProvider返回已注册的全局跟踪提供程序。如果未注册任何内容,则返回NoopTracerProvider的实例。
使用跟踪提供程序创建一个命名的tracer。例如
1
|
tracer := global.GetTracerProvider().Tracer("example.com/foo")
|
或者
1
|
tracer := global.Tracer("example.com/foo")
|
func Handle
Handle是ErrorHandler()的便捷函数。
func SetErrorHandler
1
|
func SetErrorHandler(h ErrorHandler)
|
SetErrorHandler将全局ErrorHandler设置为h。
func SetTextMapPropagator
1
|
func SetTextMapPropagator(propagator propagation.TextMapPropagator)
|
SetTextMapPropagator将propagator设置为全局TextMapPropagator。
func SetTracerProvider
1
|
func SetTracerProvider(tp trace.TracerProvider)
|
SetTracerProvider registers tp
as the global trace provider.
SetTracerProvider将tp注册为全局trace provider。
func Tracer
1
|
func Tracer(name string) trace.Tracer
|
tracer创建一个实现tracer接口的命名tracer。如果名称为空字符串,则提供程序将使用默认名称。
这是 GetTracerProvider().Tracer(name) 的缩写
func Version
版本是正在使用的OpenTelemetry的当前发行版本。
type ErrorHandler
1
2
3
4
5
|
type ErrorHandler interface {
// Handle handles any error deemed irremediable by an OpenTelemetry
// component.
Handle(error)
}
|
ErrorHandler处理不可补救的事件。
func GetErrorHandler
1
|
func GetErrorHandler() ErrorHandler
|
GetErrorHandler返回全局ErrorHandler实例。如果没有设置ErrorHandler实例(SetErrorHandler),则返回将错误记录到STDERR的默认ErrorHandler。
trace
概述
trace 提供了OpenTelemetry APItrace部分的实现。
该程序包目前处于GA预发布阶段。在我们trace不断发展的OpenTelemetry规范和用户反馈的过程中,向后不兼容的更改可能会在后续的次要版本中引入。
为了参与分布式trace,需要为要执行的操作创建span,作为trace工作流的一部分。它是最简单的形式:
1
2
3
4
5
6
7
8
9
10
11
12
|
var tracer trace.Tracer
func init() {
tracer = otel.Tracer("instrumentation/package/name")
}
func operation(ctx context.Context) {
var span trace.Span
ctx, span = tracer.Start(ctx, "operation")
defer span.End()
// ...
}
|
Tracer是仪器特有的,用于创建span。仪器应设计为接受TracerProvider,从中可以创建自己的唯一Tracer。或者,可以将go.opentelemetry.io/otel软件包中已注册的全局TracerProvider用作默认值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
const (
name = "instrumentation/package/name"
version = "0.1.0"
)
type Instrumentation struct {
tracer trace.Tracer
}
func NewInstrumentation(tp trace.TracerProvider) *Instrumentation {
if tp == nil {
tp = otel.TracerProvider()
}
return &Instrumentation{
tracer: tp.Tracer(name, trace.WithInstrumentationVersion(version)),
}
}
func operation(ctx context.Context, inst *Instrumentation) {
var span trace.Span
ctx, span = inst.tracer.Start(ctx, "operation")
defer span.End()
// ...
}
|
常量
1
2
3
4
5
6
7
8
9
10
11
|
const (
// FlagsSampled is a bitmask with the sampled bit set. A SpanContext
// with the sampling bit set means the span is sampled.
FlagsSampled = byte(0x01)
// FlagsDeferred is a bitmask with the deferred bit set. A SpanContext
// with the deferred bit set means the sampling decision has been
// defered to the receiver.
FlagsDeferred = byte(0x02)
// FlagsDebug is a bitmask with the debug bit set.
FlagsDebug = byte(0x04)
)
|
func ContextWithRemoteSpanContext
1
|
func ContextWithRemoteSpanContext(parent context.Context, remote SpanContext) context.Context
|
ContextWithRemoteSpanContext返回具有远程集的父级副本作为远程span上下文。
func ContextWithSpan
1
|
func ContextWithSpan(parent context.Context, span Span) context.Context
|
ContextWithSpan返回span设置为current的parent的副本。
type Event
1
2
3
4
5
6
7
8
9
10
|
type Event struct {
// Name is the name of this event
Name string
// Attributes describe the aspects of the event.
Attributes []attribute.KeyValue
// Time at which this event was recorded.
Time time.Time
}
|
Event是Span一生中发生的一件事情。
type EventOption
1
2
3
4
|
type EventOption interface {
ApplyEvent(*SpanConfig)
// contains filtered or unexported methods
}
|
EventOption将spanEvent选项应用于SpanConfig。
type InstrumentationOption
1
2
3
|
type InstrumentationOption interface {
TracerOption
}
|
InstrumentationOption是用于应用特定于工具的选项的接口。
func WithInstrumentationVersion
1
|
func WithInstrumentationVersion(version string) InstrumentationOption
|
WithInstrumentationVersion设置检测版本。
type LifeCycleOption
1
2
3
4
|
type LifeCycleOption interface {
SpanOption
EventOption
}
|
LifeCycleOption将span生命周期选项应用于SpanConfig。这些选项设置与整个生命周期中的Event相关的值,例如开始,结束,遇到错误以及其他用户定义的值得注意的Event。
func WithAttributes
1
|
func WithAttributes(attributes ...attribute.KeyValue) LifeCycleOption
|
WithAttributes添加与span生命周期Event相关的属性。这些属性用于描述当向Span的开始或结束Event提供此选项时Span代表的工作。否则,这些属性将提供有关正在记录的Event的其他信息(例如,错误,状态更改,处理进度,系统Event)。
如果传递了这些选项中的多个,则每个后续选项的属性将扩展属性,而不是覆盖属性。不能保证结果属性的唯一性。
func WithTimestamp
1
|
func WithTimestamp(t time.Time) LifeCycleOption
|
WithTimestamp设置span生命周期时刻(例如开始,停止,错误)的时间。
type Link
1
2
3
4
|
type Link struct {
SpanContext
Attributes []attribute.KeyValue
}
|
Link是两个Span之间的关系。关系可以在同一Trace内,也可以在不同Trace之间。
例如,在以下情况下使用Link:
- 批处理:一批操作可能包含一些操作,与一个或多个Trace/span相关联。由于只能有一个父SpanContext,则使用Link来保持对批处理中所有操作的SpanContext。
- Public Endpoint:针对一个传入客户端请求的SpanContext。公共端点应被视为不受信任。在这种情况下,以自己的身份进行trace,并需要创建抽样决策,但是在某些情况下,此新trace需要与原始trace相关形式。Link用于保留对原始SpanContext的引用,并且trace关系。
type Span
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
type Span interface {
// Tracer returns the Tracer that created the Span. Tracer MUST NOT be
// nil.
Tracer() Tracer
// End completes the Span. The Span is considered complete and ready to be
// delivered through the rest of the telemetry pipeline after this method
// is called. Therefore, updates to the Span are not allowed after this
// method has been called.
End(options ...SpanOption)
// AddEvent adds an event with the provided name and options.
AddEvent(name string, options ...EventOption)
// IsRecording returns the recording state of the Span. It will return
// true if the Span is active and events can be recorded.
IsRecording() bool
// RecordError records an error as a Span event.
RecordError(err error, options ...EventOption)
// SpanContext returns the SpanContext of the Span. The returned
// SpanContext is usable even after the End has been called for the Span.
SpanContext() SpanContext
// SetStatus sets the status of the Span in the form of a code and a
// message. SetStatus overrides the value of previous calls to SetStatus
// on the Span.
SetStatus(code codes.Code, msg string)
// SetName sets the Span name.
SetName(name string)
// SetAttributes sets kv as attributes of the Span. If a key from kv
// already exists for an attribute of the Span it will be overwritten with
// the value contained in kv.
SetAttributes(kv ...attribute.KeyValue)
}
|
span是trace的单个组成部分。它表示trace的工作流的单个命名和定时操作。trace用于创建span,然后由该span表示的操作可以在操作本身结束时正确结束span。
func SpanFromContext
1
|
func SpanFromContext(ctx context.Context) Span
|
SpanFromContext返回来自ctx的当前范围,如果未设置,则返回noop范围。
type SpanConfig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
type SpanConfig struct {
// Attributes describe the associated qualities of a Span.
Attributes []attribute.KeyValue
// Timestamp is a time in a Span life-cycle.
Timestamp time.Time
// Links are the associations a Span has with other Spans.
Links []Link
// NewRoot identifies a Span as the root Span for a new trace. This is
// commonly used when an existing trace crosses trust boundaries and the
// remote parent span context should be ignored for security.
NewRoot bool
// SpanKind is the role a Span has in a trace.
SpanKind SpanKind
}
|
SpanConfig是Span的一组选项。
func NewEventConfig
1
|
func NewEventConfig(options ...EventOption) *SpanConfig
|
NewEventConfig将所有EventOptions应用于返回的SpanConfig。如果未传递任何时间戳选项,则返回的SpanConfig会将Timestamp设置为呼叫时间,否则将不对返回的SpanConfig进行验证。
func NewSpanConfig
1
|
func NewSpanConfig(options ...SpanOption) *SpanConfig
|
NewSpanConfig将所有选项应用于返回的SpanConfig。对返回的SpanConfig不会执行任何验证(例如,不进行唯一性检查或数据绑定),它将留给SDK来执行此操作。
type SpanContext
1
2
3
|
type SpanContext struct {
// contains filtered or unexported fields
}
|
SpanContext包含有关Span的标识trace信息。
func NewSpanContext
1
|
func NewSpanContext(config SpanContextConfig) SpanContext
|
NewSpanContext使用提供的SpanContextConfig中的值构造一个SpanContext。
func RemoteSpanContextFromContext
1
|
func RemoteSpanContextFromContext(ctx context.Context) SpanContext
|
RemoteSpanContextFromContext从ctx返回远程span上下文。
func SpanContextFromContext
1
|
func SpanContextFromContext(ctx context.Context) SpanContext
|
SpanContextFromContext从ctx返回当前的SpanContext,如果未设置,则返回一个空的SpanContext。
func (SpanContext) Equal
1
|
func (sc SpanContext) Equal(other SpanContext) bool
|
Equal是确定两个SpanContext值是否相等的谓词。
func (SpanContext) HasSpanID
1
|
func (sc SpanContext) HasSpanID() bool
|
HasSpanID检查SpanContext是否具有有效的SpanID。
func (SpanContext) HasTraceID
1
|
func (sc SpanContext) HasTraceID() bool
|
HasTraceID检查SpanContext是否具有有效的TraceID。
func (SpanContext) IsDebug
1
|
func (sc SpanContext) IsDebug() bool
|
如果在trace标志中设置了调试位,则IsDebug返回。
func (SpanContext) IsDeferred
1
|
func (sc SpanContext) IsDeferred() bool
|
如果在trace标志中设置了延迟位,则IsDeferred返回。
func (SpanContext) IsRemote
1
|
func (sc SpanContext) IsRemote() bool
|
IsRemote指示SpanContext是否表示远程创建的Span。
func (SpanContext) IsSampled
1
|
func (sc SpanContext) IsSampled() bool
|
如果在trace标志中设置了采样位,则IsSampled返回。
func (SpanContext) IsValid
1
|
func (sc SpanContext) IsValid() bool
|
如果SpanContext有效,则IsValid返回。有效的span上下文具有有效的TraceID和SpanID。
func (SpanContext) MarshalJSON
1
|
func (sc SpanContext) MarshalJSON() ([]byte, error)
|
MarshalJSON实现了一个自定义的marshal函数来对SpanContext进行编码。
func (SpanContext) SpanID
1
|
func (sc SpanContext) SpanID() SpanID
|
SpanID从SpanContext返回SpanID。
func (SpanContext) TraceFlags
1
|
func (sc SpanContext) TraceFlags() byte
|
TraceFlags从SpanContext返回标志。
func (SpanContext) TraceID
1
|
func (sc SpanContext) TraceID() TraceID
|
TraceID从SpanContext返回TraceID。
func (SpanContext) TraceState
1
|
func (sc SpanContext) TraceState() TraceState
|
TraceState从SpanContext返回TraceState。
func (SpanContext) WithRemote
1
|
func (sc SpanContext) WithRemote(remote bool) SpanContext
|
WithRemote返回sc的副本,其中Remote属性设置为remote。
func (SpanContext) WithSpanID
1
|
func (sc SpanContext) WithSpanID(spanID SpanID) SpanContext
|
WithSpanID返回替换了SpanID的新SpanContext。
func (SpanContext) WithTraceFlags
1
|
func (sc SpanContext) WithTraceFlags(flags byte) SpanContext
|
WithTraceFlags返回替换了TraceFlags的新SpanContext。
func (SpanContext) WithTraceID
1
|
func (sc SpanContext) WithTraceID(traceID TraceID) SpanContext
|
WithTraceID返回替换了TraceID的新SpanContext。
func (SpanContext) WithTraceState
1
|
func (sc SpanContext) WithTraceState(state TraceState) SpanContext
|
WithTraceState返回替换了TraceState的新SpanContext。
type SpanContextConfig
1
2
3
4
5
6
7
|
type SpanContextConfig struct {
TraceID TraceID
SpanID SpanID
TraceFlags byte
TraceState TraceState
Remote bool
}
|
SpanContextConfig包含可用于构造不可变SpanContext的可变字段。
type SpanID
SpanID是trace中span的唯一标识。
func SpanIDFromHex
1
|
func SpanIDFromHex(h string) (SpanID, error)
|
如果SpanIDFromHex符合w3c trace-context规范,则从十六进制字符串返回SpanID。详情请参阅https://www.w3.org/TR/trace-context/#parent-id
func (SpanID) IsValid
1
|
func (s SpanID) IsValid() bool
|
IsValid检查SpanID是否有效。有效的SpanID不仅由零组成。
func (SpanID) MarshalJSON
1
|
func (s SpanID) MarshalJSON() ([]byte, error)
|
MarshalJSON实现了一个自定义的marshal函数,以将SpanID编码为十六进制字符串。
func (SpanID) String
1
|
func (s SpanID) String() string
|
String返回SpanID的十六进制字符串表示形式
type SpanKind
SpanKind是Span在trace中扮演的角色。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
const (
// SpanKindUnspecified is an unspecified SpanKind and is not a valid
// SpanKind. SpanKindUnspecified should be replaced with SpanKindInternal
// if it is received.
SpanKindUnspecified SpanKind = 0
// SpanKindInternal is a SpanKind for a Span that represents an internal
// operation within an application.
SpanKindInternal SpanKind = 1
// SpanKindServer is a SpanKind for a Span that represents the operation
// of handling a request from a client.
SpanKindServer SpanKind = 2
// SpanKindClient is a SpanKind for a Span that represents the operation
// of client making a request to a server.
SpanKindClient SpanKind = 3
// SpanKindProducer is a SpanKind for a Span that represents the operation
// of a producer sending a message to a message broker. Unlike
// SpanKindClient and SpanKindServer, there is often no direct
// relationship between this kind of Span and a SpanKindConsumer kind. A
// SpanKindProducer Span will end once the message is accepted by the
// message broker which might not overlap with the processing of that
// message.
SpanKindProducer SpanKind = 4
// SpanKindConsumer is a SpanKind for a Span that represents the operation
// of a consumer receiving a message from a message broker. Like
// SpanKindProducer Spans, there is often no direct relationship between
// this Span and the Span that produced the message.
SpanKindConsumer SpanKind = 5
)
|
为方便起见,这些匹配原型定义,请参见 https://github.com/open-telemetry/opentelemetry-proto/blob/30d237e1ff3ab7aa50e0922b5bebdd93505090af/opentelemetry/proto/trace/v1/trace.proto#L101-L129
未指定的值不是有效的“ SpanKind”。使用“ ValidateSpanKind()”将span类型强制为有效值。
func ValidateSpanKind
1
|
func ValidateSpanKind(spanKind SpanKind) SpanKind
|
ValidateSpanKind返回有效的span种类值。这会将无效值强制为默认值SpanKindInternal。
func (SpanKind) String
1
|
func (sk SpanKind) String() string
|
String以小写形式返回SpanKind的指定名称。
type SpanOption
1
2
3
4
|
type SpanOption interface {
ApplySpan(*SpanConfig)
// contains filtered or unexported methods
}
|
SpanOption将选项应用于SpanConfig。
func WithLinks
1
|
func WithLinks(links ...Link) SpanOption
|
WithLinks将Link添加到span。这些Link将添加到现有的SpanLink中,即不会被覆盖。
func WithNewRoot
1
|
func WithNewRoot() SpanOption
|
WithNewRoot指定将span视为根span。定义span的trace标识符时,将忽略任何现有的父span上下文。
func WithSpanKind
1
|
func WithSpanKind(kind SpanKind) SpanOption
|
WithSpanKind设置span的SpanKind。
type TraceID
TraceID是trace的唯一标识。nolint:golint
func TraceIDFromHex
1
|
func TraceIDFromHex(h string) (TraceID, error)
|
如果TraceIDFromHex符合W3C trace-context规范,则从十六进制字符串返回TraceID。详情请参阅 https://www.w3.org/TR/trace-context/#trace-id nolint:golint
func (TraceID) IsValid
1
|
func (t TraceID) IsValid() bool
|
IsValid检查traceTraceID是否有效。有效的traceID不仅由零组成。
func (TraceID) MarshalJSON
1
|
func (t TraceID) MarshalJSON() ([]byte, error)
|
MarshalJSON实现了一个自定义的marshal函数,将TraceID编码为十六进制字符串。
func (TraceID) String
1
|
func (t TraceID) String() string
|
String返回TraceID的十六进制字符串表示形式
type TraceState
1
2
3
|
type TraceState struct {
// contains filtered or unexported fields
}
|
TraceState在不同的分布式trace系统中提供了其他特定于供应商的trace标识信息。它表示由键/值对组成的不可变列表。列表中最多可以包含32个条目。
每个列表成员的键和值必须根据W3Ctrace上下文规范有效(请参阅https://www.w3.org/TR/trace-context-1/#key和https://www.w3.org/ TR / trace-context-1 /#value )。
根据W3Ctrace上下文规范,trace状态必须始终有效。所有变异操作都会验证其输入,并在参数有效的情况下返回新的TraceState。
func TraceStateFromKeyValues
1
|
func TraceStateFromKeyValues(kvs ...attribute.KeyValue) (TraceState, error)
|
TraceStateFromKeyValues是从提供的键/值对创建新的TraceState的便捷方法。
func (TraceState) Delete
1
|
func (ts TraceState) Delete(key attribute.Key) (TraceState, error)
|
删除将从trace状态中删除指定的条目。
func (TraceState) Get
1
|
func (ts TraceState) Get(key attribute.Key) attribute.Value
|
Get从trace状态返回给定键的值。如果找不到密钥或提供的密钥无效,则返回一个空值。
func (TraceState) Insert
1
|
func (ts TraceState) Insert(entry attribute.KeyValue) (TraceState, error)
|
如果不存在,Insert会添加一个新的键/值。否则,更新现有条目。根据W3Ctrace上下文规范要求,新的或更新的条目总是插入到TraceState的开头,即左侧。
func (TraceState) IsEmpty
1
|
func (ts TraceState) IsEmpty() bool
|
如果TraceState不包含任何条目,则IsEmpty返回true
func (TraceState) MarshalJSON
1
|
func (ts TraceState) MarshalJSON() ([]byte, error)
|
MarshalJSON实现了自定义的marshal函数以对trace状态进行编码。
func (TraceState) String
1
|
func (ts TraceState) String() string
|
String根据W3Ctrace上下文规范将trace状态作为有效的字符串返回。
type Tracer
1
2
3
4
|
type Tracer interface {
// Start creates a span.
Start(ctx context.Context, spanName string, opts ...SpanOption) (context.Context, Span)
}
|
Tracer是Spans的创建者。
type TracerConfig
1
2
3
4
5
|
type TracerConfig struct {
// InstrumentationVersion is the version of the library providing
// instrumentation.
InstrumentationVersion string
}
|
TracerConfig是Tracer的一组选项。
func NewTracerConfig
1
|
func NewTracerConfig(options ...TracerOption) *TracerConfig
|
NewTracerConfig将所有选项应用于返回的TracerConfig。
type TracerOption
1
2
3
4
|
type TracerOption interface {
ApplyTracer(*TracerConfig)
// contains filtered or unexported methods
}
|
TracerOption将选项应用于TracerConfig。
type TracerProvider
1
2
3
4
5
6
7
8
9
10
11
|
type TracerProvider interface {
// Tracer creates an implementation of the Tracer interface.
// The instrumentationName must be the name of the library providing
// instrumentation. This name may be the same as the instrumented code
// only if that code provides built-in instrumentation. If the
// instrumentationName is empty, then a implementation defined default
// name will be used instead.
//
// This method must be concurrency safe.
Tracer(instrumentationName string, opts ...TracerOption) Tracer
}
|
TracerProvider提供对仪器trace的访问。
func NewNoopTracerProvider
1
|
func NewNoopTracerProvider() TracerProvider
|
NewNoopTracerProvider返回不执行任何操作的TracerProvider实现。从返回的TracerProvider创建的Tracer和Span也不会执行任何操作。
exporter/trace/jaeger
func WithAgentEndpoint
1
|
func WithAgentEndpoint(agentEndpoint string) func() (batchUploader, error)
|
WithAgentEndpoint指示导出器将跨度发送到该地址的jaeger-agent。例如,localhost:6831。
func WithBufferMaxCount
1
|
func WithBufferMaxCount(bufferMaxCount int) func(o *options)
|
WithBufferMaxCount定义可以在内存中缓冲的跟踪总数
func WithCollectorEndpoint
1
|
func WithCollectorEndpoint(collectorEndpoint string, options ...CollectorEndpointOption) func() (batchUploader, error)
|
WithCollectorEndpoint定义Jaeger HTTP Thrift收集器的完整URL。例如,http:// localhost:14268 / api / traces
func WithOnError
1
|
func WithOnError(onError func(err error)) func(o *options)
|
WithOnError设置在上载跨度数据时发生错误时要调用的挂钩。如果未设置自定义钩子,则会记录错误。
func WithPassword
1
|
func WithPassword(password string) func(o *CollectorEndpointOptions)
|
WithPassword设置需要基本身份验证时使用的密码。
func WithProcess
1
|
func WithProcess(process Process) func(o *options)
|
WithProcess使用有关导出过程的信息来设置过程。
func WithUsername
1
|
func WithUsername(username string) func(o *CollectorEndpointOptions)
|
WithUsername设置需要基本身份验证时要使用的用户名。
type CollectorEndpointOption
1
|
type CollectorEndpointOption func(o *CollectorEndpointOptions)
|
type CollectorEndpointOptions
1
2
3
|
type CollectorEndpointOptions struct {
// contains filtered or unexported fields
}
|
type EndpointOption
1
|
type EndpointOption func() (batchUploader, error)
|
type Exporter
1
2
3
|
type Exporter struct {
// contains filtered or unexported fields
}
|
导出器是trace.Exporter的实现,可将跨度上传到Jaeger。
func NewExporter
1
|
func NewExporter(endpointOption EndpointOption, opts ...Option) (*Exporter, error)
|
NewExporter返回trace.Exporter实现,该实现将收集的跨度导出到Jaeger。
func (*Exporter) ExportSpan
1
|
func (e *Exporter) ExportSpan(ctx context.Context, d *export.SpanData)
|
ExportSpan将SpanData导出到Jaeger。
func (*Exporter) Flush
1
|
func (e *Exporter) Flush()
|
刷新等待导出的跟踪范围上载。
如果您的程序即将结束并且您不想丢失最近的跨度,则此功能很有用。
type Option
1
|
type Option func(*options)
|
type Process
1
2
3
4
5
6
7
|
type Process struct {
// ServiceName is the Jaeger service name.
ServiceName string
// Tags are added to Jaeger Process exports
Tags []core.KeyValue
}
|
流程包含导出到jaeger的有关跟踪数据源的信息。