otelSDK

package module
v0.0.0-...-721405e Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2022 License: Apache-2.0 Imports: 17 Imported by: 2

README

otelSDK

This SDK wraps the Opentelemetry libraries to increase maintainability and time to development. The rationale being that if you dont use this approach, then OTEL imports will run throughout your code base and should changes be necessary they will be required for each instance of use.

The SDK currently only supports stdout tracing to file, this will be improved shortly

Simple usage

Instantiate at application startup
ctx := context.TODO()
var shutdownTracer otelSDK.ShutdownTracerFn
ctx, shutdownTracer, err = myOtel.ConfigureStdoutTracing(ctx, APPNAME)
if err != nil {
    defer shutdownTracer()
}
Add HTTP middleware
// Example comments for github.com/gorilla/mux but works with any
// var handler http.Handler

handler = myOtel.NewTracedHttpHandler(context.TODO(), "myAppName", handler)

// add other handlers, e.g.
// handler = HttpLogger(handler, route.Name)

// router.
//    Methods(route.Method).
//    Path(route.Pattern).
//    Name(route.Name).
//    Handler(handler)
Instrument your top level HTTP request handlers, plus any processing within as required
func myRequestHandler(w http.ResponseWriter, r *http.Request) {
{
    var span otelSDK.Span
    ctx, span = otelSDK.TraceStart(ctx, "MyAppName", "MyCausationId", "MyCorrelationId")
    defer otelSDK.TraceEnd(span)

    // insert your request handler code here
    // set any attributes and events as required:
    span = otelSDK.TraceSetStringAttribute(span, key, value)
    otelSDK.TraceAddEvent(span, "my note that an event occurred")

    // at end of the handler, write any errors
    if err != nil {
        myOtel.TraceError(span, err)
    }
}
Include any other third party libs directly in your code

opentelemetry.io/docs/instrumentation/go/libraries/

Documentation

Index

Constants

View Source
const (
	CONTEXT_APPNAME        ContextType = "appName"
	CONTEXT_CAUSATION_ID               = "causationId"
	CONTEXT_CORRELATION_ID             = "correlationId"
	CONTEXT_LOG_ENABLED                = "LOG_ENABLED"
	CONTEXT_LOG_LEVEL                  = "LOG_LEVEL"
)

Variables

This section is empty.

Functions

func NewTracedHttpHandler

func NewTracedHttpHandler(ctx context.Context, appName string, handler http.Handler) (wrappedHandler http.Handler)

func TraceAddEvent

func TraceAddEvent(span OtelSpan, note string)

An event is a human-readable message on a span that represents “something happening” during it’s lifetime.

func TraceEnd

func TraceEnd(span OtelSpan)

func TraceError

func TraceError(span OtelSpan, err error)

func TraceMapLoadFromJSON

func TraceMapLoadFromJSON(ctx context.Context, b []byte) (newCtx context.Context, err error)

Deserialise the tracing context from a JSON byte array e.g. when received from a message payload that has passed through a broker

func TraceMapSerialiseToJSON

func TraceMapSerialiseToJSON(ctx context.Context) (b []byte, err error)

Serialise the tracing context into a JSON byte array, e.g. used for embedding in a message payload to pass through a broker

Types

type ContextType

type ContextType string

type OtelExporter

type OtelExporter *stdouttrace.Exporter

wrap otel objects so clients avoid additional imports

type OtelSpan

type OtelSpan trace.Span

wrap otel objects so clients avoid additional imports

func TraceSetStringAttribute

func TraceSetStringAttribute(span OtelSpan, key, value string) (newSpan OtelSpan)

func TraceStart

func TraceStart(ctx context.Context, appName, causationId, correlationId string) (ctx2 context.Context, span OtelSpan)

start tracing

type OtelTracer

type OtelTracer trace.Tracer

wrap otel objects so clients avoid additional imports

func NewTracer

func NewTracer(ctx context.Context, appName string, tp *sdktrace.TracerProvider) (t OtelTracer)

type ShutdownTracerFn

type ShutdownTracerFn func()

func ConfigureFileTracing

func ConfigureFileTracing(ctx context.Context, appName, filename string) (newCtx context.Context, shutdownFn ShutdownTracerFn, err error)

func ConfigureStdoutTracing

func ConfigureStdoutTracing(ctx context.Context, appName string) (newCtx context.Context, shutdownFn ShutdownTracerFn, err error)

Jump to

Keyboard shortcuts

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