From 7549906bff3853c8efea1b99df9860d23a6fabf1 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 25 Jan 2021 15:53:25 -0800 Subject: [PATCH 01/12] adding propagator for opentracing header format This commit adds support for the OpenTracing header format --- propagators/opentracing/doc.go | 16 ++ .../opentracing/opentracing_data_test.go | 221 ++++++++++++++++++ .../opentracing/opentracing_example_test.go | 26 +++ .../opentracing_integration_test.go | 117 ++++++++++ .../opentracing/opentracing_propagator.go | 139 +++++++++++ .../opentracing_propagator_test.go | 147 ++++++++++++ 6 files changed, 666 insertions(+) create mode 100644 propagators/opentracing/doc.go create mode 100644 propagators/opentracing/opentracing_data_test.go create mode 100644 propagators/opentracing/opentracing_example_test.go create mode 100644 propagators/opentracing/opentracing_integration_test.go create mode 100644 propagators/opentracing/opentracing_propagator.go create mode 100644 propagators/opentracing/opentracing_propagator_test.go diff --git a/propagators/opentracing/doc.go b/propagators/opentracing/doc.go new file mode 100644 index 00000000000..8e7915ff245 --- /dev/null +++ b/propagators/opentracing/doc.go @@ -0,0 +1,16 @@ +// 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. + +// This package implements the OpenTracing propagator +package opentracing // import "go.opentelemetry.io/contrib/propagators/opentracing" diff --git a/propagators/opentracing/opentracing_data_test.go b/propagators/opentracing/opentracing_data_test.go new file mode 100644 index 00000000000..11d4d80b431 --- /dev/null +++ b/propagators/opentracing/opentracing_data_test.go @@ -0,0 +1,221 @@ +// 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 opentracing_test + +import ( + "go.opentelemetry.io/otel/api/trace" +) + +const ( + traceID16Str = "a3ce929d0e0e4736" + traceID32Str = "a1ce929d0e0e4736a3ce929d0e0e4736" + spanIDStr = "00f067aa0ba902b7" + traceIDHeader = "ot-tracer-traceid" + spanIDHeader = "ot-tracer-spanid" + sampledHeader = "ot-tracer-sampled" +) + +var ( + traceID16 = trace.ID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36} + traceID32 = trace.ID{0xa1, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36} + spanID = trace.SpanID{0x00, 0xf0, 0x67, 0xaa, 0x0b, 0xa9, 0x02, 0xb7} +) + +type extractTest struct { + name string + headers map[string]string + expected trace.SpanContext +} + +var extractHeaders = []extractTest{ + { + "empty", + map[string]string{}, + trace.SpanContext{}, + }, + { + "sampling state not sample", + map[string]string{ + traceIDHeader: traceID32Str, + spanIDHeader: spanIDStr, + sampledHeader: "0", + }, + trace.SpanContext{ + TraceID: traceID32, + SpanID: spanID, + }, + }, + { + "sampling state sampled", + map[string]string{ + traceIDHeader: traceID32Str, + spanIDHeader: spanIDStr, + sampledHeader: "1", + }, + trace.SpanContext{ + TraceID: traceID32, + SpanID: spanID, + TraceFlags: trace.FlagsSampled, + }, + }, + { + "left padding 64 bit trace ID", + map[string]string{ + traceIDHeader: traceID16Str, + spanIDHeader: spanIDStr, + sampledHeader: "1", + }, + trace.SpanContext{ + TraceID: traceID16, + SpanID: spanID, + TraceFlags: trace.FlagsSampled, + }, + }, + { + "128 bit trace ID", + map[string]string{ + traceIDHeader: traceID32Str, + spanIDHeader: spanIDStr, + sampledHeader: "1", + }, + trace.SpanContext{ + TraceID: traceID32, + SpanID: spanID, + TraceFlags: trace.FlagsSampled, + }, + }, +} + +var invalidExtractHeaders = []extractTest{ + { + name: "trace ID length > 32", + headers: map[string]string{ + traceIDHeader: traceID32Str + "0000", + spanIDHeader: spanIDStr, + sampledHeader: "1", + }, + }, + { + name: "trace ID length is not 32 or 16", + headers: map[string]string{ + traceIDHeader: "1234567890abcd01234", + spanIDHeader: spanIDStr, + sampledHeader: "1", + }, + }, + { + name: "span ID length is not 16 or 32", + headers: map[string]string{ + traceIDHeader: traceID32Str, + spanIDHeader: spanIDStr + "0000", + sampledHeader: "1", + }, + }, + { + name: "invalid trace ID", + headers: map[string]string{ + traceIDHeader: "zcd00v0000000000a3ce929d0e0e4736", + spanIDHeader: spanIDStr, + sampledHeader: "1", + }, + }, + { + name: "invalid span ID", + headers: map[string]string{ + traceIDHeader: traceID32Str, + spanIDHeader: "00f0wiredba902b7", + sampledHeader: "1", + }, + }, + { + name: "invalid sampled", + headers: map[string]string{ + traceIDHeader: traceID32Str, + spanIDHeader: spanIDStr, + sampledHeader: "wired", + }, + }, + { + name: "missing headers", + headers: map[string]string{}, + }, + { + name: "empty header value", + headers: map[string]string{ + traceIDHeader: "", + }, + }, +} + +type injectTest struct { + name string + sc trace.SpanContext + wantHeaders map[string]string +} + +var injectHeaders = []injectTest{ + { + name: "sampled", + sc: trace.SpanContext{ + TraceID: traceID32, + SpanID: spanID, + TraceFlags: trace.FlagsSampled, + }, + wantHeaders: map[string]string{ + traceIDHeader: traceID16Str, + spanIDHeader: spanIDStr, + sampledHeader: "1", + }, + }, + { + name: "not sampled", + sc: trace.SpanContext{ + TraceID: traceID32, + SpanID: spanID, + }, + wantHeaders: map[string]string{ + traceIDHeader: traceID16Str, + spanIDHeader: spanIDStr, + sampledHeader: "0", + }, + }, +} + +var invalidInjectHeaders = []injectTest{ + { + name: "empty", + sc: trace.SpanContext{}, + }, + { + name: "missing traceID", + sc: trace.SpanContext{ + SpanID: spanID, + TraceFlags: trace.FlagsSampled, + }, + }, + { + name: "missing spanID", + sc: trace.SpanContext{ + TraceID: traceID32, + TraceFlags: trace.FlagsSampled, + }, + }, + { + name: "missing both traceID and spanID", + sc: trace.SpanContext{ + TraceFlags: trace.FlagsSampled, + }, + }, +} diff --git a/propagators/opentracing/opentracing_example_test.go b/propagators/opentracing/opentracing_example_test.go new file mode 100644 index 00000000000..3d99c3eff24 --- /dev/null +++ b/propagators/opentracing/opentracing_example_test.go @@ -0,0 +1,26 @@ +// 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 opentracing_test + +import ( + "go.opentelemetry.io/contrib/propagators/opentracing" + "go.opentelemetry.io/otel/api/global" +) + +func ExampleOpenTracing() { + ot := opentracing.OpenTracing{} + // register ot propagator + global.SetTextMapPropagator(ot) +} diff --git a/propagators/opentracing/opentracing_integration_test.go b/propagators/opentracing/opentracing_integration_test.go new file mode 100644 index 00000000000..0db3a0a9fb1 --- /dev/null +++ b/propagators/opentracing/opentracing_integration_test.go @@ -0,0 +1,117 @@ +// 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 opentracing_test + +import ( + "context" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" + + mocktracer "go.opentelemetry.io/contrib/internal/trace" + "go.opentelemetry.io/contrib/propagators/opentracing" + "go.opentelemetry.io/otel/api/trace" +) + +var ( + mockTracer = mocktracer.NewTracer("") + _, mockSpan = mockTracer.Start(context.Background(), "") +) + +func TestExtractOpenTracing(t *testing.T) { + testGroup := []struct { + name string + testcases []extractTest + }{ + { + name: "valid test case", + testcases: extractHeaders, + }, + { + name: "invalid test case", + testcases: invalidExtractHeaders, + }, + } + + for _, tg := range testGroup { + propagator := opentracing.OpenTracing{} + + for _, tc := range tg.testcases { + t.Run(tc.name, func(t *testing.T) { + req, _ := http.NewRequest("GET", "http://example.com", nil) + for k, v := range tc.headers { + req.Header.Set(k, v) + } + + ctx := context.Background() + ctx = propagator.Extract(ctx, req.Header) + resSc := trace.RemoteSpanContextFromContext(ctx) + if diff := cmp.Diff(resSc, tc.expected); diff != "" { + t.Errorf("%s: %s: -got +want %s", tg.name, tc.name, diff) + } + }) + } + } +} + +type testSpan struct { + trace.Span + sc trace.SpanContext +} + +func (s testSpan) SpanContext() trace.SpanContext { + return s.sc +} + +func TestInjectOpenTracing(t *testing.T) { + testGroup := []struct { + name string + testcases []injectTest + }{ + { + name: "valid test case", + testcases: injectHeaders, + }, + { + name: "invalid test case", + testcases: invalidInjectHeaders, + }, + } + + for _, tg := range testGroup { + for _, tc := range tg.testcases { + propagator := opentracing.OpenTracing{} + t.Run(tc.name, func(t *testing.T) { + req, _ := http.NewRequest("GET", "http://example.com", nil) + ctx := trace.ContextWithSpan( + context.Background(), + testSpan{ + Span: mockSpan, + sc: tc.sc, + }, + ) + propagator.Inject(ctx, req.Header) + + for h, v := range tc.wantHeaders { + result, want := req.Header.Get(h), v + if diff := cmp.Diff(result, want); diff != "" { + t.Errorf("%s: %s, header=%s: -got +want %s", tg.name, tc.name, h, diff) + } + } + }) + } + } +} diff --git a/propagators/opentracing/opentracing_propagator.go b/propagators/opentracing/opentracing_propagator.go new file mode 100644 index 00000000000..5219cf231c4 --- /dev/null +++ b/propagators/opentracing/opentracing_propagator.go @@ -0,0 +1,139 @@ +// 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 opentracing + +// TODO: support baggage + +import ( + "context" + "errors" + "strings" + + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/api/trace" +) + +const ( + // Default OpenTracing Header names. + traceIDHeader = "ot-tracer-traceid" + spanIDHeader = "ot-tracer-spanid" + sampledHeader = "ot-tracer-sampled" + + otTraceIDPadding = "0000000000000000" + + traceID64BitsWidth = 64 / 4 // 16 hex character Trace ID. +) + +var ( + empty = trace.EmptySpanContext() + + errInvalidSampledHeader = errors.New("invalid OpenTracing Sampled header found") + errInvalidTraceIDHeader = errors.New("invalid OpenTracing traceID header found") + errInvalidSpanIDHeader = errors.New("invalid OpenTracing spanID header found") + errInvalidScope = errors.New("require either both traceID and spanID or none") +) + +// OpenTracing propagator serializes SpanContext to/from OpenTracing Headers. +type OpenTracing struct { +} + +var _ otel.TextMapPropagator = OpenTracing{} + +// Inject injects a context into the carrier as OpenTracing headers. +// NOTE: In order to interop with systems that use the OpenTracing header format, trace ids MUST be 64-bits +func (ot OpenTracing) Inject(ctx context.Context, carrier otel.TextMapCarrier) { + sc := trace.SpanFromContext(ctx).SpanContext() + + if sc.TraceID.IsValid() && sc.SpanID.IsValid() { + carrier.Set(traceIDHeader, sc.TraceID.String()[len(sc.TraceID.String())-traceID64BitsWidth:]) + carrier.Set(spanIDHeader, sc.SpanID.String()) + } + + if sc.IsSampled() { + carrier.Set(sampledHeader, "1") + } else { + carrier.Set(sampledHeader, "0") + } + +} + +// Extract extracts a context from the carrier if it contains OpenTracing headers. +func (ot OpenTracing) Extract(ctx context.Context, carrier otel.TextMapCarrier) context.Context { + var ( + sc trace.SpanContext + err error + ) + + var ( + traceID = carrier.Get(traceIDHeader) + spanID = carrier.Get(spanIDHeader) + sampled = carrier.Get(sampledHeader) + ) + sc, err = extract(traceID, spanID, sampled) + if err != nil || !sc.IsValid() { + return ctx + } + return trace.ContextWithRemoteSpanContext(ctx, sc) +} + +func (ot OpenTracing) Fields() []string { + return []string{traceIDHeader, spanIDHeader, sampledHeader} +} + +// extract reconstructs a SpanContext from header values based on OpenTracing +// headers. +func extract(traceID, spanID, sampled string) (trace.SpanContext, error) { + var ( + err error + requiredCount int + sc = trace.SpanContext{} + ) + + switch strings.ToLower(sampled) { + case "0", "false": + // Zero value for TraceFlags sample bit is unset. + case "1", "true": + sc.TraceFlags = trace.FlagsSampled + case "": + sc.TraceFlags = trace.FlagsDeferred + default: + return empty, errInvalidSampledHeader + } + + if traceID != "" { + requiredCount++ + id := traceID + if len(traceID) == 16 { + // Pad 64-bit trace IDs. + id = otTraceIDPadding + traceID + } + if sc.TraceID, err = trace.IDFromHex(id); err != nil { + return empty, errInvalidTraceIDHeader + } + } + + if spanID != "" { + requiredCount++ + if sc.SpanID, err = trace.SpanIDFromHex(spanID); err != nil { + return empty, errInvalidSpanIDHeader + } + } + + if requiredCount != 0 && requiredCount != 2 { + return empty, errInvalidScope + } + + return sc, nil +} diff --git a/propagators/opentracing/opentracing_propagator_test.go b/propagators/opentracing/opentracing_propagator_test.go new file mode 100644 index 00000000000..bb05dc5e86a --- /dev/null +++ b/propagators/opentracing/opentracing_propagator_test.go @@ -0,0 +1,147 @@ +// 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 opentracing + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/otel/api/trace" +) + +var ( + traceID = trace.ID{0, 0, 0, 0, 0, 0, 0, 0, 0x7b, 0, 0, 0, 0, 0, 0x1, 0xc8} + traceID128Str = "00000000000000007b000000000001c8" + zeroTraceIDStr = "00000000000000000000000000000000" + traceID64Str = "7b000000000001c8" + spanID = trace.SpanID{0, 0, 0, 0, 0, 0, 0, 0x7b} + zeroSpanIDStr = "0000000000000000" + spanIDStr = "000000000000007b" +) + +func TestOpenTracing_Extract(t *testing.T) { + testData := []struct { + traceID string + spanID string + sampled string + expected trace.SpanContext + err error + }{ + { + traceID128Str, spanIDStr, "1", + trace.SpanContext{ + TraceID: traceID, + SpanID: spanID, + TraceFlags: trace.FlagsSampled, + }, + nil, + }, + { + traceID64Str, spanIDStr, "1", + trace.SpanContext{ + TraceID: traceID, + SpanID: spanID, + TraceFlags: trace.FlagsSampled, + }, + nil, + }, + { + traceID128Str, spanIDStr, "", + trace.SpanContext{ + TraceID: traceID, + SpanID: spanID, + TraceFlags: trace.FlagsDeferred, + }, + nil, + }, + { + // if we didn't set sampled bit when debug bit is 1, then assuming it's not sampled + traceID128Str, spanIDStr, "0", + trace.SpanContext{ + TraceID: traceID, + SpanID: spanID, + TraceFlags: 0x00, + }, + nil, + }, + { + traceID128Str, spanIDStr, "1", + trace.SpanContext{ + TraceID: traceID, + SpanID: spanID, + TraceFlags: trace.FlagsSampled, + }, + nil, + }, + { + fmt.Sprintf("%32s", "This_is_a_string_len_64"), spanIDStr, "1", + trace.SpanContext{}, + errInvalidTraceIDHeader, + }, + { + "000000000007b00000000000001c8", spanIDStr, "1", + trace.SpanContext{}, + errInvalidTraceIDHeader, + }, + { + traceID128Str, fmt.Sprintf("%16s", "wiredspanid"), "1", + trace.SpanContext{}, + errInvalidSpanIDHeader, + }, + { + traceID128Str, "0000000000010", "1", + trace.SpanContext{}, + errInvalidSpanIDHeader, + }, + { + // reject invalid traceID(0) and spanID(0) + zeroTraceIDStr, zeroSpanIDStr, "1", + trace.SpanContext{}, + errInvalidTraceIDHeader, + }, + { + // reject invalid spanID(0) + traceID128Str, zeroSpanIDStr, "1", + trace.SpanContext{}, + errInvalidSpanIDHeader, + }, + { + // reject invalid spanID(0) + traceID128Str, spanIDStr, "invalid", + trace.SpanContext{}, + errInvalidSampledHeader, + }, + } + + for _, test := range testData { + // headerVal := strings.Join([]string{}, separator) + sc, err := extract(test.traceID, test.spanID, test.sampled) + + info := []interface{}{ + "trace ID: %q, span ID: %q, sampled: %q", + test.traceID, + test.spanID, + test.sampled, + } + + if !assert.Equal(t, test.err, err, info...) { + continue + } + + assert.Equal(t, test.expected, sc, info...) + } +} From 33f8681906cf65bffa4b22fd79ce3dbd7b8c8fc0 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Tue, 26 Jan 2021 09:39:30 -0800 Subject: [PATCH 02/12] update to 0.16.0 --- propagators/opentracing/opentracing_data_test.go | 6 +++--- .../opentracing/opentracing_example_test.go | 4 ++-- .../opentracing/opentracing_integration_test.go | 8 ++++---- propagators/opentracing/opentracing_propagator.go | 14 +++++++------- .../opentracing/opentracing_propagator_test.go | 4 ++-- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/propagators/opentracing/opentracing_data_test.go b/propagators/opentracing/opentracing_data_test.go index 11d4d80b431..3289f27a1db 100644 --- a/propagators/opentracing/opentracing_data_test.go +++ b/propagators/opentracing/opentracing_data_test.go @@ -15,7 +15,7 @@ package opentracing_test import ( - "go.opentelemetry.io/otel/api/trace" + "go.opentelemetry.io/otel/trace" ) const ( @@ -28,8 +28,8 @@ const ( ) var ( - traceID16 = trace.ID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36} - traceID32 = trace.ID{0xa1, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36} + traceID16 = trace.TraceID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36} + traceID32 = trace.TraceID{0xa1, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36} spanID = trace.SpanID{0x00, 0xf0, 0x67, 0xaa, 0x0b, 0xa9, 0x02, 0xb7} ) diff --git a/propagators/opentracing/opentracing_example_test.go b/propagators/opentracing/opentracing_example_test.go index 3d99c3eff24..abfc75eebb2 100644 --- a/propagators/opentracing/opentracing_example_test.go +++ b/propagators/opentracing/opentracing_example_test.go @@ -16,11 +16,11 @@ package opentracing_test import ( "go.opentelemetry.io/contrib/propagators/opentracing" - "go.opentelemetry.io/otel/api/global" + "go.opentelemetry.io/otel" ) func ExampleOpenTracing() { ot := opentracing.OpenTracing{} // register ot propagator - global.SetTextMapPropagator(ot) + otel.SetTextMapPropagator(ot) } diff --git a/propagators/opentracing/opentracing_integration_test.go b/propagators/opentracing/opentracing_integration_test.go index 0db3a0a9fb1..53f5af4dc06 100644 --- a/propagators/opentracing/opentracing_integration_test.go +++ b/propagators/opentracing/opentracing_integration_test.go @@ -21,13 +21,13 @@ import ( "github.com/google/go-cmp/cmp" - mocktracer "go.opentelemetry.io/contrib/internal/trace" "go.opentelemetry.io/contrib/propagators/opentracing" - "go.opentelemetry.io/otel/api/trace" + "go.opentelemetry.io/otel/oteltest" + "go.opentelemetry.io/otel/trace" ) var ( - mockTracer = mocktracer.NewTracer("") + mockTracer = oteltest.NewTracerProvider().Tracer("") _, mockSpan = mockTracer.Start(context.Background(), "") ) @@ -59,7 +59,7 @@ func TestExtractOpenTracing(t *testing.T) { ctx := context.Background() ctx = propagator.Extract(ctx, req.Header) resSc := trace.RemoteSpanContextFromContext(ctx) - if diff := cmp.Diff(resSc, tc.expected); diff != "" { + if diff := cmp.Diff(resSc, tc.expected, cmp.AllowUnexported(trace.TraceState{})); diff != "" { t.Errorf("%s: %s: -got +want %s", tg.name, tc.name, diff) } }) diff --git a/propagators/opentracing/opentracing_propagator.go b/propagators/opentracing/opentracing_propagator.go index 5219cf231c4..0c9ab324d2b 100644 --- a/propagators/opentracing/opentracing_propagator.go +++ b/propagators/opentracing/opentracing_propagator.go @@ -21,8 +21,8 @@ import ( "errors" "strings" - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/api/trace" + "go.opentelemetry.io/otel/propagation" + "go.opentelemetry.io/otel/trace" ) const ( @@ -37,7 +37,7 @@ const ( ) var ( - empty = trace.EmptySpanContext() + empty = trace.SpanContext{} errInvalidSampledHeader = errors.New("invalid OpenTracing Sampled header found") errInvalidTraceIDHeader = errors.New("invalid OpenTracing traceID header found") @@ -49,11 +49,11 @@ var ( type OpenTracing struct { } -var _ otel.TextMapPropagator = OpenTracing{} +var _ propagation.TextMapPropagator = OpenTracing{} // Inject injects a context into the carrier as OpenTracing headers. // NOTE: In order to interop with systems that use the OpenTracing header format, trace ids MUST be 64-bits -func (ot OpenTracing) Inject(ctx context.Context, carrier otel.TextMapCarrier) { +func (ot OpenTracing) Inject(ctx context.Context, carrier propagation.TextMapCarrier) { sc := trace.SpanFromContext(ctx).SpanContext() if sc.TraceID.IsValid() && sc.SpanID.IsValid() { @@ -70,7 +70,7 @@ func (ot OpenTracing) Inject(ctx context.Context, carrier otel.TextMapCarrier) { } // Extract extracts a context from the carrier if it contains OpenTracing headers. -func (ot OpenTracing) Extract(ctx context.Context, carrier otel.TextMapCarrier) context.Context { +func (ot OpenTracing) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context { var ( sc trace.SpanContext err error @@ -119,7 +119,7 @@ func extract(traceID, spanID, sampled string) (trace.SpanContext, error) { // Pad 64-bit trace IDs. id = otTraceIDPadding + traceID } - if sc.TraceID, err = trace.IDFromHex(id); err != nil { + if sc.TraceID, err = trace.TraceIDFromHex(id); err != nil { return empty, errInvalidTraceIDHeader } } diff --git a/propagators/opentracing/opentracing_propagator_test.go b/propagators/opentracing/opentracing_propagator_test.go index bb05dc5e86a..5f2d0ff7fe1 100644 --- a/propagators/opentracing/opentracing_propagator_test.go +++ b/propagators/opentracing/opentracing_propagator_test.go @@ -20,11 +20,11 @@ import ( "github.com/stretchr/testify/assert" - "go.opentelemetry.io/otel/api/trace" + "go.opentelemetry.io/otel/trace" ) var ( - traceID = trace.ID{0, 0, 0, 0, 0, 0, 0, 0, 0x7b, 0, 0, 0, 0, 0, 0x1, 0xc8} + traceID = trace.TraceID{0, 0, 0, 0, 0, 0, 0, 0, 0x7b, 0, 0, 0, 0, 0, 0x1, 0xc8} traceID128Str = "00000000000000007b000000000001c8" zeroTraceIDStr = "00000000000000000000000000000000" traceID64Str = "7b000000000001c8" From dc45688a5784f3f78c2501f53d3911c5febc948b Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Tue, 26 Jan 2021 15:40:15 -0800 Subject: [PATCH 03/12] implementing baggage injection, extract still needs work --- .../opentracing/opentracing_data_test.go | 52 ++++++++++++++----- .../opentracing_integration_test.go | 21 +++++++- .../opentracing/opentracing_propagator.go | 17 +++++- .../opentracing_propagator_test.go | 1 - 4 files changed, 74 insertions(+), 17 deletions(-) diff --git a/propagators/opentracing/opentracing_data_test.go b/propagators/opentracing/opentracing_data_test.go index 3289f27a1db..a210096edf1 100644 --- a/propagators/opentracing/opentracing_data_test.go +++ b/propagators/opentracing/opentracing_data_test.go @@ -15,28 +15,41 @@ package opentracing_test import ( + "go.opentelemetry.io/otel/label" "go.opentelemetry.io/otel/trace" ) const ( - traceID16Str = "a3ce929d0e0e4736" - traceID32Str = "a1ce929d0e0e4736a3ce929d0e0e4736" - spanIDStr = "00f067aa0ba902b7" - traceIDHeader = "ot-tracer-traceid" - spanIDHeader = "ot-tracer-spanid" - sampledHeader = "ot-tracer-sampled" + traceID16Str = "a3ce929d0e0e4736" + traceID32Str = "a1ce929d0e0e4736a3ce929d0e0e4736" + spanIDStr = "00f067aa0ba902b7" + traceIDHeader = "ot-tracer-traceid" + spanIDHeader = "ot-tracer-spanid" + sampledHeader = "ot-tracer-sampled" + baggageKey = "test" + baggageValue = "value123" + baggageHeader = "ot-baggage-test" + baggageKey2 = "test2" + baggageValue2 = "value456" + baggageHeader2 = "ot-baggage-test2" ) var ( - traceID16 = trace.TraceID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36} - traceID32 = trace.TraceID{0xa1, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36} - spanID = trace.SpanID{0x00, 0xf0, 0x67, 0xaa, 0x0b, 0xa9, 0x02, 0xb7} + traceID16 = trace.TraceID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36} + traceID32 = trace.TraceID{0xa1, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36} + spanID = trace.SpanID{0x00, 0xf0, 0x67, 0xaa, 0x0b, 0xa9, 0x02, 0xb7} + emptyBaggage = &label.Set{} + baggageSet = label.NewSet( + label.String(baggageKey, baggageValue), + label.String(baggageKey2, baggageValue2), + ) ) type extractTest struct { name string headers map[string]string expected trace.SpanContext + baggage *label.Set } var extractHeaders = []extractTest{ @@ -44,6 +57,7 @@ var extractHeaders = []extractTest{ "empty", map[string]string{}, trace.SpanContext{}, + emptyBaggage, }, { "sampling state not sample", @@ -56,6 +70,7 @@ var extractHeaders = []extractTest{ TraceID: traceID32, SpanID: spanID, }, + emptyBaggage, }, { "sampling state sampled", @@ -63,12 +78,16 @@ var extractHeaders = []extractTest{ traceIDHeader: traceID32Str, spanIDHeader: spanIDStr, sampledHeader: "1", + baggageHeader: baggageValue, }, trace.SpanContext{ TraceID: traceID32, SpanID: spanID, TraceFlags: trace.FlagsSampled, }, + emptyBaggage, + // TODO: once baggage extraction is supported, re-enable this + // &baggageSet, }, { "left padding 64 bit trace ID", @@ -82,6 +101,7 @@ var extractHeaders = []extractTest{ SpanID: spanID, TraceFlags: trace.FlagsSampled, }, + emptyBaggage, }, { "128 bit trace ID", @@ -95,6 +115,7 @@ var extractHeaders = []extractTest{ SpanID: spanID, TraceFlags: trace.FlagsSampled, }, + emptyBaggage, }, } @@ -163,6 +184,7 @@ type injectTest struct { name string sc trace.SpanContext wantHeaders map[string]string + baggage []label.KeyValue } var injectHeaders = []injectTest{ @@ -185,10 +207,16 @@ var injectHeaders = []injectTest{ TraceID: traceID32, SpanID: spanID, }, + baggage: []label.KeyValue{ + label.String(baggageKey, baggageValue), + label.String(baggageKey2, baggageValue2), + }, wantHeaders: map[string]string{ - traceIDHeader: traceID16Str, - spanIDHeader: spanIDStr, - sampledHeader: "0", + traceIDHeader: traceID16Str, + spanIDHeader: spanIDStr, + sampledHeader: "0", + baggageHeader: baggageValue, + baggageHeader2: baggageValue2, }, }, } diff --git a/propagators/opentracing/opentracing_integration_test.go b/propagators/opentracing/opentracing_integration_test.go index 53f5af4dc06..71012042636 100644 --- a/propagators/opentracing/opentracing_integration_test.go +++ b/propagators/opentracing/opentracing_integration_test.go @@ -22,6 +22,7 @@ import ( "github.com/google/go-cmp/cmp" "go.opentelemetry.io/contrib/propagators/opentracing" + "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/oteltest" "go.opentelemetry.io/otel/trace" ) @@ -62,6 +63,18 @@ func TestExtractOpenTracing(t *testing.T) { if diff := cmp.Diff(resSc, tc.expected, cmp.AllowUnexported(trace.TraceState{})); diff != "" { t.Errorf("%s: %s: -got +want %s", tg.name, tc.name, diff) } + m := baggage.Set(ctx) + mi := tc.baggage.Iter() + for mi.Next() { + label := mi.Label() + val, ok := m.Value(label.Key) + if !ok { + t.Errorf("%s: %s: expected key '%s'", tg.name, tc.name, label.Key) + } + if diff := cmp.Diff(label.Value.AsString(), val.AsString()); diff != "" { + t.Errorf("%s: %s: -got +want %s", tg.name, tc.name, diff) + } + } }) } } @@ -96,8 +109,12 @@ func TestInjectOpenTracing(t *testing.T) { propagator := opentracing.OpenTracing{} t.Run(tc.name, func(t *testing.T) { req, _ := http.NewRequest("GET", "http://example.com", nil) - ctx := trace.ContextWithSpan( - context.Background(), + + ctx := baggage.ContextWithValues(context.Background(), + tc.baggage..., + ) + ctx = trace.ContextWithSpan( + ctx, testSpan{ Span: mockSpan, sc: tc.sc, diff --git a/propagators/opentracing/opentracing_propagator.go b/propagators/opentracing/opentracing_propagator.go index 0c9ab324d2b..79336f49bd6 100644 --- a/propagators/opentracing/opentracing_propagator.go +++ b/propagators/opentracing/opentracing_propagator.go @@ -14,13 +14,14 @@ package opentracing -// TODO: support baggage - import ( "context" "errors" + "fmt" "strings" + "go.opentelemetry.io/otel/baggage" + "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/trace" ) @@ -67,6 +68,14 @@ func (ot OpenTracing) Inject(ctx context.Context, carrier propagation.TextMapCar carrier.Set(sampledHeader, "0") } + m := baggage.Set(ctx) + mi := m.Iter() + + for mi.Next() { + label := mi.Label() + carrier.Set(fmt.Sprintf("ot-baggage-%s", label.Key), label.Value.Emit()) + } + } // Extract extracts a context from the carrier if it contains OpenTracing headers. @@ -85,6 +94,10 @@ func (ot OpenTracing) Extract(ctx context.Context, carrier propagation.TextMapCa if err != nil || !sc.IsValid() { return ctx } + // TODO: implement extracting baggage + // + // this currently is not achievable without an implementation of `keys` + // on the carrier return trace.ContextWithRemoteSpanContext(ctx, sc) } diff --git a/propagators/opentracing/opentracing_propagator_test.go b/propagators/opentracing/opentracing_propagator_test.go index 5f2d0ff7fe1..1ad57f4a299 100644 --- a/propagators/opentracing/opentracing_propagator_test.go +++ b/propagators/opentracing/opentracing_propagator_test.go @@ -128,7 +128,6 @@ func TestOpenTracing_Extract(t *testing.T) { } for _, test := range testData { - // headerVal := strings.Join([]string{}, separator) sc, err := extract(test.traceID, test.spanID, test.sampled) info := []interface{}{ From 7e0cfc6537b5684edb211f0aebd23ee5e676cae8 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Wed, 27 Jan 2021 08:44:24 -0800 Subject: [PATCH 04/12] comment out unused test, will re-enable in the future --- propagators/opentracing/opentracing_data_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/propagators/opentracing/opentracing_data_test.go b/propagators/opentracing/opentracing_data_test.go index a210096edf1..2554f5d559d 100644 --- a/propagators/opentracing/opentracing_data_test.go +++ b/propagators/opentracing/opentracing_data_test.go @@ -39,10 +39,11 @@ var ( traceID32 = trace.TraceID{0xa1, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36, 0xa3, 0xce, 0x92, 0x9d, 0x0e, 0x0e, 0x47, 0x36} spanID = trace.SpanID{0x00, 0xf0, 0x67, 0xaa, 0x0b, 0xa9, 0x02, 0xb7} emptyBaggage = &label.Set{} - baggageSet = label.NewSet( - label.String(baggageKey, baggageValue), - label.String(baggageKey2, baggageValue2), - ) + // TODO: once baggage extraction is supported, re-enable this + // baggageSet = label.NewSet( + // label.String(baggageKey, baggageValue), + // label.String(baggageKey2, baggageValue2), + // ) ) type extractTest struct { From 236628ef48876c44a430568fd4a9ec6967c96d14 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 1 Feb 2021 09:34:17 -0800 Subject: [PATCH 05/12] update name --- propagators/{opentracing => ot}/doc.go | 2 +- .../ot_data_test.go} | 2 +- .../ot_example_test.go} | 10 +++---- .../ot_integration_test.go} | 12 ++++---- .../ot_propagator.go} | 30 +++++++++---------- .../ot_propagator_test.go} | 4 +-- 6 files changed, 30 insertions(+), 30 deletions(-) rename propagators/{opentracing => ot}/doc.go (88%) rename propagators/{opentracing/opentracing_data_test.go => ot/ot_data_test.go} (99%) rename propagators/{opentracing/opentracing_example_test.go => ot/ot_example_test.go} (79%) rename propagators/{opentracing/opentracing_integration_test.go => ot/ot_integration_test.go} (92%) rename propagators/{opentracing/opentracing_propagator.go => ot/ot_propagator.go} (76%) rename propagators/{opentracing/opentracing_propagator_test.go => ot/ot_propagator_test.go} (98%) diff --git a/propagators/opentracing/doc.go b/propagators/ot/doc.go similarity index 88% rename from propagators/opentracing/doc.go rename to propagators/ot/doc.go index 8e7915ff245..7e6f0379f58 100644 --- a/propagators/opentracing/doc.go +++ b/propagators/ot/doc.go @@ -13,4 +13,4 @@ // limitations under the License. // This package implements the OpenTracing propagator -package opentracing // import "go.opentelemetry.io/contrib/propagators/opentracing" +package ot // import "go.opentelemetry.io/contrib/propagators/ot" diff --git a/propagators/opentracing/opentracing_data_test.go b/propagators/ot/ot_data_test.go similarity index 99% rename from propagators/opentracing/opentracing_data_test.go rename to propagators/ot/ot_data_test.go index 2554f5d559d..316136e4bb0 100644 --- a/propagators/opentracing/opentracing_data_test.go +++ b/propagators/ot/ot_data_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package opentracing_test +package ot_test import ( "go.opentelemetry.io/otel/label" diff --git a/propagators/opentracing/opentracing_example_test.go b/propagators/ot/ot_example_test.go similarity index 79% rename from propagators/opentracing/opentracing_example_test.go rename to propagators/ot/ot_example_test.go index abfc75eebb2..66898e76132 100644 --- a/propagators/opentracing/opentracing_example_test.go +++ b/propagators/ot/ot_example_test.go @@ -12,15 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -package opentracing_test +package ot_test import ( - "go.opentelemetry.io/contrib/propagators/opentracing" + "go.opentelemetry.io/contrib/propagators/ot" "go.opentelemetry.io/otel" ) -func ExampleOpenTracing() { - ot := opentracing.OpenTracing{} +func ExampleOT() { + ot_propagator := ot.OT{} // register ot propagator - otel.SetTextMapPropagator(ot) + otel.SetTextMapPropagator(ot_propagator) } diff --git a/propagators/opentracing/opentracing_integration_test.go b/propagators/ot/ot_integration_test.go similarity index 92% rename from propagators/opentracing/opentracing_integration_test.go rename to propagators/ot/ot_integration_test.go index 71012042636..46c7954170a 100644 --- a/propagators/opentracing/opentracing_integration_test.go +++ b/propagators/ot/ot_integration_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package opentracing_test +package ot_test import ( "context" @@ -21,7 +21,7 @@ import ( "github.com/google/go-cmp/cmp" - "go.opentelemetry.io/contrib/propagators/opentracing" + "go.opentelemetry.io/contrib/propagators/ot" "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/oteltest" "go.opentelemetry.io/otel/trace" @@ -32,7 +32,7 @@ var ( _, mockSpan = mockTracer.Start(context.Background(), "") ) -func TestExtractOpenTracing(t *testing.T) { +func TestExtractOT(t *testing.T) { testGroup := []struct { name string testcases []extractTest @@ -48,7 +48,7 @@ func TestExtractOpenTracing(t *testing.T) { } for _, tg := range testGroup { - propagator := opentracing.OpenTracing{} + propagator := ot.OT{} for _, tc := range tg.testcases { t.Run(tc.name, func(t *testing.T) { @@ -89,7 +89,7 @@ func (s testSpan) SpanContext() trace.SpanContext { return s.sc } -func TestInjectOpenTracing(t *testing.T) { +func TestInjectOT(t *testing.T) { testGroup := []struct { name string testcases []injectTest @@ -106,7 +106,7 @@ func TestInjectOpenTracing(t *testing.T) { for _, tg := range testGroup { for _, tc := range tg.testcases { - propagator := opentracing.OpenTracing{} + propagator := ot.OT{} t.Run(tc.name, func(t *testing.T) { req, _ := http.NewRequest("GET", "http://example.com", nil) diff --git a/propagators/opentracing/opentracing_propagator.go b/propagators/ot/ot_propagator.go similarity index 76% rename from propagators/opentracing/opentracing_propagator.go rename to propagators/ot/ot_propagator.go index 79336f49bd6..8ec2ffea411 100644 --- a/propagators/opentracing/opentracing_propagator.go +++ b/propagators/ot/ot_propagator.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package opentracing +package ot import ( "context" @@ -27,7 +27,7 @@ import ( ) const ( - // Default OpenTracing Header names. + // Default OT Header names. traceIDHeader = "ot-tracer-traceid" spanIDHeader = "ot-tracer-spanid" sampledHeader = "ot-tracer-sampled" @@ -40,21 +40,21 @@ const ( var ( empty = trace.SpanContext{} - errInvalidSampledHeader = errors.New("invalid OpenTracing Sampled header found") - errInvalidTraceIDHeader = errors.New("invalid OpenTracing traceID header found") - errInvalidSpanIDHeader = errors.New("invalid OpenTracing spanID header found") + errInvalidSampledHeader = errors.New("invalid OT Sampled header found") + errInvalidTraceIDHeader = errors.New("invalid OT traceID header found") + errInvalidSpanIDHeader = errors.New("invalid OT spanID header found") errInvalidScope = errors.New("require either both traceID and spanID or none") ) -// OpenTracing propagator serializes SpanContext to/from OpenTracing Headers. -type OpenTracing struct { +// OT propagator serializes SpanContext to/from OT Headers. +type OT struct { } -var _ propagation.TextMapPropagator = OpenTracing{} +var _ propagation.TextMapPropagator = OT{} -// Inject injects a context into the carrier as OpenTracing headers. -// NOTE: In order to interop with systems that use the OpenTracing header format, trace ids MUST be 64-bits -func (ot OpenTracing) Inject(ctx context.Context, carrier propagation.TextMapCarrier) { +// Inject injects a context into the carrier as OT headers. +// NOTE: In order to interop with systems that use the OT header format, trace ids MUST be 64-bits +func (o OT) Inject(ctx context.Context, carrier propagation.TextMapCarrier) { sc := trace.SpanFromContext(ctx).SpanContext() if sc.TraceID.IsValid() && sc.SpanID.IsValid() { @@ -78,8 +78,8 @@ func (ot OpenTracing) Inject(ctx context.Context, carrier propagation.TextMapCar } -// Extract extracts a context from the carrier if it contains OpenTracing headers. -func (ot OpenTracing) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context { +// Extract extracts a context from the carrier if it contains OT headers. +func (o OT) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context { var ( sc trace.SpanContext err error @@ -101,11 +101,11 @@ func (ot OpenTracing) Extract(ctx context.Context, carrier propagation.TextMapCa return trace.ContextWithRemoteSpanContext(ctx, sc) } -func (ot OpenTracing) Fields() []string { +func (o OT) Fields() []string { return []string{traceIDHeader, spanIDHeader, sampledHeader} } -// extract reconstructs a SpanContext from header values based on OpenTracing +// extract reconstructs a SpanContext from header values based on OT // headers. func extract(traceID, spanID, sampled string) (trace.SpanContext, error) { var ( diff --git a/propagators/opentracing/opentracing_propagator_test.go b/propagators/ot/ot_propagator_test.go similarity index 98% rename from propagators/opentracing/opentracing_propagator_test.go rename to propagators/ot/ot_propagator_test.go index 1ad57f4a299..4a8e5e0b1ec 100644 --- a/propagators/opentracing/opentracing_propagator_test.go +++ b/propagators/ot/ot_propagator_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package opentracing +package ot import ( "fmt" @@ -33,7 +33,7 @@ var ( spanIDStr = "000000000000007b" ) -func TestOpenTracing_Extract(t *testing.T) { +func TestOT_Extract(t *testing.T) { testData := []struct { traceID string spanID string From 530b2721d9097e67eecbc5d7bac660eb4515023d Mon Sep 17 00:00:00 2001 From: alrex Date: Mon, 1 Feb 2021 11:31:46 -0800 Subject: [PATCH 06/12] Update propagators/ot/ot_propagator.go Co-authored-by: Yuri Shkuro --- propagators/ot/ot_propagator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/propagators/ot/ot_propagator.go b/propagators/ot/ot_propagator.go index 8ec2ffea411..08b10e50ad1 100644 --- a/propagators/ot/ot_propagator.go +++ b/propagators/ot/ot_propagator.go @@ -46,7 +46,7 @@ var ( errInvalidScope = errors.New("require either both traceID and spanID or none") ) -// OT propagator serializes SpanContext to/from OT Headers. +// OT propagator serializes SpanContext to/from ot-trace-* headers. type OT struct { } From 94e6a16cf632009f26b69050526a804d88082da7 Mon Sep 17 00:00:00 2001 From: alrex Date: Mon, 1 Feb 2021 11:43:21 -0800 Subject: [PATCH 07/12] Update propagators/ot/doc.go Co-authored-by: Yuri Shkuro --- propagators/ot/doc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/propagators/ot/doc.go b/propagators/ot/doc.go index 7e6f0379f58..e3a537e78e0 100644 --- a/propagators/ot/doc.go +++ b/propagators/ot/doc.go @@ -12,5 +12,5 @@ // See the License for the specific language governing permissions and // limitations under the License. -// This package implements the OpenTracing propagator +// This package implements the ot-tracer-* propagator used by Basic Tracer implementation from the OpenTracing project package ot // import "go.opentelemetry.io/contrib/propagators/ot" From befccc831e643c63495b64ea847c481d6121853d Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 1 Feb 2021 11:46:10 -0800 Subject: [PATCH 08/12] fix var name --- propagators/ot/ot_example_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/propagators/ot/ot_example_test.go b/propagators/ot/ot_example_test.go index 66898e76132..0d1e87680f7 100644 --- a/propagators/ot/ot_example_test.go +++ b/propagators/ot/ot_example_test.go @@ -20,7 +20,7 @@ import ( ) func ExampleOT() { - ot_propagator := ot.OT{} + otPropagator := ot.OT{} // register ot propagator - otel.SetTextMapPropagator(ot_propagator) + otel.SetTextMapPropagator(otPropagator) } From 0a05d0b696c7f43185fc95d2cba398008525b1f6 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 1 Feb 2021 11:46:29 -0800 Subject: [PATCH 09/12] add link to keys issue --- propagators/ot/ot_propagator.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/propagators/ot/ot_propagator.go b/propagators/ot/ot_propagator.go index 08b10e50ad1..72a07c390b2 100644 --- a/propagators/ot/ot_propagator.go +++ b/propagators/ot/ot_propagator.go @@ -97,7 +97,8 @@ func (o OT) Extract(ctx context.Context, carrier propagation.TextMapCarrier) con // TODO: implement extracting baggage // // this currently is not achievable without an implementation of `keys` - // on the carrier + // on the carrier, see: + // https://github.com/open-telemetry/opentelemetry-go/issues/1493 return trace.ContextWithRemoteSpanContext(ctx, sc) } From dc99fd62e468e00968ad553c8bcf67d2532d9fd7 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 1 Feb 2021 11:50:26 -0800 Subject: [PATCH 10/12] dont inject anything if trace/span ids are not valid --- propagators/ot/ot_propagator.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/propagators/ot/ot_propagator.go b/propagators/ot/ot_propagator.go index 72a07c390b2..7a4f9d5716b 100644 --- a/propagators/ot/ot_propagator.go +++ b/propagators/ot/ot_propagator.go @@ -60,6 +60,9 @@ func (o OT) Inject(ctx context.Context, carrier propagation.TextMapCarrier) { if sc.TraceID.IsValid() && sc.SpanID.IsValid() { carrier.Set(traceIDHeader, sc.TraceID.String()[len(sc.TraceID.String())-traceID64BitsWidth:]) carrier.Set(spanIDHeader, sc.SpanID.String()) + } else { + // don't bother injecting anything if both trace/span IDs are not valid + return } if sc.IsSampled() { From 71cfe26f78823f85c522ebf6512c551286f75245 Mon Sep 17 00:00:00 2001 From: alrex Date: Fri, 5 Feb 2021 08:53:53 -0800 Subject: [PATCH 11/12] Update propagators/ot/ot_propagator.go Co-authored-by: Anthony Mirabella --- propagators/ot/ot_propagator.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/propagators/ot/ot_propagator.go b/propagators/ot/ot_propagator.go index 7a4f9d5716b..0cc9aed30dd 100644 --- a/propagators/ot/ot_propagator.go +++ b/propagators/ot/ot_propagator.go @@ -57,14 +57,14 @@ var _ propagation.TextMapPropagator = OT{} func (o OT) Inject(ctx context.Context, carrier propagation.TextMapCarrier) { sc := trace.SpanFromContext(ctx).SpanContext() - if sc.TraceID.IsValid() && sc.SpanID.IsValid() { - carrier.Set(traceIDHeader, sc.TraceID.String()[len(sc.TraceID.String())-traceID64BitsWidth:]) - carrier.Set(spanIDHeader, sc.SpanID.String()) - } else { - // don't bother injecting anything if both trace/span IDs are not valid + if !sc.TraceID.IsValid() || !sc.SpanID.IsValid() { + // don't bother injecting anything if either trace or span IDs are not valid return } + carrier.Set(traceIDHeader, sc.TraceID.String()[len(sc.TraceID.String())-traceID64BitsWidth:]) + carrier.Set(spanIDHeader, sc.SpanID.String()) + if sc.IsSampled() { carrier.Set(sampledHeader, "1") } else { From fdf131f6c9a8d631cd646f7dcac54cc11af1c6e1 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Fri, 5 Feb 2021 08:55:22 -0800 Subject: [PATCH 12/12] update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91b685de7d9..39ecbbca8a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Added + +- Adding `ot-tracer` propagator (#562) + ## [0.16.0] - 2021-01-13 ### Fixed