metrics

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package metrics provides interfaces for metrics collection and monitoring.

Package metrics provides basic monitoring and metrics collection for scanorama. It supports counters, gauges, and histograms with label support for tracking application performance and operational metrics.

Index

Constants

View Source
const (
	// Scan metrics.
	MetricScanDuration = "scan_duration_seconds"
	MetricScanTotal    = "scan_total"
	MetricScanErrors   = "scan_errors_total"
	MetricPortsScanned = "ports_scanned_total"
	MetricHostsScanned = "hosts_scanned_total"

	// Discovery metrics.
	MetricDiscoveryDuration = "discovery_duration_seconds"
	MetricDiscoveryTotal    = "discovery_total"
	MetricDiscoveryErrors   = "discovery_errors_total"
	MetricHostsDiscovered   = "hosts_discovered_total"

	// Database metrics.
	MetricDatabaseQueries     = "database_queries_total"
	MetricDatabaseErrors      = "database_errors_total"
	MetricDatabaseDuration    = "database_query_duration_seconds"
	MetricDatabaseConnections = "database_connections_active"

	// System metrics.
	MetricMemoryUsage = "memory_usage_bytes"
	MetricGoroutines  = "goroutines_active"
	MetricUptime      = "uptime_seconds"
)

Predefined metric names for common operations.

View Source
const (
	LabelScanType  = "scan_type"
	LabelTarget    = "target"
	LabelNetwork   = "network"
	LabelMethod    = "method"
	LabelStatus    = "status"
	LabelOperation = "operation"
	LabelError     = "error"
	LabelComponent = "component"
)

Common label keys.

Variables

This section is empty.

Functions

func Counter

func Counter(name string, labels Labels)

Counter increments a counter metric on the default registry.

func Gauge

func Gauge(name string, value float64, labels Labels)

Gauge sets a gauge metric on the default registry.

func GetMetrics

func GetMetrics() map[string]*Metric

GetMetrics returns all metrics from the default registry.

func Histogram

func Histogram(name string, value float64, labels Labels)

Histogram records a histogram value on the default registry.

func IncrementHostsDiscovered

func IncrementHostsDiscovered(network, method string, count int)

IncrementHostsDiscovered increments the hosts discovered counter.

func IncrementScanErrors

func IncrementScanErrors(scanType, target, errorType string)

IncrementScanErrors increments the scan error counter.

func IncrementScanTotal

func IncrementScanTotal(scanType, status string)

IncrementScanTotal increments the total scan counter.

func RecordDatabaseQuery

func RecordDatabaseQuery(operation string, duration time.Duration, success bool)

RecordDatabaseQuery records database query metrics.

func RecordDiscoveryDuration

func RecordDiscoveryDuration(network, method string, duration time.Duration)

RecordDiscoveryDuration records the duration of a discovery operation.

func RecordScanDuration

func RecordScanDuration(scanType, target string, duration time.Duration)

RecordScanDuration records the duration of a scan operation.

func Reset

func Reset()

Reset clears all metrics from the default registry.

func SetActiveConnections

func SetActiveConnections(count int)

SetActiveConnections sets the number of active database connections.

func SetDefault

func SetDefault(registry *Registry)

SetDefault sets the default metrics registry.

func SetEnabled

func SetEnabled(enabled bool)

SetEnabled enables or disables metrics collection on the default registry.

Types

type Labels

type Labels map[string]string

Labels represents key-value pairs for metric labels.

type Metric

type Metric struct {
	Name      string
	Type      MetricType
	Value     float64
	Labels    Labels
	Timestamp time.Time
}

Metric represents a single metric with its metadata.

type MetricType

type MetricType string

MetricType represents the type of metric.

const (
	TypeCounter   MetricType = "counter"
	TypeGauge     MetricType = "gauge"
	TypeHistogram MetricType = "histogram"
)

type MetricsRegistry

type MetricsRegistry interface {
	// SetEnabled enables or disables metrics collection.
	SetEnabled(enabled bool)

	// IsEnabled returns whether metrics collection is enabled.
	IsEnabled() bool

	// Counter increments a counter metric with the given name and labels.
	Counter(name string, labels Labels)

	// Gauge sets a gauge metric to the specified value with the given name and labels.
	Gauge(name string, value float64, labels Labels)

	// Histogram records a value in a histogram metric with the given name and labels.
	Histogram(name string, value float64, labels Labels)

	// GetMetrics returns a snapshot of all current metrics.
	GetMetrics() map[string]*Metric

	// Reset clears all metrics from the registry.
	Reset()
}

MetricsRegistry defines the interface for metrics collection and management. This interface allows for easy mocking and testing of metrics functionality.

type Registry

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

Registry holds all metrics and provides collection functionality.

func Default

func Default() *Registry

Default returns the default metrics registry.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new metrics registry.

func (*Registry) Counter

func (r *Registry) Counter(name string, labels Labels)

Counter increments a counter metric.

func (*Registry) Gauge

func (r *Registry) Gauge(name string, value float64, labels Labels)

Gauge sets a gauge metric value.

func (*Registry) GetMetrics

func (r *Registry) GetMetrics() map[string]*Metric

GetMetrics returns a snapshot of all current metrics.

func (*Registry) Histogram

func (r *Registry) Histogram(name string, value float64, labels Labels)

Histogram records a value in a histogram metric.

func (*Registry) IsEnabled

func (r *Registry) IsEnabled() bool

IsEnabled returns whether metrics collection is enabled.

func (*Registry) Reset

func (r *Registry) Reset()

Reset clears all metrics.

func (*Registry) SetEnabled

func (r *Registry) SetEnabled(enabled bool)

SetEnabled enables or disables metrics collection.

type Timer

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

Timer provides a simple way to measure execution time.

func NewTimer

func NewTimer(name string, labels Labels) *Timer

NewTimer creates a new timer for measuring execution time.

func (*Timer) Stop

func (t *Timer) Stop()

Stop stops the timer and records the duration as a histogram.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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