1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-04 09:43:23 +02:00
opentelemetry-go/bridge/opencensus
Tyler Yahn 8e76ab23b4
Release v1.15.0/v0.38.0 (#4035)
* Bump versions

* Prepare stable-v1 for version v1.15.0

* Prepare experimental-metrics for version v0.38.0

* Fix otlpmetric tests

Check pre 1.0 as well

* Update the chagelog
2023-04-28 07:48:08 -07:00
..
internal docs(typos): Run codespell to fix typos (#3980) 2023-04-11 17:28:13 -07:00
test Release v1.15.0/v0.38.0 (#4035) 2023-04-28 07:48:08 -07:00
bridge.go chore: adds vanity import check. (#2255) 2021-09-27 22:37:26 -04:00
doc.go Update OpenCensus bridge docs with limitations (#2145) 2021-07-29 11:24:52 -07:00
go.mod Release v1.15.0/v0.38.0 (#4035) 2023-04-28 07:48:08 -07:00
go.sum dependabot updates Sun Apr 9 14:31:33 UTC 2023 (#3979) 2023-04-09 07:43:00 -07:00
metric_test.go Update the metric Export interface to accept a *ResourceMetrics instead of ResourceMetrics (#3853) 2023-03-16 10:58:43 -07:00
metric.go Update the metric Export interface to accept a *ResourceMetrics instead of ResourceMetrics (#3853) 2023-03-16 10:58:43 -07:00
README.md Merge metric SDK development branch "new_sdk/main" into "main" (#3175) 2022-09-15 18:41:24 -07:00

OpenCensus Bridge

The OpenCensus Bridge helps facilitate the migration of an application from OpenCensus to OpenTelemetry.

Caveat about OpenCensus

Installing a metric or tracing bridge will cause OpenCensus telemetry to be exported by OpenTelemetry exporters. Since OpenCensus telemetry uses globals, installing a bridge will result in telemetry collection from all libraries that use OpenCensus, including some you may not expect. For example (#1928), if a client library generates traces with OpenCensus, installing the bridge will cause those traces to be exported by OpenTelemetry.

Tracing

The Problem: Mixing OpenCensus and OpenTelemetry libraries

In a perfect world, one would simply migrate their entire go application --including custom instrumentation, libraries, and exporters-- from OpenCensus to OpenTelemetry all at once. In the real world, dependency constraints, third-party ownership of libraries, or other reasons may require mixing OpenCensus and OpenTelemetry libraries in a single application.

However, if you create the following spans in a go application:

ctx, ocSpan := opencensus.StartSpan(context.Background(), "OuterSpan")
defer ocSpan.End()
ctx, otSpan := opentelemetryTracer.Start(ctx, "MiddleSpan")
defer otSpan.End()
ctx, ocSpan := opencensus.StartSpan(ctx, "InnerSpan")
defer ocSpan.End()

OpenCensus reports (to OpenCensus exporters):

[--------OuterSpan------------]
    [----InnerSpan------]

OpenTelemetry reports (to OpenTelemetry exporters):

   [-----MiddleSpan--------]

Instead, I would prefer (to a single set of exporters):

[--------OuterSpan------------]
   [-----MiddleSpan--------]
    [----InnerSpan------]

The bridge solution

The bridge implements the OpenCensus trace API using OpenTelemetry. This would cause, for example, a span recorded with OpenCensus' StartSpan() method to be equivalent to recording a span using OpenTelemetry's tracer.Start() method. Funneling all tracing API calls to OpenTelemetry APIs results in the desired unified span hierarchy.

User Journey

Starting from an application using entirely OpenCensus APIs:

  1. Instantiate OpenTelemetry SDK and Exporters
  2. Override OpenCensus' DefaultTracer with the bridge
  3. Migrate libraries individually from OpenCensus to OpenTelemetry
  4. Remove OpenCensus exporters and configuration

To override OpenCensus' DefaultTracer with the bridge:

import (
	octrace "go.opencensus.io/trace"
	"go.opentelemetry.io/otel/bridge/opencensus"
	"go.opentelemetry.io/otel"
)

tracer := otel.GetTracerProvider().Tracer("bridge")
octrace.DefaultTracer = opencensus.NewTracer(tracer)

Be sure to set the Tracer name to your instrumentation package name instead of "bridge".

Incompatibilities

OpenCensus and OpenTelemetry APIs are not entirely compatible. If the bridge finds any incompatibilities, it will log them. Incompatibilities include:

  • Custom OpenCensus Samplers specified during StartSpan are ignored.
  • Links cannot be added to OpenCensus spans.
  • OpenTelemetry Debug or Deferred trace flags are dropped after an OpenCensus span is created.