Skip to content

Commit 1074d64

Browse files
authored
Add scope version to opencensus trace and metric bridges (#4584)
1 parent 3f17bcb commit 1074d64

File tree

6 files changed

+73
-5
lines changed

6 files changed

+73
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1111
### Added
1212

1313
- Add `go.opentelemetry.io/otel/bridge/opencensus.InstallTraceBridge`, which installs the OpenCensus trace bridge, and replaces `opencensus.NewTracer`. (#4567)
14+
- Add scope version to trace and metric bridges in `go.opentelemetry.io/otel/bridge/opencensus`. (#4584)
1415

1516
### Deprecated
1617

bridge/opencensus/metric.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func (p *MetricProducer) Produce(context.Context) ([]metricdata.ScopeMetrics, er
5656
}
5757
return []metricdata.ScopeMetrics{{
5858
Scope: instrumentation.Scope{
59-
Name: scopeName,
59+
Name: scopeName,
60+
Version: Version(),
6061
},
6162
Metrics: otelmetrics,
6263
}}, err

bridge/opencensus/metric_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func TestMetricProducer(t *testing.T) {
6464
},
6565
expected: []metricdata.ScopeMetrics{{
6666
Scope: instrumentation.Scope{
67-
Name: scopeName,
67+
Name: scopeName,
68+
Version: Version(),
6869
},
6970
Metrics: []metricdata.Metrics{
7071
{
@@ -112,7 +113,8 @@ func TestMetricProducer(t *testing.T) {
112113
},
113114
expected: []metricdata.ScopeMetrics{{
114115
Scope: instrumentation.Scope{
115-
Name: scopeName,
116+
Name: scopeName,
117+
Version: Version(),
116118
},
117119
Metrics: []metricdata.Metrics{
118120
{

bridge/opencensus/trace.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ func NewTracer(tracer trace.Tracer) octrace.Tracer {
3636
// the global OpenCensus tracer implementation. Once the bridge is installed,
3737
// spans recorded using OpenCensus are redirected to the OpenTelemetry SDK.
3838
func InstallTraceBridge(opts ...TraceOption) {
39+
octrace.DefaultTracer = newTraceBridge(opts)
40+
}
41+
42+
func newTraceBridge(opts []TraceOption) octrace.Tracer {
3943
cfg := newTraceConfig(opts)
40-
tracer := cfg.tp.Tracer(scopeName)
41-
octrace.DefaultTracer = internal.NewTracer(tracer)
44+
return internal.NewTracer(
45+
cfg.tp.Tracer(scopeName, trace.WithInstrumentationVersion(Version())),
46+
)
4247
}
4348

4449
// OTelSpanContextToOC converts from an OpenTelemetry SpanContext to an

bridge/opencensus/trace_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package opencensus // import "go.opentelemetry.io/otel/bridge/opencensus"
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
23+
24+
"go.opentelemetry.io/otel/sdk/trace"
25+
"go.opentelemetry.io/otel/sdk/trace/tracetest"
26+
)
27+
28+
func TestNewTraceBridge(t *testing.T) {
29+
exporter := tracetest.NewInMemoryExporter()
30+
tp := trace.NewTracerProvider(trace.WithSyncer(exporter))
31+
bridge := newTraceBridge([]TraceOption{WithTracerProvider(tp)})
32+
_, span := bridge.StartSpan(context.Background(), "foo")
33+
span.End()
34+
gotSpans := exporter.GetSpans()
35+
require.Len(t, gotSpans, 1)
36+
gotSpan := gotSpans[0]
37+
assert.Equal(t, gotSpan.InstrumentationLibrary.Name, scopeName)
38+
assert.Equal(t, gotSpan.InstrumentationLibrary.Version, Version())
39+
}

bridge/opencensus/version.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package opencensus // import "go.opentelemetry.io/otel/bridge/opencensus"
16+
17+
// Version is the current release version of the opencensus bridge.
18+
func Version() string {
19+
return "0.42.0"
20+
}

0 commit comments

Comments
 (0)