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

Refactor the OpenCensus Bridge #2144

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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

Expand Down
15 changes: 15 additions & 0 deletions bridge/opencensus/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
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"
)

Expand All @@ -27,3 +29,16 @@ import (
func NewTracer(tracer trace.Tracer) octrace.Tracer {
return internal.NewTracer(tracer)
}

// 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)
}

// OCSpanContextToOTel converts from an OpenCensus SpanContext to an
// OpenTelemetry SpanContext.
func OCSpanContextToOTel(sc octrace.SpanContext) trace.SpanContext {
return oc2otel.SpanContext(sc)
}
3 changes: 2 additions & 1 deletion bridge/opencensus/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
56 changes: 56 additions & 0 deletions bridge/opencensus/internal/oc2otel/attributes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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)
}
}
52 changes: 52 additions & 0 deletions bridge/opencensus/internal/oc2otel/tracer_start_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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")
}
}
Loading