Categorygithub.com/nxdir-s/telemetry
modulepackage
1.0.5
Repository: https://github.com/nxdir-s/telemetry.git
Documentation: pkg.go.dev

# README

Telemetry

This repository contains utilities for working with Open Telemetry. You can find a getting started guide with OpenTelemetry in Go on opentelemetry.io

Usage

Initialize the telemetry providers within main()

cfg := &telemetry.Config{
    ServiceName:    os.Getenv("OTEL_SERVICE_NAME"),
    Endpoint:       os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"),
}

ctx, cleanup, err := telemetry.InitProviders(ctx, cfg)
if err != nil {
    // handle error
}

Example Lambda Setup

func main() {
    ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
    defer cancel()

    logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
    slog.SetDefault(logger)

    cfg := &telemetry.Config{
        ServiceName:    os.Getenv("OTEL_SERVICE_NAME"),
        Endpoint:       os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"),
        Lambda:         true,
    }

    ctx, cleanup, err := telemetry.InitProviders(ctx, cfg)
    if err != nil {
        // handle error
    }

    adapter := primary.NewLambdaAdapter()

    // any remaining setup for lambda...

    lambda.StartWithOptions(
        otellambda.InstrumentHandler(adapter.HandleRequest,
            otellambda.WithTracerProvider(otel.GetTracerProvider()),
            otellambda.WithFlusher(otel.GetTracerProvider().(*trace.TracerProvider)),
            otellambda.WithPropagator(otel.GetTextMapPropagator()),
        ),
        lambda.WithContext(ctx),
        lambda.WithEnableSIGTERM(func() {
            cancel()
        }),
    )
}

Instrumentation

Applications can be manually instrumented or you can use any of the officially supported instrumentation libraries

docs: https://opentelemetry.io/docs/languages/go/instrumentation/#metrics


To add custom spans within your application, the following can be done

ctx, span := tracer.Start(ctx, "Adapter.HandleRequest")
defer span.End()

docs: https://opentelemetry.io/docs/languages/go/instrumentation/#creating-spans


AWS SDK

Add the following after initialization to instrument the aws sdk

// init aws config
cfg, err := awsConfig.LoadDefaultConfig(ctx)
if err != nil {
    // handle error
}

// instrument all aws clients
otelaws.AppendMiddlewares(&cfg.APIOptions)

Host and Runtime Metrics

Add the following after initialization to instrument host and runtime metric collection

err = host.Start(host.WithMeterProvider(otel.GetMeterProvider()))
if err != nil {
    // handle error
}

err = runtime.Start(runtime.WithMeterProvider(otel.GetMeterProvider()), runtime.WithMinimumReadMemStatsInterval(time.Second))
if err != nil {
    // handle error
}

# Functions

InitProviders initializes trace and metric providers.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author