metrics

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package metrics provides a way to collect metrics in a thread-safe way

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

func Make(ctx context.Context, workers int) context.Context

Make context with metrics

func WithWorkerID

func WithWorkerID(ctx context.Context, id int) context.Context

WithWorkerID sets worker ID in the context

func WorkerID

func WorkerID(ctx context.Context) int

WorkerID returns worker ID from the context

Types

type Stats

type Stats struct {
	// raw counters
	Processed int
	Errors    int
	Dropped   int

	// timing
	ProcessingTime time.Duration
	WaitTime       time.Duration
	InitTime       time.Duration
	WrapTime       time.Duration
	TotalTime      time.Duration

	// derived stats, calculated on GetStats
	RatePerSec  float64       // items processed per second
	AvgLatency  time.Duration // average processing time per item
	ErrorRate   float64       // portion of errors
	DroppedRate float64       // portion of dropped items
	Utilization float64       // portion of time spent processing vs waiting
}

Stats represents worker-specific metrics with derived values

func (Stats) String added in v0.2.0

func (s Stats) String() string

String returns stats info formatted as string

type TimerType added in v0.2.0

type TimerType int

TimerType is a type of timer to measure

const (
	TimerProc TimerType = iota // processing time
	TimerWait                  // wait time
	TimerInit                  // initialization time
	TimerWrap                  // wrap-up time
)

Timer types

type Value

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

Value holds both per-worker stats and shared user stats

func Get

func Get(ctx context.Context) *Value

Get metrics from context. If not found, creates new instance with same worker count as stored in context.

func New

func New(workers int) *Value

New makes thread-safe metrics collector with specified number of workers

func (*Value) Add

func (m *Value) Add(key string, delta int) int

Add increments value for a given key and returns new value

func (*Value) AddWaitTime added in v0.3.1

func (m *Value) AddWaitTime(wid int, d time.Duration)

AddWaitTime adds wait time directly to worker stats

func (*Value) Get

func (m *Value) Get(key string) int

Get returns value for given key from shared stats

func (*Value) GetStats added in v0.2.0

func (m *Value) GetStats() Stats

GetStats returns combined stats from all workers. For accurate results, call this method after pool completion (Close/Wait returned). Calling during active processing may return inconsistent values due to concurrent writes.

func (*Value) Inc

func (m *Value) Inc(key string) int

Inc increments value for given key by one

func (*Value) IncDropped added in v0.2.0

func (m *Value) IncDropped(wid int)

IncDropped increments dropped count for worker

func (*Value) IncErrors added in v0.2.0

func (m *Value) IncErrors(wid int)

IncErrors increments errors count for worker

func (*Value) IncProcessed added in v0.2.0

func (m *Value) IncProcessed(wid int)

IncProcessed increments processed count for worker

func (*Value) StartTimer

func (m *Value) StartTimer(wid int, t TimerType) func()

StartTimer returns a function that when called will record the duration in worker stats

func (*Value) String

func (m *Value) String() string

String returns sorted key:vals string representation of user-defined metrics

Jump to

Keyboard shortcuts

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