2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-07-02 03:17:50 +08:00
|
|
|
//
|
|
|
|
// 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-05-02 20:17:11 +08:00
|
|
|
package trace
|
2019-07-02 03:17:50 +08:00
|
|
|
|
|
|
|
import (
|
2019-10-23 03:01:33 -03:00
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2019-07-02 03:17:50 +08:00
|
|
|
)
|
|
|
|
|
2019-08-02 10:42:47 -07:00
|
|
|
const (
|
2020-07-07 16:38:52 -07:00
|
|
|
// 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)
|
2019-10-28 14:05:06 -03:00
|
|
|
|
|
|
|
ErrInvalidHexID errorConst = "trace-id and span-id can only contain [0-9a-f] characters, all lowercase"
|
|
|
|
|
|
|
|
ErrInvalidTraceIDLength errorConst = "hex encoded trace-id must have length equals to 32"
|
|
|
|
ErrNilTraceID errorConst = "trace-id can't be all zero"
|
|
|
|
|
|
|
|
ErrInvalidSpanIDLength errorConst = "hex encoded span-id must have length equals to 16"
|
|
|
|
ErrNilSpanID errorConst = "span-id can't be all zero"
|
2019-08-02 10:42:47 -07:00
|
|
|
)
|
|
|
|
|
2019-10-28 14:05:06 -03:00
|
|
|
type errorConst string
|
|
|
|
|
|
|
|
func (e errorConst) Error() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
2020-05-02 20:23:09 +08:00
|
|
|
// ID is a unique identity of a trace.
|
|
|
|
type ID [16]byte
|
2019-10-23 03:01:33 -03:00
|
|
|
|
2020-05-02 20:23:09 +08:00
|
|
|
var nilTraceID ID
|
2019-10-28 14:05:06 -03:00
|
|
|
var _ json.Marshaler = nilTraceID
|
2019-10-23 03:01:33 -03:00
|
|
|
|
2019-10-31 22:55:51 +01:00
|
|
|
// IsValid checks whether the trace ID is valid. A valid trace ID does
|
|
|
|
// not consist of zeros only.
|
2020-05-02 20:23:09 +08:00
|
|
|
func (t ID) IsValid() bool {
|
2019-10-23 03:01:33 -03:00
|
|
|
return !bytes.Equal(t[:], nilTraceID[:])
|
|
|
|
}
|
|
|
|
|
2019-10-31 22:55:51 +01:00
|
|
|
// MarshalJSON implements a custom marshal function to encode TraceID
|
|
|
|
// as a hex string.
|
2020-05-02 20:23:09 +08:00
|
|
|
func (t ID) MarshalJSON() ([]byte, error) {
|
2020-04-17 04:12:48 +05:30
|
|
|
return json.Marshal(t.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the hex string representation form of a TraceID
|
2020-05-02 20:23:09 +08:00
|
|
|
func (t ID) String() string {
|
2020-04-17 04:12:48 +05:30
|
|
|
return hex.EncodeToString(t[:])
|
2019-10-28 14:05:06 -03:00
|
|
|
}
|
|
|
|
|
2019-10-31 22:55:51 +01:00
|
|
|
// SpanID is a unique identify of a span in a trace.
|
2019-10-28 14:05:06 -03:00
|
|
|
type SpanID [8]byte
|
|
|
|
|
|
|
|
var nilSpanID SpanID
|
|
|
|
var _ json.Marshaler = nilSpanID
|
|
|
|
|
2019-10-31 22:55:51 +01:00
|
|
|
// IsValid checks whether the span ID is valid. A valid span ID does
|
|
|
|
// not consist of zeros only.
|
2019-10-28 14:05:06 -03:00
|
|
|
func (s SpanID) IsValid() bool {
|
|
|
|
return !bytes.Equal(s[:], nilSpanID[:])
|
|
|
|
}
|
|
|
|
|
2019-10-31 22:55:51 +01:00
|
|
|
// MarshalJSON implements a custom marshal function to encode SpanID
|
|
|
|
// as a hex string.
|
2019-10-28 14:05:06 -03:00
|
|
|
func (s SpanID) MarshalJSON() ([]byte, error) {
|
2020-04-17 04:12:48 +05:30
|
|
|
return json.Marshal(s.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the hex string representation form of a SpanID
|
|
|
|
func (s SpanID) String() string {
|
|
|
|
return hex.EncodeToString(s[:])
|
2019-10-28 14:05:06 -03:00
|
|
|
}
|
|
|
|
|
2020-05-02 20:23:09 +08:00
|
|
|
// IDFromHex returns a TraceID from a hex string if it is compliant
|
2019-10-23 03:01:33 -03:00
|
|
|
// with the w3c trace-context specification.
|
|
|
|
// See more at https://www.w3.org/TR/trace-context/#trace-id
|
2020-05-02 20:23:09 +08:00
|
|
|
func IDFromHex(h string) (ID, error) {
|
|
|
|
t := ID{}
|
2019-10-23 03:01:33 -03:00
|
|
|
if len(h) != 32 {
|
2019-10-28 14:05:06 -03:00
|
|
|
return t, ErrInvalidTraceIDLength
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decodeHex(h, t[:]); err != nil {
|
|
|
|
return t, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !t.IsValid() {
|
|
|
|
return t, ErrNilTraceID
|
2019-10-23 03:01:33 -03:00
|
|
|
}
|
2019-10-28 14:05:06 -03: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 {
|
|
|
|
return s, ErrInvalidSpanIDLength
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decodeHex(h, s[:]); err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.IsValid() {
|
|
|
|
return s, ErrNilSpanID
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
2019-10-23 03:01:33 -03:00
|
|
|
|
2019-10-28 14:05:06 -03:00
|
|
|
func decodeHex(h string, b []byte) error {
|
2019-10-23 03:01:33 -03:00
|
|
|
for _, r := range h {
|
|
|
|
switch {
|
|
|
|
case 'a' <= r && r <= 'f':
|
|
|
|
continue
|
|
|
|
case '0' <= r && r <= '9':
|
|
|
|
continue
|
|
|
|
default:
|
2019-10-28 14:05:06 -03:00
|
|
|
return ErrInvalidHexID
|
2019-10-23 03:01:33 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 14:05:06 -03:00
|
|
|
decoded, err := hex.DecodeString(h)
|
2019-10-23 03:01:33 -03:00
|
|
|
if err != nil {
|
2019-10-28 14:05:06 -03:00
|
|
|
return err
|
2019-10-23 03:01:33 -03:00
|
|
|
}
|
|
|
|
|
2019-11-18 19:47:04 +01:00
|
|
|
copy(b, decoded)
|
2019-10-28 14:05:06 -03:00
|
|
|
return nil
|
2019-10-23 03:01:33 -03:00
|
|
|
}
|
|
|
|
|
2019-10-31 22:55:51 +01:00
|
|
|
// SpanContext contains basic information about the span - its trace
|
|
|
|
// ID, span ID and trace flags.
|
2019-07-02 03:17:50 +08:00
|
|
|
type SpanContext struct {
|
2020-05-02 20:23:09 +08:00
|
|
|
TraceID ID
|
2019-10-28 14:05:06 -03:00
|
|
|
SpanID SpanID
|
2019-09-25 14:37:36 -07:00
|
|
|
TraceFlags byte
|
2019-07-02 03:17:50 +08:00
|
|
|
}
|
|
|
|
|
2019-10-31 22:55:51 +01:00
|
|
|
// EmptySpanContext is meant for internal use to return invalid span
|
|
|
|
// context during error conditions.
|
2019-08-13 01:59:35 +08:00
|
|
|
func EmptySpanContext() SpanContext {
|
|
|
|
return SpanContext{}
|
|
|
|
}
|
2019-07-02 03:17:50 +08:00
|
|
|
|
2019-10-31 22:55:51 +01:00
|
|
|
// IsValid checks if the span context is valid. A valid span context
|
|
|
|
// has a valid trace ID and a valid span ID.
|
2019-08-06 00:58:51 +08:00
|
|
|
func (sc SpanContext) IsValid() bool {
|
|
|
|
return sc.HasTraceID() && sc.HasSpanID()
|
|
|
|
}
|
|
|
|
|
2019-10-31 22:55:51 +01:00
|
|
|
// HasTraceID checks if the span context has a valid trace ID.
|
2019-07-02 03:17:50 +08:00
|
|
|
func (sc SpanContext) HasTraceID() bool {
|
2019-10-28 14:05:06 -03:00
|
|
|
return sc.TraceID.IsValid()
|
2019-07-02 03:17:50 +08:00
|
|
|
}
|
|
|
|
|
2019-10-31 22:55:51 +01:00
|
|
|
// HasSpanID checks if the span context has a valid span ID.
|
2019-07-02 03:17:50 +08:00
|
|
|
func (sc SpanContext) HasSpanID() bool {
|
2019-10-28 14:05:06 -03:00
|
|
|
return sc.SpanID.IsValid()
|
2019-07-02 03:17:50 +08:00
|
|
|
}
|
|
|
|
|
2020-09-08 16:07:59 -07:00
|
|
|
// IsDeferred returns if the deferred bit is set in the trace flags.
|
|
|
|
func (sc SpanContext) IsDeferred() bool {
|
2020-07-07 16:38:52 -07:00
|
|
|
return sc.TraceFlags&FlagsDeferred == FlagsDeferred
|
|
|
|
}
|
|
|
|
|
2020-09-08 16:07:59 -07:00
|
|
|
// IsDebug returns if the debug bit is set in the trace flags.
|
|
|
|
func (sc SpanContext) IsDebug() bool {
|
2020-07-07 16:38:52 -07:00
|
|
|
return sc.TraceFlags&FlagsDebug == FlagsDebug
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSampled returns if the sampling bit is set in the trace flags.
|
2019-08-02 10:42:47 -07:00
|
|
|
func (sc SpanContext) IsSampled() bool {
|
2020-07-07 16:38:52 -07:00
|
|
|
return sc.TraceFlags&FlagsSampled == FlagsSampled
|
2019-08-02 10:42:47 -07:00
|
|
|
}
|