otelwrapper

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2022 License: MIT Imports: 10 Imported by: 0

README

otelwrapper

Simple wrapper for OTel tracer provider in golang

Installation

go get -u github.com/x-qdo/otelwrapper

also, most likely you need this as well

go get -u go.opentelemetry.io/otel/trace

Initialization of TracerProvider

// let's create a dummy context
ctx, cancelF := context.WithCancel(context.Background())
wg := new(sync.WaitGroup)

...

tp, err := tracer.InitTracerProvider("my_sexy_service", "default")
if err != nil {
    return nil, err
}

go tracer.ShutdownWaiting(tp, ctx, wg)

...

ctx, span := otel.Tracer("my_first_tracer").Start(ctx, "my_first_span")
defer span.End()

...

span.AddEvent("my fist event")
span.SetAttributes(attribute.Key("foo").String("boo"))

So, as you can see we initiate new global tracesdk.TracerProvider here and set up a listener to gracefully shut it down as soon as cancelF() will be called. sync.WaitGroup we need here just to wait until an application finalizes everything.

Under the hood InitTracerProvider() creates an instance of OTLP Exporter. Please pay your attention that you must set OTEL_EXPORTER_OTLP_ENDPOINT to as its environment variable. For example,

OTEL_EXPORTER_OTLP_ENDPOINT="http://127.0.0.1:4318"

If you don't want to use OTLP as your span delivery, then you have to create an exporter by yourself. For example, you need Jaeger:

//Create the Jaeger exporter
exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(os.Getenv("OTEL_EXPORTER_JAEGER_ENDPOINT"))))
if err != nil {
    return nil, err
}

tp, err := tracer.InitTracerProvider("my_sexy_service", "default", exp)
if err != nil {
    return nil, err
}

Btw, you can have multiple exporters:

tracer.InitTracerProvider("my_sexy_service", "default", exp1, exp2, ...)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitTracerProvider

func InitTracerProvider(serviceName, namespace string, exporters ...tracesdk.SpanExporter) (*tracesdk.TracerProvider, error)

InitTracerProvider returns an OpenTelemetry TracerProvider configured to use the exporters (OTLP by default) that will send spans to the provided url. The returned TracerProvider will also use a Resource configured with all the information about the application.

func ShutdownWaiting

func ShutdownWaiting(tp *tracesdk.TracerProvider, ctx context.Context, wg *sync.WaitGroup)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL