instr

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package instr provides instrumentation utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunSpan

func RunSpan(ctx context.Context, name string, fn func(ctx context.Context, span *Span) error) error

RunSpan will wrap a function with a span to automatically handle error tracking and ending

func SpanLogger

func SpanLogger(ctx context.Context, id string, attrs ...logging.Attr) zerolog.Logger

SpanLogger fetches a logger for the supplied span id. This will search up the context for a potential parent id, and properly set all attributes on the found logger.

The base logger used is the default logger from the passed context.

Types

type PrometheusMetrics

type PrometheusMetrics struct {
	// contains filtered or unexported fields
}

func NewPrometheusMetrics

func NewPrometheusMetrics(opts ...PrometheusMetricsOpt) (*PrometheusMetrics, error)

NewPrometheusMetrics creates a new prometheus metrics object. This will setup sane default prometheus metrics, with additional configuration with `...PrometheusMetricsOpt`.

func (*PrometheusMetrics) Handler

func (p *PrometheusMetrics) Handler() http.Handler

Handler returns the HTTP handler for this specific instance of the Prometheus metrics registry. If the `WithDefaultRegistry` option was used, then calling `promhttp.Handler()` will return the same handler.

func (*PrometheusMetrics) Middleware

func (p *PrometheusMetrics) Middleware(next http.Handler) http.Handler

Middleware returns an HTTP middleware handler that tracks request count and duration.

func (*PrometheusMetrics) SpanCtx

func (p *PrometheusMetrics) SpanCtx(ctx context.Context, name string, fn func(ctx context.Context, id string) error) error

SpanCtx is an extremely basic span function wrapper to track execution time. This is NOT a replacement for otel, just a simple exercise that may prove useful in certain cases. The function provides the spanId as the argument `id` in the function.

In addition, this function transiently passes the error to the caller

The context will contain a spanID key to track the current span, and if SpanCtx is called within the runtime of another span, the parent span id will be embeded into the span.

func (*PrometheusMetrics) StartSpan

func (p *PrometheusMetrics) StartSpan(ctx context.Context, name string) *Span

StartSpan starts a span with a given function name using the existing registry found in the PrometheusMetrics

type PrometheusMetricsOpt

type PrometheusMetricsOpt func(*PrometheusMetrics) error

func WithCustomRegistry

func WithCustomRegistry(registry *prometheus.Registry) PrometheusMetricsOpt

func WithGlobalRegistry

func WithGlobalRegistry() PrometheusMetricsOpt

func WithNoGoMetrics

func WithNoGoMetrics() PrometheusMetricsOpt

func WithPromMetrics

func WithPromMetrics(m ...prometheus.Collector) PrometheusMetricsOpt

type Span

type Span struct {
	// contains filtered or unexported fields
}

func StartSpan

func StartSpan(ctx context.Context, name string) *Span

StartSpan starts a span using the default prometheus registry

func (*Span) End

func (s *Span) End()

End ends the span and observes the underlying prometheus metric

func (*Span) Error

func (s *Span) Error(err error) error

Error observes an error and optionally transiently passes it to the caller

func (*Span) GetDuration

func (s *Span) GetDuration() time.Duration

GetDuration returns the current duration of the span in seconds

func (*Span) Logger

func (s *Span) Logger(attrs ...logging.Attr) zerolog.Logger

Logger returns a new logger for the span.

func (*Span) StartChildSpan

func (s *Span) StartChildSpan(name string) *Span

func (*Span) TraceLog

func (s *Span) TraceLog()

TraceLog will print a trace log record using the default zerolog logger reporting status about the span

Jump to

Keyboard shortcuts

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