sauté

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 8 Imported by: 0

README

🍳 Super-Automatic Telemetry

Go Reference Licence GitLab Release (by release name)


Sauté makes it super easy to set up OpenTelemetry in Go.

With a single function call both metrics and tracing are set up based on environment variables.

Configuration

Most of the configuration is through environment variables, as per the Environment Variable Specification (see also the OpenTelemetry Protocol Exporter documentation), but some things can also be set with options to sauté.Init().

Example

import sauté "gitlab.com/biffen/saute"

func main() {
	// Set up context, logging, etc.

	fini, _ := sauté.Init(ctx)
	defer fini()

	// The rest of your application goes here.
}

Licence

MIT

Documentation

Overview

Package sauté: Super-automatic telemetry. 🪄

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CodeAttributes added in v0.0.3

func CodeAttributes() (attrs []attribute.KeyValue)

CodeAttributes returns ‘code.*’ attributes for the caller.

func Init

func Init(
	ctx context.Context,
	options ...Option,
) (stop func(), err error)

Init is the main function of sauté. It starts telemetry and returns a function to stop it.

If things go really bad it returns an error. The stop function is never nil, but it might be a no-op.

Most things are configured with environment variables, see OpenTelemetry’s Environment Variable Specification. Option:s can be used to override some things.

Example
package main

import (
	"context"
	"log/slog"
	"os"
	"os/signal"
	"syscall"

	"github.com/dotse/slug"

	sauté "gitlab.com/biffen/saute"
)

func main() {
	// Set up a context.Context
	ctx, stop := signal.NotifyContext(
		context.Background(),
		os.Interrupt,
		syscall.SIGTERM,
	)
	defer stop()

	// Set up logging
	slog.SetDefault(slog.New(slug.NewHandler(slug.HandlerOptions{
		HandlerOptions: slog.HandlerOptions{
			Level: slog.LevelDebug,
		},
	}, os.Stderr)))

	// Now start telemetry!
	fini, err := sauté.Init(ctx,
		sauté.LogLevel(slog.LevelDebug),
	)
	// Optionally handle a serious error
	if err != nil {
		panic(err)
	}

	// Don’t forget to stop telemetry
	defer fini()

}
Output:

func TraceFunc added in v0.0.3

func TraceFunc(
	ctx context.Context,
	tracer trace.Tracer,
	opts ...trace.SpanStartOption,
) (context.Context, trace.Span)

TraceFunc returns a new go.opentelemetry.io/otel/trace.Span for the calling function.

An optional go.opentelemetry.io/otel/trace.Tracer can be passed. If it is nil a default Tracer is used.

Example

package mypackage

func MyFunction(ctx context.Context) {
  ctx, span := saute.TraceFunc(ctx, nil)
  defer span.End()

  // The span is named something like
  // ‘example.example/mymodule/mypackage.MyFunction’ and has attributes for
  // file, line, function, etc.
}

Types

type Option

type Option interface {
	internal.Option
}

func Attributes

func Attributes(attributes ...attribute.KeyValue) Option

Attributes adds attributes to all telemetry.

func Detectors

func Detectors(detectors ...resource.Detector) Option

Detectors adds go.opentelemetry.io/otel/sdk/resource.Detector:s for resources.

func ErrorHandler added in v0.0.2

func ErrorHandler(handler otel.ErrorHandler) Option

ErrorHandler specifies an go.opentelemetry.io/otel.ErrorHandler to set. The default is to log the error (see also: Logger).

Using this function with a nil argument sets a no-op error handler.

See also: go.opentelemetry.io/otel.SetErrorHandler.

func Getenv

func Getenv(getenv func(string) string) Option

Getenv sets the function to use for getting environment variables. The default is os.Getenv.

func Host

func Host(host bool) Option

Host sets whether to use go.opentelemetry.io/contrib/instrumentation/host for host metrics. The default is true.

func LogLevel

func LogLevel(level slog.Leveler) Option

LogLevel sets the log level of the default logger. If Logger is used then this does nothing. The default is parsed from OTEL_LOG_LEVEL.

func Logger

func Logger(logger *slog.Logger) Option

Logger sets a logger for telemetry. Default is log/slog’s default with a sybsystem=telemetry attribute.

func Resource

func Resource(res *resource.Resource) Option

Resource sets a custom go.opentelemetry.io/otel/sdk/resource.Resource to merge with the others.

func Runtime

func Runtime(runtime bool) Option

Runtime sets whether to use go.opentelemetry.io/contrib/instrumentation/runtime for runtime metrics. The default is true.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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