An open API service indexing awesome lists of open source software.

https://github.com/nxdir-s/telemetry

Open Telemetry Utilities for Go
https://github.com/nxdir-s/telemetry

go golang observability opentelemetry opentelemetry-go telemetry

Last synced: 2 months ago
JSON representation

Open Telemetry Utilities for Go

Awesome Lists containing this project

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](https://opentelemetry.io/docs/languages/go/getting-started/)

## Usage

Initialize the telemetry providers within `main()`

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

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


Example Lambda Setup

```go
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"),
OtelEndpoint: os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"),
Lambda: true,
}

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() {
cleanup()
cancel()
}),
)
}
```

## Instrumentation

Applications can be manually instrumented or you can use any of the [officially supported instrumentation libraries](https://github.com/open-telemetry/opentelemetry-go-contrib/tree/main/instrumentation)

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


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

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

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