metrics

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const CollectorKey = shared.MetricsCollectorKey
View Source
const MetricsKey = shared.MetricsKey

Variables

This section is empty.

Functions

func AssertMetricDoesNotExist

func AssertMetricDoesNotExist(t interface{}, collector metrics.Metrics, name string, tags ...string)

AssertMetricDoesNotExist asserts that a metric does not exist

func AssertMetricExists

func AssertMetricExists(t interface{}, collector metrics.Metrics, name string, tags ...string)

AssertMetricExists asserts that a metric exists

func AssertMetricValue

func AssertMetricValue(t interface{}, collector metrics.Metrics, name string, expected float64, tags ...string)

AssertMetricValue asserts that a metric has the expected value

func GetMetricsInstance

func GetMetricsInstance(container shared.Container) (shared.Metrics, error)

GetMetricsInstance retrieves the metrics collector from DI container

func MetricsMiddleware

func MetricsMiddleware(collector internal.Metrics) func(next http.Handler) http.Handler

MetricsMiddleware creates middleware for collecting HTTP metrics

func RegisterMetricsCollector

func RegisterMetricsCollector(container shared.Container, config *CollectorConfig) error

RegisterMetricsCollector registers the metrics collector directly with DI container

func RegisterMetricsEndpoints

func RegisterMetricsEndpoints(router shared.Router, collector internal.Metrics, config *EndpointConfig, logger logger.Logger) error

RegisterMetricsEndpoints registers metrics endpoints with a router

func ValidateExportFormat

func ValidateExportFormat(data []byte, format internal.ExportFormat) error

ValidateExportFormat validates that export format is valid

func WaitForMetricValue

func WaitForMetricValue(collector metrics.Metrics, name string, expected float64, timeout time.Duration, tags ...string) error

WaitForMetricValue waits for a metric to reach the expected value

Types

type CollectorConfig

type CollectorConfig = shared.MetricsConfig

CollectorConfig contains configuration for the metrics collector

func DefaultCollectorConfig

func DefaultCollectorConfig() *CollectorConfig

DefaultCollectorConfig returns default collector configuration

type CollectorStats

type CollectorStats = metrics.CollectorStats

CollectorStats contains statistics about the metrics collector

type CounterData

type CounterData struct {
	Name  string
	Value float64
	Tags  map[string]string
}

Test data structures

type EndpointConfig

type EndpointConfig struct {
	Enabled       bool          `yaml:"enabled" json:"enabled"`
	PrefixPath    string        `yaml:"prefix_path" json:"prefix_path"`
	MetricsPath   string        `yaml:"metrics_path" json:"metrics_path"`
	HealthPath    string        `yaml:"health_path" json:"health_path"`
	StatsPath     string        `yaml:"stats_path" json:"stats_path"`
	EnableCORS    bool          `yaml:"enable_cors" json:"enable_cors"`
	RequireAuth   bool          `yaml:"require_auth" json:"require_auth"`
	CacheDuration time.Duration `yaml:"cache_duration" json:"cache_duration"`
}

EndpointConfig contains configuration for metrics endpoints

type ExporterConfig

type ExporterConfig = shared.MetricsExporterConfig[map[string]interface{}]

ExporterConfig contains configuration for exporters

type GaugeData

type GaugeData struct {
	Name  string
	Value float64
	Tags  map[string]string
}

type HistogramData

type HistogramData struct {
	Name   string
	Values []float64
	Tags   map[string]string
}

type IntegrationTestEnvironment

type IntegrationTestEnvironment struct {
	Collector metrics.Metrics
	Registry  Registry
	Exporters map[internal.ExportFormat]internal.Exporter
	Logger    logger.Logger
	// contains filtered or unexported fields
}

IntegrationTestEnvironment provides a complete integration test environment

func NewIntegrationTestEnvironment

func NewIntegrationTestEnvironment(useRealImplementations bool) *IntegrationTestEnvironment

NewIntegrationTestEnvironment creates a new integration test environment

func (*IntegrationTestEnvironment) Cleanup

func (e *IntegrationTestEnvironment) Cleanup()

Cleanup cleans up the integration test environment

func (*IntegrationTestEnvironment) RunFullIntegrationTest

func (e *IntegrationTestEnvironment) RunFullIntegrationTest() error

RunFullIntegrationTest runs a complete integration test

func (*IntegrationTestEnvironment) Setup

