2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-09-23 11:51:32 -07: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-09-08 16:07:59 -07:00
|
|
|
package propagators
|
2019-09-23 11:51:32 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
|
2020-01-28 19:13:46 +01:00
|
|
|
"go.opentelemetry.io/otel/api/propagation"
|
2020-09-08 16:07:59 -07:00
|
|
|
"go.opentelemetry.io/otel/api/trace"
|
2019-09-23 11:51:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-02-20 19:31:21 +01:00
|
|
|
supportedVersion = 0
|
|
|
|
maxVersion = 254
|
2020-04-30 23:55:16 +08:00
|
|
|
traceparentHeader = "traceparent"
|
2020-05-02 15:23:03 -04:00
|
|
|
tracestateHeader = "tracestate"
|
|
|
|
)
|
|
|
|
|
|
|
|
type traceContextPropagatorKeyType uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
tracestateKey traceContextPropagatorKeyType = 0
|
2019-09-23 11:51:32 -07:00
|
|
|
)
|
|
|
|
|
2020-09-08 16:07:59 -07: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.
|
2019-12-03 22:52:04 +01:00
|
|
|
type TraceContext struct{}
|
2019-09-23 11:51:32 -07:00
|
|
|
|
2020-02-14 08:16:05 +01:00
|
|
|
var _ propagation.HTTPPropagator = TraceContext{}
|
2020-05-04 16:34:07 -04:00
|
|
|
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})(?:-.*)?$")
|
2019-09-23 11:51:32 -07:00
|
|
|
|
2020-09-08 16:07:59 -07:00
|
|
|
// Inject injects a context into the supplier as W3C Trace Context HTTP
|
|
|
|
// headers.
|
2020-09-08 08:20:14 -07:00
|
|
|
func (tc TraceContext) Inject(ctx context.Context, supplier propagation.HTTPSupplier) {
|
2020-05-02 15:23:03 -04:00
|
|
|
tracestate := ctx.Value(tracestateKey)
|
2020-05-04 16:34:07 -04:00
|
|
|
if state, ok := tracestate.(string); tracestate != nil && ok {
|
|
|
|
supplier.Set(tracestateHeader, state)
|
2020-05-02 15:23:03 -04:00
|
|
|
}
|
|
|
|
|
2020-09-08 16:07:59 -07:00
|
|
|
sc := trace.SpanFromContext(ctx).SpanContext()
|
2020-03-05 10:12:10 -08:00
|
|
|
if !sc.IsValid() {
|
|
|
|
return
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
2020-04-17 04:12:48 +05:30
|
|
|
h := fmt.Sprintf("%.2x-%s-%s-%.2x",
|
2020-03-05 10:12:10 -08:00
|
|
|
supportedVersion,
|
2020-04-17 04:12:48 +05:30
|
|
|
sc.TraceID,
|
2020-03-05 10:12:10 -08:00
|
|
|
sc.SpanID,
|
2020-09-08 16:07:59 -07:00
|
|
|
sc.TraceFlags&trace.FlagsSampled)
|
2020-03-05 10:12:10 -08:00
|
|
|
supplier.Set(traceparentHeader, h)
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
|
|
|
|
2020-09-08 16:07:59 -07:00
|
|
|
// Extract extracts a context from the supplier if it contains W3C Trace
|
|
|
|
// Context headers.
|
2020-02-14 08:16:05 +01:00
|
|
|
func (tc TraceContext) Extract(ctx context.Context, supplier propagation.HTTPSupplier) context.Context {
|
2020-05-02 15:23:03 -04:00
|
|
|
state := supplier.Get(tracestateHeader)
|
|
|
|
if state != "" {
|
|
|
|
ctx = context.WithValue(ctx, tracestateKey, state)
|
|
|
|
}
|
|
|
|
|
2020-04-27 20:37:40 +02:00
|
|
|
sc := tc.extract(supplier)
|
|
|
|
if !sc.IsValid() {
|
|
|
|
return ctx
|
|
|
|
}
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.ContextWithRemoteSpanContext(ctx, sc)
|
2019-10-17 15:08:44 -07:00
|
|
|
}
|
|
|
|
|
2020-09-08 16:07:59 -07:00
|
|
|
func (tc TraceContext) extract(supplier propagation.HTTPSupplier) trace.SpanContext {
|
2020-03-02 23:50:53 +01:00
|
|
|
h := supplier.Get(traceparentHeader)
|
2019-09-23 11:51:32 -07:00
|
|
|
if h == "" {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
|
|
|
|
2020-05-02 15:23:03 -04:00
|
|
|
matches := traceCtxRegExp.FindStringSubmatch(h)
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
|
|
|
|
2020-05-02 15:23:03 -04:00
|
|
|
if len(matches) < 5 { // four subgroups plus the overall match
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
|
|
|
|
2020-05-02 15:23:03 -04:00
|
|
|
if len(matches[1]) != 2 {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
2020-05-02 15:23:03 -04:00
|
|
|
ver, err := hex.DecodeString(matches[1])
|
2019-09-23 11:51:32 -07:00
|
|
|
if err != nil {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
|
|
|
version := int(ver[0])
|
|
|
|
if version > maxVersion {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
|
|
|
|
2020-05-02 15:23:03 -04:00
|
|
|
if version == 0 && len(matches) != 5 { // four subgroups plus the overall match
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
|
|
|
|
2020-05-02 15:23:03 -04:00
|
|
|
if len(matches[2]) != 32 {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
|
|
|
|
2020-09-08 16:07:59 -07:00
|
|
|
var sc trace.SpanContext
|
2019-09-23 11:51:32 -07:00
|
|
|
|
2020-09-08 16:07:59 -07:00
|
|
|
sc.TraceID, err = trace.IDFromHex(matches[2][:32])
|
2019-09-23 11:51:32 -07:00
|
|
|
if err != nil {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
|
|
|
|
2020-05-02 15:23:03 -04:00
|
|
|
if len(matches[3]) != 16 {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
2020-09-08 16:07:59 -07:00
|
|
|
sc.SpanID, err = trace.SpanIDFromHex(matches[3])
|
2019-09-23 11:51:32 -07:00
|
|
|
if err != nil {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
|
|
|
|
2020-05-02 15:23:03 -04:00
|
|
|
if len(matches[4]) != 2 {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
2020-05-02 15:23:03 -04:00
|
|
|
opts, err := hex.DecodeString(matches[4])
|
2019-09-23 11:51:32 -07:00
|
|
|
if err != nil || len(opts) < 1 || (version == 0 && opts[0] > 2) {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
2020-07-07 16:38:52 -07:00
|
|
|
// Clear all flags other than the trace-context supported sampling bit.
|
2020-09-08 16:07:59 -07:00
|
|
|
sc.TraceFlags = opts[0] & trace.FlagsSampled
|
2019-09-23 11:51:32 -07:00
|
|
|
|
|
|
|
if !sc.IsValid() {
|
2020-09-08 16:07:59 -07:00
|
|
|
return trace.EmptySpanContext()
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
2020-09-08 16:07:59 -07:00
|
|
|
// GetAllKeys returns the HTTP header names this propagator will use when
|
|
|
|
// injecting.
|
2020-09-08 08:20:14 -07:00
|
|
|
func (tc TraceContext) GetAllKeys() []string {
|
2020-05-02 15:23:03 -04:00
|
|
|
return []string{traceparentHeader, tracestateHeader}
|
2019-09-23 11:51:32 -07:00
|
|
|
}
|