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