func (e *IntegrationTestEnvironment) Setup() error

Setup sets up the integration test environment

type Metrics

type Metrics = metrics.Metrics

Metrics defines the interface for metrics collection

func New

func New(config *CollectorConfig, logger logger.Logger) Metrics

New creates a new metrics collector

func NewNoOpMetrics

func NewNoOpMetrics() Metrics

NewNoOpMetrics creates a no-op metrics collector that implements the full Metrics interface but performs no actual metric collection or storage. Useful for testing, benchmarking, or when metrics are disabled.

type MetricsDataGenerator

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

MetricsDataGenerator generates test metrics data

func NewMetricsDataGenerator

func NewMetricsDataGenerator(seed int64) *MetricsDataGenerator

NewMetricsDataGenerator creates a new data generator

func (*MetricsDataGenerator) GenerateCounterData

func (g *MetricsDataGenerator) GenerateCounterData(count int) []CounterData

GenerateCounterData generates counter test data

func (*MetricsDataGenerator) GenerateGaugeData

func (g *MetricsDataGenerator) GenerateGaugeData(count int) []GaugeData

GenerateGaugeData generates gauge test data

func (*MetricsDataGenerator) GenerateHistogramData

func (g *MetricsDataGenerator) GenerateHistogramData(count int) []HistogramData

GenerateHistogramData generates histogram test data

func (*MetricsDataGenerator) GenerateTimerData

func (g *MetricsDataGenerator) GenerateTimerData(count int) []TimerData

GenerateTimerData generates timer test data

type MetricsEndpointHandler

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

MetricsEndpointHandler handles metrics endpoint requests

func NewMetricsEndpointHandler

func NewMetricsEndpointHandler(collector shared.Metrics, config *EndpointConfig, logger logger.Logger) *MetricsEndpointHandler

NewMetricsEndpointHandler creates a new metrics endpoint handler

func (*MetricsEndpointHandler) RegisterEndpoints

func (h *MetricsEndpointHandler) RegisterEndpoints(r shared.Router) error

RegisterEndpoints registers metrics endpoints with the router

type MetricsTestFixture

type MetricsTestFixture struct {
	Collector *MockMetricsCollector
	Registry  Registry
	Exporters map[internal.ExportFormat]*MockExporter
	Logger    logger.Logger
	// contains filtered or unexported fields
}

MetricsTestFixture provides a complete test environment for metrics

func NewMetricsTestFixture

func NewMetricsTestFixture() *MetricsTestFixture

NewMetricsTestFixture creates a new test fixture

func (*MetricsTestFixture) Cleanup

func (f *MetricsTestFixture) Cleanup()

Cleanup cleans up the test fixture

func (*MetricsTestFixture) Context

func (f *MetricsTestFixture) Context() context.Context

Context returns the test context

func (*MetricsTestFixture) CreateTestMetrics

func (f *MetricsTestFixture) CreateTestMetrics()

CreateTestMetrics creates a set of test metrics

func (*MetricsTestFixture) StartCollector

func (f *MetricsTestFixture) StartCollector() error

StartCollector starts the mock collector

type MockCounter

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

MockCounter implements Counter interface

func NewMockCounter

func NewMockCounter(name string, tags map[string]string) *MockCounter

func (*MockCounter) Add

func (c *MockCounter) Add(value float64)

func (*MockCounter) AddCallCount

func (c *MockCounter) AddCallCount() int

func (*MockCounter) Dec

func (c *MockCounter) Dec()

func (*MockCounter) Get

func (c *MockCounter) Get() float64

func (*MockCounter) Inc

func (c *MockCounter) Inc()

func (*MockCounter) IncCallCount

func (c *MockCounter) IncCallCount() int

func (*MockCounter) Name

func (c *MockCounter) Name() string

func (*MockCounter) Reset

func (c *MockCounter) Reset() error

func (*MockCounter) Tags

func (c *MockCounter) Tags() map[string]string

func (*MockCounter) WasResetCalled

func (c *MockCounter) WasResetCalled() bool

func (*MockCounter) WithLabels

func (c *MockCounter) WithLabels(labels map[string]string) shared.Counter

type MockCustomCollector

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

MockCustomCollector implements CustomCollector interface

func NewMockCustomCollector

func NewMockCustomCollector(name string) *MockCustomCollector

func (*MockCustomCollector) Collect

