From 680a95eb7f28476a49730fabb69432043446bbf8 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Tue, 27 Jul 2021 15:55:20 -0700 Subject: [PATCH 01/17] Add test pkg for integration tests with SDK --- bridge/opencensus/bridge.go | 172 ++---------------- bridge/opencensus/doc.go | 17 +- bridge/opencensus/go.mod | 3 +- .../opencensus/internal/oc2otel/attributes.go | 46 +++++ .../internal/oc2otel/span_context.go | 32 ++++ .../oc2otel/span_context_test.go} | 54 +----- .../internal/oc2otel/tracer_start_options.go | 44 +++++ .../otel2oc/span_context.go} | 22 +-- .../internal/otel2oc/span_context_test.go | 66 +++++++ bridge/opencensus/internal/span.go | 131 +++++++++++++ bridge/opencensus/internal/span_test.go | 66 +++++++ bridge/opencensus/internal/tracer.go | 66 +++++++ bridge/opencensus/{ => test}/bridge_test.go | 137 ++++++++------ bridge/opencensus/test/go.mod | 72 ++++++++ bridge/opencensus/test/go.sum | 106 +++++++++++ 15 files changed, 743 insertions(+), 291 deletions(-) create mode 100644 bridge/opencensus/internal/oc2otel/attributes.go create mode 100644 bridge/opencensus/internal/oc2otel/span_context.go rename bridge/opencensus/{utils/utils_test.go => internal/oc2otel/span_context_test.go} (63%) create mode 100644 bridge/opencensus/internal/oc2otel/tracer_start_options.go rename bridge/opencensus/{utils/utils.go => internal/otel2oc/span_context.go} (56%) create mode 100644 bridge/opencensus/internal/otel2oc/span_context_test.go create mode 100644 bridge/opencensus/internal/span.go create mode 100644 bridge/opencensus/internal/span_test.go create mode 100644 bridge/opencensus/internal/tracer.go rename bridge/opencensus/{ => test}/bridge_test.go (61%) create mode 100644 bridge/opencensus/test/go.mod create mode 100644 bridge/opencensus/test/go.sum diff --git a/bridge/opencensus/bridge.go b/bridge/opencensus/bridge.go index ecf1d5181ca..b30dab57ae9 100644 --- a/bridge/opencensus/bridge.go +++ b/bridge/opencensus/bridge.go @@ -15,15 +15,11 @@ package opencensus import ( - "context" - "fmt" - octrace "go.opencensus.io/trace" - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/bridge/opencensus/utils" - "go.opentelemetry.io/otel/codes" + "go.opentelemetry.io/otel/bridge/opencensus/internal" + "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" + "go.opentelemetry.io/otel/bridge/opencensus/internal/otel2oc" "go.opentelemetry.io/otel/trace" ) @@ -31,160 +27,18 @@ import ( // uses OpenTelemetry APIs. Using this implementation of Tracer "upgrades" // libraries that use OpenCensus to OpenTelemetry to facilitate a migration. func NewTracer(tracer trace.Tracer) octrace.Tracer { - return &otelTracer{tracer: tracer} -} - -type otelTracer struct { - tracer trace.Tracer -} - -var _ octrace.Tracer = (*otelTracer)(nil) - -func (o *otelTracer) StartSpan(ctx context.Context, name string, s ...octrace.StartOption) (context.Context, *octrace.Span) { - ctx, sp := o.tracer.Start(ctx, name, convertStartOptions(s, name)...) - return ctx, octrace.NewSpan(&span{otSpan: sp}) -} - -func convertStartOptions(optFns []octrace.StartOption, name string) []trace.SpanStartOption { - var ocOpts octrace.StartOptions - for _, fn := range optFns { - fn(&ocOpts) - } - otOpts := []trace.SpanStartOption{} - switch ocOpts.SpanKind { - case octrace.SpanKindClient: - otOpts = append(otOpts, trace.WithSpanKind(trace.SpanKindClient)) - case octrace.SpanKindServer: - otOpts = append(otOpts, trace.WithSpanKind(trace.SpanKindServer)) - case octrace.SpanKindUnspecified: - otOpts = append(otOpts, trace.WithSpanKind(trace.SpanKindUnspecified)) - } - - if ocOpts.Sampler != nil { - otel.Handle(fmt.Errorf("ignoring custom sampler for span %q created by OpenCensus because OpenTelemetry does not support creating a span with a custom sampler", name)) - } - return otOpts -} - -func (o *otelTracer) StartSpanWithRemoteParent(ctx context.Context, name string, parent octrace.SpanContext, s ...octrace.StartOption) (context.Context, *octrace.Span) { - // make sure span context is zero'd out so we use the remote parent - ctx = trace.ContextWithSpan(ctx, nil) - ctx = trace.ContextWithRemoteSpanContext(ctx, utils.OCSpanContextToOTel(parent)) - return o.StartSpan(ctx, name, s...) -} - -func (o *otelTracer) FromContext(ctx context.Context) *octrace.Span { - otSpan := trace.SpanFromContext(ctx) - return octrace.NewSpan(&span{otSpan: otSpan}) -} - -func (o *otelTracer) NewContext(parent context.Context, s *octrace.Span) context.Context { - if otSpan, ok := s.Internal().(*span); ok { - return trace.ContextWithSpan(parent, otSpan.otSpan) - } - otel.Handle(fmt.Errorf("unable to create context with span %q, since it was created using a different tracer", s.String())) - return parent -} - -type span struct { - otSpan trace.Span -} - -func (s *span) IsRecordingEvents() bool { - return s.otSpan.IsRecording() -} - -func (s *span) End() { - s.otSpan.End() -} - -func (s *span) SpanContext() octrace.SpanContext { - return utils.OTelSpanContextToOC(s.otSpan.SpanContext()) -} - -func (s *span) SetName(name string) { - s.otSpan.SetName(name) -} - -func (s *span) SetStatus(status octrace.Status) { - s.otSpan.SetStatus(codes.Code(status.Code), status.Message) -} - -func (s *span) AddAttributes(attributes ...octrace.Attribute) { - s.otSpan.SetAttributes(convertAttributes(attributes)...) -} - -func convertAttributes(attributes []octrace.Attribute) []attribute.KeyValue { - otAttributes := make([]attribute.KeyValue, len(attributes)) - for i, a := range attributes { - otAttributes[i] = attribute.KeyValue{ - Key: attribute.Key(a.Key()), - Value: convertValue(a.Value()), - } - } - return otAttributes -} - -func convertValue(ocval interface{}) attribute.Value { - switch v := ocval.(type) { - case bool: - return attribute.BoolValue(v) - case int64: - return attribute.Int64Value(v) - case float64: - return attribute.Float64Value(v) - case string: - return attribute.StringValue(v) - default: - return attribute.StringValue("unknown") - } -} - -func (s *span) Annotate(attributes []octrace.Attribute, str string) { - s.otSpan.AddEvent(str, trace.WithAttributes(convertAttributes(attributes)...)) -} - -func (s *span) Annotatef(attributes []octrace.Attribute, format string, a ...interface{}) { - s.Annotate(attributes, fmt.Sprintf(format, a...)) -} - -var ( - uncompressedKey = attribute.Key("uncompressed byte size") - compressedKey = attribute.Key("compressed byte size") -) - -func (s *span) AddMessageSendEvent(messageID, uncompressedByteSize, compressedByteSize int64) { - s.otSpan.AddEvent("message send", - trace.WithAttributes( - attribute.KeyValue{ - Key: uncompressedKey, - Value: attribute.Int64Value(uncompressedByteSize), - }, - attribute.KeyValue{ - Key: compressedKey, - Value: attribute.Int64Value(compressedByteSize), - }), - ) -} - -func (s *span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compressedByteSize int64) { - s.otSpan.AddEvent("message receive", - trace.WithAttributes( - attribute.KeyValue{ - Key: uncompressedKey, - Value: attribute.Int64Value(uncompressedByteSize), - }, - attribute.KeyValue{ - Key: compressedKey, - Value: attribute.Int64Value(compressedByteSize), - }), - ) + return internal.NewTracer(tracer) } -func (s *span) AddLink(l octrace.Link) { - otel.Handle(fmt.Errorf("ignoring OpenCensus link %+v for span %q because OpenTelemetry doesn't support setting links after creation", l, s.String())) +// OTelSpanContextToOC converts from an OpenTelemetry SpanContext to an +// OpenCensus SpanContext, and handles any incompatibilities with the global +// error handler. +func OTelSpanContextToOC(sc trace.SpanContext) octrace.SpanContext { + return otel2oc.SpanContext(sc) } -func (s *span) String() string { - return fmt.Sprintf("span %s", s.otSpan.SpanContext().SpanID().String()) +// OCSpanContextToOTel converts from an OpenCensus SpanContext to an +// OpenTelemetry SpanContext. +func OCSpanContextToOTel(sc octrace.SpanContext) trace.SpanContext { + return oc2otel.SpanContext(sc) } diff --git a/bridge/opencensus/doc.go b/bridge/opencensus/doc.go index 1705e102b80..9ade26c665b 100644 --- a/bridge/opencensus/doc.go +++ b/bridge/opencensus/doc.go @@ -12,5 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package opencensus provides a migration bridge forwarding the OpenCensus API to the OpenTelemetry SDK. +// Package opencensus provides a migration bridge from OpenCensus to +// OpenTelemetry. The NewTracer function should be used to create an +// OpenCensus Tracer from an OpenTelemetry Tracer. This Tracer can be use in +// place of any existing OpenCensus Tracer and will generate OpenTelemetry +// spans for traces. These spans will be exported by the OpenTelemetry +// TracerProvider the original OpenTelemetry Tracer came from. +// +// There are known limitations to this bridge: +// +// - The AddLink method for OpenCensus Spans is not compatible with the +// OpenTelemetry Span. No link can be added to an OpenTelemetry Span once it +// is started. Any calls to this method for the OpenCensus Span will result +// in an error being sent to the OpenTelemetry default ErrorHandler. +// +// - The NewContext method of the OpenCensus Tracer cannot embed an OpenCensus +// Span in a context unless that Span was created by that Tracer. package opencensus // import "go.opentelemetry.io/otel/bridge/opencensus" diff --git a/bridge/opencensus/go.mod b/bridge/opencensus/go.mod index 054159eae31..46983755edd 100644 --- a/bridge/opencensus/go.mod +++ b/bridge/opencensus/go.mod @@ -6,7 +6,6 @@ require ( go.opencensus.io v0.22.6-0.20201102222123-380f4078db9f go.opentelemetry.io/otel v1.0.0-RC2 go.opentelemetry.io/otel/metric v0.22.0 - go.opentelemetry.io/otel/oteltest v1.0.0-RC2 go.opentelemetry.io/otel/sdk v1.0.0-RC2 go.opentelemetry.io/otel/sdk/export/metric v0.22.0 go.opentelemetry.io/otel/trace v1.0.0-RC2 @@ -71,3 +70,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ./test diff --git a/bridge/opencensus/internal/oc2otel/attributes.go b/bridge/opencensus/internal/oc2otel/attributes.go new file mode 100644 index 00000000000..490460938a4 --- /dev/null +++ b/bridge/opencensus/internal/oc2otel/attributes.go @@ -0,0 +1,46 @@ +// 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 oc2otel + +import ( + octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/attribute" +) + +func Attributes(attr []octrace.Attribute) []attribute.KeyValue { + otelAttr := make([]attribute.KeyValue, len(attr)) + for i, a := range attr { + otelAttr[i] = attribute.KeyValue{ + Key: attribute.Key(a.Key()), + Value: AttributeValue(a.Value()), + } + } + return otelAttr +} + +func AttributeValue(ocval interface{}) attribute.Value { + switch v := ocval.(type) { + case bool: + return attribute.BoolValue(v) + case int64: + return attribute.Int64Value(v) + case float64: + return attribute.Float64Value(v) + case string: + return attribute.StringValue(v) + default: + return attribute.StringValue("unknown") + } +} diff --git a/bridge/opencensus/internal/oc2otel/span_context.go b/bridge/opencensus/internal/oc2otel/span_context.go new file mode 100644 index 00000000000..713bea0487f --- /dev/null +++ b/bridge/opencensus/internal/oc2otel/span_context.go @@ -0,0 +1,32 @@ +// 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 oc2otel + +import ( + octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/trace" +) + +func SpanContext(sc octrace.SpanContext) trace.SpanContext { + var traceFlags trace.TraceFlags + if sc.IsSampled() { + traceFlags = trace.FlagsSampled + } + return trace.NewSpanContext(trace.SpanContextConfig{ + TraceID: trace.TraceID(sc.TraceID), + SpanID: trace.SpanID(sc.SpanID), + TraceFlags: traceFlags, + }) +} diff --git a/bridge/opencensus/utils/utils_test.go b/bridge/opencensus/internal/oc2otel/span_context_test.go similarity index 63% rename from bridge/opencensus/utils/utils_test.go rename to bridge/opencensus/internal/oc2otel/span_context_test.go index cc9674c7fdd..899f27c890f 100644 --- a/bridge/opencensus/utils/utils_test.go +++ b/bridge/opencensus/internal/oc2otel/span_context_test.go @@ -12,63 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -package utils +package oc2otel import ( "testing" - "go.opencensus.io/trace/tracestate" - octrace "go.opencensus.io/trace" - + "go.opencensus.io/trace/tracestate" "go.opentelemetry.io/otel/trace" ) -func TestOTelSpanContextToOC(t *testing.T) { - for _, tc := range []struct { - description string - input trace.SpanContext - expected octrace.SpanContext - }{ - { - description: "empty", - }, - { - description: "sampled", - input: trace.NewSpanContext(trace.SpanContextConfig{ - TraceID: trace.TraceID([16]byte{1}), - SpanID: trace.SpanID([8]byte{2}), - TraceFlags: trace.FlagsSampled, - }), - expected: octrace.SpanContext{ - TraceID: octrace.TraceID([16]byte{1}), - SpanID: octrace.SpanID([8]byte{2}), - TraceOptions: octrace.TraceOptions(0x1), - }, - }, - { - description: "not sampled", - input: trace.NewSpanContext(trace.SpanContextConfig{ - TraceID: trace.TraceID([16]byte{1}), - SpanID: trace.SpanID([8]byte{2}), - }), - expected: octrace.SpanContext{ - TraceID: octrace.TraceID([16]byte{1}), - SpanID: octrace.SpanID([8]byte{2}), - TraceOptions: octrace.TraceOptions(0), - }, - }, - } { - t.Run(tc.description, func(t *testing.T) { - output := OTelSpanContextToOC(tc.input) - if output != tc.expected { - t.Fatalf("Got %+v spancontext, exepected %+v.", output, tc.expected) - } - }) - } -} - -func TestOCSpanContextToOTel(t *testing.T) { +func TestSpanContextConversion(t *testing.T) { for _, tc := range []struct { description string input octrace.SpanContext @@ -116,7 +70,7 @@ func TestOCSpanContextToOTel(t *testing.T) { }, } { t.Run(tc.description, func(t *testing.T) { - output := OCSpanContextToOTel(tc.input) + output := SpanContext(tc.input) if !output.Equal(tc.expected) { t.Fatalf("Got %+v spancontext, exepected %+v.", output, tc.expected) } diff --git a/bridge/opencensus/internal/oc2otel/tracer_start_options.go b/bridge/opencensus/internal/oc2otel/tracer_start_options.go new file mode 100644 index 00000000000..3cd8d1ac94a --- /dev/null +++ b/bridge/opencensus/internal/oc2otel/tracer_start_options.go @@ -0,0 +1,44 @@ +// 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 oc2otel + +import ( + "fmt" + + octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/trace" +) + +func StartOptions(optFns []octrace.StartOption, name string) []trace.SpanStartOption { + var ocOpts octrace.StartOptions + for _, fn := range optFns { + fn(&ocOpts) + } + otOpts := []trace.SpanStartOption{} + switch ocOpts.SpanKind { + case octrace.SpanKindClient: + otOpts = append(otOpts, trace.WithSpanKind(trace.SpanKindClient)) + case octrace.SpanKindServer: + otOpts = append(otOpts, trace.WithSpanKind(trace.SpanKindServer)) + case octrace.SpanKindUnspecified: + otOpts = append(otOpts, trace.WithSpanKind(trace.SpanKindUnspecified)) + } + + if ocOpts.Sampler != nil { + otel.Handle(fmt.Errorf("ignoring custom sampler for span %q created by OpenCensus because OpenTelemetry does not support creating a span with a custom sampler", name)) + } + return otOpts +} diff --git a/bridge/opencensus/utils/utils.go b/bridge/opencensus/internal/otel2oc/span_context.go similarity index 56% rename from bridge/opencensus/utils/utils.go rename to bridge/opencensus/internal/otel2oc/span_context.go index 91f586d57a7..c2628461d4b 100644 --- a/bridge/opencensus/utils/utils.go +++ b/bridge/opencensus/internal/otel2oc/span_context.go @@ -12,18 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -package utils // import "go.opentelemetry.io/otel/bridge/opencensus/utils" +package otel2oc import ( octrace "go.opencensus.io/trace" - "go.opentelemetry.io/otel/trace" ) -// OTelSpanContextToOC converts from an OpenTelemetry SpanContext to an -// OpenCensus SpanContext, and handles any incompatibilities with the global -// error handler. -func OTelSpanContextToOC(sc trace.SpanContext) octrace.SpanContext { +func SpanContext(sc trace.SpanContext) octrace.SpanContext { var to octrace.TraceOptions if sc.IsSampled() { // OpenCensus doesn't expose functions to directly set sampled @@ -35,17 +31,3 @@ func OTelSpanContextToOC(sc trace.SpanContext) octrace.SpanContext { TraceOptions: to, } } - -// OCSpanContextToOTel converts from an OpenCensus SpanContext to an -// OpenTelemetry SpanContext. -func OCSpanContextToOTel(sc octrace.SpanContext) trace.SpanContext { - var traceFlags trace.TraceFlags - if sc.IsSampled() { - traceFlags = trace.FlagsSampled - } - return trace.NewSpanContext(trace.SpanContextConfig{ - TraceID: trace.TraceID(sc.TraceID), - SpanID: trace.SpanID(sc.SpanID), - TraceFlags: traceFlags, - }) -} diff --git a/bridge/opencensus/internal/otel2oc/span_context_test.go b/bridge/opencensus/internal/otel2oc/span_context_test.go new file mode 100644 index 00000000000..8ba776b54cd --- /dev/null +++ b/bridge/opencensus/internal/otel2oc/span_context_test.go @@ -0,0 +1,66 @@ +// 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 otel2oc + +import ( + "testing" + + octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/trace" +) + +func TestSpanContextConversion(t *testing.T) { + for _, tc := range []struct { + description string + input trace.SpanContext + expected octrace.SpanContext + }{ + { + description: "empty", + }, + { + description: "sampled", + input: trace.NewSpanContext(trace.SpanContextConfig{ + TraceID: trace.TraceID([16]byte{1}), + SpanID: trace.SpanID([8]byte{2}), + TraceFlags: trace.FlagsSampled, + }), + expected: octrace.SpanContext{ + TraceID: octrace.TraceID([16]byte{1}), + SpanID: octrace.SpanID([8]byte{2}), + TraceOptions: octrace.TraceOptions(0x1), + }, + }, + { + description: "not sampled", + input: trace.NewSpanContext(trace.SpanContextConfig{ + TraceID: trace.TraceID([16]byte{1}), + SpanID: trace.SpanID([8]byte{2}), + }), + expected: octrace.SpanContext{ + TraceID: octrace.TraceID([16]byte{1}), + SpanID: octrace.SpanID([8]byte{2}), + TraceOptions: octrace.TraceOptions(0), + }, + }, + } { + t.Run(tc.description, func(t *testing.T) { + output := SpanContext(tc.input) + if output != tc.expected { + t.Fatalf("Got %+v spancontext, exepected %+v.", output, tc.expected) + } + }) + } +} diff --git a/bridge/opencensus/internal/span.go b/bridge/opencensus/internal/span.go new file mode 100644 index 00000000000..adf36e05d44 --- /dev/null +++ b/bridge/opencensus/internal/span.go @@ -0,0 +1,131 @@ +// 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 internal + +import ( + "fmt" + + octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" + "go.opentelemetry.io/otel/bridge/opencensus/internal/otel2oc" + "go.opentelemetry.io/otel/codes" + "go.opentelemetry.io/otel/trace" +) + +const ( + // MessageSendEvent is the name of the message send event. + MessageSendEvent = "message send" + // MessageReceiveEvent is the name of the message receive event. + MessageReceiveEvent = "message receive" +) + +var ( + // UncompressedKey is used for the uncompressed byte size attribute. + UncompressedKey = attribute.Key("uncompressed byte size") + // CompressedKey is used for the compressed byte size attribute. + CompressedKey = attribute.Key("compressed byte size") +) + +// Span is an OpenCensus SpanInterface wrapper for an OpenTelemetry Span. +type Span struct { + otelSpan trace.Span +} + +// NewSpan returns an OpenCensus Span wrapping an OpenTelemetry Span. +func NewSpan(s trace.Span) *octrace.Span { + return octrace.NewSpan(&Span{otelSpan: s}) +} + +// IsRecordingEvents returns true if events are being recorded for this span. +func (s *Span) IsRecordingEvents() bool { + return s.otelSpan.IsRecording() +} + +// End ends thi span. +func (s *Span) End() { + s.otelSpan.End() +} + +// SpanContext returns the SpanContext of this span. +func (s *Span) SpanContext() octrace.SpanContext { + return otel2oc.SpanContext(s.otelSpan.SpanContext()) +} + +// SetName sets the name of this span, if it is recording events. +func (s *Span) SetName(name string) { + s.otelSpan.SetName(name) +} + +// SetStatus sets the status of this span, if it is recording events. +func (s *Span) SetStatus(status octrace.Status) { + s.otelSpan.SetStatus(codes.Code(status.Code), status.Message) +} + +// AddAttributes sets attributes in this span. +func (s *Span) AddAttributes(attributes ...octrace.Attribute) { + s.otelSpan.SetAttributes(oc2otel.Attributes(attributes)...) +} + +// Annotate adds an annotation with attributes to this span. +func (s *Span) Annotate(attributes []octrace.Attribute, str string) { + s.otelSpan.AddEvent(str, trace.WithAttributes(oc2otel.Attributes(attributes)...)) +} + +// Annotatef adds a formatted annotation with attributes to this span. +func (s *Span) Annotatef(attributes []octrace.Attribute, format string, a ...interface{}) { + s.Annotate(attributes, fmt.Sprintf(format, a...)) +} + +// AddMessageSendEvent adds a message send event to this span. +func (s *Span) AddMessageSendEvent(messageID, uncompressedByteSize, compressedByteSize int64) { + s.otelSpan.AddEvent(MessageSendEvent, + trace.WithAttributes( + attribute.KeyValue{ + Key: UncompressedKey, + Value: attribute.Int64Value(uncompressedByteSize), + }, + attribute.KeyValue{ + Key: CompressedKey, + Value: attribute.Int64Value(compressedByteSize), + }), + ) +} + +// AddMessageReceiveEvent adds a message receive event to this span. +func (s *Span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compressedByteSize int64) { + s.otelSpan.AddEvent(MessageReceiveEvent, + trace.WithAttributes( + attribute.KeyValue{ + Key: UncompressedKey, + Value: attribute.Int64Value(uncompressedByteSize), + }, + attribute.KeyValue{ + Key: CompressedKey, + Value: attribute.Int64Value(compressedByteSize), + }), + ) +} + +// AddLink adds a link to this span. +func (s *Span) AddLink(l octrace.Link) { + otel.Handle(fmt.Errorf("ignoring OpenCensus link %+v for span %q because OpenTelemetry doesn't support setting links after creation", l, s.String())) +} + +// String prints a string representation of this span. +func (s *Span) String() string { + return fmt.Sprintf("span %s", s.otelSpan.SpanContext().SpanID().String()) +} diff --git a/bridge/opencensus/internal/span_test.go b/bridge/opencensus/internal/span_test.go new file mode 100644 index 00000000000..37da15cc4ed --- /dev/null +++ b/bridge/opencensus/internal/span_test.go @@ -0,0 +1,66 @@ +// 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 internal + +import ( + "testing" + + "go.opentelemetry.io/otel/trace" +) + +type span struct { + trace.Span + + recording bool + ended bool + sc trace.SpanContext +} + +func (s *span) IsRecording() bool { return s.recording } +func (s *span) End(...trace.SpanEndOption) { s.ended = true } +func (s *span) SpanContext() trace.SpanContext { return s.sc } + +func TestSpanIsRecordingEvents(t *testing.T) { + s := &span{recording: true} + ocS := NewSpan(s) + if !ocS.IsRecordingEvents() { + t.Errorf("span.IsRecordingEvents() = false, want true") + } + s.recording = false + if ocS.IsRecordingEvents() { + t.Errorf("span.IsRecordingEvents() = true, want false") + } +} + +func TestSpanEnd(t *testing.T) { + s := new(span) + ocS := NewSpan(s) + if s.ended { + t.Fatal("new span already ended") + } + + ocS.End() + if !s.ended { + t.Error("span.End() did not end OpenTelemetry span") + } +} + +func TestSpanSpanContext(t *testing.T) { + sc := trace.NewSpanContext(trace.SpanContextConfig{ + TraceID: [16]byte{1}, + SpanID: [8]byte{1}, + }) + converted := util +} diff --git a/bridge/opencensus/internal/tracer.go b/bridge/opencensus/internal/tracer.go new file mode 100644 index 00000000000..107fb8eb259 --- /dev/null +++ b/bridge/opencensus/internal/tracer.go @@ -0,0 +1,66 @@ +// 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 internal + +import ( + "context" + "fmt" + + octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" + "go.opentelemetry.io/otel/bridge/opencensus/utils" + "go.opentelemetry.io/otel/trace" +) + +// Tracer is an OpenCensus Tracer that wraps an OpenTelemetry Tracer. +type Tracer struct { + otelTracer trace.Tracer +} + +// NewTracer returns an OpenCensus Tracer that wraps the OpenTelemetry tracer. +func NewTracer(tracer trace.Tracer) octrace.Tracer { + return &Tracer{otelTracer: tracer} +} + +// StartSpan starts a new child span of the current span in the context. If +// there is no span in the context, it creates a new trace and span. +func (o *Tracer) StartSpan(ctx context.Context, name string, s ...octrace.StartOption) (context.Context, *octrace.Span) { + ctx, sp := o.otelTracer.Start(ctx, name, oc2otel.StartOptions(s, name)...) + return ctx, NewSpan(sp) +} + +// StartSpanWithRemoteParent starts a new child span of the span from the +// given parent. +func (o *Tracer) StartSpanWithRemoteParent(ctx context.Context, name string, parent octrace.SpanContext, s ...octrace.StartOption) (context.Context, *octrace.Span) { + // make sure span context is zero'd out so we use the remote parent + ctx = trace.ContextWithSpan(ctx, nil) + ctx = trace.ContextWithRemoteSpanContext(ctx, utils.OCSpanContextToOTel(parent)) + return o.StartSpan(ctx, name, s...) +} + +// FromContext returns the Span stored in a context. +func (o *Tracer) FromContext(ctx context.Context) *octrace.Span { + return NewSpan(trace.SpanFromContext(ctx)) +} + +// NewContext returns a new context with the given Span attached. +func (o *Tracer) NewContext(parent context.Context, s *octrace.Span) context.Context { + if otSpan, ok := s.Internal().(*Span); ok { + return trace.ContextWithSpan(parent, otSpan.otelSpan) + } + otel.Handle(fmt.Errorf("unable to create context with span %q, since it was created using a different tracer", s.String())) + return parent +} diff --git a/bridge/opencensus/bridge_test.go b/bridge/opencensus/test/bridge_test.go similarity index 61% rename from bridge/opencensus/bridge_test.go rename to bridge/opencensus/test/bridge_test.go index 4616500dfa1..4b4ddbd7f67 100644 --- a/bridge/opencensus/bridge_test.go +++ b/bridge/opencensus/test/bridge_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package opencensus +package test import ( "context" @@ -21,17 +21,20 @@ import ( octrace "go.opencensus.io/trace" "go.opentelemetry.io/otel/attribute" + ocbridge "go.opentelemetry.io/otel/bridge/opencensus" + "go.opentelemetry.io/otel/bridge/opencensus/internal" "go.opentelemetry.io/otel/bridge/opencensus/utils" "go.opentelemetry.io/otel/codes" - "go.opentelemetry.io/otel/oteltest" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/sdk/trace/tracetest" "go.opentelemetry.io/otel/trace" ) func TestMixedAPIs(t *testing.T) { - sr := new(oteltest.SpanRecorder) - tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)) + sr := tracetest.NewSpanRecorder() + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) tracer := tp.Tracer("mixedapitracer") - octrace.DefaultTracer = NewTracer(tracer) + octrace.DefaultTracer = ocbridge.NewTracer(tracer) func() { ctx := context.Background() @@ -52,7 +55,7 @@ func TestMixedAPIs(t *testing.T) { defer otspan2.End() }() - spans := sr.Completed() + spans := sr.Ended() if len(spans) != 4 { for _, span := range spans { @@ -61,28 +64,27 @@ func TestMixedAPIs(t *testing.T) { t.Fatalf("Got %d spans, exepected %d.", len(spans), 4) } - parent := &oteltest.Span{} - for i := range spans { - // Reverse the order we look at the spans in, since they are listed in last-to-first order. - i = len(spans) - i - 1 - // Verify that OpenCensus spans and opentelemetry spans have each other as parents. - if spans[i].ParentSpanID() != parent.SpanContext().SpanID() { - t.Errorf("Span %v had parent %v. Expected %d", spans[i].Name(), spans[i].ParentSpanID(), parent.SpanContext().SpanID()) + var parent trace.SpanContext + for i := len(spans) - 1; i >= 0; i-- { + // Verify that OpenCensus spans and OpenTelemetry spans have each + // other as parents. + if psid := spans[i].Parent().SpanID(); psid != parent.SpanID() { + t.Errorf("Span %v had parent %v. Expected %v", spans[i].Name(), psid, parent.SpanID()) } - parent = spans[i] + parent = spans[i].SpanContext() } } func TestStartOptions(t *testing.T) { - sr := new(oteltest.SpanRecorder) - tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)) - octrace.DefaultTracer = NewTracer(tp.Tracer("startoptionstracer")) + sr := tracetest.NewSpanRecorder() + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) + octrace.DefaultTracer = ocbridge.NewTracer(tp.Tracer("startoptionstracer")) ctx := context.Background() _, span := octrace.StartSpan(ctx, "OpenCensusSpan", octrace.WithSpanKind(octrace.SpanKindClient)) span.End() - spans := sr.Completed() + spans := sr.Ended() if len(spans) != 1 { t.Fatalf("Got %d spans, exepected %d", len(spans), 1) @@ -94,10 +96,10 @@ func TestStartOptions(t *testing.T) { } func TestStartSpanWithRemoteParent(t *testing.T) { - sr := new(oteltest.SpanRecorder) - tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)) + sr := tracetest.NewSpanRecorder() + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) tracer := tp.Tracer("remoteparent") - octrace.DefaultTracer = NewTracer(tracer) + octrace.DefaultTracer = ocbridge.NewTracer(tracer) ctx := context.Background() ctx, parent := tracer.Start(ctx, "OpenTelemetrySpan1") @@ -105,22 +107,22 @@ func TestStartSpanWithRemoteParent(t *testing.T) { _, span := octrace.StartSpanWithRemoteParent(ctx, "OpenCensusSpan", utils.OTelSpanContextToOC(parent.SpanContext())) span.End() - spans := sr.Completed() + spans := sr.Ended() if len(spans) != 1 { t.Fatalf("Got %d spans, exepected %d", len(spans), 1) } - if spans[0].ParentSpanID() != parent.SpanContext().SpanID() { - t.Errorf("Span %v, had parent %v. Expected %d", spans[0].Name(), spans[0].ParentSpanID(), parent.SpanContext().SpanID()) + if psid := spans[0].Parent().SpanID(); psid != parent.SpanContext().SpanID() { + t.Errorf("Span %v, had parent %v. Expected %d", spans[0].Name(), psid, parent.SpanContext().SpanID()) } } func TestToFromContext(t *testing.T) { - sr := new(oteltest.SpanRecorder) - tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)) + sr := tracetest.NewSpanRecorder() + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) tracer := tp.Tracer("tofromcontext") - octrace.DefaultTracer = NewTracer(tracer) + octrace.DefaultTracer = ocbridge.NewTracer(tracer) func() { ctx := context.Background() @@ -129,7 +131,7 @@ func TestToFromContext(t *testing.T) { defer otSpan1.End() // Use NewContext instead of the context from Start - ctx = octrace.NewContext(ctx, octrace.NewSpan(&span{otSpan: otSpan1})) + ctx = octrace.NewContext(ctx, internal.NewSpan(otSpan1)) ctx, _ = tracer.Start(ctx, "OpenTelemetrySpan2") @@ -139,28 +141,27 @@ func TestToFromContext(t *testing.T) { }() - spans := sr.Completed() + spans := sr.Ended() if len(spans) != 2 { t.Fatalf("Got %d spans, exepected %d.", len(spans), 2) } - parent := &oteltest.Span{} - for i := range spans { - // Reverse the order we look at the spans in, since they are listed in last-to-first order. - i = len(spans) - i - 1 - // Verify that OpenCensus spans and opentelemetry spans have each other as parents. - if spans[i].ParentSpanID() != parent.SpanContext().SpanID() { - t.Errorf("Span %v had parent %v. Expected %d", spans[i].Name(), spans[i].ParentSpanID(), parent.SpanContext().SpanID()) + var parent trace.SpanContext + for i := len(spans) - 1; i >= 0; i-- { + // Verify that OpenCensus spans and OpenTelemetry spans have each + // other as parents. + if psid := spans[i].Parent().SpanID(); psid != parent.SpanID() { + t.Errorf("Span %v had parent %v. Expected %v", spans[i].Name(), psid, parent.SpanID()) } - parent = spans[i] + parent = spans[i].SpanContext() } } func TestIsRecordingEvents(t *testing.T) { - sr := new(oteltest.SpanRecorder) - tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)) - octrace.DefaultTracer = NewTracer(tp.Tracer("isrecordingevents")) + sr := tracetest.NewSpanRecorder() + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) + octrace.DefaultTracer = ocbridge.NewTracer(tp.Tracer("isrecordingevents")) ctx := context.Background() _, ocspan := octrace.StartSpan(ctx, "OpenCensusSpan1") @@ -169,10 +170,18 @@ func TestIsRecordingEvents(t *testing.T) { } } +func attrsMap(s []attribute.KeyValue) map[attribute.Key]attribute.Value { + m := make(map[attribute.Key]attribute.Value, len(s)) + for _, a := range s { + m[a.Key] = a.Value + } + return m +} + func TestSetThings(t *testing.T) { - sr := new(oteltest.SpanRecorder) - tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)) - octrace.DefaultTracer = NewTracer(tp.Tracer("setthings")) + sr := tracetest.NewSpanRecorder() + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) + octrace.DefaultTracer = ocbridge.NewTracer(tp.Tracer("setthings")) ctx := context.Background() _, ocspan := octrace.StartSpan(ctx, "OpenCensusSpan1") @@ -199,7 +208,7 @@ func TestSetThings(t *testing.T) { ocspan.AddMessageReceiveEvent(246, 135, 369) ocspan.End() - spans := sr.Completed() + spans := sr.Ended() if len(spans) != 1 { t.Fatalf("Got %d spans, exepected %d.", len(spans), 1) @@ -210,24 +219,25 @@ func TestSetThings(t *testing.T) { t.Errorf("Got name %v, expected span-foo", s.Name()) } - if s.StatusCode().String() != codes.Error.String() { - t.Errorf("Got code %v, expected 1", s.StatusCode().String()) + if s.Status().Code != codes.Error { + t.Errorf("Got code %v, expected %v", s.Status().Code, codes.Error) } - if s.StatusMessage() != "foo" { - t.Errorf("Got code %v, expected foo", s.StatusMessage()) + if s.Status().Description != "foo" { + t.Errorf("Got code %v, expected foo", s.Status().Description) } - if v := s.Attributes()[attribute.Key("bool")]; !v.AsBool() { + attrs := attrsMap(s.Attributes()) + if v := attrs[attribute.Key("bool")]; !v.AsBool() { t.Errorf("Got attributes[bool] %v, expected true", v.AsBool()) } - if v := s.Attributes()[attribute.Key("int64")]; v.AsInt64() != 12345 { + if v := attrs[attribute.Key("int64")]; v.AsInt64() != 12345 { t.Errorf("Got attributes[int64] %v, expected 12345", v.AsInt64()) } - if v := s.Attributes()[attribute.Key("float64")]; v.AsFloat64() != 12.345 { + if v := attrs[attribute.Key("float64")]; v.AsFloat64() != 12.345 { t.Errorf("Got attributes[float64] %v, expected 12.345", v.AsFloat64()) } - if v := s.Attributes()[attribute.Key("string")]; v.AsString() != "stringval" { + if v := attrs[attribute.Key("string")]; v.AsString() != "stringval" { t.Errorf("Got attributes[string] %v, expected stringval", v.AsString()) } @@ -235,43 +245,50 @@ func TestSetThings(t *testing.T) { t.Fatalf("Got len(events) = %v, expected 4", len(s.Events())) } annotateEvent := s.Events()[0] + aeAttrs := attrsMap(annotateEvent.Attributes) annotatefEvent := s.Events()[1] + afeAttrs := attrsMap(annotatefEvent.Attributes) sendEvent := s.Events()[2] receiveEvent := s.Events()[3] - if v := annotateEvent.Attributes[attribute.Key("string")]; v.AsString() != "annotateval" { + if v := aeAttrs[attribute.Key("string")]; v.AsString() != "annotateval" { t.Errorf("Got annotateEvent.Attributes[string] = %v, expected annotateval", v.AsString()) } if annotateEvent.Name != "annotate" { t.Errorf("Got annotateEvent.Name = %v, expected annotate", annotateEvent.Name) } - if v := annotatefEvent.Attributes[attribute.Key("int64")]; v.AsInt64() != 12345 { + if v := afeAttrs[attribute.Key("int64")]; v.AsInt64() != 12345 { t.Errorf("Got annotatefEvent.Attributes[int64] = %v, expected 12345", v.AsInt64()) } - if v := annotatefEvent.Attributes[attribute.Key("float64")]; v.AsFloat64() != 12.345 { + if v := afeAttrs[attribute.Key("float64")]; v.AsFloat64() != 12.345 { t.Errorf("Got annotatefEvent.Attributes[float64] = %v, expected 12.345", v.AsFloat64()) } if annotatefEvent.Name != "annotate67890" { t.Errorf("Got annotatefEvent.Name = %v, expected annotate67890", annotatefEvent.Name) } - if v := annotateEvent.Attributes[attribute.Key("string")]; v.AsString() != "annotateval" { + if v := aeAttrs[attribute.Key("string")]; v.AsString() != "annotateval" { t.Errorf("Got annotateEvent.Attributes[string] = %v, expected annotateval", v.AsString()) } + + uncompressedKey := attribute.Key("uncompressed byte size") + compressedKey := attribute.Key("compressed byte size") + seAttrs := attrsMap(sendEvent.Attributes) + reAttrs := attrsMap(receiveEvent.Attributes) if sendEvent.Name != "message send" { t.Errorf("Got sendEvent.Name = %v, expected message send", sendEvent.Name) } - if v := sendEvent.Attributes[uncompressedKey]; v.AsInt64() != 456 { + if v := seAttrs[uncompressedKey]; v.AsInt64() != 456 { t.Errorf("Got sendEvent.Attributes[uncompressedKey] = %v, expected 456", v.AsInt64()) } - if v := sendEvent.Attributes[compressedKey]; v.AsInt64() != 789 { + if v := seAttrs[compressedKey]; v.AsInt64() != 789 { t.Errorf("Got sendEvent.Attributes[compressedKey] = %v, expected 789", v.AsInt64()) } if receiveEvent.Name != "message receive" { t.Errorf("Got receiveEvent.Name = %v, expected message receive", receiveEvent.Name) } - if v := receiveEvent.Attributes[uncompressedKey]; v.AsInt64() != 135 { + if v := reAttrs[uncompressedKey]; v.AsInt64() != 135 { t.Errorf("Got receiveEvent.Attributes[uncompressedKey] = %v, expected 135", v.AsInt64()) } - if v := receiveEvent.Attributes[compressedKey]; v.AsInt64() != 369 { + if v := reAttrs[compressedKey]; v.AsInt64() != 369 { t.Errorf("Got receiveEvent.Attributes[compressedKey] = %v, expected 369", v.AsInt64()) } } diff --git a/bridge/opencensus/test/go.mod b/bridge/opencensus/test/go.mod new file mode 100644 index 00000000000..afef3cd52f6 --- /dev/null +++ b/bridge/opencensus/test/go.mod @@ -0,0 +1,72 @@ +module go.opentelemetry.io/otel/bridge/opencensus/test + +go 1.16 + +replace go.opentelemetry.io/otel => ../../.. + +replace go.opentelemetry.io/otel/bridge/opencensus => ../ + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ./ + +replace go.opentelemetry.io/otel/bridge/opentracing => ../../opentracing + +replace go.opentelemetry.io/otel/example/jaeger => ../../../example/jaeger + +replace go.opentelemetry.io/otel/example/namedtracer => ../../../example/namedtracer + +replace go.opentelemetry.io/otel/example/opencensus => ../../../example/opencensus + +replace go.opentelemetry.io/otel/example/otel-collector => ../../../example/otel-collector + +replace go.opentelemetry.io/otel/example/passthrough => ../../../example/passthrough + +replace go.opentelemetry.io/otel/example/prometheus => ../../../example/prometheus + +replace go.opentelemetry.io/otel/example/zipkin => ../../../example/zipkin + +replace go.opentelemetry.io/otel/exporters/jaeger => ../../../exporters/jaeger + +replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric => ../../../exporters/otlp/otlpmetric + +replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc => ../../../exporters/otlp/otlpmetric/otlpmetricgrpc + +replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/exporters/otlp/otlptrace => ../../../exporters/otlp/otlptrace + +replace go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc => ../../../exporters/otlp/otlptrace/otlptracegrpc + +replace go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp => ../../../exporters/otlp/otlptrace/otlptracehttp + +replace go.opentelemetry.io/otel/exporters/prometheus => ../../../exporters/prometheus + +replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../../exporters/stdout/stdoutmetric + +replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../../exporters/stdout/stdouttrace + +replace go.opentelemetry.io/otel/exporters/zipkin => ../../../exporters/zipkin + +replace go.opentelemetry.io/otel/internal/metric => ../../../internal/metric + +replace go.opentelemetry.io/otel/internal/tools => ../../../internal/tools + +replace go.opentelemetry.io/otel/metric => ../../../metric + +replace go.opentelemetry.io/otel/oteltest => ../../../oteltest + +replace go.opentelemetry.io/otel/sdk => ../../../sdk + +replace go.opentelemetry.io/otel/sdk/export/metric => ../../../sdk/export/metric + +replace go.opentelemetry.io/otel/sdk/metric => ../../../sdk/metric + +replace go.opentelemetry.io/otel/trace => ../../../trace + +require ( + go.opencensus.io v0.23.0 + go.opentelemetry.io/otel v1.0.0-RC1 + go.opentelemetry.io/otel/bridge/opencensus v0.0.0-00010101000000-000000000000 + go.opentelemetry.io/otel/oteltest v1.0.0-RC1 + go.opentelemetry.io/otel/sdk v1.0.0-RC1 + go.opentelemetry.io/otel/trace v1.0.0-RC1 +) diff --git a/bridge/opencensus/test/go.sum b/bridge/opencensus/test/go.sum new file mode 100644 index 00000000000..d09490158d9 --- /dev/null +++ b/bridge/opencensus/test/go.sum @@ -0,0 +1,106 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY= +github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +go.opencensus.io v0.22.6-0.20201102222123-380f4078db9f/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= +go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M= +go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 h1:iGu644GcxtEcrInvDsQRCwJjtCIOlT2V7IRt6ah2Whw= +golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From 86efa52b1b840b6785267cbf0c6f3b8f962186f3 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 28 Jul 2021 08:39:09 -0700 Subject: [PATCH 02/17] Use conversion package for tracer --- bridge/opencensus/internal/tracer.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bridge/opencensus/internal/tracer.go b/bridge/opencensus/internal/tracer.go index 107fb8eb259..2df42ab721c 100644 --- a/bridge/opencensus/internal/tracer.go +++ b/bridge/opencensus/internal/tracer.go @@ -21,7 +21,6 @@ import ( octrace "go.opencensus.io/trace" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" - "go.opentelemetry.io/otel/bridge/opencensus/utils" "go.opentelemetry.io/otel/trace" ) @@ -47,7 +46,7 @@ func (o *Tracer) StartSpan(ctx context.Context, name string, s ...octrace.StartO func (o *Tracer) StartSpanWithRemoteParent(ctx context.Context, name string, parent octrace.SpanContext, s ...octrace.StartOption) (context.Context, *octrace.Span) { // make sure span context is zero'd out so we use the remote parent ctx = trace.ContextWithSpan(ctx, nil) - ctx = trace.ContextWithRemoteSpanContext(ctx, utils.OCSpanContextToOTel(parent)) + ctx = trace.ContextWithRemoteSpanContext(ctx, oc2otel.SpanContext(parent)) return o.StartSpan(ctx, name, s...) } From cd14e0d55c73c64c768d459c2341f3a1b676bfb7 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 28 Jul 2021 08:39:47 -0700 Subject: [PATCH 03/17] Add unit tests for span --- bridge/opencensus/internal/span_test.go | 209 +++++++++++++++++++++++- 1 file changed, 205 insertions(+), 4 deletions(-) diff --git a/bridge/opencensus/internal/span_test.go b/bridge/opencensus/internal/span_test.go index 37da15cc4ed..2cd57f365de 100644 --- a/bridge/opencensus/internal/span_test.go +++ b/bridge/opencensus/internal/span_test.go @@ -17,6 +17,12 @@ package internal import ( "testing" + octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" + "go.opentelemetry.io/otel/bridge/opencensus/internal/otel2oc" + "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" ) @@ -26,11 +32,21 @@ type span struct { recording bool ended bool sc trace.SpanContext + name string + sCode codes.Code + sMsg string + attrs []attribute.KeyValue + eName string + eOpts []trace.EventOption } -func (s *span) IsRecording() bool { return s.recording } -func (s *span) End(...trace.SpanEndOption) { s.ended = true } -func (s *span) SpanContext() trace.SpanContext { return s.sc } +func (s *span) IsRecording() bool { return s.recording } +func (s *span) End(...trace.SpanEndOption) { s.ended = true } +func (s *span) SpanContext() trace.SpanContext { return s.sc } +func (s *span) SetName(n string) { s.name = n } +func (s *span) SetStatus(c codes.Code, d string) { s.sCode, s.sMsg = c, d } +func (s *span) SetAttributes(a ...attribute.KeyValue) { s.attrs = a } +func (s *span) AddEvent(n string, o ...trace.EventOption) { s.eName, s.eOpts = n, o } func TestSpanIsRecordingEvents(t *testing.T) { s := &span{recording: true} @@ -62,5 +78,190 @@ func TestSpanSpanContext(t *testing.T) { TraceID: [16]byte{1}, SpanID: [8]byte{1}, }) - converted := util + // Do not test the conversion, only that the method is called. + converted := otel2oc.SpanContext(sc) + + s := &span{sc: sc} + ocS := NewSpan(s) + if ocS.SpanContext() != converted { + t.Error("span.SpanContext did not use OpenTelemetry SpanContext") + } +} + +func TestSpanSetName(t *testing.T) { + // OpenCensus does not set a name if not recording. + s := &span{recording: true} + ocS := NewSpan(s) + name := "test name" + ocS.SetName(name) + if s.name != name { + t.Error("span.SetName did not set OpenTelemetry span name") + } +} + +func TestSpanSetStatus(t *testing.T) { + // OpenCensus does not set a status if not recording. + s := &span{recording: true} + ocS := NewSpan(s) + + c, d := codes.Error, "error" + status := octrace.Status{Code: int32(c), Message: d} + ocS.SetStatus(status) + + if s.sCode != c { + t.Error("span.SetStatus failed to set OpenTelemetry status code") + } + if s.sMsg != d { + t.Error("span.SetStatus failed to set OpenTelemetry status description") + } +} + +func TestSpanAddAttributes(t *testing.T) { + attrs := []octrace.Attribute{ + octrace.BoolAttribute("a", true), + } + // Do not test the conversion, only that the method is called. + converted := oc2otel.Attributes(attrs) + + // OpenCensus does not set attributes if not recording. + s := &span{recording: true} + ocS := NewSpan(s) + ocS.AddAttributes(attrs...) + + if len(s.attrs) != len(converted) || s.attrs[0] != converted[0] { + t.Error("span.AddAttributes failed to set OpenTelemetry attributes") + } +} + +func TestSpanAnnotate(t *testing.T) { + name := "annotation" + attrs := []octrace.Attribute{ + octrace.BoolAttribute("a", true), + } + // Do not test the conversion, only that the method is called. + want := oc2otel.Attributes(attrs) + + // OpenCensus does not set attributes if not recording. + s := &span{recording: true} + ocS := NewSpan(s) + ocS.Annotate(attrs, name) + + if s.eName != name { + t.Error("span.Annotate did not set event name") + } + + got := trace.NewEventConfig(s.eOpts...).Attributes() + if len(want) != len(got) || want[0] != got[0] { + t.Error("span.Annotate did not set event options") + } +} + +func TestSpanAnnotatef(t *testing.T) { + format := "annotation %s" + attrs := []octrace.Attribute{ + octrace.BoolAttribute("a", true), + } + // Do not test the conversion, only that the method is called. + want := oc2otel.Attributes(attrs) + + // OpenCensus does not set attributes if not recording. + s := &span{recording: true} + ocS := NewSpan(s) + ocS.Annotatef(attrs, format, "a") + + if s.eName != "annotation a" { + t.Error("span.Annotatef did not set event name") + } + + got := trace.NewEventConfig(s.eOpts...).Attributes() + if len(want) != len(got) || want[0] != got[0] { + t.Error("span.Annotatef did not set event options") + } +} + +func TestSpanAddMessageSendEvent(t *testing.T) { + var u, c int64 = 1, 2 + + // OpenCensus does not set attributes if not recording. + s := &span{recording: true} + ocS := NewSpan(s) + ocS.AddMessageSendEvent(0, u, c) + + if s.eName != MessageSendEvent { + t.Error("span.AddMessageSendEvent did not set event name") + } + + got := trace.NewEventConfig(s.eOpts...).Attributes() + if len(got) != 2 { + t.Fatalf("span.AddMessageSendEvent set %d attributes, want 2", len(got)) + } + + want := attribute.KeyValue{Key: UncompressedKey, Value: attribute.Int64Value(u)} + if got[0] != want { + t.Errorf("span.AddMessageSendEvent wrong uncompressed attribute: %v", got[0]) + } + + want = attribute.KeyValue{Key: CompressedKey, Value: attribute.Int64Value(c)} + if got[1] != want { + t.Errorf("span.AddMessageSendEvent wrong compressed attribute: %v", got[1]) + } +} + +func TestSpanAddMessageReceiveEvent(t *testing.T) { + var u, c int64 = 3, 4 + + // OpenCensus does not set attributes if not recording. + s := &span{recording: true} + ocS := NewSpan(s) + ocS.AddMessageReceiveEvent(0, u, c) + + if s.eName != MessageReceiveEvent { + t.Error("span.AddMessageReceiveEvent did not set event name") + } + + got := trace.NewEventConfig(s.eOpts...).Attributes() + if len(got) != 2 { + t.Fatalf("span.AddMessageReceiveEvent set %d attributes, want 2", len(got)) + } + + want := attribute.KeyValue{Key: UncompressedKey, Value: attribute.Int64Value(u)} + if got[0] != want { + t.Errorf("span.AddMessageReceiveEvent wrong uncompressed attribute: %v", got[0]) + } + + want = attribute.KeyValue{Key: CompressedKey, Value: attribute.Int64Value(c)} + if got[1] != want { + t.Errorf("span.AddMessageReceiveEvent wrong compressed attribute: %v", got[1]) + } +} + +type handler struct{ err error } + +func (h *handler) Handle(e error) { h.err = e } + +func TestSpanAddLinkFails(t *testing.T) { + h := new(handler) + otel.SetErrorHandler(h) + + // OpenCensus does not set attributes if not recording. + s := &span{recording: true} + ocS := NewSpan(s) + ocS.AddLink(octrace.Link{}) + + if h.err == nil { + t.Error("span.AddLink failed to raise an error") + } +} + +func TestSpanString(t *testing.T) { + sc := trace.NewSpanContext(trace.SpanContextConfig{ + TraceID: [16]byte{1}, + SpanID: [8]byte{1}, + }) + + s := &span{sc: sc} + ocS := NewSpan(s) + if expected := "span 0100000000000000"; ocS.String() != expected { + t.Errorf("span.String = %q, not %q", ocS.String(), expected) + } } From c70ab35bb2a3b511dac9edf23d451c6188043ec3 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 28 Jul 2021 10:52:29 -0700 Subject: [PATCH 04/17] Move span tests to external testing pkg --- bridge/opencensus/internal/span_test.go | 39 +++++++++++++------------ 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/bridge/opencensus/internal/span_test.go b/bridge/opencensus/internal/span_test.go index 2cd57f365de..5b46c6b9e1b 100644 --- a/bridge/opencensus/internal/span_test.go +++ b/bridge/opencensus/internal/span_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package internal +package internal_test import ( "testing" @@ -20,6 +20,7 @@ import ( octrace "go.opencensus.io/trace" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/bridge/opencensus/internal" "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" "go.opentelemetry.io/otel/bridge/opencensus/internal/otel2oc" "go.opentelemetry.io/otel/codes" @@ -50,7 +51,7 @@ func (s *span) AddEvent(n string, o ...trace.EventOption) { s.eName, s.eOpts = n func TestSpanIsRecordingEvents(t *testing.T) { s := &span{recording: true} - ocS := NewSpan(s) + ocS := internal.NewSpan(s) if !ocS.IsRecordingEvents() { t.Errorf("span.IsRecordingEvents() = false, want true") } @@ -62,7 +63,7 @@ func TestSpanIsRecordingEvents(t *testing.T) { func TestSpanEnd(t *testing.T) { s := new(span) - ocS := NewSpan(s) + ocS := internal.NewSpan(s) if s.ended { t.Fatal("new span already ended") } @@ -82,7 +83,7 @@ func TestSpanSpanContext(t *testing.T) { converted := otel2oc.SpanContext(sc) s := &span{sc: sc} - ocS := NewSpan(s) + ocS := internal.NewSpan(s) if ocS.SpanContext() != converted { t.Error("span.SpanContext did not use OpenTelemetry SpanContext") } @@ -91,7 +92,7 @@ func TestSpanSpanContext(t *testing.T) { func TestSpanSetName(t *testing.T) { // OpenCensus does not set a name if not recording. s := &span{recording: true} - ocS := NewSpan(s) + ocS := internal.NewSpan(s) name := "test name" ocS.SetName(name) if s.name != name { @@ -102,7 +103,7 @@ func TestSpanSetName(t *testing.T) { func TestSpanSetStatus(t *testing.T) { // OpenCensus does not set a status if not recording. s := &span{recording: true} - ocS := NewSpan(s) + ocS := internal.NewSpan(s) c, d := codes.Error, "error" status := octrace.Status{Code: int32(c), Message: d} @@ -125,7 +126,7 @@ func TestSpanAddAttributes(t *testing.T) { // OpenCensus does not set attributes if not recording. s := &span{recording: true} - ocS := NewSpan(s) + ocS := internal.NewSpan(s) ocS.AddAttributes(attrs...) if len(s.attrs) != len(converted) || s.attrs[0] != converted[0] { @@ -143,7 +144,7 @@ func TestSpanAnnotate(t *testing.T) { // OpenCensus does not set attributes if not recording. s := &span{recording: true} - ocS := NewSpan(s) + ocS := internal.NewSpan(s) ocS.Annotate(attrs, name) if s.eName != name { @@ -166,7 +167,7 @@ func TestSpanAnnotatef(t *testing.T) { // OpenCensus does not set attributes if not recording. s := &span{recording: true} - ocS := NewSpan(s) + ocS := internal.NewSpan(s) ocS.Annotatef(attrs, format, "a") if s.eName != "annotation a" { @@ -184,10 +185,10 @@ func TestSpanAddMessageSendEvent(t *testing.T) { // OpenCensus does not set attributes if not recording. s := &span{recording: true} - ocS := NewSpan(s) + ocS := internal.NewSpan(s) ocS.AddMessageSendEvent(0, u, c) - if s.eName != MessageSendEvent { + if s.eName != internal.MessageSendEvent { t.Error("span.AddMessageSendEvent did not set event name") } @@ -196,12 +197,12 @@ func TestSpanAddMessageSendEvent(t *testing.T) { t.Fatalf("span.AddMessageSendEvent set %d attributes, want 2", len(got)) } - want := attribute.KeyValue{Key: UncompressedKey, Value: attribute.Int64Value(u)} + want := attribute.KeyValue{Key: internal.UncompressedKey, Value: attribute.Int64Value(u)} if got[0] != want { t.Errorf("span.AddMessageSendEvent wrong uncompressed attribute: %v", got[0]) } - want = attribute.KeyValue{Key: CompressedKey, Value: attribute.Int64Value(c)} + want = attribute.KeyValue{Key: internal.CompressedKey, Value: attribute.Int64Value(c)} if got[1] != want { t.Errorf("span.AddMessageSendEvent wrong compressed attribute: %v", got[1]) } @@ -212,10 +213,10 @@ func TestSpanAddMessageReceiveEvent(t *testing.T) { // OpenCensus does not set attributes if not recording. s := &span{recording: true} - ocS := NewSpan(s) + ocS := internal.NewSpan(s) ocS.AddMessageReceiveEvent(0, u, c) - if s.eName != MessageReceiveEvent { + if s.eName != internal.MessageReceiveEvent { t.Error("span.AddMessageReceiveEvent did not set event name") } @@ -224,12 +225,12 @@ func TestSpanAddMessageReceiveEvent(t *testing.T) { t.Fatalf("span.AddMessageReceiveEvent set %d attributes, want 2", len(got)) } - want := attribute.KeyValue{Key: UncompressedKey, Value: attribute.Int64Value(u)} + want := attribute.KeyValue{Key: internal.UncompressedKey, Value: attribute.Int64Value(u)} if got[0] != want { t.Errorf("span.AddMessageReceiveEvent wrong uncompressed attribute: %v", got[0]) } - want = attribute.KeyValue{Key: CompressedKey, Value: attribute.Int64Value(c)} + want = attribute.KeyValue{Key: internal.CompressedKey, Value: attribute.Int64Value(c)} if got[1] != want { t.Errorf("span.AddMessageReceiveEvent wrong compressed attribute: %v", got[1]) } @@ -245,7 +246,7 @@ func TestSpanAddLinkFails(t *testing.T) { // OpenCensus does not set attributes if not recording. s := &span{recording: true} - ocS := NewSpan(s) + ocS := internal.NewSpan(s) ocS.AddLink(octrace.Link{}) if h.err == nil { @@ -260,7 +261,7 @@ func TestSpanString(t *testing.T) { }) s := &span{sc: sc} - ocS := NewSpan(s) + ocS := internal.NewSpan(s) if expected := "span 0100000000000000"; ocS.String() != expected { t.Errorf("span.String = %q, not %q", ocS.String(), expected) } From ebdb11d078ca3bf52726f2814b29debb315d910e Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 28 Jul 2021 13:28:47 -0700 Subject: [PATCH 05/17] Use resetable error handler for testing --- bridge/opencensus/internal/handler.go | 20 +++++++++++++++++++ .../internal/oc2otel/tracer_start_options.go | 19 +++++++++--------- bridge/opencensus/internal/span.go | 3 +-- bridge/opencensus/internal/span_test.go | 9 ++------- bridge/opencensus/internal/tracer.go | 9 ++++++--- 5 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 bridge/opencensus/internal/handler.go diff --git a/bridge/opencensus/internal/handler.go b/bridge/opencensus/internal/handler.go new file mode 100644 index 00000000000..83740ae01e7 --- /dev/null +++ b/bridge/opencensus/internal/handler.go @@ -0,0 +1,20 @@ +// 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 internal + +import "go.opentelemetry.io/otel" + +// Handle is the package level ErrorHandle. It is overwritable for testing. +var Handle = otel.Handle diff --git a/bridge/opencensus/internal/oc2otel/tracer_start_options.go b/bridge/opencensus/internal/oc2otel/tracer_start_options.go index 3cd8d1ac94a..37addbe1242 100644 --- a/bridge/opencensus/internal/oc2otel/tracer_start_options.go +++ b/bridge/opencensus/internal/oc2otel/tracer_start_options.go @@ -18,27 +18,28 @@ import ( "fmt" octrace "go.opencensus.io/trace" - "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/trace" ) -func StartOptions(optFns []octrace.StartOption, name string) []trace.SpanStartOption { +func StartOptions(optFuncs []octrace.StartOption) ([]trace.SpanStartOption, error) { var ocOpts octrace.StartOptions - for _, fn := range optFns { + for _, fn := range optFuncs { fn(&ocOpts) } - otOpts := []trace.SpanStartOption{} + + var otelOpts []trace.SpanStartOption switch ocOpts.SpanKind { case octrace.SpanKindClient: - otOpts = append(otOpts, trace.WithSpanKind(trace.SpanKindClient)) + otelOpts = append(otelOpts, trace.WithSpanKind(trace.SpanKindClient)) case octrace.SpanKindServer: - otOpts = append(otOpts, trace.WithSpanKind(trace.SpanKindServer)) + otelOpts = append(otelOpts, trace.WithSpanKind(trace.SpanKindServer)) case octrace.SpanKindUnspecified: - otOpts = append(otOpts, trace.WithSpanKind(trace.SpanKindUnspecified)) + otelOpts = append(otelOpts, trace.WithSpanKind(trace.SpanKindUnspecified)) } + var err error if ocOpts.Sampler != nil { - otel.Handle(fmt.Errorf("ignoring custom sampler for span %q created by OpenCensus because OpenTelemetry does not support creating a span with a custom sampler", name)) + err = fmt.Errorf("unsupported sampler: %v", ocOpts.Sampler) } - return otOpts + return otelOpts, err } diff --git a/bridge/opencensus/internal/span.go b/bridge/opencensus/internal/span.go index adf36e05d44..091dfcdaa04 100644 --- a/bridge/opencensus/internal/span.go +++ b/bridge/opencensus/internal/span.go @@ -18,7 +18,6 @@ import ( "fmt" octrace "go.opencensus.io/trace" - "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" "go.opentelemetry.io/otel/bridge/opencensus/internal/otel2oc" @@ -122,7 +121,7 @@ func (s *Span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compresse // AddLink adds a link to this span. func (s *Span) AddLink(l octrace.Link) { - otel.Handle(fmt.Errorf("ignoring OpenCensus link %+v for span %q because OpenTelemetry doesn't support setting links after creation", l, s.String())) + Handle(fmt.Errorf("ignoring OpenCensus link %+v for span %q because OpenTelemetry doesn't support setting links after creation", l, s.String())) } // String prints a string representation of this span. diff --git a/bridge/opencensus/internal/span_test.go b/bridge/opencensus/internal/span_test.go index 5b46c6b9e1b..9911bd476db 100644 --- a/bridge/opencensus/internal/span_test.go +++ b/bridge/opencensus/internal/span_test.go @@ -18,7 +18,6 @@ import ( "testing" octrace "go.opencensus.io/trace" - "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/bridge/opencensus/internal" "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" @@ -236,13 +235,9 @@ func TestSpanAddMessageReceiveEvent(t *testing.T) { } } -type handler struct{ err error } - -func (h *handler) Handle(e error) { h.err = e } - func TestSpanAddLinkFails(t *testing.T) { - h := new(handler) - otel.SetErrorHandler(h) + h, restore := withHandler() + defer restore() // OpenCensus does not set attributes if not recording. s := &span{recording: true} diff --git a/bridge/opencensus/internal/tracer.go b/bridge/opencensus/internal/tracer.go index 2df42ab721c..6b65647d6c8 100644 --- a/bridge/opencensus/internal/tracer.go +++ b/bridge/opencensus/internal/tracer.go @@ -19,7 +19,6 @@ import ( "fmt" octrace "go.opencensus.io/trace" - "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" "go.opentelemetry.io/otel/trace" ) @@ -37,7 +36,11 @@ func NewTracer(tracer trace.Tracer) octrace.Tracer { // StartSpan starts a new child span of the current span in the context. If // there is no span in the context, it creates a new trace and span. func (o *Tracer) StartSpan(ctx context.Context, name string, s ...octrace.StartOption) (context.Context, *octrace.Span) { - ctx, sp := o.otelTracer.Start(ctx, name, oc2otel.StartOptions(s, name)...) + otelOpts, err := oc2otel.StartOptions(s) + if err != nil { + Handle(fmt.Errorf("starting span %q: %w", name, err)) + } + ctx, sp := o.otelTracer.Start(ctx, name, otelOpts...) return ctx, NewSpan(sp) } @@ -60,6 +63,6 @@ func (o *Tracer) NewContext(parent context.Context, s *octrace.Span) context.Con if otSpan, ok := s.Internal().(*Span); ok { return trace.ContextWithSpan(parent, otSpan.otelSpan) } - otel.Handle(fmt.Errorf("unable to create context with span %q, since it was created using a different tracer", s.String())) + Handle(fmt.Errorf("unable to create context with span %q, since it was created using a different tracer", s.String())) return parent } From 7252e6e28162976856ed51593357294dc039d5a1 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 28 Jul 2021 13:29:25 -0700 Subject: [PATCH 06/17] Add unit tests for tracer.go --- bridge/opencensus/internal/tracer_test.go | 158 ++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 bridge/opencensus/internal/tracer_test.go diff --git a/bridge/opencensus/internal/tracer_test.go b/bridge/opencensus/internal/tracer_test.go new file mode 100644 index 00000000000..3c940d7bcb5 --- /dev/null +++ b/bridge/opencensus/internal/tracer_test.go @@ -0,0 +1,158 @@ +// 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 internal_test + +import ( + "context" + "testing" + + octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/bridge/opencensus/internal" + "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" + "go.opentelemetry.io/otel/bridge/opencensus/internal/otel2oc" + "go.opentelemetry.io/otel/trace" +) + +type handler struct{ err error } + +func (h *handler) Handle(e error) { h.err = e } + +func withHandler() (*handler, func()) { + h := new(handler) + original := internal.Handle + internal.Handle = h.Handle + return h, func() { internal.Handle = original } +} + +type tracer struct { + ctx context.Context + name string + opts []trace.SpanStartOption +} + +func (t *tracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { + t.ctx, t.name, t.opts = ctx, name, opts + noop := trace.NewNoopTracerProvider().Tracer("testing") + return noop.Start(ctx, name, opts...) +} + +func TestTracerStartSpan(t *testing.T) { + h, restore := withHandler() + defer restore() + + otelTracer := &tracer{} + ocTracer := internal.NewTracer(otelTracer) + + ctx := context.WithValue(context.Background(), "key", "value") + name := "testing span" + ocTracer.StartSpan(ctx, name, octrace.WithSpanKind(octrace.SpanKindClient)) + if h.err != nil { + t.Fatalf("OC tracer.StartSpan errored: %v", h.err) + } + + if otelTracer.ctx != ctx { + t.Error("OTel tracer.Start called with wrong context") + } + if otelTracer.name != name { + t.Error("OTel tracer.Start called with wrong name") + } + sk := trace.SpanKindClient + c := trace.NewSpanStartConfig(otelTracer.opts...) + if c.SpanKind() != sk { + t.Errorf("OTel tracer.Start called with wrong options: %#v", c) + } +} + +func TestTracerStartSpanReportsErrors(t *testing.T) { + h, restore := withHandler() + defer restore() + + ocTracer := internal.NewTracer(&tracer{}) + ocTracer.StartSpan(context.Background(), "", octrace.WithSampler(octrace.AlwaysSample())) + if h.err == nil { + t.Error("OC tracer.StartSpan no error when converting Sampler") + } +} + +func TestTracerStartSpanWithRemoteParent(t *testing.T) { + otelTracer := new(tracer) + ocTracer := internal.NewTracer(otelTracer) + sc := octrace.SpanContext{TraceID: [16]byte{1}, SpanID: [8]byte{1}} + converted := oc2otel.SpanContext(sc).WithRemote(true) + + ocTracer.StartSpanWithRemoteParent(context.Background(), "", sc) + + got := trace.SpanContextFromContext(otelTracer.ctx) + if !got.Equal(converted) { + t.Error("tracer.StartSpanWithRemoteParent failed to set remote parent") + } +} + +func TestTracerFromContext(t *testing.T) { + sc := trace.NewSpanContext(trace.SpanContextConfig{ + TraceID: [16]byte{1}, + SpanID: [8]byte{1}, + }) + ctx := trace.ContextWithSpanContext(context.Background(), sc) + + noop := trace.NewNoopTracerProvider().Tracer("TestTracerFromContext") + // Test using the fact that the No-Op span will propagate a span context . + ctx, _ = noop.Start(ctx, "test") + + got := internal.NewTracer(noop).FromContext(ctx).SpanContext() + // Do not test the convedsion, only that the propagtion. + want := otel2oc.SpanContext(sc) + if got != want { + t.Errorf("tracer.FromContext returned wrong context: %#v", got) + } +} + +func TestTracerNewContext(t *testing.T) { + sc := trace.NewSpanContext(trace.SpanContextConfig{ + TraceID: [16]byte{1}, + SpanID: [8]byte{1}, + }) + ctx := trace.ContextWithSpanContext(context.Background(), sc) + + noop := trace.NewNoopTracerProvider().Tracer("TestTracerNewContext") + // Test using the fact that the No-Op span will propagate a span context . + _, s := noop.Start(ctx, "test") + + ocTracer := internal.NewTracer(noop) + ctx = ocTracer.NewContext(context.Background(), internal.NewSpan(s)) + got := trace.SpanContextFromContext(ctx) + + if !got.Equal(sc) { + t.Error("tracer.NewContext did not attach Span to context") + } +} + +type differentSpan struct { + octrace.SpanInterface +} + +func (s *differentSpan) String() string { return "testing span" } + +func TestTracerNewContextErrors(t *testing.T) { + h, restore := withHandler() + defer restore() + + ocTracer := internal.NewTracer(&tracer{}) + ocSpan := octrace.NewSpan(&differentSpan{}) + ocTracer.NewContext(context.Background(), ocSpan) + if h.err == nil { + t.Error("tracer.NewContext did not error for unrecognized span") + } +} From 339ba60b807dd65af4716a0eb86721cc935f1150 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 28 Jul 2021 13:30:36 -0700 Subject: [PATCH 07/17] Add Sampler limitation to doc --- bridge/opencensus/doc.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bridge/opencensus/doc.go b/bridge/opencensus/doc.go index 9ade26c665b..80d80da6f78 100644 --- a/bridge/opencensus/doc.go +++ b/bridge/opencensus/doc.go @@ -28,4 +28,8 @@ // // - The NewContext method of the OpenCensus Tracer cannot embed an OpenCensus // Span in a context unless that Span was created by that Tracer. +// +// - Conversion of custom OpenCensus Samplers to OpenTelemetry is not +// implemented. An error will be sent to the OpenTelemetry default +// ErrorHandler if this is attempted. package opencensus // import "go.opentelemetry.io/otel/bridge/opencensus" From 99ba555d1f83e5b22ec059b5987898105968c178 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 29 Jul 2021 08:23:34 -0700 Subject: [PATCH 08/17] Add tests for attribute conversion --- .../internal/oc2otel/attributes_test.go | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 bridge/opencensus/internal/oc2otel/attributes_test.go diff --git a/bridge/opencensus/internal/oc2otel/attributes_test.go b/bridge/opencensus/internal/oc2otel/attributes_test.go new file mode 100644 index 00000000000..2260c2cc363 --- /dev/null +++ b/bridge/opencensus/internal/oc2otel/attributes_test.go @@ -0,0 +1,55 @@ +// 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 oc2otel + +import ( + "testing" + + octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/attribute" +) + +func TestAttributes(t *testing.T) { + in := []octrace.Attribute{ + octrace.BoolAttribute("bool", true), + octrace.Int64Attribute("int64", 49), + octrace.Float64Attribute("float64", 1.618), + octrace.StringAttribute("key", "val"), + } + + want := []attribute.KeyValue{ + attribute.Bool("bool", true), + attribute.Int64("int64", 49), + attribute.Float64("float64", 1.618), + attribute.String("key", "val"), + } + got := Attributes(in) + + if len(got) != len(want) { + t.Errorf("Attributes conversion failed: want %#v, got %#v", want, got) + } + for i := range got { + if g, w := got[i], want[i]; g != w { + t.Errorf("Attributes conversion: want %#v, got %#v", w, g) + } + } +} + +func TestAttributeValueUnknown(t *testing.T) { + got := AttributeValue([]byte{}) + if got != attribute.StringValue("unknown") { + t.Errorf("AttributeValue of unknown wrong: %#v", got) + } +} From a80ed66dc6341e7196db8a782204e7b3194747e3 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 29 Jul 2021 09:28:30 -0700 Subject: [PATCH 09/17] Add unit tests for start option conversion --- .../oc2otel/tracer_start_options_test.go | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 bridge/opencensus/internal/oc2otel/tracer_start_options_test.go diff --git a/bridge/opencensus/internal/oc2otel/tracer_start_options_test.go b/bridge/opencensus/internal/oc2otel/tracer_start_options_test.go new file mode 100644 index 00000000000..2dc4c710090 --- /dev/null +++ b/bridge/opencensus/internal/oc2otel/tracer_start_options_test.go @@ -0,0 +1,51 @@ +// 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 oc2otel + +import ( + "testing" + + octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/trace" +) + +func TestStartOptionsSpanKind(t *testing.T) { + conv := map[int]trace.SpanKind{ + octrace.SpanKindClient: trace.SpanKindClient, + octrace.SpanKindServer: trace.SpanKindServer, + octrace.SpanKindUnspecified: trace.SpanKindUnspecified, + } + + for oc, otel := range conv { + ocOpts := []octrace.StartOption{octrace.WithSpanKind(oc)} + otelOpts, err := StartOptions(ocOpts) + if err != nil { + t.Errorf("StartOptions errored: %v", err) + continue + } + c := trace.NewSpanStartConfig(otelOpts...) + if c.SpanKind() != otel { + t.Errorf("conversion of SpanKind start option: got %v, want %v", c.SpanKind(), otel) + } + } +} + +func TestStartOptionsSamplerErrors(t *testing.T) { + ocOpts := []octrace.StartOption{octrace.WithSampler(octrace.AlwaysSample())} + _, err := StartOptions(ocOpts) + if err == nil { + t.Error("StartOptions should error Sampler option") + } +} From 40367bf2be5bbd9529d0b1bc6c2cfd8e8c1c6d20 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 29 Jul 2021 09:31:16 -0700 Subject: [PATCH 10/17] Crosslink mods --- bridge/opentracing/go.mod | 2 ++ example/jaeger/go.mod | 2 ++ example/namedtracer/go.mod | 2 ++ example/opencensus/go.mod | 2 ++ example/otel-collector/go.mod | 2 ++ example/passthrough/go.mod | 2 ++ example/prometheus/go.mod | 2 ++ example/zipkin/go.mod | 2 ++ exporters/jaeger/go.mod | 2 ++ exporters/otlp/otlpmetric/go.mod | 2 ++ exporters/otlp/otlpmetric/otlpmetricgrpc/go.mod | 2 ++ exporters/otlp/otlpmetric/otlpmetrichttp/go.mod | 2 ++ exporters/otlp/otlptrace/go.mod | 2 ++ exporters/otlp/otlptrace/otlptracegrpc/go.mod | 2 ++ exporters/otlp/otlptrace/otlptracehttp/go.mod | 2 ++ exporters/prometheus/go.mod | 2 ++ exporters/stdout/stdoutmetric/go.mod | 2 ++ exporters/stdout/stdouttrace/go.mod | 2 ++ exporters/zipkin/go.mod | 2 ++ go.mod | 2 ++ internal/metric/go.mod | 2 ++ internal/tools/go.mod | 2 ++ metric/go.mod | 2 ++ oteltest/go.mod | 2 ++ sdk/export/metric/go.mod | 2 ++ sdk/go.mod | 2 ++ sdk/metric/go.mod | 2 ++ trace/go.mod | 2 ++ 28 files changed, 56 insertions(+) diff --git a/bridge/opentracing/go.mod b/bridge/opentracing/go.mod index f57d23a896f..e623cf8c29d 100644 --- a/bridge/opentracing/go.mod +++ b/bridge/opentracing/go.mod @@ -67,3 +67,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../opencensus/test diff --git a/example/jaeger/go.mod b/example/jaeger/go.mod index ee5a0459aa3..a2646dcaae7 100644 --- a/example/jaeger/go.mod +++ b/example/jaeger/go.mod @@ -67,3 +67,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/example/namedtracer/go.mod b/example/namedtracer/go.mod index b942edf274c..a8cfd6783f1 100644 --- a/example/namedtracer/go.mod +++ b/example/namedtracer/go.mod @@ -69,3 +69,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/example/opencensus/go.mod b/example/opencensus/go.mod index 8767ee83b6e..2934f5753c0 100644 --- a/example/opencensus/go.mod +++ b/example/opencensus/go.mod @@ -71,3 +71,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/example/otel-collector/go.mod b/example/otel-collector/go.mod index d39e9955ba4..a2a4be851ad 100644 --- a/example/otel-collector/go.mod +++ b/example/otel-collector/go.mod @@ -70,3 +70,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/example/passthrough/go.mod b/example/passthrough/go.mod index 394942f2958..59071d08ae3 100644 --- a/example/passthrough/go.mod +++ b/example/passthrough/go.mod @@ -70,3 +70,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/example/prometheus/go.mod b/example/prometheus/go.mod index e776eabcfcb..05bfb72e3c7 100644 --- a/example/prometheus/go.mod +++ b/example/prometheus/go.mod @@ -69,3 +69,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/example/zipkin/go.mod b/example/zipkin/go.mod index 85215c8b289..0ca2cea1e54 100644 --- a/example/zipkin/go.mod +++ b/example/zipkin/go.mod @@ -68,3 +68,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/exporters/jaeger/go.mod b/exporters/jaeger/go.mod index c547bae5ba3..b0c78c8352f 100644 --- a/exporters/jaeger/go.mod +++ b/exporters/jaeger/go.mod @@ -71,3 +71,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../stdout/stdo replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/exporters/otlp/otlpmetric/go.mod b/exporters/otlp/otlpmetric/go.mod index 86e021fa696..1c0045dd64c 100644 --- a/exporters/otlp/otlpmetric/go.mod +++ b/exporters/otlp/otlpmetric/go.mod @@ -77,3 +77,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../stdout/s replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ./otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../../bridge/opencensus/test diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/go.mod b/exporters/otlp/otlpmetric/otlpmetricgrpc/go.mod index d43b19b1530..c24301c0335 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/go.mod +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/go.mod @@ -75,3 +75,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../../stdou replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../../stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../../../bridge/opencensus/test diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/go.mod b/exporters/otlp/otlpmetric/otlpmetrichttp/go.mod index 1c70f44776e..61340430ba1 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/go.mod +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/go.mod @@ -77,3 +77,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../../stdou replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../../stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc => ../otlpmetricgrpc + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../../../bridge/opencensus/test diff --git a/exporters/otlp/otlptrace/go.mod b/exporters/otlp/otlptrace/go.mod index a36b6611705..7741f7b2ede 100644 --- a/exporters/otlp/otlptrace/go.mod +++ b/exporters/otlp/otlptrace/go.mod @@ -74,3 +74,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../stdout/s replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../../bridge/opencensus/test diff --git a/exporters/otlp/otlptrace/otlptracegrpc/go.mod b/exporters/otlp/otlptrace/otlptracegrpc/go.mod index 2a93bfd6d75..825eb26b02a 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/go.mod +++ b/exporters/otlp/otlptrace/otlptracegrpc/go.mod @@ -70,3 +70,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../../stdou replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../../stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../../../bridge/opencensus/test diff --git a/exporters/otlp/otlptrace/otlptracehttp/go.mod b/exporters/otlp/otlptrace/otlptracehttp/go.mod index 19e7b51f1dc..ec13eb8d084 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/go.mod +++ b/exporters/otlp/otlptrace/otlptracehttp/go.mod @@ -68,3 +68,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../../stdou replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../../stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../../../bridge/opencensus/test diff --git a/exporters/prometheus/go.mod b/exporters/prometheus/go.mod index d25f482f1d1..9fc8b739312 100644 --- a/exporters/prometheus/go.mod +++ b/exporters/prometheus/go.mod @@ -73,3 +73,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../stdout/stdo replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/exporters/stdout/stdoutmetric/go.mod b/exporters/stdout/stdoutmetric/go.mod index a832ad6a02e..49c47ae9115 100644 --- a/exporters/stdout/stdoutmetric/go.mod +++ b/exporters/stdout/stdoutmetric/go.mod @@ -71,3 +71,5 @@ replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc => ../ replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../../bridge/opencensus/test diff --git a/exporters/stdout/stdouttrace/go.mod b/exporters/stdout/stdouttrace/go.mod index 8614c481647..07a5626a71c 100644 --- a/exporters/stdout/stdouttrace/go.mod +++ b/exporters/stdout/stdouttrace/go.mod @@ -69,3 +69,5 @@ replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc => ../ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../stdoutmetric replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../../bridge/opencensus/test diff --git a/exporters/zipkin/go.mod b/exporters/zipkin/go.mod index 15db8639a33..45d5f064862 100644 --- a/exporters/zipkin/go.mod +++ b/exporters/zipkin/go.mod @@ -72,3 +72,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../stdout/stdo replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/go.mod b/go.mod index e2bc6d30a2f..0a9e1a9eaf0 100644 --- a/go.mod +++ b/go.mod @@ -67,3 +67,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ./exporters/st replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ./exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ./exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ./bridge/opencensus/test diff --git a/internal/metric/go.mod b/internal/metric/go.mod index fc3a9dc9600..45a121a7181 100644 --- a/internal/metric/go.mod +++ b/internal/metric/go.mod @@ -67,3 +67,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/internal/tools/go.mod b/internal/tools/go.mod index cc179c4725f..b792670e161 100644 --- a/internal/tools/go.mod +++ b/internal/tools/go.mod @@ -71,3 +71,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/metric/go.mod b/metric/go.mod index a310d7b0b5a..f8fcf1e7833 100644 --- a/metric/go.mod +++ b/metric/go.mod @@ -68,3 +68,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../exporters/s replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../bridge/opencensus/test diff --git a/oteltest/go.mod b/oteltest/go.mod index 830f9e2ea6b..29dc60a1bad 100644 --- a/oteltest/go.mod +++ b/oteltest/go.mod @@ -67,3 +67,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../exporters/s replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../bridge/opencensus/test diff --git a/sdk/export/metric/go.mod b/sdk/export/metric/go.mod index c5e1e6aeb41..21b5b47bf38 100644 --- a/sdk/export/metric/go.mod +++ b/sdk/export/metric/go.mod @@ -68,3 +68,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../../expor replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../../bridge/opencensus/test diff --git a/sdk/go.mod b/sdk/go.mod index 05fa75ade69..bca9816cc52 100644 --- a/sdk/go.mod +++ b/sdk/go.mod @@ -69,3 +69,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../exporters/s replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../bridge/opencensus/test diff --git a/sdk/metric/go.mod b/sdk/metric/go.mod index 76ec1de69ed..c085602e07c 100644 --- a/sdk/metric/go.mod +++ b/sdk/metric/go.mod @@ -71,3 +71,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporter replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test diff --git a/trace/go.mod b/trace/go.mod index 1907de16a78..08a79eeeecf 100644 --- a/trace/go.mod +++ b/trace/go.mod @@ -67,3 +67,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../exporters/s replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../exporters/stdout/stdouttrace replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../exporters/otlp/otlpmetric/otlpmetrichttp + +replace go.opentelemetry.io/otel/bridge/opencensus/test => ../bridge/opencensus/test From ba900c32d44784079028b663c777fdf54555b0ae Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 29 Jul 2021 10:07:26 -0700 Subject: [PATCH 11/17] Add dependabot config for new mod --- .github/dependabot.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index a96d1f8ede0..1b2d8c0ce82 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -46,6 +46,16 @@ updates: schedule: day: sunday interval: weekly + - + package-ecosystem: gomod + directory: /bridge/opencensus/test + labels: + - dependencies + - go + - "Skip Changelog" + schedule: + day: sunday + interval: weekly - package-ecosystem: gomod directory: /example/prom-collector From 42c958dd9c3638c1f20520287866e5a227ea0bd1 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 29 Jul 2021 10:07:56 -0700 Subject: [PATCH 12/17] Replace use of utils pkg --- bridge/opencensus/test/bridge_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bridge/opencensus/test/bridge_test.go b/bridge/opencensus/test/bridge_test.go index 4b4ddbd7f67..79e13233c94 100644 --- a/bridge/opencensus/test/bridge_test.go +++ b/bridge/opencensus/test/bridge_test.go @@ -23,7 +23,6 @@ import ( "go.opentelemetry.io/otel/attribute" ocbridge "go.opentelemetry.io/otel/bridge/opencensus" "go.opentelemetry.io/otel/bridge/opencensus/internal" - "go.opentelemetry.io/otel/bridge/opencensus/utils" "go.opentelemetry.io/otel/codes" sdktrace "go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/otel/sdk/trace/tracetest" @@ -104,7 +103,7 @@ func TestStartSpanWithRemoteParent(t *testing.T) { ctx := context.Background() ctx, parent := tracer.Start(ctx, "OpenTelemetrySpan1") - _, span := octrace.StartSpanWithRemoteParent(ctx, "OpenCensusSpan", utils.OTelSpanContextToOC(parent.SpanContext())) + _, span := octrace.StartSpanWithRemoteParent(ctx, "OpenCensusSpan", ocbridge.OTelSpanContextToOC(parent.SpanContext())) span.End() spans := sr.Ended() From 61a6c3f9597fe9bdd2c254cb9d98eba6a59aeb65 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 29 Jul 2021 10:08:42 -0700 Subject: [PATCH 13/17] Lint cleanup --- bridge/opencensus/internal/oc2otel/attributes.go | 1 + bridge/opencensus/internal/oc2otel/attributes_test.go | 1 + bridge/opencensus/internal/oc2otel/span_context.go | 1 + bridge/opencensus/internal/oc2otel/span_context_test.go | 1 + bridge/opencensus/internal/oc2otel/tracer_start_options.go | 1 + .../internal/oc2otel/tracer_start_options_test.go | 1 + bridge/opencensus/internal/otel2oc/span_context.go | 1 + bridge/opencensus/internal/otel2oc/span_context_test.go | 1 + bridge/opencensus/internal/span.go | 1 + bridge/opencensus/internal/span_test.go | 1 + bridge/opencensus/internal/tracer.go | 1 + bridge/opencensus/internal/tracer_test.go | 1 + bridge/opencensus/test/go.mod | 7 +++---- 13 files changed, 15 insertions(+), 4 deletions(-) diff --git a/bridge/opencensus/internal/oc2otel/attributes.go b/bridge/opencensus/internal/oc2otel/attributes.go index 490460938a4..aea7f42693c 100644 --- a/bridge/opencensus/internal/oc2otel/attributes.go +++ b/bridge/opencensus/internal/oc2otel/attributes.go @@ -16,6 +16,7 @@ package oc2otel import ( octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/attribute" ) diff --git a/bridge/opencensus/internal/oc2otel/attributes_test.go b/bridge/opencensus/internal/oc2otel/attributes_test.go index 2260c2cc363..44d40ec899a 100644 --- a/bridge/opencensus/internal/oc2otel/attributes_test.go +++ b/bridge/opencensus/internal/oc2otel/attributes_test.go @@ -18,6 +18,7 @@ import ( "testing" octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/attribute" ) diff --git a/bridge/opencensus/internal/oc2otel/span_context.go b/bridge/opencensus/internal/oc2otel/span_context.go index 713bea0487f..32c120c7b55 100644 --- a/bridge/opencensus/internal/oc2otel/span_context.go +++ b/bridge/opencensus/internal/oc2otel/span_context.go @@ -16,6 +16,7 @@ package oc2otel import ( octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/trace" ) diff --git a/bridge/opencensus/internal/oc2otel/span_context_test.go b/bridge/opencensus/internal/oc2otel/span_context_test.go index 899f27c890f..ba0f1d0bfcc 100644 --- a/bridge/opencensus/internal/oc2otel/span_context_test.go +++ b/bridge/opencensus/internal/oc2otel/span_context_test.go @@ -19,6 +19,7 @@ import ( octrace "go.opencensus.io/trace" "go.opencensus.io/trace/tracestate" + "go.opentelemetry.io/otel/trace" ) diff --git a/bridge/opencensus/internal/oc2otel/tracer_start_options.go b/bridge/opencensus/internal/oc2otel/tracer_start_options.go index 37addbe1242..b867329103d 100644 --- a/bridge/opencensus/internal/oc2otel/tracer_start_options.go +++ b/bridge/opencensus/internal/oc2otel/tracer_start_options.go @@ -18,6 +18,7 @@ import ( "fmt" octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/trace" ) diff --git a/bridge/opencensus/internal/oc2otel/tracer_start_options_test.go b/bridge/opencensus/internal/oc2otel/tracer_start_options_test.go index 2dc4c710090..6a525424cab 100644 --- a/bridge/opencensus/internal/oc2otel/tracer_start_options_test.go +++ b/bridge/opencensus/internal/oc2otel/tracer_start_options_test.go @@ -18,6 +18,7 @@ import ( "testing" octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/trace" ) diff --git a/bridge/opencensus/internal/otel2oc/span_context.go b/bridge/opencensus/internal/otel2oc/span_context.go index c2628461d4b..a9bbf2c68f9 100644 --- a/bridge/opencensus/internal/otel2oc/span_context.go +++ b/bridge/opencensus/internal/otel2oc/span_context.go @@ -16,6 +16,7 @@ package otel2oc import ( octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/trace" ) diff --git a/bridge/opencensus/internal/otel2oc/span_context_test.go b/bridge/opencensus/internal/otel2oc/span_context_test.go index 8ba776b54cd..236aa89689a 100644 --- a/bridge/opencensus/internal/otel2oc/span_context_test.go +++ b/bridge/opencensus/internal/otel2oc/span_context_test.go @@ -18,6 +18,7 @@ import ( "testing" octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/trace" ) diff --git a/bridge/opencensus/internal/span.go b/bridge/opencensus/internal/span.go index 091dfcdaa04..0f8533d9fe6 100644 --- a/bridge/opencensus/internal/span.go +++ b/bridge/opencensus/internal/span.go @@ -18,6 +18,7 @@ import ( "fmt" octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" "go.opentelemetry.io/otel/bridge/opencensus/internal/otel2oc" diff --git a/bridge/opencensus/internal/span_test.go b/bridge/opencensus/internal/span_test.go index 9911bd476db..f31cbf6bb72 100644 --- a/bridge/opencensus/internal/span_test.go +++ b/bridge/opencensus/internal/span_test.go @@ -18,6 +18,7 @@ import ( "testing" octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/bridge/opencensus/internal" "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" diff --git a/bridge/opencensus/internal/tracer.go b/bridge/opencensus/internal/tracer.go index 6b65647d6c8..8590933a2b9 100644 --- a/bridge/opencensus/internal/tracer.go +++ b/bridge/opencensus/internal/tracer.go @@ -19,6 +19,7 @@ import ( "fmt" octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" "go.opentelemetry.io/otel/trace" ) diff --git a/bridge/opencensus/internal/tracer_test.go b/bridge/opencensus/internal/tracer_test.go index 3c940d7bcb5..1a576c796bd 100644 --- a/bridge/opencensus/internal/tracer_test.go +++ b/bridge/opencensus/internal/tracer_test.go @@ -19,6 +19,7 @@ import ( "testing" octrace "go.opencensus.io/trace" + "go.opentelemetry.io/otel/bridge/opencensus/internal" "go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" "go.opentelemetry.io/otel/bridge/opencensus/internal/otel2oc" diff --git a/bridge/opencensus/test/go.mod b/bridge/opencensus/test/go.mod index afef3cd52f6..66499eb5019 100644 --- a/bridge/opencensus/test/go.mod +++ b/bridge/opencensus/test/go.mod @@ -64,9 +64,8 @@ replace go.opentelemetry.io/otel/trace => ../../../trace require ( go.opencensus.io v0.23.0 - go.opentelemetry.io/otel v1.0.0-RC1 + go.opentelemetry.io/otel v1.0.0-RC2 go.opentelemetry.io/otel/bridge/opencensus v0.0.0-00010101000000-000000000000 - go.opentelemetry.io/otel/oteltest v1.0.0-RC1 - go.opentelemetry.io/otel/sdk v1.0.0-RC1 - go.opentelemetry.io/otel/trace v1.0.0-RC1 + go.opentelemetry.io/otel/sdk v1.0.0-RC2 + go.opentelemetry.io/otel/trace v1.0.0-RC2 ) From 3853e6defa899a9289d4730fe67555cbd30bb592 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 29 Jul 2021 10:12:49 -0700 Subject: [PATCH 14/17] Use ctx key type --- bridge/opencensus/internal/tracer_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bridge/opencensus/internal/tracer_test.go b/bridge/opencensus/internal/tracer_test.go index 1a576c796bd..1e3518a7efe 100644 --- a/bridge/opencensus/internal/tracer_test.go +++ b/bridge/opencensus/internal/tracer_test.go @@ -49,6 +49,8 @@ func (t *tracer) Start(ctx context.Context, name string, opts ...trace.SpanStart return noop.Start(ctx, name, opts...) } +type ctxKey string + func TestTracerStartSpan(t *testing.T) { h, restore := withHandler() defer restore() @@ -56,7 +58,7 @@ func TestTracerStartSpan(t *testing.T) { otelTracer := &tracer{} ocTracer := internal.NewTracer(otelTracer) - ctx := context.WithValue(context.Background(), "key", "value") + ctx := context.WithValue(context.Background(), ctxKey("key"), "value") name := "testing span" ocTracer.StartSpan(ctx, name, octrace.WithSpanKind(octrace.SpanKindClient)) if h.err != nil { From 34f0786d7834bce9d23597ab43c555b52720723f Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 29 Jul 2021 10:16:47 -0700 Subject: [PATCH 15/17] Update Handle comment --- bridge/opencensus/internal/handler.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bridge/opencensus/internal/handler.go b/bridge/opencensus/internal/handler.go index 83740ae01e7..2d6a5160049 100644 --- a/bridge/opencensus/internal/handler.go +++ b/bridge/opencensus/internal/handler.go @@ -16,5 +16,6 @@ package internal import "go.opentelemetry.io/otel" -// Handle is the package level ErrorHandle. It is overwritable for testing. +// Handle is the package level function to handle errors. It can be +// overwritten for testing. var Handle = otel.Handle From 07e5b3094a6a9c012788ebdc4b055884c0826431 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 29 Jul 2021 10:32:45 -0700 Subject: [PATCH 16/17] Add changes to changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 058fe149e15..e8046961543 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Removed - Removed metrics test package `go.opentelemetry.io/otel/sdk/export/metric/metrictest`. (#2105) +- Removed the `go.opentelemetry.io/otel/bridge/opencensus/utils` package. + The contents of this package now exist in the `go.opentelemetry.io/otel/bridge/opencensus` package. (#2144) ### Fixed From 99a178363ca5462d56b7bc9b5c4b00643578c852 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 29 Jul 2021 10:33:05 -0700 Subject: [PATCH 17/17] Update bridge/opencensus/test go.mod --- bridge/opencensus/test/go.mod | 18 +++++++++--------- bridge/opencensus/test/go.sum | 2 ++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/bridge/opencensus/test/go.mod b/bridge/opencensus/test/go.mod index 66499eb5019..24f96a36f03 100644 --- a/bridge/opencensus/test/go.mod +++ b/bridge/opencensus/test/go.mod @@ -1,6 +1,14 @@ module go.opentelemetry.io/otel/bridge/opencensus/test -go 1.16 +go 1.15 + +require ( + go.opencensus.io v0.23.0 + go.opentelemetry.io/otel v1.0.0-RC2 + go.opentelemetry.io/otel/bridge/opencensus v0.21.0 + go.opentelemetry.io/otel/sdk v1.0.0-RC2 + go.opentelemetry.io/otel/trace v1.0.0-RC2 +) replace go.opentelemetry.io/otel => ../../.. @@ -61,11 +69,3 @@ replace go.opentelemetry.io/otel/sdk/export/metric => ../../../sdk/export/metric replace go.opentelemetry.io/otel/sdk/metric => ../../../sdk/metric replace go.opentelemetry.io/otel/trace => ../../../trace - -require ( - go.opencensus.io v0.23.0 - go.opentelemetry.io/otel v1.0.0-RC2 - go.opentelemetry.io/otel/bridge/opencensus v0.0.0-00010101000000-000000000000 - go.opentelemetry.io/otel/sdk v1.0.0-RC2 - go.opentelemetry.io/otel/trace v1.0.0-RC2 -) diff --git a/bridge/opencensus/test/go.sum b/bridge/opencensus/test/go.sum index d09490158d9..9cb1b023e7f 100644 --- a/bridge/opencensus/test/go.sum +++ b/bridge/opencensus/test/go.sum @@ -76,6 +76,7 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= @@ -98,6 +99,7 @@ google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=