metrics

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(metric Metric) error

Register registers a metric in the default registry

func Unregister

func Unregister(name string, labels map[string]string)

Unregister removes a metric from the default registry

Types

type Counter

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

Counter is a cumulative metric that only increases

Example
counter := NewCounter("requests_total", "Total number of requests", map[string]string{"method": "GET"})

counter.Inc()
counter.Add(5)

fmt.Println(counter.Value())
Output:
6

func NewCounter

func NewCounter(name, help string, labels map[string]string) *Counter

NewCounter creates a new counter metric

func (*Counter) Add

func (c *Counter) Add(delta uint64)

Add adds the given value to the counter

func (*Counter) Help

func (c *Counter) Help() string

Help returns the help text

func (*Counter) Inc

func (c *Counter) Inc()

Inc increments the counter by 1

func (*Counter) Labels

func (c *Counter) Labels() map[string]string

Labels returns the metric labels

func (*Counter) Name

func (c *Counter) Name() string

Name returns the metric name

func (*Counter) Reset

func (c *Counter) Reset()

Reset resets the counter to 0

func (*Counter) Type

func (c *Counter) Type() MetricType

Type returns the metric type

func (*Counter) Value

func (c *Counter) Value() interface{}

Value returns the current value

type Gauge

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

Gauge is a metric that can go up or down

Example
gauge := NewGauge("temperature_celsius", "Current temperature in Celsius", nil)

gauge.Set(22.5)
gauge.Add(2.5)

fmt.Println(gauge.Value())
Output:
25

func NewGauge

func NewGauge(name, help string, labels map[string]string) *Gauge

NewGauge creates a new gauge metric

func (*Gauge) Add

func (g *Gauge) Add(delta float64)

Add adds the given value to the gauge

func (*Gauge) Dec

func (g *Gauge) Dec()

Dec decrements the gauge by 1

func (*Gauge) Help

func (g *Gauge) Help() string

Help returns the help text

func (*Gauge) Inc

func (g *Gauge) Inc()

Inc increments the gauge by 1

func (*Gauge) Labels

func (g *Gauge) Labels() map[string]string

Labels returns the metric labels

func (*Gauge) Name

func (g *Gauge) Name() string

Name returns the metric name

func (*Gauge) Reset

func (g *Gauge) Reset()

Reset resets the gauge to 0

func (*Gauge) Set

func (g *Gauge) Set(value float64)

Set sets the gauge to the given value

func (*Gauge) Sub

func (g *Gauge) Sub(delta float64)

Sub subtracts the given value from the gauge

func (*Gauge) Type

func (g *Gauge) Type() MetricType

Type returns the metric type

func (*Gauge) Value

func (g *Gauge) Value() interface{}

Value returns the current value

type Handler

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

Handler provides HTTP handlers for metrics endpoints

func NewHandler

func NewHandler(registry *Registry) *Handler

NewHandler creates a new HTTP handler for metrics

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(mux *http.ServeMux)

RegisterRoutes registers metrics routes on the given mux

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler interface

type Histogram

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

Histogram tracks the distribution of values

Example
histogram := NewHistogram("request_duration_seconds", "Request duration in seconds", nil, []float64{0.1, 0.5, 1.0})

histogram.Observe(0.05)
histogram.Observe(0.3)
histogram.Observe(0.75)

value := histogram.Value().(map[string]interface{})
fmt.Println(value["count"])
Output:
3

func NewHistogram

func NewHistogram(name, help string, labels map[string]string, buckets []float64) *Histogram

NewHistogram creates a new histogram metric

func (*Histogram) Help

func (h *Histogram) Help() string

Help returns the help text

func (*Histogram) Labels

func (h *Histogram) Labels() map[string]string

Labels returns the metric labels

func (*Histogram) Name

func (h *Histogram) Name() string

Name returns the metric name

func (*Histogram) Observe

func (h *Histogram) Observe(value float64)

Observe adds a single observation to the histogram

func (*Histogram) ObserveDuration

func (h *Histogram) ObserveDuration(start time.Time)

ObserveDuration observes a duration in seconds

func (*Histogram) Reset

func (h *Histogram) Reset()

Reset resets the histogram

func (*Histogram) Type

func (h *Histogram) Type() MetricType

Type returns the metric type

func (*Histogram) Value

func (h *Histogram) Value() interface{}

Value returns the histogram data

type Metric

type Metric interface {
	Name() string
	Type() MetricType
	Help() string
	Value() interface{}
	Labels() map[string]string
	Reset()
}

Metric represents a single metric

type MetricType

type MetricType int

MetricType represents the type of metric