func (c *MockCustomCollector) Collect() map[string]interface{}

func (*MockCustomCollector) CollectCallCount

func (c *MockCustomCollector) CollectCallCount() int

func (*MockCustomCollector) IsEnabled

func (c *MockCustomCollector) IsEnabled() bool

func (*MockCustomCollector) Name

func (c *MockCustomCollector) Name() string

func (*MockCustomCollector) Reset

func (c *MockCustomCollector) Reset() error

func (*MockCustomCollector) SetMetric

func (c *MockCustomCollector) SetMetric(key string, value interface{})

func (*MockCustomCollector) WasResetCalled

func (c *MockCustomCollector) WasResetCalled() bool

type MockExporter

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

MockExporter implements Exporter interface

func NewMockExporter

func NewMockExporter(format string) *MockExporter

func (*MockExporter) Export

func (e *MockExporter) Export(metrics map[string]interface{}) ([]byte, error)

func (*MockExporter) ExportCallCount

func (e *MockExporter) ExportCallCount() int

func (*MockExporter) Format

func (e *MockExporter) Format() string

func (*MockExporter) SetExportData

func (e *MockExporter) SetExportData(data []byte)

func (*MockExporter) SetExportError

func (e *MockExporter) SetExportError(err error)

func (*MockExporter) Stats

func (e *MockExporter) Stats() interface{}

type MockGauge

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

MockGauge implements Gauge interface

func NewMockGauge

func NewMockGauge(name string, tags map[string]string) *MockGauge

func (*MockGauge) Add

func (g *MockGauge) Add(value float64)

func (*MockGauge) Dec

func (g *MockGauge) Dec()

func (*MockGauge) Get

func (g *MockGauge) Get() float64

func (*MockGauge) Inc

func (g *MockGauge) Inc()

func (*MockGauge) Name

func (g *MockGauge) Name() string

func (*MockGauge) Reset

func (g *MockGauge) Reset() error

func (*MockGauge) Set

func (g *MockGauge) Set(value float64)

func (*MockGauge) SetCallCount

func (g *MockGauge) SetCallCount() int

func (*MockGauge) Tags

func (g *MockGauge) Tags() map[string]string

func (*MockGauge) WasResetCalled

func (g *MockGauge) WasResetCalled() bool

func (*MockGauge) WithLabels

func (g *MockGauge) WithLabels(labels map[string]string) shared.Gauge

type MockHistogram

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

MockHistogram implements Histogram interface

func NewMockHistogram

func NewMockHistogram(name string, tags map[string]string) *MockHistogram

func (*MockHistogram) GetBuckets

func (h *MockHistogram) GetBuckets() map[float64]uint64

func (*MockHistogram) GetCount

func (h *MockHistogram) GetCount() uint64

func (*MockHistogram) GetMean

func (h *MockHistogram) GetMean() float64

func (*MockHistogram) GetPercentile

func (h *MockHistogram) GetPercentile(percentile float64) float64

func (*MockHistogram) GetSum

func (h *MockHistogram) GetSum() float64

func (*MockHistogram) Name

func (h *MockHistogram) Name() string

func (*MockHistogram) Observe

func (h *MockHistogram) Observe(value float64)

func (*MockHistogram) ObserveCallCount

func (h *MockHistogram) ObserveCallCount() int

func (*MockHistogram) ObserveDuration

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

func (*MockHistogram) Reset

func (h *MockHistogram) Reset() error

func (*MockHistogram) Tags

func (h *MockHistogram) Tags() map[string]string

func (*MockHistogram) WasResetCalled

func (h *MockHistogram) WasResetCalled() bool

func (*MockHistogram) WithLabels

func (h *MockHistogram) WithLabels(labels map[string]string) shared.Histogram

type MockMetricsCollector

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

MockMetricsCollector implements MetricsCollector interface for testing

func NewMockMetricsCollector

func NewMockMetricsCollector() *MockMetricsCollector

NewMockMetricsCollector creates a new mock metrics collector

func (*MockMetricsCollector) Counter

func (m *MockMetricsCollector) Counter(name string, tags ...string) internal.Counter

Mock metric creation methods

func (*MockMetricsCollector) Dependencies

func (m *MockMetricsCollector) Dependencies() []string

func (*MockMetricsCollector) Export

func (m *MockMetricsCollector) Export(format internal.ExportFormat) ([]byte, error)

Export functionality

