mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-02-09 13:37:12 +02:00
* Add new propagators package * Move B3 propagator to propagators Update all `api/trace` dependencies in the propagator. Export the isDeferred and isDebug methods of the SpanContext. These are needed by the B3 propagator to track trace state. * Move W3C trace context propagator to propagators * Update package docs with supported encodings * Move unified propagators code to own file * Update b3 exported documentation * Update trace_context exported documentation * Add code examples for B3 propagator * Add TraceContext example code * Remove internal package Move common testing declarations to the propagators_test.go file. * Add changes to Changelog * Add test to check default propagators
161 lines
4.3 KiB
Go
161 lines
4.3 KiB
Go
// 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 propagators
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"regexp"
|
|
|
|
"go.opentelemetry.io/otel/api/propagation"
|
|
"go.opentelemetry.io/otel/api/trace"
|
|
)
|
|
|
|
const (
|
|
supportedVersion = 0
|
|
maxVersion = 254
|
|
traceparentHeader = "traceparent"
|
|
tracestateHeader = "tracestate"
|
|
)
|
|
|
|
type traceContextPropagatorKeyType uint
|
|
|
|
const (
|
|
tracestateKey traceContextPropagatorKeyType = 0
|
|
)
|
|
|
|
// 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{}
|
|
|
|
var _ propagation.HTTPPropagator = TraceContext{}
|
|
var traceCtxRegExp = regexp.MustCompile("^(?P<version>[0-9a-f]{2})-(?P<traceID>[a-f0-9]{32})-(?P<spanID>[a-f0-9]{16})-(?P<traceFlags>[a-f0-9]{2})(?:-.*)?$")
|
|
|
|
// Inject injects a context into the supplier as W3C Trace Context HTTP
|
|
// headers.
|
|
func (tc TraceContext) Inject(ctx context.Context, supplier propagation.HTTPSupplier) {
|
|
tracestate := ctx.Value(tracestateKey)
|
|
if state, ok := tracestate.(string); tracestate != nil && ok {
|
|
supplier.Set(tracestateHeader, state)
|
|
}
|
|
|
|
sc := trace.SpanFromContext(ctx).SpanContext()
|
|
if !sc.IsValid() {
|
|
return
|
|
}
|
|
h := fmt.Sprintf("%.2x-%s-%s-%.2x",
|
|
supportedVersion,
|
|
sc.TraceID,
|
|
sc.SpanID,
|
|
sc.TraceFlags&trace.FlagsSampled)
|
|
supplier.Set(traceparentHeader, h)
|
|
}
|
|
|
|
// Extract extracts a context from the supplier if it contains W3C Trace
|
|
// Context headers.
|
|
func (tc TraceContext) Extract(ctx context.Context, supplier propagation.HTTPSupplier) context.Context {
|
|
state := supplier.Get(tracestateHeader)
|
|
if state != "" {
|
|
ctx = context.WithValue(ctx, tracestateKey, state)
|
|
}
|
|
|
|
sc := tc.extract(supplier)
|
|
if !sc.IsValid() {
|
|
return ctx
|
|
}
|
|
return trace.ContextWithRemoteSpanContext(ctx, sc)
|
|
}
|
|
|
|
func (tc TraceContext) extract(supplier propagation.HTTPSupplier) trace.SpanContext {
|
|
h := supplier.Get(traceparentHeader)
|
|
if h == "" {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
|
|
matches := traceCtxRegExp.FindStringSubmatch(h)
|
|
|
|
if len(matches) == 0 {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
|
|
if len(matches) < 5 { // four subgroups plus the overall match
|
|
return trace.EmptySpanContext()
|
|
}
|
|
|
|
if len(matches[1]) != 2 {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
ver, err := hex.DecodeString(matches[1])
|
|
if err != nil {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
version := int(ver[0])
|
|
if version > maxVersion {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
|
|
if version == 0 && len(matches) != 5 { // four subgroups plus the overall match
|
|
return trace.EmptySpanContext()
|
|
}
|
|
|
|
if len(matches[2]) != 32 {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
|
|
var sc trace.SpanContext
|
|
|
|
sc.TraceID, err = trace.IDFromHex(matches[2][:32])
|
|
if err != nil {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
|
|
if len(matches[3]) != 16 {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
sc.SpanID, err = trace.SpanIDFromHex(matches[3])
|
|
if err != nil {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
|
|
if len(matches[4]) != 2 {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
opts, err := hex.DecodeString(matches[4])
|
|
if err != nil || len(opts) < 1 || (version == 0 && opts[0] > 2) {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
// Clear all flags other than the trace-context supported sampling bit.
|
|
sc.TraceFlags = opts[0] & trace.FlagsSampled
|
|
|
|
if !sc.IsValid() {
|
|
return trace.EmptySpanContext()
|
|
}
|
|
|
|
return sc
|
|
}
|
|
|
|
// GetAllKeys returns the HTTP header names this propagator will use when
|
|
// injecting.
|
|
func (tc TraceContext) GetAllKeys() []string {
|
|
return []string{traceparentHeader, tracestateHeader}
|
|
}
|