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.

package trace

import (
	
	
	
)

FlagsSampled is a bitmask with the sampled bit set. A SpanContext with the sampling bit set means the span is sampled.
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.
FlagsDebug is a bitmask with the debug bit set.
	FlagsDebug = byte(0x04)

	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"
)

type errorConst string

func ( errorConst) () string {
	return string()
}
ID is a unique identity of a trace.
type ID [16]byte

var nilTraceID ID
var _ json.Marshaler = nilTraceID
IsValid checks whether the trace ID is valid. A valid trace ID does not consist of zeros only.
func ( ID) () bool {
	return !bytes.Equal([:], nilTraceID[:])
}
MarshalJSON implements a custom marshal function to encode TraceID as a hex string.
func ( ID) () ([]byte, error) {
	return json.Marshal(.String())
}
String returns the hex string representation form of a TraceID
func ( ID) () string {
	return hex.EncodeToString([:])
}
SpanID is a unique identify of a span in a trace.
IsValid checks whether the span ID is valid. A valid span ID does not consist of zeros only.
func ( SpanID) () bool {
	return !bytes.Equal([:], nilSpanID[:])
}
MarshalJSON implements a custom marshal function to encode SpanID as a hex string.
func ( SpanID) () ([]byte, error) {
	return json.Marshal(.String())
}
String returns the hex string representation form of a SpanID
func ( SpanID) () string {
	return hex.EncodeToString([:])
}
IDFromHex 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
func ( string) (ID, error) {
	 := ID{}
	if len() != 32 {
		return , ErrInvalidTraceIDLength
	}

	if  := decodeHex(, [:]);  != nil {
		return , 
	}

	if !.IsValid() {
		return , ErrNilTraceID
	}
	return , 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 ( string) (SpanID, error) {
	 := SpanID{}
	if len() != 16 {
		return , ErrInvalidSpanIDLength
	}

	if  := decodeHex(, [:]);  != nil {
		return , 
	}

	if !.IsValid() {
		return , ErrNilSpanID
	}
	return , nil
}

func ( string,  []byte) error {
	for ,  := range  {
		switch {
		case 'a' <=  &&  <= 'f':
			continue
		case '0' <=  &&  <= '9':
			continue
		default:
			return ErrInvalidHexID
		}
	}

	,  := hex.DecodeString()
	if  != nil {
		return 
	}

	copy(, )
	return nil
}
SpanContext contains basic information about the span - its trace ID, span ID and trace flags.
EmptySpanContext is meant for internal use to return invalid span context during error conditions.
IsValid checks if the span context is valid. A valid span context has a valid trace ID and a valid span ID.
func ( SpanContext) () bool {
	return .HasTraceID() && .HasSpanID()
}
HasTraceID checks if the span context has a valid trace ID.
func ( SpanContext) () bool {
	return .TraceID.IsValid()
}
HasSpanID checks if the span context has a valid span ID.
func ( SpanContext) () bool {
	return .SpanID.IsValid()
}
isDeferred returns if the deferred bit is set in the trace flags.
isDebug returns if the debug bit is set in the trace flags.
func ( SpanContext) () bool {
	return .TraceFlags&FlagsDebug == FlagsDebug
}
IsSampled returns if the sampling bit is set in the trace flags.