func (*MockMetricsCollector) ExportToFile

func (m *MockMetricsCollector) ExportToFile(format internal.ExportFormat, filename string) error

func (*MockMetricsCollector) Gauge

func (m *MockMetricsCollector) Gauge(name string, tags ...string) internal.Gauge

func (*MockMetricsCollector) GetCollectors

func (m *MockMetricsCollector) GetCollectors() []internal.CustomCollector

func (*MockMetricsCollector) GetMetrics

func (m *MockMetricsCollector) GetMetrics() map[string]interface{}

Metrics retrieval

func (*MockMetricsCollector) GetMetricsByTag

func (m *MockMetricsCollector) GetMetricsByTag(tagKey, tagValue string) map[string]interface{}

func (*MockMetricsCollector) GetMetricsByType

func (m *MockMetricsCollector) GetMetricsByType(metricType internal.MetricType) map[string]interface{}

func (*MockMetricsCollector) GetStats

Statistics

func (*MockMetricsCollector) Health

func (m *MockMetricsCollector) Health(ctx context.Context) error

func (*MockMetricsCollector) Histogram

func (m *MockMetricsCollector) Histogram(name string, tags ...string) internal.Histogram

func (*MockMetricsCollector) Name

func (m *MockMetricsCollector) Name() string

Service lifecycle methods

func (*MockMetricsCollector) RegisterCollector

func (m *MockMetricsCollector) RegisterCollector(collector internal.CustomCollector) error

Custom collector management

func (*MockMetricsCollector) Reload

func (m *MockMetricsCollector) Reload(config *shared.MetricsConfig) error

func (*MockMetricsCollector) Reset

func (m *MockMetricsCollector) Reset() error

Management

func (*MockMetricsCollector) ResetMetric

func (m *MockMetricsCollector) ResetMetric(name string) error

func (*MockMetricsCollector) SetExportData

func (m *MockMetricsCollector) SetExportData(format internal.ExportFormat, data []byte)

func (*MockMetricsCollector) SetExportError

func (m *MockMetricsCollector) SetExportError(err error)

func (*MockMetricsCollector) SetHealthCheckError

func (m *MockMetricsCollector) SetHealthCheckError(err error)

Test helper methods

func (*MockMetricsCollector) Start

func (m *MockMetricsCollector) Start(ctx context.Context) error

func (*MockMetricsCollector) Stop

func (*MockMetricsCollector) Timer

func (m *MockMetricsCollector) Timer(name string, tags ...string) internal.Timer

func (*MockMetricsCollector) UnregisterCollector

func (m *MockMetricsCollector) UnregisterCollector(name string) error

func (*MockMetricsCollector) WasResetCalled

func (m *MockMetricsCollector) WasResetCalled() bool

func (*MockMetricsCollector) WasResetMetricCalled

func (m *MockMetricsCollector) WasResetMetricCalled(name string) bool

type MockTimer

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

MockTimer implements Timer interface

func NewMockTimer

func NewMockTimer(name string, tags map[string]string) *MockTimer

func (*MockTimer) Get

func (t *MockTimer) Get() time.Duration

func (*MockTimer) GetCount

func (t *MockTimer) GetCount() uint64

func (*MockTimer) GetMax

func (t *MockTimer) GetMax() time.Duration

func (*MockTimer) GetMean

func (t *MockTimer) GetMean() time.Duration

func (*MockTimer) GetMin

func (t *MockTimer) GetMin() time.Duration

func (*MockTimer) GetPercentile

func (t *MockTimer) GetPercentile(percentile float64) time.Duration

func (*MockTimer) Name

func (t *MockTimer) Name() string

func (*MockTimer) Record

func (t *MockTimer) Record(duration time.Duration)

func (*MockTimer) RecordCallCount

func (t *MockTimer) RecordCallCount() int

func (*MockTimer) Reset

func (t *MockTimer) Reset()

func (*MockTimer) Tags

func (t *MockTimer) Tags() map[string]string

func (*MockTimer) Time

func (t *MockTimer) Time() func()

func (*MockTimer) WasResetCalled

func (t *MockTimer) WasResetCalled() bool

type PerformanceResult

type PerformanceResult struct {
	Operations   int64         `json:"operations"`
	Duration     time.Duration `json:"duration"`
	Workers      int           `json:"workers"`
	OpsPerSecond float64       `json:"ops_per_second"`
	OpsPerWorker float64       `json:"ops_per_worker"`
}

