diff --git a/CHANGELOG.md b/CHANGELOG.md index 94870c638d6..8d6a4df4b5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added - Add `go.opentelemetry.io/otel/bridge/opencensus.InstallTraceBridge`, which installs the OpenCensus trace bridge, and replaces `opencensus.NewTracer`. (#4567) +- Add scope version to trace and metric bridges in `go.opentelemetry.io/otel/bridge/opencensus`. (#4584) ### Deprecated diff --git a/bridge/opencensus/metric.go b/bridge/opencensus/metric.go index 577c311f3fd..888e82f5ff3 100644 --- a/bridge/opencensus/metric.go +++ b/bridge/opencensus/metric.go @@ -56,7 +56,8 @@ func (p *MetricProducer) Produce(context.Context) ([]metricdata.ScopeMetrics, er } return []metricdata.ScopeMetrics{{ Scope: instrumentation.Scope{ - Name: scopeName, + Name: scopeName, + Version: Version(), }, Metrics: otelmetrics, }}, err diff --git a/bridge/opencensus/metric_test.go b/bridge/opencensus/metric_test.go index 29ec835c8ba..901953c3a81 100644 --- a/bridge/opencensus/metric_test.go +++ b/bridge/opencensus/metric_test.go @@ -64,7 +64,8 @@ func TestMetricProducer(t *testing.T) { }, expected: []metricdata.ScopeMetrics{{ Scope: instrumentation.Scope{ - Name: scopeName, + Name: scopeName, + Version: Version(), }, Metrics: []metricdata.Metrics{ { @@ -112,7 +113,8 @@ func TestMetricProducer(t *testing.T) { }, expected: []metricdata.ScopeMetrics{{ Scope: instrumentation.Scope{ - Name: scopeName, + Name: scopeName, + Version: Version(), }, Metrics: []metricdata.Metrics{ { diff --git a/bridge/opencensus/trace.go b/bridge/opencensus/trace.go index 1c45e975cfd..b1df5a3ca6c 100644 --- a/bridge/opencensus/trace.go +++ b/bridge/opencensus/trace.go @@ -36,9 +36,14 @@ func NewTracer(tracer trace.Tracer) octrace.Tracer { // the global OpenCensus tracer implementation. Once the bridge is installed, // spans recorded using OpenCensus are redirected to the OpenTelemetry SDK. func InstallTraceBridge(opts ...TraceOption) { + octrace.DefaultTracer = newTraceBridge(opts) +} + +func newTraceBridge(opts []TraceOption) octrace.Tracer { cfg := newTraceConfig(opts) - tracer := cfg.tp.Tracer(scopeName) - octrace.DefaultTracer = internal.NewTracer(tracer) + return internal.NewTracer( + cfg.tp.Tracer(scopeName, trace.WithInstrumentationVersion(Version())), + ) } // OTelSpanContextToOC converts from an OpenTelemetry SpanContext to an diff --git a/bridge/opencensus/trace_test.go b/bridge/opencensus/trace_test.go new file mode 100644 index 00000000000..529a77070c7 --- /dev/null +++ b/bridge/opencensus/trace_test.go @@ -0,0 +1,39 @@ +// 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 opencensus // import "go.opentelemetry.io/otel/bridge/opencensus" + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/sdk/trace/tracetest" +) + +func TestNewTraceBridge(t *testing.T) { + exporter := tracetest.NewInMemoryExporter() + tp := trace.NewTracerProvider(trace.WithSyncer(exporter)) + bridge := newTraceBridge([]TraceOption{WithTracerProvider(tp)}) + _, span := bridge.StartSpan(context.Background(), "foo") + span.End() + gotSpans := exporter.GetSpans() + require.Len(t, gotSpans, 1) + gotSpan := gotSpans[0] + assert.Equal(t, gotSpan.InstrumentationLibrary.Name, scopeName) + assert.Equal(t, gotSpan.InstrumentationLibrary.Version, Version()) +} diff --git a/bridge/opencensus/version.go b/bridge/opencensus/version.go new file mode 100644 index 00000000000..2ce168e845a --- /dev/null +++ b/bridge/opencensus/version.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 opencensus // import "go.opentelemetry.io/otel/bridge/opencensus" + +// Version is the current release version of the opencensus bridge. +func Version() string { + return "0.42.0" +}