Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scope version to opencensus trace and metric bridges #4584

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
// 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)

Check warning on line 39 in bridge/opencensus/trace.go

View check run for this annotation

Codecov / codecov/patch

bridge/opencensus/trace.go#L39

Added line #L39 was not covered by tests
}

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"
}