trace: 需要首先初始化trace,trace可以理解为span构成的有向无环图。
span: span是具体业务的一种抽象
A trace is a directed acyclic graph of spans.
A span is a logical representation of some work done in your application.
Each span has these minimum attributes: an operation name, a start time, and a finish time.
In order to continue the trace over the process boundaries and RPC calls, we need a way to propagate the span context over the wire. The OpenTracing API provides two functions in the Tracer interface to do that, Inject(spanContext, format, carrier) and Extract(format, carrier).
format 和 carrier 就是附带的一些参数,这些参数会被下一个程序接收到。
format是这些参数的格式,在openntraceing中有3个标准的格式。
The format parameter refers to one of the three standard encodings the OpenTracing API defines:
TextMap where span context is encoded as a collection of string key-value pairs,
Binary where span context is encoded as an opaque byte array,
HTTPHeaders, which is similar to TextMap except that the keys must be safe to be used as HTTP headers.
carrier是附带的内容
The carrier is an abstraction over the underlying RPC framework. For example, a carrier for TextMap format is an interface that allows the tracer to write key-value pairs via Set(key, value) function, while a carrier for Binary format is simply an io.Writer.
//StartSpanFromContext 建立一个span,是父亲节点的一个child。 // If we think of the trace as a directed acyclic graph where nodes are the spans and edges are the causal relationships between them, then the ChildOf option is used to create one such edge between span and rootSpan. In the API the edges are represented by SpanReference type that consists of a SpanContext and a label. The SpanContext represents an immutable, thread-safe portion of the span that can be used to establish references or to propagate it over the wire. The label, or ReferenceType, describes the nature of the relationship. ChildOf relationship means that the rootSpan has a logical dependency on the child span before rootSpan can complete its operation. Another standard reference type in OpenTracing is FollowsFrom, which means the rootSpan is the ancestor in the DAG, but it does not depend on the completion of the child span, for example if the child represents a best-effort, fire-and-forget cache write. funcformatString(ctx context.Context, helloTo string)string { span, _ := opentracing.StartSpanFromContext(ctx, "formatString") defer span.Finish()