Skip to content

Commit

Permalink
Add scope version to opencensus trace and metric bridges (#4584)
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole authored Oct 11, 2023
1 parent 3f17bcb commit 1074d64
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion bridge/opencensus/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions bridge/opencensus/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func TestMetricProducer(t *testing.T) {
},
expected: []metricdata.ScopeMetrics{{
Scope: instrumentation.Scope{
Name: scopeName,
Name: scopeName,
Version: Version(),
},
Metrics: []metricdata.Metrics{
{
Expand Down Expand Up @@ -112,7 +113,8 @@ func TestMetricProducer(t *testing.T) {
},
expected: []metricdata.ScopeMetrics{{
Scope: instrumentation.Scope{
Name: scopeName,
Name: scopeName,
Version: Version(),
},
Metrics: []metricdata.Metrics{
{
Expand Down
9 changes: 7 additions & 2 deletions bridge/opencensus/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions bridge/opencensus/trace_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
20 changes: 20 additions & 0 deletions bridge/opencensus/version.go
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 1074d64

Please sign in to comment.