2020-10-09 04:58:56 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
package trace // import "go.opentelemetry.io/otel/trace"
|
2020-10-09 04:58:56 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-10-09 04:58:56 +02:00
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// FlagsSampled is a bitmask with the sampled bit set. A SpanContext
|
|
|
|
// with the sampling bit set means the span is sampled.
|
2021-04-05 19:21:42 +02:00
|
|
|
FlagsSampled = TraceFlags(0x01)
|
2020-10-09 04:58:56 +02:00
|
|
|
|
2020-10-12 17:43:26 +02:00
|
|
|
errInvalidHexID errorConst = "trace-id and span-id can only contain [0-9a-f] characters, all lowercase"
|
2020-10-09 04:58:56 +02:00
|
|
|
|
2020-10-12 17:43:26 +02:00
|
|
|
errInvalidTraceIDLength errorConst = "hex encoded trace-id must have length equals to 32"
|
|
|
|
errNilTraceID errorConst = "trace-id can't be all zero"
|
2020-10-09 04:58:56 +02:00
|
|
|
|
2020-10-12 17:43:26 +02:00
|
|
|
errInvalidSpanIDLength errorConst = "hex encoded span-id must have length equals to 16"
|
|
|
|
errNilSpanID errorConst = "span-id can't be all zero"
|
2020-10-09 04:58:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type errorConst string
|
|
|
|
|
|
|
|
func (e errorConst) Error() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceID is a unique identity of a trace.
|
2021-06-08 19:10:01 +02:00
|
|
|
// nolint:revive // revive complains about stutter of `trace.TraceID`.
|
2020-10-09 04:58:56 +02:00
|
|
|
type TraceID [16]byte
|
|
|
|
|
|
|
|
var nilTraceID TraceID
|
|
|
|
var _ json.Marshaler = nilTraceID
|
|
|
|
|
|
|
|
// IsValid checks whether the trace TraceID is valid. A valid trace ID does
|
|
|
|
// not consist of zeros only.
|
|
|
|
func (t TraceID) IsValid() bool {
|
|
|
|
return !bytes.Equal(t[:], nilTraceID[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements a custom marshal function to encode TraceID
|
|
|
|
// as a hex string.
|
|
|
|
func (t TraceID) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(t.String())
|
|
|
|
}
|
|
|
|
|
2022-04-25 22:22:49 +02:00
|
|
|
// String returns the hex string representation form of a TraceID.
|
2020-10-09 04:58:56 +02:00
|
|
|
func (t TraceID) String() string {
|
|
|
|
return hex.EncodeToString(t[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpanID is a unique identity of a span in a trace.
|
|
|
|
type SpanID [8]byte
|
|
|
|
|
|
|
|
var nilSpanID SpanID
|
|
|
|
var _ json.Marshaler = nilSpanID
|
|
|
|
|
|
|
|
// IsValid checks whether the SpanID is valid. A valid SpanID does not consist
|
|
|
|
// of zeros only.
|
|
|
|
func (s SpanID) IsValid() bool {
|
|
|
|
return !bytes.Equal(s[:], nilSpanID[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements a custom marshal function to encode SpanID
|
|
|
|
// as a hex string.
|
|
|
|
func (s SpanID) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(s.String())
|
|
|
|
}
|
|
|
|
|
2022-04-25 22:22:49 +02:00
|
|
|
// String returns the hex string representation form of a SpanID.
|
2020-10-09 04:58:56 +02:00
|
|
|
func (s SpanID) String() string {
|
|
|
|
return hex.EncodeToString(s[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceIDFromHex returns a TraceID from a hex string if it is compliant with
|
|
|
|
// the W3C trace-context specification. See more at
|
|
|
|
// https://www.w3.org/TR/trace-context/#trace-id
|
2021-06-08 19:10:01 +02:00
|
|
|
// nolint:revive // revive complains about stutter of `trace.TraceIDFromHex`.
|
2020-10-09 04:58:56 +02:00
|
|
|
func TraceIDFromHex(h string) (TraceID, error) {
|
|
|
|
t := TraceID{}
|
|
|
|
if len(h) != 32 {
|
2020-10-12 17:43:26 +02:00
|
|
|
return t, errInvalidTraceIDLength
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := decodeHex(h, t[:]); err != nil {
|
|
|
|
return t, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !t.IsValid() {
|
2020-10-12 17:43:26 +02:00
|
|
|
return t, errNilTraceID
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpanIDFromHex returns a SpanID from a hex string if it is compliant
|
|
|
|
// with the w3c trace-context specification.
|
|
|
|
// See more at https://www.w3.org/TR/trace-context/#parent-id
|
|
|
|
func SpanIDFromHex(h string) (SpanID, error) {
|
|
|
|
s := SpanID{}
|
|
|
|
if len(h) != 16 {
|
2020-10-12 17:43:26 +02:00
|
|
|
return s, errInvalidSpanIDLength
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := decodeHex(h, s[:]); err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.IsValid() {
|
2020-10-12 17:43:26 +02:00
|
|
|
return s, errNilSpanID
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeHex(h string, b []byte) error {
|
|
|
|
for _, r := range h {
|
|
|
|
switch {
|
|
|
|
case 'a' <= r && r <= 'f':
|
|
|
|
continue
|
|
|
|
case '0' <= r && r <= '9':
|
|
|
|
continue
|
|
|
|
default:
|
2020-10-12 17:43:26 +02:00
|
|
|
return errInvalidHexID
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, err := hex.DecodeString(h)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(b, decoded)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 22:22:49 +02:00
|
|
|
// TraceFlags contains flags that can be set on a SpanContext.
|
2021-06-08 19:10:01 +02:00
|
|
|
type TraceFlags byte //nolint:revive // revive complains about stutter of `trace.TraceFlags`.
|
2021-04-05 19:21:42 +02:00
|
|
|
|
|
|
|
// IsSampled returns if the sampling bit is set in the TraceFlags.
|
|
|
|
func (tf TraceFlags) IsSampled() bool {
|
|
|
|
return tf&FlagsSampled == FlagsSampled
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSampled sets the sampling bit in a new copy of the TraceFlags.
|
|
|
|
func (tf TraceFlags) WithSampled(sampled bool) TraceFlags {
|
|
|
|
if sampled {
|
|
|
|
return tf | FlagsSampled
|
|
|
|
}
|
|
|
|
|
|
|
|
return tf &^ FlagsSampled
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements a custom marshal function to encode TraceFlags
|
|
|
|
// as a hex string.
|
|
|
|
func (tf TraceFlags) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(tf.String())
|
|
|
|
}
|
|
|
|
|
2022-04-25 22:22:49 +02:00
|
|
|
// String returns the hex string representation form of TraceFlags.
|
2021-04-05 19:21:42 +02:00
|
|
|
func (tf TraceFlags) String() string {
|
|
|
|
return hex.EncodeToString([]byte{byte(tf)}[:])
|
|
|
|
}
|
|
|
|
|
2021-03-09 18:17:29 +02:00
|
|
|
// SpanContextConfig contains mutable fields usable for constructing
|
|
|
|
// an immutable SpanContext.
|
|
|
|
type SpanContextConfig struct {
|
2020-10-09 04:58:56 +02:00
|
|
|
TraceID TraceID
|
|
|
|
SpanID SpanID
|
2021-04-05 19:21:42 +02:00
|
|
|
TraceFlags TraceFlags
|
2020-12-21 23:11:48 +02:00
|
|
|
TraceState TraceState
|
2021-03-18 18:05:37 +02:00
|
|
|
Remote bool
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
|
2021-03-09 18:17:29 +02:00
|
|
|
// NewSpanContext constructs a SpanContext using values from the provided
|
|
|
|
// SpanContextConfig.
|
|
|
|
func NewSpanContext(config SpanContextConfig) SpanContext {
|
|
|
|
return SpanContext{
|
|
|
|
traceID: config.TraceID,
|
|
|
|
spanID: config.SpanID,
|
|
|
|
traceFlags: config.TraceFlags,
|
|
|
|
traceState: config.TraceState,
|
2021-03-18 18:05:37 +02:00
|
|
|
remote: config.Remote,
|
2021-03-09 18:17:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpanContext contains identifying trace information about a Span.
|
|
|
|
type SpanContext struct {
|
|
|
|
traceID TraceID
|
|
|
|
spanID SpanID
|
2021-04-05 19:21:42 +02:00
|
|
|
traceFlags TraceFlags
|
2021-03-09 18:17:29 +02:00
|
|
|
traceState TraceState
|
2021-03-18 18:05:37 +02:00
|
|
|
remote bool
|
2021-03-09 18:17:29 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 16:53:26 +02:00
|
|
|
var _ json.Marshaler = SpanContext{}
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
// IsValid returns if the SpanContext is valid. A valid span context has a
|
|
|
|
// valid TraceID and SpanID.
|
|
|
|
func (sc SpanContext) IsValid() bool {
|
|
|
|
return sc.HasTraceID() && sc.HasSpanID()
|
|
|
|
}
|
|
|
|
|
2021-03-18 18:05:37 +02:00
|
|
|
// IsRemote indicates whether the SpanContext represents a remotely-created Span.
|
|
|
|
func (sc SpanContext) IsRemote() bool {
|
|
|
|
return sc.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithRemote returns a copy of sc with the Remote property set to remote.
|
|
|
|
func (sc SpanContext) WithRemote(remote bool) SpanContext {
|
|
|
|
return SpanContext{
|
|
|
|
traceID: sc.traceID,
|
|
|
|
spanID: sc.spanID,
|
|
|
|
traceFlags: sc.traceFlags,
|
|
|
|
traceState: sc.traceState,
|
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 18:17:29 +02:00
|
|
|
// TraceID returns the TraceID from the SpanContext.
|
|
|
|
func (sc SpanContext) TraceID() TraceID {
|
|
|
|
return sc.traceID
|
|
|
|
}
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
// HasTraceID checks if the SpanContext has a valid TraceID.
|
|
|
|
func (sc SpanContext) HasTraceID() bool {
|
2021-03-09 18:17:29 +02:00
|
|
|
return sc.traceID.IsValid()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTraceID returns a new SpanContext with the TraceID replaced.
|
|
|
|
func (sc SpanContext) WithTraceID(traceID TraceID) SpanContext {
|
|
|
|
return SpanContext{
|
|
|
|
traceID: traceID,
|
|
|
|
spanID: sc.spanID,
|
|
|
|
traceFlags: sc.traceFlags,
|
|
|
|
traceState: sc.traceState,
|
2021-03-18 18:05:37 +02:00
|
|
|
remote: sc.remote,
|
2021-03-09 18:17:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpanID returns the SpanID from the SpanContext.
|
|
|
|
func (sc SpanContext) SpanID() SpanID {
|
|
|
|
return sc.spanID
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasSpanID checks if the SpanContext has a valid SpanID.
|
|
|
|
func (sc SpanContext) HasSpanID() bool {
|
2021-03-09 18:17:29 +02:00
|
|
|
return sc.spanID.IsValid()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSpanID returns a new SpanContext with the SpanID replaced.
|
|
|
|
func (sc SpanContext) WithSpanID(spanID SpanID) SpanContext {
|
|
|
|
return SpanContext{
|
|
|
|
traceID: sc.traceID,
|
|
|
|
spanID: spanID,
|
|
|
|
traceFlags: sc.traceFlags,
|
|
|
|
traceState: sc.traceState,
|
2021-03-18 18:05:37 +02:00
|
|
|
remote: sc.remote,
|
2021-03-09 18:17:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceFlags returns the flags from the SpanContext.
|
2021-04-05 19:21:42 +02:00
|
|
|
func (sc SpanContext) TraceFlags() TraceFlags {
|
2021-03-09 18:17:29 +02:00
|
|
|
return sc.traceFlags
|
|
|
|
}
|
|
|
|
|
2021-04-05 19:21:42 +02:00
|
|
|
// IsSampled returns if the sampling bit is set in the SpanContext's TraceFlags.
|
|
|
|
func (sc SpanContext) IsSampled() bool {
|
|
|
|
return sc.traceFlags.IsSampled()
|
|
|
|
}
|
|
|
|
|
2021-03-09 18:17:29 +02:00
|
|
|
// WithTraceFlags returns a new SpanContext with the TraceFlags replaced.
|
2021-04-05 19:21:42 +02:00
|
|
|
func (sc SpanContext) WithTraceFlags(flags TraceFlags) SpanContext {
|
2021-03-09 18:17:29 +02:00
|
|
|
return SpanContext{
|
|
|
|
traceID: sc.traceID,
|
|
|
|
spanID: sc.spanID,
|
|
|
|
traceFlags: flags,
|
|
|
|
traceState: sc.traceState,
|
2021-03-18 18:05:37 +02:00
|
|
|
remote: sc.remote,
|
2021-03-09 18:17:29 +02:00
|
|
|
}
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
|
2021-03-09 18:17:29 +02:00
|
|
|
// TraceState returns the TraceState from the SpanContext.
|
|
|
|
func (sc SpanContext) TraceState() TraceState {
|
|
|
|
return sc.traceState
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTraceState returns a new SpanContext with the TraceState replaced.
|
|
|
|
func (sc SpanContext) WithTraceState(state TraceState) SpanContext {
|
|
|
|
return SpanContext{
|
|
|
|
traceID: sc.traceID,
|
|
|
|
spanID: sc.spanID,
|
|
|
|
traceFlags: sc.traceFlags,
|
|
|
|
traceState: state,
|
2021-03-18 18:05:37 +02:00
|
|
|
remote: sc.remote,
|
2021-03-09 18:17:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal is a predicate that determines whether two SpanContext values are equal.
|
|
|
|
func (sc SpanContext) Equal(other SpanContext) bool {
|
|
|
|
return sc.traceID == other.traceID &&
|
|
|
|
sc.spanID == other.spanID &&
|
|
|
|
sc.traceFlags == other.traceFlags &&
|
2021-03-18 18:05:37 +02:00
|
|
|
sc.traceState.String() == other.traceState.String() &&
|
|
|
|
sc.remote == other.remote
|
2021-03-09 18:17:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements a custom marshal function to encode a SpanContext.
|
|
|
|
func (sc SpanContext) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(SpanContextConfig{
|
|
|
|
TraceID: sc.traceID,
|
|
|
|
SpanID: sc.spanID,
|
|
|
|
TraceFlags: sc.traceFlags,
|
|
|
|
TraceState: sc.traceState,
|
2021-03-18 18:05:37 +02:00
|
|
|
Remote: sc.remote,
|
2021-03-09 18:17:29 +02:00
|
|
|
})
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Span is the individual component of a trace. It represents a single named
|
|
|
|
// and timed operation of a workflow that is traced. A Tracer is used to
|
|
|
|
// create a Span and it is then up to the operation the Span represents to
|
|
|
|
// properly end the Span when the operation itself ends.
|
2021-06-18 16:56:11 +02:00
|
|
|
//
|
|
|
|
// Warning: methods may be added to this interface in minor releases.
|
2020-10-09 04:58:56 +02:00
|
|
|
type Span interface {
|
2020-11-09 20:57:34 +02:00
|
|
|
// 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.
|
2021-05-27 16:53:56 +02:00
|
|
|
End(options ...SpanEndOption)
|
2020-10-09 04:58:56 +02:00
|
|
|
|
2020-10-16 17:09:27 +02:00
|
|
|
// AddEvent adds an event with the provided name and options.
|
|
|
|
AddEvent(name string, options ...EventOption)
|
2020-10-09 04:58:56 +02:00
|
|
|
|
|
|
|
// IsRecording returns the recording state of the Span. It will return
|
|
|
|
// true if the Span is active and events can be recorded.
|
|
|
|
IsRecording() bool
|
|
|
|
|
2021-04-01 22:07:46 +02:00
|
|
|
// RecordError will record err as an exception span event for this span. An
|
2021-05-26 16:33:54 +02:00
|
|
|
// additional call to SetStatus is required if the Status of the Span should
|
|
|
|
// be set to Error, as this method does not change the Span status. If this
|
|
|
|
// span is not being recorded or err is nil then this method does nothing.
|
2020-10-16 17:09:27 +02:00
|
|
|
RecordError(err error, options ...EventOption)
|
2020-10-09 04:58:56 +02:00
|
|
|
|
2021-05-26 16:33:54 +02:00
|
|
|
// SpanContext returns the SpanContext of the Span. The returned SpanContext
|
|
|
|
// is usable even after the End method has been called for the Span.
|
2020-10-09 04:58:56 +02:00
|
|
|
SpanContext() SpanContext
|
|
|
|
|
|
|
|
// SetStatus sets the status of the Span in the form of a code and a
|
2021-05-03 21:00:54 +02:00
|
|
|
// description, overriding previous values set. The description is only
|
|
|
|
// included in a status when the code is for an error.
|
|
|
|
SetStatus(code codes.Code, description string)
|
2020-10-09 04:58:56 +02:00
|
|
|
|
|
|
|
// 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.
|
2021-02-18 19:59:37 +02:00
|
|
|
SetAttributes(kv ...attribute.KeyValue)
|
2021-06-17 18:05:44 +02:00
|
|
|
|
|
|
|
// TracerProvider returns a TracerProvider that can be used to generate
|
|
|
|
// additional Spans on the same telemetry pipeline as the current Span.
|
|
|
|
TracerProvider() TracerProvider
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Link is the relationship between two Spans. The relationship can be within
|
|
|
|
// the same Trace or across different Traces.
|
|
|
|
//
|
|
|
|
// For example, a Link is used in the following situations:
|
|
|
|
//
|
|
|
|
// 1. Batch Processing: A batch of operations may contain operations
|
|
|
|
// associated with one or more traces/spans. Since there can only be one
|
|
|
|
// parent SpanContext, a Link is used to keep reference to the
|
|
|
|
// SpanContext of all operations in the batch.
|
|
|
|
// 2. Public Endpoint: A SpanContext for an in incoming client request on a
|
|
|
|
// public endpoint should be considered untrusted. In such a case, a new
|
|
|
|
// trace with its own identity and sampling decision needs to be created,
|
|
|
|
// but this new trace needs to be related to the original trace in some
|
|
|
|
// form. A Link is used to keep reference to the original SpanContext and
|
|
|
|
// track the relationship.
|
|
|
|
type Link struct {
|
2021-04-06 16:51:15 +02:00
|
|
|
// SpanContext of the linked Span.
|
2021-05-04 02:10:02 +02:00
|
|
|
SpanContext SpanContext
|
2021-04-06 16:51:15 +02:00
|
|
|
|
|
|
|
// Attributes describe the aspects of the link.
|
2021-02-18 19:59:37 +02:00
|
|
|
Attributes []attribute.KeyValue
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 20:39:27 +02:00
|
|
|
// LinkFromContext returns a link encapsulating the SpanContext in the provided ctx.
|
|
|
|
func LinkFromContext(ctx context.Context, attrs ...attribute.KeyValue) Link {
|
|
|
|
return Link{
|
|
|
|
SpanContext: SpanContextFromContext(ctx),
|
|
|
|
Attributes: attrs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
// SpanKind is the role a Span plays in a Trace.
|
|
|
|
type SpanKind int
|
|
|
|
|
2020-10-12 17:43:26 +02:00
|
|
|
// As a convenience, these match the proto definition, see
|
|
|
|
// https://github.com/open-telemetry/opentelemetry-proto/blob/30d237e1ff3ab7aa50e0922b5bebdd93505090af/opentelemetry/proto/trace/v1/trace.proto#L101-L129
|
|
|
|
//
|
|
|
|
// The unspecified value is not a valid `SpanKind`. Use `ValidateSpanKind()`
|
|
|
|
// to coerce a span kind to a valid value.
|
2020-10-09 04:58:56 +02:00
|
|
|
const (
|
2020-10-12 17:43:26 +02:00
|
|
|
// SpanKindUnspecified is an unspecified SpanKind and is not a valid
|
|
|
|
// SpanKind. SpanKindUnspecified should be replaced with SpanKindInternal
|
|
|
|
// if it is received.
|
2020-10-09 04:58:56 +02:00
|
|
|
SpanKindUnspecified SpanKind = 0
|
2020-10-12 17:43:26 +02:00
|
|
|
// 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
|
2020-10-09 04:58:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateSpanKind returns a valid span kind value. This will coerce
|
|
|
|
// invalid values into the default value, SpanKindInternal.
|
|
|
|
func ValidateSpanKind(spanKind SpanKind) SpanKind {
|
|
|
|
switch spanKind {
|
|
|
|
case SpanKindInternal,
|
|
|
|
SpanKindServer,
|
|
|
|
SpanKindClient,
|
|
|
|
SpanKindProducer,
|
|
|
|
SpanKindConsumer:
|
|
|
|
// valid
|
|
|
|
return spanKind
|
|
|
|
default:
|
|
|
|
return SpanKindInternal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the specified name of the SpanKind in lower-case.
|
|
|
|
func (sk SpanKind) String() string {
|
|
|
|
switch sk {
|
|
|
|
case SpanKindInternal:
|
|
|
|
return "internal"
|
|
|
|
case SpanKindServer:
|
|
|
|
return "server"
|
|
|
|
case SpanKindClient:
|
|
|
|
return "client"
|
|
|
|
case SpanKindProducer:
|
|
|
|
return "producer"
|
|
|
|
case SpanKindConsumer:
|
|
|
|
return "consumer"
|
|
|
|
default:
|
|
|
|
return "unspecified"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracer is the creator of Spans.
|
2021-06-18 16:56:11 +02:00
|
|
|
//
|
|
|
|
// Warning: methods may be added to this interface in minor releases.
|
2020-10-09 04:58:56 +02:00
|
|
|
type Tracer interface {
|
2021-04-30 00:22:59 +02:00
|
|
|
// Start creates a span and a context.Context containing the newly-created span.
|
|
|
|
//
|
|
|
|
// If the context.Context provided in `ctx` contains a Span then the newly-created
|
|
|
|
// Span will be a child of that span, otherwise it will be a root span. This behavior
|
|
|
|
// can be overridden by providing `WithNewRoot()` as a SpanOption, causing the
|
|
|
|
// newly-created Span to be a root span even if `ctx` contains a Span.
|
|
|
|
//
|
|
|
|
// When creating a Span it is recommended to provide all known span attributes using
|
|
|
|
// the `WithAttributes()` SpanOption as samplers will only have access to the
|
|
|
|
// attributes provided when a Span is created.
|
|
|
|
//
|
|
|
|
// Any Span that is created MUST also be ended. This is the responsibility of the user.
|
|
|
|
// Implementations of this API may leak memory or other resources if Spans are not ended.
|
2021-05-27 16:53:56 +02:00
|
|
|
Start(ctx context.Context, spanName string, opts ...SpanStartOption) (context.Context, Span)
|
2020-10-09 04:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TracerProvider provides access to instrumentation Tracers.
|
2021-06-18 16:56:11 +02:00
|
|
|
//
|
|
|
|
// Warning: methods may be added to this interface in minor releases.
|
2020-10-09 04:58:56 +02:00
|
|
|
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.
|
2021-03-03 22:09:58 +02:00
|
|
|
//
|
|
|
|
// This method must be concurrency safe.
|
2020-10-09 04:58:56 +02:00
|
|
|
Tracer(instrumentationName string, opts ...TracerOption) Tracer
|
|
|
|
}
|