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