From 6b4acf47b8cd3b106cc29d3ad6cad764ebf880fb Mon Sep 17 00:00:00 2001 From: Krzesimir Nowak Date: Tue, 28 Jan 2020 19:13:46 +0100 Subject: [PATCH] Reorganize propagation code (shrink PR 381) (#444) * Rename distributedcontext package to correlation Correlation is the name we agreed upon. * Move trace propagators to api/trace The trace propagators tests had to be moved to a testtrace subpackage to avoid import cycles between api/trace and internal/trace. Needed to shut up golint about stutter in trace.TraceContext - TraceContext is a name of a W3C spec, so this stutter is expected. It's certainly still better than golint's suggestion of having trace.Context. * Rename api/propagators to api/propagation This package will not contain any propagators in the long run, just the interface definitions. Co-authored-by: Joshua MacDonald --- api/correlation/context.go | 31 +++ .../map.go | 4 +- .../map_test.go | 2 +- api/distributedcontext/context.go | 56 ---- .../binary_propagator.go | 2 +- .../binary_propagator_test.go | 8 +- .../doc.go | 4 +- .../text_noop_propagator.go | 8 +- .../text_propagator.go | 8 +- api/propagators/doc.go | 18 -- api/{propagators => trace}/b3_propagator.go | 22 +- .../b3_propagator_benchmark_test.go | 7 +- .../testtrace}/b3_propagator_data_test.go | 260 +++++++++--------- .../testtrace}/b3_propagator_test.go | 19 +- ...trace_context_propagator_benchmark_test.go | 6 +- .../trace_context_propagator_test.go | 23 +- .../trace_context_propagator.go | 27 +- bridge/opentracing/internal/mock.go | 22 +- example/basic/main.go | 6 +- example/grpc/middleware/tracing/tracing.go | 4 +- example/http-stackdriver/client/client.go | 4 +- example/http-stackdriver/server/server.go | 4 +- example/http/client/client.go | 4 +- example/http/server/server.go | 4 +- example/namedtracer/main.go | 4 +- plugin/grpctrace/grpctrace.go | 4 +- plugin/httptrace/httptrace.go | 4 +- plugin/othttp/handler.go | 10 +- plugin/othttp/handler_test.go | 4 +- plugin/othttp/wrap.go | 4 +- 30 files changed, 271 insertions(+), 312 deletions(-) create mode 100644 api/correlation/context.go rename api/{distributedcontext => correlation}/map.go (94%) rename api/{distributedcontext => correlation}/map_test.go (99%) delete mode 100644 api/distributedcontext/context.go rename api/{propagators => propagation}/binary_propagator.go (99%) rename api/{propagators => propagation}/binary_propagator_test.go (97%) rename api/{distributedcontext => propagation}/doc.go (77%) rename api/{propagators => propagation}/text_noop_propagator.go (86%) rename api/{propagators => propagation}/text_propagator.go (92%) delete mode 100644 api/propagators/doc.go rename api/{propagators => trace}/b3_propagator.go (87%) rename api/{propagators => trace/testtrace}/b3_propagator_benchmark_test.go (93%) rename api/{propagators => trace/testtrace}/b3_propagator_data_test.go (51%) rename api/{propagators => trace/testtrace}/b3_propagator_test.go (89%) rename api/{propagators => trace/testtrace}/trace_context_propagator_benchmark_test.go (96%) rename api/{propagators => trace/testtrace}/trace_context_propagator_test.go (95%) rename api/{propagators => trace}/trace_context_propagator.go (88%) diff --git a/api/correlation/context.go b/api/correlation/context.go new file mode 100644 index 00000000000..5c7eba1cc20 --- /dev/null +++ b/api/correlation/context.go @@ -0,0 +1,31 @@ +package correlation + +import ( + "context" + + "go.opentelemetry.io/otel/api/core" +) + +type correlationsType struct{} + +var correlationsKey = &correlationsType{} + +// WithMap enters a Map into a new Context. +func WithMap(ctx context.Context, m Map) context.Context { + return context.WithValue(ctx, correlationsKey, m) +} + +// WithMap enters a key:value set into a new Context. +func NewContext(ctx context.Context, keyvalues ...core.KeyValue) context.Context { + return WithMap(ctx, FromContext(ctx).Apply(MapUpdate{ + MultiKV: keyvalues, + })) +} + +// FromContext gets the current Map from a Context. +func FromContext(ctx context.Context) Map { + if m, ok := ctx.Value(correlationsKey).(Map); ok { + return m + } + return NewEmptyMap() +} diff --git a/api/distributedcontext/map.go b/api/correlation/map.go similarity index 94% rename from api/distributedcontext/map.go rename to api/correlation/map.go index 8dff15689ed..ec954bf95e8 100644 --- a/api/distributedcontext/map.go +++ b/api/correlation/map.go @@ -12,12 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -package distributedcontext +package correlation import ( "go.opentelemetry.io/otel/api/core" ) +// TODO Comments needed! This was formerly known as distributedcontext.Map + type entry struct { value core.Value } diff --git a/api/distributedcontext/map_test.go b/api/correlation/map_test.go similarity index 99% rename from api/distributedcontext/map_test.go rename to api/correlation/map_test.go index 15aed53f158..5161b17e5e9 100644 --- a/api/distributedcontext/map_test.go +++ b/api/correlation/map_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package distributedcontext +package correlation import ( "fmt" diff --git a/api/distributedcontext/context.go b/api/distributedcontext/context.go deleted file mode 100644 index e1e62ff4f8e..00000000000 --- a/api/distributedcontext/context.go +++ /dev/null @@ -1,56 +0,0 @@ -// 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. - -package distributedcontext - -import ( - "context" - "runtime/pprof" - - "go.opentelemetry.io/otel/api/core" -) - -type ctxEntriesType struct{} - -var ( - ctxEntriesKey = &ctxEntriesType{} -) - -func WithMap(ctx context.Context, m Map) context.Context { - return context.WithValue(ctx, ctxEntriesKey, m) -} - -func NewContext(ctx context.Context, keyvalues ...core.KeyValue) context.Context { - return WithMap(ctx, FromContext(ctx).Apply(MapUpdate{ - MultiKV: keyvalues, - })) -} - -func FromContext(ctx context.Context) Map { - if m, ok := ctx.Value(ctxEntriesKey).(Map); ok { - return m - } - return NewEmptyMap() -} - -// Note: the golang pprof.Do API forces this memory allocation, we -// should file an issue about that. (There's a TODO in the source.) -func Do(ctx context.Context, f func(ctx context.Context)) { - m := FromContext(ctx) - keyvals := make([]string, 0, 2*len(m.m)) - for k, v := range m.m { - keyvals = append(keyvals, string(k), v.value.Emit()) - } - pprof.Do(ctx, pprof.Labels(keyvals...), f) -} diff --git a/api/propagators/binary_propagator.go b/api/propagation/binary_propagator.go similarity index 99% rename from api/propagators/binary_propagator.go rename to api/propagation/binary_propagator.go index 9a73c054b15..4ec599f286e 100644 --- a/api/propagators/binary_propagator.go +++ b/api/propagation/binary_propagator.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package propagators +package propagation import ( "go.opentelemetry.io/otel/api/core" diff --git a/api/propagators/binary_propagator_test.go b/api/propagation/binary_propagator_test.go similarity index 97% rename from api/propagators/binary_propagator_test.go rename to api/propagation/binary_propagator_test.go index 8f5d090b45a..65df8e5b460 100644 --- a/api/propagators/binary_propagator_test.go +++ b/api/propagation/binary_propagator_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package propagators_test +package propagation_test import ( "testing" @@ -20,14 +20,14 @@ import ( "github.com/google/go-cmp/cmp" "go.opentelemetry.io/otel/api/core" - "go.opentelemetry.io/otel/api/propagators" + "go.opentelemetry.io/otel/api/propagation" ) func TestExtractSpanContextFromBytes(t *testing.T) { traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736") spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7") - propagator := propagators.Binary() + propagator := propagation.Binary() tests := []struct { name string bytes []byte @@ -123,7 +123,7 @@ func TestConvertSpanContextToBytes(t *testing.T) { traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736") spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7") - propagator := propagators.Binary() + propagator := propagation.Binary() tests := []struct { name string sc core.SpanContext diff --git a/api/distributedcontext/doc.go b/api/propagation/doc.go similarity index 77% rename from api/distributedcontext/doc.go rename to api/propagation/doc.go index 8dc2f231ad2..5b74bf4b812 100644 --- a/api/distributedcontext/doc.go +++ b/api/propagation/doc.go @@ -12,4 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -package distributedcontext // import "go.opentelemetry.io/otel/api/distributedcontext" +// Package propagation contains interface definition for BinaryFormat and +// TextFormat propagators. +package propagation // import "go.opentelemetry.io/otel/api/propagation" diff --git a/api/propagators/text_noop_propagator.go b/api/propagation/text_noop_propagator.go similarity index 86% rename from api/propagators/text_noop_propagator.go rename to api/propagation/text_noop_propagator.go index 85429712118..bea0e6dfabb 100644 --- a/api/propagators/text_noop_propagator.go +++ b/api/propagation/text_noop_propagator.go @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -package propagators +package propagation import ( "context" "go.opentelemetry.io/otel/api/core" - dctx "go.opentelemetry.io/otel/api/distributedcontext" + "go.opentelemetry.io/otel/api/correlation" ) // NoopTextFormat implements TextFormat that does nothing. @@ -31,8 +31,8 @@ func (np NoopTextFormat) Inject(ctx context.Context, supplier Supplier) { } // Extract does nothing and returns an empty SpanContext -func (np NoopTextFormat) Extract(ctx context.Context, supplier Supplier) (core.SpanContext, dctx.Map) { - return core.EmptySpanContext(), dctx.NewEmptyMap() +func (np NoopTextFormat) Extract(ctx context.Context, supplier Supplier) (core.SpanContext, correlation.Map) { + return core.EmptySpanContext(), correlation.NewEmptyMap() } // GetAllKeys returns empty list of strings. diff --git a/api/propagators/text_propagator.go b/api/propagation/text_propagator.go similarity index 92% rename from api/propagators/text_propagator.go rename to api/propagation/text_propagator.go index c39c835c595..96c8d8bb97c 100644 --- a/api/propagators/text_propagator.go +++ b/api/propagation/text_propagator.go @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -package propagators +package propagation import ( "context" "go.opentelemetry.io/otel/api/core" - dctx "go.opentelemetry.io/otel/api/distributedcontext" + "go.opentelemetry.io/otel/api/correlation" ) // TextFormat is an interface that specifies methods to inject and extract SpanContext @@ -33,10 +33,10 @@ type TextFormat interface { Inject(ctx context.Context, supplier Supplier) // Extract method retrieves encoded SpanContext using supplier from the associated carrier. - // It decodes the SpanContext and returns it and a dctx of correlated context. + // It decodes the SpanContext and returns it and a baggage of correlated context. // If no SpanContext was retrieved OR if the retrieved SpanContext is invalid then // an empty SpanContext is returned. - Extract(ctx context.Context, supplier Supplier) (core.SpanContext, dctx.Map) + Extract(ctx context.Context, supplier Supplier) (core.SpanContext, correlation.Map) // GetAllKeys returns all the keys that this propagator injects/extracts into/from a // carrier. The use cases for this are diff --git a/api/propagators/doc.go b/api/propagators/doc.go deleted file mode 100644 index 5c7c9816941..00000000000 --- a/api/propagators/doc.go +++ /dev/null @@ -1,18 +0,0 @@ -// 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. - -// Package propagators contains interface definition for BinaryFormat and -// TextFormat propagators and implementation of propagators for different -// format and suppliers. -package propagators // import "go.opentelemetry.io/otel/api/propagators" diff --git a/api/propagators/b3_propagator.go b/api/trace/b3_propagator.go similarity index 87% rename from api/propagators/b3_propagator.go rename to api/trace/b3_propagator.go index b608be9e0cc..9cee1bc4d07 100644 --- a/api/propagators/b3_propagator.go +++ b/api/trace/b3_propagator.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package propagators +package trace import ( "context" @@ -20,8 +20,8 @@ import ( "strings" "go.opentelemetry.io/otel/api/core" - dctx "go.opentelemetry.io/otel/api/distributedcontext" - "go.opentelemetry.io/otel/api/trace" + "go.opentelemetry.io/otel/api/correlation" + "go.opentelemetry.io/otel/api/propagation" ) const ( @@ -50,10 +50,10 @@ type B3 struct { SingleHeader bool } -var _ TextFormat = B3{} +var _ propagation.TextFormat = B3{} -func (b3 B3) Inject(ctx context.Context, supplier Supplier) { - sc := trace.SpanFromContext(ctx).SpanContext() +func (b3 B3) Inject(ctx context.Context, supplier propagation.Supplier) { + sc := SpanFromContext(ctx).SpanContext() if sc.IsValid() { if b3.SingleHeader { sampled := sc.TraceFlags & core.TraceFlagsSampled @@ -76,11 +76,11 @@ func (b3 B3) Inject(ctx context.Context, supplier Supplier) { } // Extract retrieves B3 Headers from the supplier -func (b3 B3) Extract(ctx context.Context, supplier Supplier) (core.SpanContext, dctx.Map) { +func (b3 B3) Extract(ctx context.Context, supplier propagation.Supplier) (core.SpanContext, correlation.Map) { if b3.SingleHeader { - return b3.extractSingleHeader(supplier), dctx.NewEmptyMap() + return b3.extractSingleHeader(supplier), correlation.NewEmptyMap() } - return b3.extract(supplier), dctx.NewEmptyMap() + return b3.extract(supplier), correlation.NewEmptyMap() } func (b3 B3) GetAllKeys() []string { @@ -90,7 +90,7 @@ func (b3 B3) GetAllKeys() []string { return []string{B3TraceIDHeader, B3SpanIDHeader, B3SampledHeader} } -func (b3 B3) extract(supplier Supplier) core.SpanContext { +func (b3 B3) extract(supplier propagation.Supplier) core.SpanContext { tid, err := core.TraceIDFromHex(supplier.Get(B3TraceIDHeader)) if err != nil { return core.EmptySpanContext() @@ -125,7 +125,7 @@ func (b3 B3) extract(supplier Supplier) core.SpanContext { return sc } -func (b3 B3) extractSingleHeader(supplier Supplier) core.SpanContext { +func (b3 B3) extractSingleHeader(supplier propagation.Supplier) core.SpanContext { h := supplier.Get(B3SingleHeader) if h == "" || h == "0" { core.EmptySpanContext() diff --git a/api/propagators/b3_propagator_benchmark_test.go b/api/trace/testtrace/b3_propagator_benchmark_test.go similarity index 93% rename from api/propagators/b3_propagator_benchmark_test.go rename to api/trace/testtrace/b3_propagator_benchmark_test.go index b3917931457..930b674d70f 100644 --- a/api/propagators/b3_propagator_benchmark_test.go +++ b/api/trace/testtrace/b3_propagator_benchmark_test.go @@ -12,14 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -package propagators_test +package testtrace_test import ( "context" "net/http" "testing" - "go.opentelemetry.io/otel/api/propagators" "go.opentelemetry.io/otel/api/trace" mocktrace "go.opentelemetry.io/otel/internal/trace" ) @@ -53,7 +52,7 @@ func BenchmarkExtractB3(b *testing.B) { } for _, tg := range testGroup { - propagator := propagators.B3{SingleHeader: tg.singleHeader} + propagator := trace.B3{SingleHeader: tg.singleHeader} for _, tt := range tg.tests { traceBenchmark(tg.name+"/"+tt.name, b, func(b *testing.B) { ctx := context.Background() @@ -97,7 +96,7 @@ func BenchmarkInjectB3(b *testing.B) { for _, tg := range testGroup { id = 0 - propagator := propagators.B3{SingleHeader: tg.singleHeader} + propagator := trace.B3{SingleHeader: tg.singleHeader} for _, tt := range tg.tests { traceBenchmark(tg.name+"/"+tt.name, b, func(b *testing.B) { req, _ := http.NewRequest("GET", "http://example.com", nil) diff --git a/api/propagators/b3_propagator_data_test.go b/api/trace/testtrace/b3_propagator_data_test.go similarity index 51% rename from api/propagators/b3_propagator_data_test.go rename to api/trace/testtrace/b3_propagator_data_test.go index 73389d47916..aefb615ff45 100644 --- a/api/propagators/b3_propagator_data_test.go +++ b/api/trace/testtrace/b3_propagator_data_test.go @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -package propagators_test +package testtrace_test import ( "go.opentelemetry.io/otel/api/core" - "go.opentelemetry.io/otel/api/propagators" + "go.opentelemetry.io/otel/api/trace" ) type extractTest struct { @@ -29,8 +29,8 @@ var extractMultipleHeaders = []extractTest{ { name: "sampling state defer", headers: map[string]string{ - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "00f067aa0ba902b7", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "00f067aa0ba902b7", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -40,9 +40,9 @@ var extractMultipleHeaders = []extractTest{ { name: "sampling state deny", headers: map[string]string{ - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "00f067aa0ba902b7", - propagators.B3SampledHeader: "0", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "00f067aa0ba902b7", + trace.B3SampledHeader: "0", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -52,9 +52,9 @@ var extractMultipleHeaders = []extractTest{ { name: "sampling state accept", headers: map[string]string{ - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "00f067aa0ba902b7", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "00f067aa0ba902b7", + trace.B3SampledHeader: "1", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -65,9 +65,9 @@ var extractMultipleHeaders = []extractTest{ { name: "sampling state as a boolean", headers: map[string]string{ - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "00f067aa0ba902b7", - propagators.B3SampledHeader: "true", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "00f067aa0ba902b7", + trace.B3SampledHeader: "true", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -78,9 +78,9 @@ var extractMultipleHeaders = []extractTest{ { name: "debug flag set", headers: map[string]string{ - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "00f067aa0ba902b7", - propagators.B3DebugFlagHeader: "1", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "00f067aa0ba902b7", + trace.B3DebugFlagHeader: "1", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -94,10 +94,10 @@ var extractMultipleHeaders = []extractTest{ // takes precedence. Hence, it is sampled. name: "debug flag set and sampling state is deny", headers: map[string]string{ - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "00f067aa0ba902b7", - propagators.B3SampledHeader: "0", - propagators.B3DebugFlagHeader: "1", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "00f067aa0ba902b7", + trace.B3SampledHeader: "0", + trace.B3DebugFlagHeader: "1", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -108,10 +108,10 @@ var extractMultipleHeaders = []extractTest{ { name: "with parent span id", headers: map[string]string{ - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "00f067aa0ba902b7", - propagators.B3SampledHeader: "1", - propagators.B3ParentSpanIDHeader: "00f067aa0ba90200", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "00f067aa0ba902b7", + trace.B3SampledHeader: "1", + trace.B3ParentSpanIDHeader: "00f067aa0ba90200", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -122,7 +122,7 @@ var extractMultipleHeaders = []extractTest{ { name: "with only sampled state header", headers: map[string]string{ - propagators.B3SampledHeader: "0", + trace.B3SampledHeader: "0", }, wantSc: core.EmptySpanContext(), }, @@ -132,7 +132,7 @@ var extractSingleHeader = []extractTest{ { name: "sampling state defer", headers: map[string]string{ - propagators.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7", + trace.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -142,7 +142,7 @@ var extractSingleHeader = []extractTest{ { name: "sampling state deny", headers: map[string]string{ - propagators.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-0", + trace.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-0", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -152,7 +152,7 @@ var extractSingleHeader = []extractTest{ { name: "sampling state accept", headers: map[string]string{ - propagators.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-1", + trace.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-1", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -163,7 +163,7 @@ var extractSingleHeader = []extractTest{ { name: "sampling state debug", headers: map[string]string{ - propagators.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-d", + trace.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-d", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -174,7 +174,7 @@ var extractSingleHeader = []extractTest{ { name: "with parent span id", headers: map[string]string{ - propagators.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-1-00000000000000cd", + trace.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-1-00000000000000cd", }, wantSc: core.SpanContext{ TraceID: traceID, @@ -185,7 +185,7 @@ var extractSingleHeader = []extractTest{ { name: "with only sampling state deny", headers: map[string]string{ - propagators.B3SingleHeader: "0", + trace.B3SingleHeader: "0", }, wantSc: core.EmptySpanContext(), }, @@ -195,143 +195,143 @@ var extractInvalidB3MultipleHeaders = []extractTest{ { name: "trace ID length > 32", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab00000000000000000000000000000000", - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "ab00000000000000000000000000000000", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "1", }, }, { name: "trace ID length >16 and <32", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab0000000000000000000000000000", - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "ab0000000000000000000000000000", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "1", }, }, { name: "trace ID length <16", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab0000000000", - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "ab0000000000", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "1", }, }, { name: "wrong span ID length", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab000000000000000000000000000000", - propagators.B3SpanIDHeader: "cd0000000000000000", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "ab000000000000000000000000000000", + trace.B3SpanIDHeader: "cd0000000000000000", + trace.B3SampledHeader: "1", }, }, { name: "wrong sampled flag length", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab000000000000000000000000000000", - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "10", + trace.B3TraceIDHeader: "ab000000000000000000000000000000", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "10", }, }, { name: "bogus trace ID", headers: map[string]string{ - propagators.B3TraceIDHeader: "qw000000000000000000000000000000", - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "qw000000000000000000000000000000", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "1", }, }, { name: "bogus span ID", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab000000000000000000000000000000", - propagators.B3SpanIDHeader: "qw00000000000000", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "ab000000000000000000000000000000", + trace.B3SpanIDHeader: "qw00000000000000", + trace.B3SampledHeader: "1", }, }, { name: "bogus sampled flag", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab000000000000000000000000000000", - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "d", + trace.B3TraceIDHeader: "ab000000000000000000000000000000", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "d", }, }, { name: "bogus debug flag (string)", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab000000000000000000000000000000", - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "1", - propagators.B3DebugFlagHeader: "d", + trace.B3TraceIDHeader: "ab000000000000000000000000000000", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "1", + trace.B3DebugFlagHeader: "d", }, }, { name: "bogus debug flag (number)", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab000000000000000000000000000000", - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "1", - propagators.B3DebugFlagHeader: "10", + trace.B3TraceIDHeader: "ab000000000000000000000000000000", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "1", + trace.B3DebugFlagHeader: "10", }, }, { name: "upper case trace ID", headers: map[string]string{ - propagators.B3TraceIDHeader: "AB000000000000000000000000000000", - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "AB000000000000000000000000000000", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "1", }, }, { name: "upper case span ID", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab000000000000000000000000000000", - propagators.B3SpanIDHeader: "CD00000000000000", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "ab000000000000000000000000000000", + trace.B3SpanIDHeader: "CD00000000000000", + trace.B3SampledHeader: "1", }, }, { name: "zero trace ID", headers: map[string]string{ - propagators.B3TraceIDHeader: "00000000000000000000000000000000", - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "00000000000000000000000000000000", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "1", }, }, { name: "zero span ID", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab000000000000000000000000000000", - propagators.B3SpanIDHeader: "0000000000000000", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "ab000000000000000000000000000000", + trace.B3SpanIDHeader: "0000000000000000", + trace.B3SampledHeader: "1", }, }, { name: "missing span ID", headers: map[string]string{ - propagators.B3TraceIDHeader: "ab000000000000000000000000000000", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "ab000000000000000000000000000000", + trace.B3SampledHeader: "1", }, }, { name: "missing trace ID", headers: map[string]string{ - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "1", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "1", }, }, { name: "missing trace ID with valid single header", headers: map[string]string{ - propagators.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-1", - propagators.B3SpanIDHeader: "cd00000000000000", - propagators.B3SampledHeader: "1", + trace.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-1", + trace.B3SpanIDHeader: "cd00000000000000", + trace.B3SampledHeader: "1", }, }, { name: "sampled header set to 1 but trace ID and span ID are missing", headers: map[string]string{ - propagators.B3SampledHeader: "1", + trace.B3SampledHeader: "1", }, }, } @@ -340,96 +340,96 @@ var extractInvalidB3SingleHeader = []extractTest{ { name: "wrong trace ID length", headers: map[string]string{ - propagators.B3SingleHeader: "ab00000000000000000000000000000000-cd00000000000000-1", + trace.B3SingleHeader: "ab00000000000000000000000000000000-cd00000000000000-1", }, }, { name: "wrong span ID length", headers: map[string]string{ - propagators.B3SingleHeader: "ab000000000000000000000000000000-cd0000000000000000-1", + trace.B3SingleHeader: "ab000000000000000000000000000000-cd0000000000000000-1", }, }, { name: "wrong sampled state length", headers: map[string]string{ - propagators.B3SingleHeader: "00-ab000000000000000000000000000000-cd00000000000000-01", + trace.B3SingleHeader: "00-ab000000000000000000000000000000-cd00000000000000-01", }, }, { name: "wrong parent span ID length", headers: map[string]string{ - propagators.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-1-cd0000000000000000", + trace.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-1-cd0000000000000000", }, }, { name: "bogus trace ID", headers: map[string]string{ - propagators.B3SingleHeader: "qw000000000000000000000000000000-cd00000000000000-1", + trace.B3SingleHeader: "qw000000000000000000000000000000-cd00000000000000-1", }, }, { name: "bogus span ID", headers: map[string]string{ - propagators.B3SingleHeader: "ab000000000000000000000000000000-qw00000000000000-1", + trace.B3SingleHeader: "ab000000000000000000000000000000-qw00000000000000-1", }, }, { name: "bogus sampled flag", headers: map[string]string{ - propagators.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-q", + trace.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-q", }, }, { name: "bogus parent span ID", headers: map[string]string{ - propagators.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-1-qw00000000000000", + trace.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-1-qw00000000000000", }, }, { name: "upper case trace ID", headers: map[string]string{ - propagators.B3SingleHeader: "AB000000000000000000000000000000-cd00000000000000-1", + trace.B3SingleHeader: "AB000000000000000000000000000000-cd00000000000000-1", }, }, { name: "upper case span ID", headers: map[string]string{ - propagators.B3SingleHeader: "ab000000000000000000000000000000-CD00000000000000-1", + trace.B3SingleHeader: "ab000000000000000000000000000000-CD00000000000000-1", }, }, { name: "upper case parent span ID", headers: map[string]string{ - propagators.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-1-EF00000000000000", + trace.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-1-EF00000000000000", }, }, { name: "zero trace ID and span ID", headers: map[string]string{ - propagators.B3SingleHeader: "00000000000000000000000000000000-0000000000000000-1", + trace.B3SingleHeader: "00000000000000000000000000000000-0000000000000000-1", }, }, { name: "missing single header with valid separate headers", headers: map[string]string{ - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "00f067aa0ba902b7", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "00f067aa0ba902b7", + trace.B3SampledHeader: "1", }, }, { name: "upper case span ID with valid separate headers", headers: map[string]string{ - propagators.B3SingleHeader: "ab000000000000000000000000000000-CD00000000000000-1", - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "00f067aa0ba902b7", - propagators.B3SampledHeader: "1", + trace.B3SingleHeader: "ab000000000000000000000000000000-CD00000000000000-1", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "00f067aa0ba902b7", + trace.B3SampledHeader: "1", }, }, { name: "with sampling set to true", headers: map[string]string{ - propagators.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-true", + trace.B3SingleHeader: "ab000000000000000000000000000000-cd00000000000000-true", }, }, } @@ -450,12 +450,12 @@ var injectB3MultipleHeader = []injectTest{ TraceFlags: core.TraceFlagsSampled, }, wantHeaders: map[string]string{ - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "0000000000000001", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "0000000000000001", + trace.B3SampledHeader: "1", }, doNotWantHeaders: []string{ - propagators.B3ParentSpanIDHeader, + trace.B3ParentSpanIDHeader, }, }, { @@ -465,12 +465,12 @@ var injectB3MultipleHeader = []injectTest{ SpanID: spanID, }, wantHeaders: map[string]string{ - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "0000000000000002", - propagators.B3SampledHeader: "0", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "0000000000000002", + trace.B3SampledHeader: "0", }, doNotWantHeaders: []string{ - propagators.B3ParentSpanIDHeader, + trace.B3ParentSpanIDHeader, }, }, { @@ -481,12 +481,12 @@ var injectB3MultipleHeader = []injectTest{ TraceFlags: 0xff, }, wantHeaders: map[string]string{ - propagators.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", - propagators.B3SpanIDHeader: "0000000000000003", - propagators.B3SampledHeader: "1", + trace.B3TraceIDHeader: "4bf92f3577b34da6a3ce929d0e0e4736", + trace.B3SpanIDHeader: "0000000000000003", + trace.B3SampledHeader: "1", }, doNotWantHeaders: []string{ - propagators.B3ParentSpanIDHeader, + trace.B3ParentSpanIDHeader, }, }, } @@ -500,13 +500,13 @@ var injectB3SingleleHeader = []injectTest{ TraceFlags: core.TraceFlagsSampled, }, wantHeaders: map[string]string{ - propagators.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-0000000000000001-1", + trace.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-0000000000000001-1", }, doNotWantHeaders: []string{ - propagators.B3TraceIDHeader, - propagators.B3SpanIDHeader, - propagators.B3SampledHeader, - propagators.B3ParentSpanIDHeader, + trace.B3TraceIDHeader, + trace.B3SpanIDHeader, + trace.B3SampledHeader, + trace.B3ParentSpanIDHeader, }, }, { @@ -516,13 +516,13 @@ var injectB3SingleleHeader = []injectTest{ SpanID: spanID, }, wantHeaders: map[string]string{ - propagators.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-0000000000000002-0", + trace.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-0000000000000002-0", }, doNotWantHeaders: []string{ - propagators.B3TraceIDHeader, - propagators.B3SpanIDHeader, - propagators.B3SampledHeader, - propagators.B3ParentSpanIDHeader, + trace.B3TraceIDHeader, + trace.B3SpanIDHeader, + trace.B3SampledHeader, + trace.B3ParentSpanIDHeader, }, }, { @@ -533,13 +533,13 @@ var injectB3SingleleHeader = []injectTest{ TraceFlags: 0xff, }, wantHeaders: map[string]string{ - propagators.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-0000000000000003-1", + trace.B3SingleHeader: "4bf92f3577b34da6a3ce929d0e0e4736-0000000000000003-1", }, doNotWantHeaders: []string{ - propagators.B3TraceIDHeader, - propagators.B3SpanIDHeader, - propagators.B3SampledHeader, - propagators.B3ParentSpanIDHeader, + trace.B3TraceIDHeader, + trace.B3SpanIDHeader, + trace.B3SampledHeader, + trace.B3ParentSpanIDHeader, }, }, } diff --git a/api/propagators/b3_propagator_test.go b/api/trace/testtrace/b3_propagator_test.go similarity index 89% rename from api/propagators/b3_propagator_test.go rename to api/trace/testtrace/b3_propagator_test.go index c043258a94b..444dfccdabc 100644 --- a/api/propagators/b3_propagator_test.go +++ b/api/trace/testtrace/b3_propagator_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package propagators_test +package testtrace_test import ( "context" @@ -21,7 +21,6 @@ import ( "github.com/google/go-cmp/cmp" - "go.opentelemetry.io/otel/api/propagators" "go.opentelemetry.io/otel/api/trace" mocktrace "go.opentelemetry.io/otel/internal/trace" ) @@ -55,7 +54,7 @@ func TestExtractB3(t *testing.T) { } for _, tg := range testGroup { - propagator := propagators.B3{SingleHeader: tg.singleHeader} + propagator := trace.B3{SingleHeader: tg.singleHeader} for _, tt := range tg.tests { t.Run(tt.name, func(t *testing.T) { req, _ := http.NewRequest("GET", "http://example.com", nil) @@ -99,7 +98,7 @@ func TestInjectB3(t *testing.T) { for _, tg := range testGroup { id = 0 - propagator := propagators.B3{SingleHeader: tg.singleHeader} + propagator := trace.B3{SingleHeader: tg.singleHeader} for _, tt := range tg.tests { t.Run(tt.name, func(t *testing.T) { req, _ := http.NewRequest("GET", "http://example.com", nil) @@ -129,11 +128,11 @@ func TestInjectB3(t *testing.T) { } func TestB3Propagator_GetAllKeys(t *testing.T) { - propagator := propagators.B3{SingleHeader: false} + propagator := trace.B3{SingleHeader: false} want := []string{ - propagators.B3TraceIDHeader, - propagators.B3SpanIDHeader, - propagators.B3SampledHeader, + trace.B3TraceIDHeader, + trace.B3SpanIDHeader, + trace.B3SampledHeader, } got := propagator.GetAllKeys() if diff := cmp.Diff(got, want); diff != "" { @@ -142,9 +141,9 @@ func TestB3Propagator_GetAllKeys(t *testing.T) { } func TestB3PropagatorWithSingleHeader_GetAllKeys(t *testing.T) { - propagator := propagators.B3{SingleHeader: true} + propagator := trace.B3{SingleHeader: true} want := []string{ - propagators.B3SingleHeader, + trace.B3SingleHeader, } got := propagator.GetAllKeys() if diff := cmp.Diff(got, want); diff != "" { diff --git a/api/propagators/trace_context_propagator_benchmark_test.go b/api/trace/testtrace/trace_context_propagator_benchmark_test.go similarity index 96% rename from api/propagators/trace_context_propagator_benchmark_test.go rename to api/trace/testtrace/trace_context_propagator_benchmark_test.go index 7086b281072..ba688930fa8 100644 --- a/api/propagators/trace_context_propagator_benchmark_test.go +++ b/api/trace/testtrace/trace_context_propagator_benchmark_test.go @@ -1,4 +1,4 @@ -package propagators +package testtrace_test import ( "context" @@ -11,7 +11,7 @@ import ( ) func BenchmarkInject(b *testing.B) { - var t TraceContext + var t trace.TraceContext injectSubBenchmarks(b, func(ctx context.Context, b *testing.B) { req, _ := http.NewRequest("GET", "http://example.com", nil) @@ -52,7 +52,7 @@ func injectSubBenchmarks(b *testing.B, fn func(context.Context, *testing.B)) { func BenchmarkExtract(b *testing.B) { extractSubBenchmarks(b, func(b *testing.B, req *http.Request) { - var propagator TraceContext + var propagator trace.TraceContext ctx := context.Background() b.ResetTimer() for i := 0; i < b.N; i++ { diff --git a/api/propagators/trace_context_propagator_test.go b/api/trace/testtrace/trace_context_propagator_test.go similarity index 95% rename from api/propagators/trace_context_propagator_test.go rename to api/trace/testtrace/trace_context_propagator_test.go index e9ffcd6aeb1..ba99c78c8f3 100644 --- a/api/propagators/trace_context_propagator_test.go +++ b/api/trace/testtrace/trace_context_propagator_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package propagators_test +package testtrace_test import ( "context" @@ -23,9 +23,8 @@ import ( "github.com/google/go-cmp/cmp" "go.opentelemetry.io/otel/api/core" - dctx "go.opentelemetry.io/otel/api/distributedcontext" + "go.opentelemetry.io/otel/api/correlation" "go.opentelemetry.io/otel/api/key" - "go.opentelemetry.io/otel/api/propagators" "go.opentelemetry.io/otel/api/trace" mocktrace "go.opentelemetry.io/otel/internal/trace" ) @@ -46,7 +45,7 @@ func mustSpanIDFromHex(s string) (t core.SpanID) { } func TestExtractValidTraceContextFromHTTPReq(t *testing.T) { - var propagator propagators.TraceContext + var propagator trace.TraceContext tests := []struct { name string header string @@ -139,7 +138,7 @@ func TestExtractValidTraceContextFromHTTPReq(t *testing.T) { } func TestExtractInvalidTraceContextFromHTTPReq(t *testing.T) { - var propagator propagators.TraceContext + var propagator trace.TraceContext wantSc := core.EmptySpanContext() tests := []struct { name string @@ -231,7 +230,7 @@ func TestInjectTraceContextToHTTPReq(t *testing.T) { Sampled: false, StartSpanID: &id, } - var propagator propagators.TraceContext + var propagator trace.TraceContext tests := []struct { name string sc core.SpanContext @@ -287,7 +286,7 @@ func TestInjectTraceContextToHTTPReq(t *testing.T) { } func TestExtractValidDistributedContextFromHTTPReq(t *testing.T) { - propagator := propagators.TraceContext{} + propagator := trace.TraceContext{} tests := []struct { name string header string @@ -350,7 +349,7 @@ func TestExtractValidDistributedContextFromHTTPReq(t *testing.T) { ctx := context.Background() _, gotCorCtx := propagator.Extract(ctx, req.Header) - wantCorCtx := dctx.NewMap(dctx.MapUpdate{MultiKV: tt.wantKVs}) + wantCorCtx := correlation.NewMap(correlation.MapUpdate{MultiKV: tt.wantKVs}) if gotCorCtx.Len() != wantCorCtx.Len() { t.Errorf( "Got and Want CorCtx are not the same size %d != %d", @@ -375,7 +374,7 @@ func TestExtractValidDistributedContextFromHTTPReq(t *testing.T) { } func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) { - propagator := propagators.TraceContext{} + propagator := trace.TraceContext{} tests := []struct { name string header string @@ -401,7 +400,7 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) { } func TestInjectCorrelationContextToHTTPReq(t *testing.T) { - propagator := propagators.TraceContext{} + propagator := trace.TraceContext{} tests := []struct { name string kvs []core.KeyValue @@ -453,7 +452,7 @@ func TestInjectCorrelationContextToHTTPReq(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { req, _ := http.NewRequest("GET", "http://example.com", nil) - ctx := dctx.WithMap(context.Background(), dctx.NewMap(dctx.MapUpdate{MultiKV: tt.kvs})) + ctx := correlation.WithMap(context.Background(), correlation.NewMap(correlation.MapUpdate{MultiKV: tt.kvs})) propagator.Inject(ctx, req.Header) gotHeader := req.Header.Get("Correlation-Context") @@ -475,7 +474,7 @@ func TestInjectCorrelationContextToHTTPReq(t *testing.T) { } func TestTraceContextPropagator_GetAllKeys(t *testing.T) { - var propagator propagators.TraceContext + var propagator trace.TraceContext want := []string{"Traceparent", "Correlation-Context"} got := propagator.GetAllKeys() if diff := cmp.Diff(got, want); diff != "" { diff --git a/api/propagators/trace_context_propagator.go b/api/trace/trace_context_propagator.go similarity index 88% rename from api/propagators/trace_context_propagator.go rename to api/trace/trace_context_propagator.go index 75e88b51d6a..e64ecacf014 100644 --- a/api/propagators/trace_context_propagator.go +++ b/api/trace/trace_context_propagator.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package propagators +package trace import ( "context" @@ -23,9 +23,9 @@ import ( "strings" "go.opentelemetry.io/otel/api/core" - dctx "go.opentelemetry.io/otel/api/distributedcontext" + "go.opentelemetry.io/otel/api/correlation" "go.opentelemetry.io/otel/api/key" - "go.opentelemetry.io/otel/api/trace" + "go.opentelemetry.io/otel/api/propagation" ) const ( @@ -36,13 +36,14 @@ const ( ) // TraceContext propagates SpanContext in W3C TraceContext format. +//nolint:golint type TraceContext struct{} -var _ TextFormat = TraceContext{} +var _ propagation.TextFormat = TraceContext{} var traceCtxRegExp = regexp.MustCompile("^[0-9a-f]{2}-[a-f0-9]{32}-[a-f0-9]{16}-[a-f0-9]{2}-?") -func (hp TraceContext) Inject(ctx context.Context, supplier Supplier) { - sc := trace.SpanFromContext(ctx).SpanContext() +func (hp TraceContext) Inject(ctx context.Context, supplier propagation.Supplier) { + sc := SpanFromContext(ctx).SpanContext() if sc.IsValid() { h := fmt.Sprintf("%.2x-%s-%.16x-%.2x", supportedVersion, @@ -52,7 +53,7 @@ func (hp TraceContext) Inject(ctx context.Context, supplier Supplier) { supplier.Set(TraceparentHeader, h) } - correlationCtx := dctx.FromContext(ctx) + correlationCtx := correlation.FromContext(ctx) firstIter := true var headerValueBuilder strings.Builder correlationCtx.Foreach(func(kv core.KeyValue) bool { @@ -72,13 +73,13 @@ func (hp TraceContext) Inject(ctx context.Context, supplier Supplier) { } func (hp TraceContext) Extract( - ctx context.Context, supplier Supplier, -) (core.SpanContext, dctx.Map) { + ctx context.Context, supplier propagation.Supplier, +) (core.SpanContext, correlation.Map) { return hp.extractSpanContext(ctx, supplier), hp.extractCorrelationCtx(ctx, supplier) } func (hp TraceContext) extractSpanContext( - ctx context.Context, supplier Supplier, + ctx context.Context, supplier propagation.Supplier, ) core.SpanContext { h := supplier.Get(TraceparentHeader) if h == "" { @@ -146,10 +147,10 @@ func (hp TraceContext) extractSpanContext( return sc } -func (hp TraceContext) extractCorrelationCtx(ctx context.Context, supplier Supplier) dctx.Map { +func (hp TraceContext) extractCorrelationCtx(ctx context.Context, supplier propagation.Supplier) correlation.Map { correlationContext := supplier.Get(CorrelationContextHeader) if correlationContext == "" { - return dctx.NewEmptyMap() + return correlation.NewEmptyMap() } contextValues := strings.Split(correlationContext, ",") @@ -185,7 +186,7 @@ func (hp TraceContext) extractCorrelationCtx(ctx context.Context, supplier Suppl keyValues = append(keyValues, key.New(trimmedName).String(trimmedValueWithProps.String())) } - return dctx.NewMap(dctx.MapUpdate{ + return correlation.NewMap(correlation.MapUpdate{ MultiKV: keyValues, }) } diff --git a/bridge/opentracing/internal/mock.go b/bridge/opentracing/internal/mock.go index 82781a0e19b..53411424806 100644 --- a/bridge/opentracing/internal/mock.go +++ b/bridge/opentracing/internal/mock.go @@ -23,7 +23,7 @@ import ( "google.golang.org/grpc/codes" otelcore "go.opentelemetry.io/otel/api/core" - oteldctx "go.opentelemetry.io/otel/api/distributedcontext" + otelcorrelation "go.opentelemetry.io/otel/api/correlation" otelkey "go.opentelemetry.io/otel/api/key" oteltrace "go.opentelemetry.io/otel/api/trace" @@ -44,7 +44,7 @@ type MockContextKeyValue struct { } type MockTracer struct { - Resources oteldctx.Map + Resources otelcorrelation.Map FinishedSpans []*MockSpan SpareTraceIDs []otelcore.TraceID SpareSpanIDs []otelcore.SpanID @@ -59,7 +59,7 @@ var _ migration.DeferredContextSetupTracerExtension = &MockTracer{} func NewMockTracer() *MockTracer { return &MockTracer{ - Resources: oteldctx.NewEmptyMap(), + Resources: otelcorrelation.NewEmptyMap(), FinishedSpans: nil, SpareTraceIDs: nil, SpareSpanIDs: nil, @@ -94,7 +94,7 @@ func (t *MockTracer) Start(ctx context.Context, name string, opts ...oteltrace.S officialTracer: t, spanContext: spanContext, recording: spanOpts.Record, - Attributes: oteldctx.NewMap(oteldctx.MapUpdate{ + Attributes: otelcorrelation.NewMap(otelcorrelation.MapUpdate{ MultiKV: spanOpts.Attributes, }), StartTime: startTime, @@ -193,10 +193,10 @@ func (t *MockTracer) DeferredContextSetupHook(ctx context.Context, span oteltrac } type MockEvent struct { - CtxAttributes oteldctx.Map + CtxAttributes otelcorrelation.Map Timestamp time.Time Name string - Attributes oteldctx.Map + Attributes otelcorrelation.Map } type MockSpan struct { @@ -206,7 +206,7 @@ type MockSpan struct { SpanKind oteltrace.SpanKind recording bool - Attributes oteldctx.Map + Attributes otelcorrelation.Map StartTime time.Time EndTime time.Time ParentSpanID otelcore.SpanID @@ -237,12 +237,12 @@ func (s *MockSpan) SetError(v bool) { } func (s *MockSpan) SetAttributes(attributes ...otelcore.KeyValue) { - s.applyUpdate(oteldctx.MapUpdate{ + s.applyUpdate(otelcorrelation.MapUpdate{ MultiKV: attributes, }) } -func (s *MockSpan) applyUpdate(update oteldctx.MapUpdate) { +func (s *MockSpan) applyUpdate(update otelcorrelation.MapUpdate) { s.Attributes = s.Attributes.Apply(update) } @@ -274,10 +274,10 @@ func (s *MockSpan) AddEvent(ctx context.Context, name string, attrs ...otelcore. func (s *MockSpan) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, name string, attrs ...otelcore.KeyValue) { s.Events = append(s.Events, MockEvent{ - CtxAttributes: oteldctx.FromContext(ctx), + CtxAttributes: otelcorrelation.FromContext(ctx), Timestamp: timestamp, Name: name, - Attributes: oteldctx.NewMap(oteldctx.MapUpdate{ + Attributes: otelcorrelation.NewMap(otelcorrelation.MapUpdate{ MultiKV: attrs, }), }) diff --git a/example/basic/main.go b/example/basic/main.go index f46e65cf416..88e854dfd3a 100644 --- a/example/basic/main.go +++ b/example/basic/main.go @@ -18,7 +18,7 @@ import ( "context" "log" - "go.opentelemetry.io/otel/api/distributedcontext" + "go.opentelemetry.io/otel/api/correlation" "go.opentelemetry.io/otel/api/global" "go.opentelemetry.io/otel/api/key" "go.opentelemetry.io/otel/api/metric" @@ -82,7 +82,7 @@ func main() { ctx := context.Background() - ctx = distributedcontext.NewContext(ctx, + ctx = correlation.NewContext(ctx, fooKey.String("foo1"), barKey.String("bar1"), ) @@ -105,7 +105,7 @@ func main() { meter.RecordBatch( // Note: call-site variables added as context Entries: - distributedcontext.NewContext(ctx, anotherKey.String("xyz")), + correlation.NewContext(ctx, anotherKey.String("xyz")), commonLabels, oneMetric.Measurement(1.0), diff --git a/example/grpc/middleware/tracing/tracing.go b/example/grpc/middleware/tracing/tracing.go index b737773ec53..ce65955d278 100644 --- a/example/grpc/middleware/tracing/tracing.go +++ b/example/grpc/middleware/tracing/tracing.go @@ -27,7 +27,7 @@ import ( "google.golang.org/grpc/status" "go.opentelemetry.io/otel/api/core" - "go.opentelemetry.io/otel/api/distributedcontext" + "go.opentelemetry.io/otel/api/correlation" "go.opentelemetry.io/otel/api/global" "go.opentelemetry.io/otel/api/key" "go.opentelemetry.io/otel/api/trace" @@ -39,7 +39,7 @@ func UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.Una metadataCopy := requestMetadata.Copy() entries, spanCtx := grpctrace.Extract(ctx, &metadataCopy) - ctx = distributedcontext.WithMap(ctx, distributedcontext.NewMap(distributedcontext.MapUpdate{ + ctx = correlation.WithMap(ctx, correlation.NewMap(correlation.MapUpdate{ MultiKV: entries, })) diff --git a/example/http-stackdriver/client/client.go b/example/http-stackdriver/client/client.go index 1cb7d13f388..0e27658fe84 100644 --- a/example/http-stackdriver/client/client.go +++ b/example/http-stackdriver/client/client.go @@ -26,7 +26,7 @@ import ( "google.golang.org/grpc/codes" - "go.opentelemetry.io/otel/api/distributedcontext" + "go.opentelemetry.io/otel/api/correlation" "go.opentelemetry.io/otel/api/global" "go.opentelemetry.io/otel/api/key" "go.opentelemetry.io/otel/api/trace" @@ -62,7 +62,7 @@ func main() { tr := global.TraceProvider().Tracer("stackdriver/example/client") client := http.DefaultClient - ctx := distributedcontext.NewContext(context.Background(), + ctx := correlation.NewContext(context.Background(), key.String("username", "donuts"), ) diff --git a/example/http-stackdriver/server/server.go b/example/http-stackdriver/server/server.go index f9ac3e1e971..bbda5a30c67 100644 --- a/example/http-stackdriver/server/server.go +++ b/example/http-stackdriver/server/server.go @@ -20,7 +20,7 @@ import ( "net/http" "os" - "go.opentelemetry.io/otel/api/distributedcontext" + "go.opentelemetry.io/otel/api/correlation" "go.opentelemetry.io/otel/api/global" "go.opentelemetry.io/otel/api/trace" "go.opentelemetry.io/otel/exporter/trace/stackdriver" @@ -58,7 +58,7 @@ func main() { helloHandler := func(w http.ResponseWriter, req *http.Request) { attrs, entries, spanCtx := httptrace.Extract(req.Context(), req) - req = req.WithContext(distributedcontext.WithMap(req.Context(), distributedcontext.NewMap(distributedcontext.MapUpdate{ + req = req.WithContext(correlation.WithMap(req.Context(), correlation.NewMap(correlation.MapUpdate{ MultiKV: entries, }))) diff --git a/example/http/client/client.go b/example/http/client/client.go index 726ae8600bb..202502b8fe9 100644 --- a/example/http/client/client.go +++ b/example/http/client/client.go @@ -25,7 +25,7 @@ import ( "google.golang.org/grpc/codes" - "go.opentelemetry.io/otel/api/distributedcontext" + "go.opentelemetry.io/otel/api/correlation" "go.opentelemetry.io/otel/api/global" "go.opentelemetry.io/otel/api/key" "go.opentelemetry.io/otel/api/trace" @@ -56,7 +56,7 @@ func main() { initTracer() client := http.DefaultClient - ctx := distributedcontext.NewContext(context.Background(), + ctx := correlation.NewContext(context.Background(), key.String("username", "donuts"), ) diff --git a/example/http/server/server.go b/example/http/server/server.go index f1b716ddd75..94b958f3e2a 100644 --- a/example/http/server/server.go +++ b/example/http/server/server.go @@ -19,7 +19,7 @@ import ( "log" "net/http" - "go.opentelemetry.io/otel/api/distributedcontext" + "go.opentelemetry.io/otel/api/correlation" "go.opentelemetry.io/otel/api/global" "go.opentelemetry.io/otel/api/trace" "go.opentelemetry.io/otel/exporter/trace/stdout" @@ -52,7 +52,7 @@ func main() { helloHandler := func(w http.ResponseWriter, req *http.Request) { attrs, entries, spanCtx := httptrace.Extract(req.Context(), req) - req = req.WithContext(distributedcontext.WithMap(req.Context(), distributedcontext.NewMap(distributedcontext.MapUpdate{ + req = req.WithContext(correlation.WithMap(req.Context(), correlation.NewMap(correlation.MapUpdate{ MultiKV: entries, }))) diff --git a/example/namedtracer/main.go b/example/namedtracer/main.go index a2c1f2c4382..18f3429f8f7 100644 --- a/example/namedtracer/main.go +++ b/example/namedtracer/main.go @@ -18,7 +18,7 @@ import ( "context" "log" - "go.opentelemetry.io/otel/api/distributedcontext" + "go.opentelemetry.io/otel/api/correlation" "go.opentelemetry.io/otel/api/global" "go.opentelemetry.io/otel/api/key" "go.opentelemetry.io/otel/api/trace" @@ -59,7 +59,7 @@ func main() { tracer := tp.Tracer("example/namedtracer/main") ctx := context.Background() - ctx = distributedcontext.NewContext(ctx, + ctx = correlation.NewContext(ctx, fooKey.String("foo1"), barKey.String("bar1"), ) diff --git a/plugin/grpctrace/grpctrace.go b/plugin/grpctrace/grpctrace.go index 4abac7d9209..74d947bcd75 100644 --- a/plugin/grpctrace/grpctrace.go +++ b/plugin/grpctrace/grpctrace.go @@ -21,7 +21,7 @@ import ( "google.golang.org/grpc/metadata" "go.opentelemetry.io/otel/api/core" - "go.opentelemetry.io/otel/api/propagators" + "go.opentelemetry.io/otel/api/trace" ) const ( @@ -30,7 +30,7 @@ const ( ) var ( - propagator = propagators.TraceContext{} + propagator = trace.TraceContext{} ) type metadataSupplier struct { diff --git a/plugin/httptrace/httptrace.go b/plugin/httptrace/httptrace.go index 398b25e87ff..376517f9709 100644 --- a/plugin/httptrace/httptrace.go +++ b/plugin/httptrace/httptrace.go @@ -20,7 +20,7 @@ import ( "go.opentelemetry.io/otel/api/core" "go.opentelemetry.io/otel/api/key" - "go.opentelemetry.io/otel/api/propagators" + "go.opentelemetry.io/otel/api/trace" ) const ( @@ -31,7 +31,7 @@ var ( HostKey = key.New("http.host") URLKey = key.New("http.url") - propagator = propagators.TraceContext{} + propagator = trace.TraceContext{} ) // Returns the Attributes, Context Entries, and SpanContext that were encoded by Inject. diff --git a/plugin/othttp/handler.go b/plugin/othttp/handler.go index c3583abe294..c6cfb29dee4 100644 --- a/plugin/othttp/handler.go +++ b/plugin/othttp/handler.go @@ -20,7 +20,7 @@ import ( "go.opentelemetry.io/otel/api/core" "go.opentelemetry.io/otel/api/global" - "go.opentelemetry.io/otel/api/propagators" + "go.opentelemetry.io/otel/api/propagation" "go.opentelemetry.io/otel/api/trace" ) @@ -50,7 +50,7 @@ type Handler struct { handler http.Handler tracer trace.Tracer - prop propagators.TextFormat + prop propagation.TextFormat spanStartOptions []trace.StartOption public bool readEvent bool @@ -79,8 +79,8 @@ func WithPublicEndpoint() Option { // WithPropagator configures the Handler with a specific propagator. If this // option isn't specificed then -// go.opentelemetry.io/otel/api/propagators.TraceContext is used. -func WithPropagator(p propagators.TextFormat) Option { +// go.opentelemetry.io/otel/api/trace.TraceContext is used. +func WithPropagator(p propagation.TextFormat) Option { return func(h *Handler) { h.prop = p } @@ -130,7 +130,7 @@ func NewHandler(handler http.Handler, operation string, opts ...Option) http.Han h := Handler{handler: handler, operation: operation} defaultOpts := []Option{ WithTracer(global.TraceProvider().Tracer("go.opentelemetry.io/plugin/othttp")), - WithPropagator(propagators.TraceContext{}), + WithPropagator(trace.TraceContext{}), WithSpanOptions(trace.WithSpanKind(trace.SpanKindServer)), } diff --git a/plugin/othttp/handler_test.go b/plugin/othttp/handler_test.go index 4bc645d10f3..aa73be7c4b6 100644 --- a/plugin/othttp/handler_test.go +++ b/plugin/othttp/handler_test.go @@ -20,7 +20,7 @@ import ( "net/http/httptest" "testing" - "go.opentelemetry.io/otel/api/propagators" + "go.opentelemetry.io/otel/api/trace" mocktrace "go.opentelemetry.io/otel/internal/trace" ) @@ -47,7 +47,7 @@ func TestBasics(t *testing.T) { if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected { t.Fatalf("got %d, expected %d", got, expected) } - if got := rr.Header().Get(propagators.TraceparentHeader); got == "" { + if got := rr.Header().Get(trace.TraceparentHeader); got == "" { t.Fatal("expected non empty trace header") } if got, expected := id, uint64(1); got != expected { diff --git a/plugin/othttp/wrap.go b/plugin/othttp/wrap.go index 95b3ec6bfe4..171eb2afddc 100644 --- a/plugin/othttp/wrap.go +++ b/plugin/othttp/wrap.go @@ -19,7 +19,7 @@ import ( "io" "net/http" - "go.opentelemetry.io/otel/api/propagators" + "go.opentelemetry.io/otel/api/propagation" ) var _ io.ReadCloser = &bodyWrapper{} @@ -50,7 +50,7 @@ func (w *bodyWrapper) Close() error { var _ http.ResponseWriter = &respWriterWrapper{} type injector interface { - Inject(context.Context, propagators.Supplier) + Inject(context.Context, propagation.Supplier) } // respWriterWrapper wraps a http.ResponseWriter in order to track the number of