obs

package module
v0.0.0-...-10fbc9a Latest Latest
Warning

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

Go to latest
Published: May 23, 2025 License: EUPL-1.2 Imports: 12 Imported by: 0

README

Convenient OpenTelemetry Start

Set up the OpenTelemetry SDK easily. See the package documentation.

Documentation

Overview

Package obs sets up the OpenTelemetry SDK as automatically as possible for your application. It configures the default providers for tracing, metrics and logging. You'll still need to add instrumentation yourself.

Example
package main

import (
	"context"
	"log"
	"log/slog"
	"time"

	"go.opentelemetry.io/contrib/detectors/aws/ecs"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/sdk/resource"

	"github.com/wolverian/obs"
)

func main() {
	shutdown, err := obs.Start(context.Background(),
		"test-app",
		// Customise your resource by e.g. using detectors for your platform
		resource.WithDetectors(ecs.NewResourceDetector()),
	)
	if err != nil {
		panic(err)
	}
	defer func() {
		ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
		defer cancelFunc()
		if err := shutdown(ctx); err != nil {
			log.Printf("error shutting down observability: %v", err)
		}
	}()

	Run(context.Background())
}

func Run(ctx context.Context) {
	slog.Info("Logs to OpenTelemetry")

	tracer := otel.Tracer("test-app")
	ctx, span := tracer.Start(ctx, "test-span")
	defer span.End()

	span.SetAttributes(attribute.String("example", "attribute"))

	// Do much work here
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Start

func Start(ctx context.Context, name string, opts ...resource.Option) (func(context.Context) error, error)

Start sets up OpenTelemetry tracing, metrics, and logging.

After calling Start, you can use the OpenTelemetry API and instrument your code. The standard library log and slog packages are configured to send logs to OpenTelemetry. You can customize the exporters using environment variables as specified in autoexport.

Returns a shutdown function to close exporters and free resources, and an error if setup fails.

Types

This section is empty.

Jump to

Keyboard shortcuts

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