observe

package
v2.15.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2025 License: MIT Imports: 15 Imported by: 3

Documentation

Overview

Package observe implements a type that combines statter, logger and tracer.

Example usage:

	func newObserver(c *cli.Context) (*observe.Observer, error) {
	    log, err := cmd.NewLogger(c)
	    if err != nil {
	    	return nil, err
	    }

	    stats, err := cmd.NewStatter(c, log)
	    if err != nil {
	    	return nil, err
	    }

	    prof, err := cmd.NewProfiler(c, "my-service", log)
	    if err != nil {
	    	return nil, err
	    }
	    profStop := func() {}
	    if prof != nil {
	    	profStop = func() { _ = prof.Stop() }
	    }

	    tracer, err := cmd.NewTracer(c, log,
	    	semconv.ServiceNameKey.String("my-service"),
	    	semconv.ServiceVersionKey.String("1.0.0"),
	    )
	    if err != nil {
	    	return nil, err
	    }
	    tracerCancel := func() { _ = tracer.Shutdown(context.Background()) }

	    return observe.New(log, stats, tracer, tracerCancel, profStop), nil
    }

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Observer

type Observer struct {
	Log       *logger.Logger
	Stats     *statter.Statter
	TraceProv trace.TracerProvider
	// contains filtered or unexported fields
}

Observer contains observability primitives.

func New

func New(log *logger.Logger, stats *statter.Statter, traceProv trace.TracerProvider, closeFns ...func()) *Observer

New returns an observer with the given observability primitives.

Example
package main

import (
	"context"

	"github.com/hamba/cmd/v2"
	"github.com/hamba/cmd/v2/observe"
	"github.com/urfave/cli/v2"

	semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
)

func main() {
	var c *cli.Context // Get this from your action

	log, err := cmd.NewLogger(c)
	if err != nil {
		// Handle error.
		return
	}

	stats, err := cmd.NewStatter(c, log)
	if err != nil {
		// Handle error.
		return
	}

	prof, err := cmd.NewProfiler(c, "my-service", log)
	if err != nil {
		return
	}
	profStop := func() {}
	if prof != nil {
		profStop = func() { _ = prof.Stop() }
	}

	tracer, err := cmd.NewTracer(c, log,
		semconv.ServiceNameKey.String("my-service"),
		semconv.ServiceVersionKey.String("1.0.0"),
	)
	if err != nil {
		// Handle error.
		return
	}
	tracerCancel := func() { _ = tracer.Shutdown(context.Background()) }

	obsrv := observe.New(log, stats, tracer, tracerCancel, profStop)

	_ = obsrv
}

func NewFake

func NewFake() *Observer

NewFake returns a fake observer that reports nothing. This is useful for tests.

func NewFromCLI added in v2.10.0

func NewFromCLI(cliCtx *cli.Context, svc string, opts *Options) (*Observer, error)

NewFromCLI returns an observer with the given observability primitives.

Example
package main

import (
	"github.com/hamba/cmd/v2/observe"
	"github.com/urfave/cli/v2"
	"go.opentelemetry.io/otel/attribute"

	semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
)

func main() {
	var c *cli.Context // Get this from your action.

	obsrv, err := observe.NewFromCLI(c, "my-service", &observe.Options{
		LogTimestamps: true,
		StatsRuntime:  true,
		TracingAttrs: []attribute.KeyValue{
			semconv.ServiceVersionKey.String("1.0.0"),
		},
	})
	if err != nil {
		// Handle error.
		return
	}

	_ = obsrv
}

func (*Observer) Close

func (o *Observer) Close()

Close closes the observability primitives.

func (*Observer) Tracer

func (o *Observer) Tracer(name string, opts ...trace.TracerOption) trace.Tracer

Tracer returns a tracer with the given name and options. If no trace provider has been set, this function will panic.

type Options added in v2.10.0

type Options struct {
	LogTimeFormat string
	LogTimestamps bool
	LogCtx        []logger.Field
	LogWriter     io.Writer

	StatsRuntime bool
	StatsTags    []statter.Tag

	TracingAttrs []attribute.KeyValue
}

Options optionally configures an observer.

Jump to

Keyboard shortcuts

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