// Copyright The OpenTelemetry Authors// SPDX-License-Identifier: Apache-2.0package telemetryimport ()const ( traceIDSize = 16 spanIDSize = 8)// TraceID is a custom data type that is used for all trace IDs.typeTraceID [traceIDSize]byte// String returns the hex string representation form of a TraceID.func ( TraceID) () string {returnhex.EncodeToString([:])}// IsEmpty returns false if id contains at least one non-zero byte.func ( TraceID) () bool {return == [traceIDSize]byte{}}// MarshalJSON converts the trace ID into a hex string enclosed in quotes.func ( TraceID) () ([]byte, error) {if .IsEmpty() {return []byte(`""`), nil }returnmarshalJSON([:])}// UnmarshalJSON inflates the trace ID from hex string, possibly enclosed in// quotes.func ( *TraceID) ( []byte) error { * = [traceIDSize]byte{}returnunmarshalJSON([:], )}// SpanID is a custom data type that is used for all span IDs.typeSpanID [spanIDSize]byte// String returns the hex string representation form of a SpanID.func ( SpanID) () string {returnhex.EncodeToString([:])}// IsEmpty returns true if the span ID contains at least one non-zero byte.func ( SpanID) () bool {return == [spanIDSize]byte{}}// MarshalJSON converts span ID into a hex string enclosed in quotes.func ( SpanID) () ([]byte, error) {if .IsEmpty() {return []byte(`""`), nil }returnmarshalJSON([:])}// UnmarshalJSON decodes span ID from hex string, possibly enclosed in quotes.func ( *SpanID) ( []byte) error { * = [spanIDSize]byte{}returnunmarshalJSON([:], )}// marshalJSON converts id into a hex string enclosed in quotes.func marshalJSON( []byte) ([]byte, error) {// Plus 2 quote chars at the start and end. := hex.EncodedLen(len()) + 2 := make([]byte, )hex.Encode([1:-1], ) [0], [-1] = '"', '"'return , nil}// unmarshalJSON inflates trace id from hex string, possibly enclosed in quotes.func unmarshalJSON( []byte, []byte) error {if := len(); >= 2 && [0] == '"' && [-1] == '"' { = [1 : -1] } := len()if == 0 {returnnil }iflen() != hex.DecodedLen() {returnerrors.New("invalid length for ID") } , := hex.Decode(, )if != nil {returnfmt.Errorf("cannot unmarshal ID from string '%s': %w", string(), ) }returnnil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.