1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/bridge/opentracing
Tyler Yahn 1e1cc901a5
Add embedded package to trace API (#4620)
* Add trace/embedded

* Update trace impl to use trace/embedded

* Add noop pkg to replace no-op impl in trace pkg

* Use trace/embedded in global impl

* Use trace/embedded in SDK impl

* Update opencensus bridge

* Update opentracing bridge

* Add changes to changelog

* Update trace/doc.go

Co-authored-by: David Ashpole <dashpole@google.com>

---------

Co-authored-by: David Ashpole <dashpole@google.com>
2023-10-19 10:16:24 -07:00
..
internal Add embedded package to trace API (#4620) 2023-10-19 10:16:24 -07:00
migration Avoid creating new references on WithDeferredSetup for every span (#3833) 2023-03-07 07:40:31 -06:00
test Use gofumpt instead of gofmt (#4623) 2023-10-16 10:02:21 -07:00
bridge_test.go Use gofumpt instead of gofmt (#4623) 2023-10-16 10:02:21 -07:00
bridge.go Add embedded package to trace API (#4620) 2023-10-19 10:16:24 -07:00
doc.go Update doc.go (#2030) 2021-06-25 07:56:12 -07:00
go.mod Release v1.19.0/v0.42.0/v0.0.7 (#4568) 2023-09-28 12:28:51 -07:00
go.sum dependabot updates Sun Oct 15 14:43:01 UTC 2023 (#4637) 2023-10-15 07:53:27 -07:00
mix_test.go Replace use of old term label with attribute (#2790) 2022-04-18 07:31:31 -07:00
provider_test.go Add embedded package to trace API (#4620) 2023-10-19 10:16:24 -07:00
provider.go Add embedded package to trace API (#4620) 2023-10-19 10:16:24 -07:00
README.md Added methods for SpanID and TraceID on bridgeSpanContext (#3966) 2023-04-25 10:11:03 -07:00
util.go docs(typos): Run codespell to fix typos (#3980) 2023-04-11 17:28:13 -07:00
wrapper.go Add embedded package to trace API (#4620) 2023-10-19 10:16:24 -07:00

OpenTelemetry/OpenTracing Bridge

Getting started

go get go.opentelemetry.io/otel/bridge/opentracing

Assuming you have configured an OpenTelemetry TracerProvider, these will be the steps to follow to wire up the bridge:

import (
	"go.opentelemetry.io/otel"
	otelBridge "go.opentelemetry.io/otel/bridge/opentracing"
)

func main() {
	/* Create tracerProvider and configure OpenTelemetry ... */
	
	otelTracer := tracerProvider.Tracer("tracer_name")
	// Use the bridgeTracer as your OpenTracing tracer.
	bridgeTracer, wrapperTracerProvider := otelBridge.NewTracerPair(otelTracer)
	// Set the wrapperTracerProvider as the global OpenTelemetry
	// TracerProvider so instrumentation will use it by default.
	otel.SetTracerProvider(wrapperTracerProvider)

	/* ... */
}

Interop from trace context from OpenTracing to OpenTelemetry

In order to get OpenTracing spans properly into the OpenTelemetry context, so they can be propagated (both internally, and externally), you will need to explicitly use the BridgeTracer for creating your OpenTracing spans, rather than a bare OpenTracing Tracer instance.

When you have started an OpenTracing Span, make sure the OpenTelemetry knows about it like this:

	ctxWithOTSpan := opentracing.ContextWithSpan(ctx, otSpan)
	ctxWithOTAndOTelSpan := bridgeTracer.ContextWithSpanHook(ctxWithOTSpan, otSpan)
	// Propagate the otSpan to both OpenTracing and OpenTelemetry
	// instrumentation by using the ctxWithOTAndOTelSpan context.

Extended Functionality

The bridge functionality can be extended beyond the OpenTracing API.

Any trace.SpanContext method can be accessed as following:

type spanContextProvider interface {
	IsSampled() bool
	TraceID() trace.TraceID
	SpanID() trace.SpanID
	TraceFlags() trace.TraceFlags
	... // any other available method can be added here to access it
}

var sc opentracing.SpanContext = ...
if s, ok := sc.(spanContextProvider); ok {
	// Use TraceID by s.TraceID()
	// Use SpanID by s.SpanID()
	// Use TraceFlags by s.TraceFlags()
	...
}