const (
	// MetricTypeCounter is a cumulative metric that only increases
	MetricTypeCounter MetricType = iota
	// MetricTypeGauge is a metric that can go up or down
	MetricTypeGauge
	// MetricTypeHistogram tracks distributions of values
	MetricTypeHistogram
	// MetricTypeSummary tracks summary statistics
	MetricTypeSummary
)

func (MetricType) String

func (mt MetricType) String() string

String returns the string representation of the metric type

type PrometheusExporter

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

PrometheusExporter exports metrics in Prometheus text format

func NewPrometheusExporter

func NewPrometheusExporter(registry *Registry) *PrometheusExporter

NewPrometheusExporter creates a new Prometheus exporter

func (*PrometheusExporter) Export

func (e *PrometheusExporter) Export(w io.Writer) error

Export writes metrics in Prometheus text format to the writer

type Registry

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

Registry manages a collection of metrics

Example
registry := NewRegistry()

counter := registry.GetOrCreateCounter("operations_total", "Total operations", nil)
counter.Inc()

gauge := registry.GetOrCreateGauge("active_connections", "Active connections", nil)
gauge.Set(42)

fmt.Println(len(registry.All()))
Output:
2

func DefaultRegistry

func DefaultRegistry() *Registry

DefaultRegistry returns the default global registry

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new metrics registry

func (*Registry) All

func (r *Registry) All() []Metric

All returns all registered metrics

func (*Registry) Clear

func (r *Registry) Clear()

Clear removes all metrics

func (*Registry) Get

func (r *Registry) Get(name string, labels map[string]string) (Metric, bool)

Get retrieves a metric by name and labels

func (*Registry) GetOrCreateCounter

func (r *Registry) GetOrCreateCounter(name, help string, labels map[string]string) *Counter

GetOrCreateCounter gets or creates a counter metric

func (*Registry) GetOrCreateGauge

func (r *Registry) GetOrCreateGauge(name, help string, labels map[string]string) *Gauge

GetOrCreateGauge gets or creates a gauge metric

func (*Registry) GetOrCreateHistogram

func (r *Registry) GetOrCreateHistogram(name, help string, labels map[string]string, buckets []float64) *Histogram

GetOrCreateHistogram gets or creates a histogram metric

func (*Registry) Register

func (r *Registry) Register(metric Metric) error

Register registers a metric

func (*Registry) Reset

func (r *Registry) Reset()

Reset resets all metrics

func (*Registry) Unregister

func (r *Registry) Unregister(name string, labels map[string]string)

Unregister removes a metric from the registry

type StreamBusMetrics

type StreamBusMetrics struct {
	// Broker metrics
	BrokerUptime         *Gauge
	BrokerStatus         *Gauge
	BrokerConnections    *Gauge
	BrokerActiveRequests *Gauge

	// Message metrics
	MessagesProduced *Counter
	MessagesConsumed *Counter
	MessagesStored   *Counter
	BytesProduced    *Counter
	BytesConsumed    *Counter
	BytesStored      *Counter

	// Topic metrics
	TopicsTotal     *Gauge
	PartitionsTotal *Gauge
	ReplicasTotal   *Gauge

	// Performance metrics
	ProduceLatency     *Histogram
	ConsumeLatency     *Histogram
	ReplicationLatency *Histogram
	CommitLatency      *Histogram

	// Consumer group metrics
	ConsumerGroups       *Gauge
	ConsumerGroupMembers *Gauge
	ConsumerGroupLag     *Gauge

	// Transaction metrics
	TransactionsActive    *Gauge
	TransactionsCommitted *Counter
	TransactionsAborted   *Counter
	TransactionDuration   *Histogram

	// Storage metrics
	StorageUsedBytes      *Gauge
	StorageAvailableBytes *Gauge
	SegmentsTotal         *Gauge
	CompactionsTotal      *Counter

	// Network metrics
	NetworkBytesIn       *Counter
	NetworkBytesOut      *Counter
	NetworkRequestsTotal *Counter
	NetworkErrorsTotal   *Counter

	// Security metrics
	AuthenticationAttempts *Counter
	AuthenticationFailures *Counter
	AuthorizationChecks    *Counter
	AuthorizationDenials   *Counter
	AuditEventsLogged      *Counter

	// Cluster metrics
	ClusterSize     *Gauge
	ClusterLeader   *Gauge
	RaftTerm        *Gauge
	RaftCommitIndex *Gauge

	// Schema registry metrics
	SchemasRegistered      *Counter
	SchemaValidations      *Counter
	SchemaValidationErrors *Counter
	// contains filtered or unexported fields
}

StreamBusMetrics holds all StreamBus-specific metrics

func NewStreamBusMetrics

func NewStreamBusMetrics(registry *Registry) *StreamBusMetrics

NewStreamBusMetrics creates a new StreamBus metrics collector

Jump to

Keyboard shortcuts

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