PerformanceResult contains performance test results

type PerformanceTestRunner

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

PerformanceTestRunner runs performance tests on metrics

func NewPerformanceTestRunner

func NewPerformanceTestRunner(collector metrics.Metrics, duration time.Duration, workers int) *PerformanceTestRunner

NewPerformanceTestRunner creates a new performance test runner

func (*PerformanceTestRunner) RunCounterTest

func (r *PerformanceTestRunner) RunCounterTest() PerformanceResult

RunCounterTest runs a performance test on counters

func (*PerformanceTestRunner) RunGaugeTest

func (r *PerformanceTestRunner) RunGaugeTest() PerformanceResult

RunGaugeTest runs a performance test on gauges

func (*PerformanceTestRunner) RunHistogramTest

func (r *PerformanceTestRunner) RunHistogramTest() PerformanceResult

RunHistogramTest runs a performance test on histograms

func (*PerformanceTestRunner) RunTimerTest

func (r *PerformanceTestRunner) RunTimerTest() PerformanceResult

RunTimerTest runs a performance test on timers

type RegisteredMetric

type RegisteredMetric struct {
	Name        string                   `json:"name"`
	Type        internal.MetricType      `json:"type"`
	Tags        map[string]string        `json:"tags"`
	Metric      interface{}              `json:"-"`
	Metadata    *internal.MetricMetadata `json:"metadata"`
	CreatedAt   time.Time                `json:"created_at"`
	UpdatedAt   time.Time                `json:"updated_at"`
	AccessCount int64                    `json:"access_count"`
	LastAccess  time.Time                `json:"last_access"`
}

RegisteredMetric represents a registered metric with metadata

func (*RegisteredMetric) GetValue

func (rm *RegisteredMetric) GetValue() interface{}

GetValue returns the current value of the metric

func (*RegisteredMetric) Reset

func (rm *RegisteredMetric) Reset() error

Reset resets the metric

func (*RegisteredMetric) UpdateAccess

func (rm *RegisteredMetric) UpdateAccess()

UpdateAccess updates access statistics

type Registry

type Registry interface {
	// Metric creation and retrieval
	GetOrCreateCounter(name string, tags map[string]string) internal.Counter
	GetOrCreateGauge(name string, tags map[string]string) internal.Gauge
	GetOrCreateHistogram(name string, tags map[string]string) internal.Histogram
	GetOrCreateTimer(name string, tags map[string]string) internal.Timer

	// Metric retrieval
	GetMetric(name string, tags map[string]string) interface{}
	GetAllMetrics() map[string]interface{}
	GetMetricsByType(metricType internal.MetricType) map[string]interface{}
	GetMetricsByTag(tagKey, tagValue string) map[string]interface{}
	GetMetricsByNamePattern(pattern string) map[string]interface{}

	// Metric management
	RegisterMetric(name string, metric interface{}, metricType internal.MetricType, tags map[string]string) error
	UnregisterMetric(name string, tags map[string]string) error
	ResetMetric(name string) error
	Reset() error

	// Statistics
	Count() int
	GetRegisteredMetrics() []*RegisteredMetric
	GetMetricMetadata(name string, tags map[string]string) *internal.MetricMetadata

	// Lifecycle
	Start() error
	Stop() error
}

Registry manages metric storage and retrieval

func NewRegistry

func NewRegistry() Registry

NewRegistry creates a new metrics registry

func NewRegistryWithConfig

func NewRegistryWithConfig(config *RegistryConfig) Registry

NewRegistryWithConfig creates a new metrics registry with custom configuration

type RegistryConfig

type RegistryConfig struct {
	MaxMetrics      int           `yaml:"max_metrics" json:"max_metrics"`
	CleanupInterval time.Duration `yaml:"cleanup_interval" json:"cleanup_interval"`
	EnableIndexing  bool          `yaml:"enable_indexing" json:"enable_indexing"`
}

RegistryConfig contains configuration for the registry

func DefaultRegistryConfig

func DefaultRegistryConfig() *RegistryConfig

DefaultRegistryConfig returns default registry configuration

type StorageConfig

type StorageConfig = shared.MetricsStorageConfig[map[string]interface{}]

StorageConfig contains storage configuration

type TimerData

type TimerData struct {
	Name      string
	Durations []time.Duration
	Tags      map[string]string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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