metrics

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package metrics provides functionality for collecting and reporting cache performance metrics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheMetrics

type CacheMetrics struct {
	// Basic Cache Metrics
	Size              atomic.Int64
	Hits              atomic.Int64
	Misses            atomic.Int64
	Evictions         atomic.Int64
	MemoryUsage       atomic.Int64
	LastOperationTime atomic.Value // time.Time

	// Batch Operation Metrics
	BatchOperations    atomic.Int64
	BatchItems         atomic.Int64
	BatchSuccess       atomic.Int64
	BatchErrors        atomic.Int64
	BatchTimeouts      atomic.Int64
	LastBatchOperation atomic.Value // time.Time

	// Compression Metrics
	CompressedItems   atomic.Int64
	DecompressedItems atomic.Int64
	CompressedBytes   atomic.Int64
	DecompressedBytes atomic.Int64
	CompressionRatio  atomic.Value // float64
	LastCompression   atomic.Value // time.Time

	// Pool Metrics
	PoolCreated     atomic.Int64
	PoolDestroyed   atomic.Int64
	PoolCurrentSize atomic.Int64
	LastPoolCleanup atomic.Value // time.Time

	// Resource Usage
	GoRoutines  atomic.Int64
	LastCleanup atomic.Value // time.Time
}

CacheMetrics represents unified metrics for all cache components

func NewCacheMetrics

func NewCacheMetrics() *CacheMetrics

NewCacheMetrics creates a new CacheMetrics instance

func (*CacheMetrics) GetSnapshot

func (m *CacheMetrics) GetSnapshot() MetricsSnapshot

GetSnapshot returns a thread-safe copy of current metrics

func (*CacheMetrics) RecordEviction

func (m *CacheMetrics) RecordEviction()

RecordEviction records a cache eviction

func (*CacheMetrics) RecordHit

func (m *CacheMetrics) RecordHit()

RecordHit records a cache hit

func (*CacheMetrics) RecordMiss

func (m *CacheMetrics) RecordMiss()

RecordMiss records a cache miss

func (*CacheMetrics) Reset

func (m *CacheMetrics) Reset()

Reset resets all metrics to zero

func (*CacheMetrics) UpdateSize

func (m *CacheMetrics) UpdateSize(size int64)

UpdateSize updates the current cache size

type ExporterType

type ExporterType string

ExporterType defines the type of metrics exporter

const (
	// StandardExporter uses the default metrics implementation
	StandardExporter ExporterType = "standard"
	// PrometheusExporterType uses Prometheus metrics
	PrometheusExporterType ExporterType = "prometheus"
)

type Metrics

type Metrics struct {

	// Capacity is the maximum number of items the cache can hold
	Capacity int64

	// LastAccess is the time of the last cache access
	LastAccess time.Time
	// contains filtered or unexported fields
}

Metrics tracks cache performance metrics

func New

func New(capacity int64) *Metrics

New creates a new Metrics instance

func (*Metrics) GetStats

func (m *Metrics) GetStats() map[string]any

GetStats returns the current metrics

func (*Metrics) HitRatio

func (m *Metrics) HitRatio() float64

HitRatio returns the cache hit ratio

type MetricsExporter

type MetricsExporter interface {
	// RecordHit records a cache hit
	RecordHit()
	// RecordMiss records a cache miss
	RecordMiss()
	// RecordEviction records a cache eviction
	RecordEviction()
	// UpdateSize updates the current cache size
	UpdateSize(size int64)
	// GetSnapshot returns a thread-safe copy of current metrics
	GetSnapshot() MetricsSnapshot
	// Reset resets all metrics to zero
	Reset()
}

MetricsExporter defines the interface for metrics exporters

func NewMetricsExporter

func NewMetricsExporter(exporterType ExporterType, cacheName string, labels map[string]string) MetricsExporter

NewMetricsExporter creates a new metrics exporter based on the specified type

type MetricsSnapshot

type MetricsSnapshot struct {
	// Basic Cache
	Size              int64
	Hits              int64
	Misses            int64
	Evictions         int64
	MemoryUsage       int64
	LastOperationTime time.Time

	// Batch Operations
	BatchOperations    int64
	BatchItems         int64
	BatchSuccess       int64
	BatchErrors        int64
	BatchTimeouts      int64
	LastBatchOperation time.Time

	// Compression
	CompressedItems   int64
	DecompressedItems int64
	CompressedBytes   int64
	DecompressedBytes int64
	CompressionRatio  float64
	LastCompression   time.Time

	// Pool
	PoolCreated     int64
	PoolDestroyed   int64
	PoolCurrentSize int64
	LastPoolCleanup time.Time

	// Resource Usage
	GoRoutines  int64
	LastCleanup time.Time
}

MetricsSnapshot is a thread-safe copy of metrics

type PrometheusMetricsExporter

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

PrometheusMetricsExporter implements MetricsExporter using Prometheus metrics

func NewPrometheusMetricsExporter

func NewPrometheusMetricsExporter(cacheName string, labels map[string]string) *PrometheusMetricsExporter

NewPrometheusMetricsExporter creates a new Prometheus metrics exporter

func (*PrometheusMetricsExporter) GetSnapshot

func (e *PrometheusMetricsExporter) GetSnapshot() MetricsSnapshot

GetSnapshot implements MetricsExporter

func (*PrometheusMetricsExporter) RecordEviction

func (e *PrometheusMetricsExporter) RecordEviction()

RecordEviction implements MetricsExporter

func (*PrometheusMetricsExporter) RecordHit

func (e *PrometheusMetricsExporter) RecordHit()

RecordHit implements MetricsExporter

func (*PrometheusMetricsExporter) RecordMiss

func (e *PrometheusMetricsExporter) RecordMiss()

RecordMiss implements MetricsExporter

func (*PrometheusMetricsExporter) Reset

func (e *PrometheusMetricsExporter) Reset()

Reset implements MetricsExporter

func (*PrometheusMetricsExporter) UpdateSize

func (e *PrometheusMetricsExporter) UpdateSize(size int64)

UpdateSize implements MetricsExporter

Jump to

Keyboard shortcuts

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