Debian Codegraph @

bridge/opentracing/README.md

raw
1 # OpenTelemetry/OpenTracing Bridge
2
3 ## Getting started
4
5 `go get go.opentelemetry.io/otel/bridge/opentracing`
6
7 Assuming you have configured an OpenTelemetry `TracerProvider`, these will be the steps to follow to wire up the bridge:
8
9 ```go
10 import (
11 "go.opentelemetry.io/otel"
12 otelBridge "go.opentelemetry.io/otel/bridge/opentracing"
13 )
14
15 func main() {
16 /* Create tracerProvider and configure OpenTelemetry ... */
17
18 otelTracer := tracerProvider.Tracer("tracer_name")
19 // Use the bridgeTracer as your OpenTracing tracer.
20 bridgeTracer, wrapperTracerProvider := otelBridge.NewTracerPair(otelTracer)
21 // Set the wrapperTracerProvider as the global OpenTelemetry
22 // TracerProvider so instrumentation will use it by default.
23 otel.SetTracerProvider(wrapperTracerProvider)
24
25 /* ... */
26 }
27 ```
28
29 ## Interop from trace context from OpenTracing to OpenTelemetry
30
31 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.
32
33 When you have started an OpenTracing Span, make sure the OpenTelemetry knows about it like this:
34
35 ```go
36 ctxWithOTSpan := opentracing.ContextWithSpan(ctx, otSpan)
37 ctxWithOTAndOTelSpan := bridgeTracer.ContextWithSpanHook(ctxWithOTSpan, otSpan)
38 // Propagate the otSpan to both OpenTracing and OpenTelemetry
39 // instrumentation by using the ctxWithOTAndOTelSpan context.
40 ```
41
42 ## Extended Functionality
43
44 The bridge functionality can be extended beyond the OpenTracing API.
45
46 Any [`trace.SpanContext`](https://pkg.go.dev/go.opentelemetry.io/otel/trace#SpanContext) method can be accessed as following:
47
48 ```go
49 type spanContextProvider interface {
50 IsSampled() bool
51 TraceID() trace.TraceID
52 SpanID() trace.SpanID
53 TraceFlags() trace.TraceFlags
54 ... // any other available method can be added here to access it
55 }
56
57 var sc opentracing.SpanContext = ...
58 if s, ok := sc.(spanContextProvider); ok {
59 // Use TraceID by s.TraceID()
60 // Use SpanID by s.SpanID()
61 // Use TraceFlags by s.TraceFlags()
62 ...
63 }
64 ```