2023-05-09 18:19:48 +01: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.
|
|
|
|
|
|
|
|
package propagation // import "go.opentelemetry.io/otel/propagation"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2024-03-11 14:34:34 +00:00
|
|
|
"strings"
|
2023-05-09 18:19:48 +01:00
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
supportedVersion = 0
|
|
|
|
maxVersion = 254
|
|
|
|
traceparentHeader = "traceparent"
|
|
|
|
tracestateHeader = "tracestate"
|
2024-03-11 14:34:34 +00:00
|
|
|
delimiter = "-"
|
2023-05-09 18:19:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TraceContext is a propagator that supports the W3C Trace Context format
|
|
|
|
// (https://www.w3.org/TR/trace-context/)
|
|
|
|
//
|
|
|
|
// This propagator will propagate the traceparent and tracestate headers to
|
|
|
|
// guarantee traces are not broken. It is up to the users of this propagator
|
|
|
|
// to choose if they want to participate in a trace by modifying the
|
|
|
|
// traceparent header and relevant parts of the tracestate header containing
|
|
|
|
// their proprietary information.
|
|
|
|
type TraceContext struct{}
|
|
|
|
|
2023-11-13 10:08:02 +00:00
|
|
|
var (
|
2024-03-11 14:34:34 +00:00
|
|
|
_ TextMapPropagator = TraceContext{}
|
|
|
|
versionPart = fmt.Sprintf("%.2X", supportedVersion)
|
2023-11-13 10:08:02 +00:00
|
|
|
)
|
2023-05-09 18:19:48 +01:00
|
|
|
|
|
|
|
// Inject set tracecontext from the Context into the carrier.
|
|
|
|
func (tc TraceContext) Inject(ctx context.Context, carrier TextMapCarrier) {
|
|
|
|
sc := trace.SpanContextFromContext(ctx)
|
|
|
|
if !sc.IsValid() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ts := sc.TraceState().String(); ts != "" {
|
|
|
|
carrier.Set(tracestateHeader, ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear all flags other than the trace-context supported sampling bit.
|
|
|
|
flags := sc.TraceFlags() & trace.FlagsSampled
|
|
|
|
|
2024-03-11 14:34:34 +00:00
|
|
|
var sb strings.Builder
|
|
|
|
sb.Grow(2 + 32 + 16 + 2 + 3)
|
|
|
|
_, _ = sb.WriteString(versionPart)
|
|
|
|
traceID := sc.TraceID()
|
|
|
|
spanID := sc.SpanID()
|
|
|
|
flagByte := [1]byte{byte(flags)}
|
|
|
|
var buf [32]byte
|
|
|
|
for _, src := range [][]byte{traceID[:], spanID[:], flagByte[:]} {
|
|
|
|
_ = sb.WriteByte(delimiter[0])
|
|
|
|
n := hex.Encode(buf[:], src)
|
|
|
|
_, _ = sb.Write(buf[:n])
|
|
|
|
}
|
|
|
|
carrier.Set(traceparentHeader, sb.String())
|
2023-05-09 18:19:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extract reads tracecontext from the carrier into a returned Context.
|
|
|
|
//
|
|
|
|
// The returned Context will be a copy of ctx and contain the extracted
|
|
|
|
// tracecontext as the remote SpanContext. If the extracted tracecontext is
|
|
|
|
// invalid, the passed ctx will be returned directly instead.
|
|
|
|
func (tc TraceContext) Extract(ctx context.Context, carrier TextMapCarrier) context.Context {
|
|
|
|
sc := tc.extract(carrier)
|
|
|
|
if !sc.IsValid() {
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
return trace.ContextWithRemoteSpanContext(ctx, sc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc TraceContext) extract(carrier TextMapCarrier) trace.SpanContext {
|
|
|
|
h := carrier.Get(traceparentHeader)
|
|
|
|
if h == "" {
|
|
|
|
return trace.SpanContext{}
|
|
|
|
}
|
|
|
|
|
2024-03-11 14:34:34 +00:00
|
|
|
var ver [1]byte
|
|
|
|
if !extractPart(ver[:], &h, 2) {
|
2023-05-09 18:19:48 +01:00
|
|
|
return trace.SpanContext{}
|
|
|
|
}
|
|
|
|
version := int(ver[0])
|
|
|
|
if version > maxVersion {
|
|
|
|
return trace.SpanContext{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var scc trace.SpanContextConfig
|
2024-03-11 14:34:34 +00:00
|
|
|
if !extractPart(scc.TraceID[:], &h, 32) {
|
2023-05-09 18:19:48 +01:00
|
|
|
return trace.SpanContext{}
|
|
|
|
}
|
2024-03-11 14:34:34 +00:00
|
|
|
if !extractPart(scc.SpanID[:], &h, 16) {
|
2023-05-09 18:19:48 +01:00
|
|
|
return trace.SpanContext{}
|
|
|
|
}
|
|
|
|
|
2024-03-11 14:34:34 +00:00
|
|
|
var opts [1]byte
|
|
|
|
if !extractPart(opts[:], &h, 2) {
|
2023-05-09 18:19:48 +01:00
|
|
|
return trace.SpanContext{}
|
|
|
|
}
|
2024-03-11 14:34:34 +00:00
|
|
|
if version == 0 && (h != "" || opts[0] > 2) {
|
|
|
|
// version 0 not allow extra
|
|
|
|
// version 0 not allow other flag
|
2023-05-09 18:19:48 +01:00
|
|
|
return trace.SpanContext{}
|
|
|
|
}
|
2024-03-11 14:34:34 +00:00
|
|
|
|
2023-05-09 18:19:48 +01:00
|
|
|
// Clear all flags other than the trace-context supported sampling bit.
|
|
|
|
scc.TraceFlags = trace.TraceFlags(opts[0]) & trace.FlagsSampled
|
|
|
|
|
|
|
|
// Ignore the error returned here. Failure to parse tracestate MUST NOT
|
|
|
|
// affect the parsing of traceparent according to the W3C tracecontext
|
|
|
|
// specification.
|
|
|
|
scc.TraceState, _ = trace.ParseTraceState(carrier.Get(tracestateHeader))
|
|
|
|
scc.Remote = true
|
|
|
|
|
|
|
|
sc := trace.NewSpanContext(scc)
|
|
|
|
if !sc.IsValid() {
|
|
|
|
return trace.SpanContext{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
2024-03-11 14:34:34 +00:00
|
|
|
// upperHex detect hex is upper case Unicode characters.
|
|
|
|
func upperHex(v string) bool {
|
|
|
|
for _, c := range v {
|
|
|
|
if c >= 'A' && c <= 'F' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractPart(dst []byte, h *string, n int) bool {
|
|
|
|
part, left, _ := strings.Cut(*h, delimiter)
|
|
|
|
*h = left
|
|
|
|
// hex.Decode decodes unsupported upper-case characters, so exclude explicitly.
|
|
|
|
if len(part) != n || upperHex(part) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if p, err := hex.Decode(dst, []byte(part)); err != nil || p != n/2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:19:48 +01:00
|
|
|
// Fields returns the keys who's values are set with Inject.
|
|
|
|
func (tc TraceContext) Fields() []string {
|
|
|
|
return []string{traceparentHeader, tracestateHeader}
|
|
|
|
}
|