otelplus

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2021 License: MIT Imports: 16 Imported by: 0

README

otelplus

.github/workflows/otelplus.yaml

Opinionated convenience for setting up and using the OpenTelemetry tracing library.

go get -u github.com/absurdlab/pkg/otelplus

Usage

// Setup a trace provider
provider, closer, _ := otelplus.Init(context.Background(),
    otelplus.Options.ParentOrRatioBasedSample(0.6),
    otelplus.Options.ServiceResource("my_app"),
    otelplus.Options.StdoutExporter(true),
)
defer func() {
    _ = closer()
}

// Setup an HTTP middleware to automatically trace
h := myHandler()
h = otelplus.AutoTrace("get_user")(h)

// Get trace id and span id from context
traceID := otelplus.TraceID(ctx)
spanID := otelplus.SpanID(ctx)

// Add details to trace
otelplus.AddEvent(ctx, "bang", map[string]interface{}{
    "foo": "bar",
    "count": 3,
})

// Send and receive baggage, use context to propagate and receive
ctx = otelplus.ContextWithBaggage(ctx, "foo", "bar") // propagate
_ = BagMemberValue(ctx, "foo") // receive

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultHTTPClient = otelhttp.DefaultClient

DefaultHTTPClient is otelhttp.DefaultClient

View Source
var Options = options{}

Options is the entry point of creating options to configure the trace provider.

Functions

func AddEvent

func AddEvent(ctx context.Context, event string, kvs map[string]interface{})

AddEvent adds the given event, with key value parameters to the current span. The permitted types for the parameters are string, int, int64, float64, bool, []string and []interface{}, otherwise the method panics.

func AutoTrace

func AutoTrace(operation string, options ...otelhttp.Option) func(handler http.Handler) http.Handler

AutoTrace returns a new HTTP middleware that automatically starts a trace, or inherits an existing trace.

func BagMemberValue

func BagMemberValue(ctx context.Context, key string) string

BagMemberValue retrieves the value of the baggage member carried by the current span.

func ContextWithBaggage

func ContextWithBaggage(ctx context.Context, kvs ...string) context.Context

ContextWithBaggage returns a new context.Context with key value pairs set as baggage.

func SpanID

func SpanID(ctx context.Context) string

SpanID extracts the span id from the current span.

func TraceID

func TraceID(ctx context.Context) string

TraceID extracts the trace id from the current span.

Types

type Closer

type Closer func() error

Closer should be invoked to properly shut down the trace provider.

func Init

Init returns a new trace provider initialized according to the given options and sets it as the global provider. It also returns a shutdown hook to be invoked to properly shut down the trace provider.

type TracerProviderOptionFunc

type TracerProviderOptionFunc func() (sdktrace.TracerProviderOption, error)

TracerProviderOptionFunc can return an tracer provider option, and potentially an error.

Jump to

Keyboard shortcuts

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