2019-09-23 20:51:32 +02:00
|
|
|
// Copyright 2019, 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.
|
|
|
|
|
2020-01-28 20:13:46 +02:00
|
|
|
package trace
|
2019-09-23 20:51:32 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
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
|
|
|
|
TraceparentHeader = "Traceparent"
|
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{}
|
2019-09-23 20:51:32 +02:00
|
|
|
var traceCtxRegExp = regexp.MustCompile("^[0-9a-f]{2}-[a-f0-9]{32}-[a-f0-9]{16}-[a-f0-9]{2}-?")
|
|
|
|
|
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-02-14 09:16:05 +02:00
|
|
|
func (TraceContext) Inject(ctx context.Context, supplier propagation.HTTPSupplier) {
|
2020-01-28 20:13:46 +02:00
|
|
|
sc := SpanFromContext(ctx).SpanContext()
|
2019-09-23 20:51:32 +02:00
|
|
|
if sc.IsValid() {
|
2019-10-23 08:01:33 +02:00
|
|
|
h := fmt.Sprintf("%.2x-%s-%.16x-%.2x",
|
2019-09-23 20:51:32 +02:00
|
|
|
supportedVersion,
|
2019-10-23 08:01:33 +02:00
|
|
|
sc.TraceIDString(),
|
2019-09-23 20:51:32 +02:00
|
|
|
sc.SpanID,
|
2019-09-25 23:37:36 +02:00
|
|
|
sc.TraceFlags&core.TraceFlagsSampled)
|
2019-10-07 19:25:11 +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-02-20 20:31:21 +02:00
|
|
|
return ContextWithRemoteSpanContext(ctx, tc.extract(supplier))
|
2019-10-18 00:08:44 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 20:31:21 +02:00
|
|
|
func (TraceContext) extract(supplier propagation.HTTPSupplier) core.SpanContext {
|
2019-10-07 19:25:11 +02:00
|
|
|
h := supplier.Get(TraceparentHeader)
|
2019-09-23 20:51:32 +02:00
|
|
|
if h == "" {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
h = strings.Trim(h, "-")
|
|
|
|
if !traceCtxRegExp.MatchString(h) {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
sections := strings.Split(h, "-")
|
|
|
|
if len(sections) < 4 {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sections[0]) != 2 {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
ver, err := hex.DecodeString(sections[0])
|
|
|
|
if err != nil {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
version := int(ver[0])
|
|
|
|
if version > maxVersion {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
if version == 0 && len(sections) != 4 {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sections[1]) != 32 {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
var sc core.SpanContext
|
|
|
|
|
2019-10-28 19:05:06 +02:00
|
|
|
sc.TraceID, err = core.TraceIDFromHex(sections[1][:32])
|
2019-09-23 20:51:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sections[2]) != 16 {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
2019-11-18 20:47:04 +02:00
|
|
|
sc.SpanID, err = core.SpanIDFromHex(sections[2])
|
2019-09-23 20:51:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sections[3]) != 2 {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
opts, err := hex.DecodeString(sections[3])
|
|
|
|
if err != nil || len(opts) < 1 || (version == 0 && opts[0] > 2) {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
2019-09-25 23:37:36 +02:00
|
|
|
sc.TraceFlags = opts[0] &^ core.TraceFlagsUnused
|
2019-09-23 20:51:32 +02:00
|
|
|
|
|
|
|
if !sc.IsValid() {
|
|
|
|
return core.EmptySpanContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|
|
|
|
|
2020-02-14 09:16:05 +02:00
|
|
|
func (TraceContext) GetAllKeys() []string {
|
2020-02-20 20:31:21 +02:00
|
|
|
return []string{TraceparentHeader}
|
2019-09-23 20:51:32 +02:00
|
|
|
}
|