metrics

package
v0.25.2 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package metrics provides interfaces for metrics collection and monitoring.

Package metrics now provides Prometheus-based metrics. The legacy in-memory registry is removed. This file retains minimal API types and no-op shims to keep existing call sites compiling while Prometheus is the source of truth.

Package metrics provides Prometheus-based metrics collection for scanorama. This replaces the custom metrics implementation with industry-standard Prometheus client library for proper observability and monitoring integration.

Index

Constants

View Source
const (
	MetricScanDuration        = "scan_duration_seconds"
	MetricScanTotal           = "scan_total"
	MetricScanErrors          = "scan_errors_total"
	MetricPortsScanned        = "ports_scanned_total"
	MetricHostsScanned        = "hosts_scanned_total"
	MetricDiscoveryDuration   = "discovery_duration_seconds"
	MetricDiscoveryTotal      = "discovery_total"
	MetricDiscoveryErrors     = "discovery_errors_total"
	MetricHostsDiscovered     = "hosts_discovered_total"
	MetricDatabaseQueries     = "database_queries_total"
	MetricDatabaseErrors      = "database_errors_total"
	MetricDatabaseDuration    = "database_query_duration_seconds"
	MetricDatabaseConnections = "database_connections_active"

	// System metrics (legacy names)
	MetricMemoryUsage = "memory_usage_bytes"
	MetricGoroutines  = "goroutines_active"
	MetricUptime      = "uptime_seconds"
)

Legacy metric name constants kept for compatibility. Prefer structured Prometheus metrics in prometheus.go.

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

	StatusSuccess = "success"
	StatusError   = "error"
)

Legacy label keys kept for compatibility.

Variables

This section is empty.

Functions

func Counter

func Counter(name string, labels Labels)

func Gauge

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

func GetMetrics

func GetMetrics() map[string]*Metric

func Histogram

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

func IncrementHostsDiscoveredPrometheus added in v0.12.0

func IncrementHostsDiscoveredPrometheus(method, network string, count int)

IncrementHostsDiscoveredPrometheus increments hosts discovered using global metrics

func IncrementScanErrorsPrometheus added in v0.12.0

func IncrementScanErrorsPrometheus(scanType, errorType string)

IncrementScanErrorsPrometheus increments scan errors using global metrics

func IncrementScanTotalPrometheus added in v0.12.0

func IncrementScanTotalPrometheus(scanType, status string)

IncrementScanTotalPrometheus increments scan total using global metrics

func IncrementScansRejectedPrometheus added in v0.13.0

func IncrementScansRejectedPrometheus(reason string)

IncrementScansRejectedPrometheus increments scans rejected using global metrics

func RecordDatabaseQueryPrometheus added in v0.12.0

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

RecordDatabaseQueryPrometheus records database query metrics using global metrics

func RecordDiscoveryDurationPrometheus added in v0.12.0

func RecordDiscoveryDurationPrometheus(method string, duration time.Duration)

RecordDiscoveryDurationPrometheus records discovery duration using global metrics

func RecordScanDurationPrometheus added in v0.12.0

func RecordScanDurationPrometheus(scanType string, duration time.Duration)

RecordScanDurationPrometheus records a scan duration using global metrics

func Reset

func Reset()

func SetActiveConnectionsPrometheus added in v0.12.0

func SetActiveConnectionsPrometheus(count int)

SetActiveConnectionsPrometheus sets active database connections using global metrics

func SetDefault

func SetDefault(registry *Registry)

func SetEnabled

func SetEnabled(enabled bool)

func SetScanQueueCapacityPrometheus added in v0.13.0

func SetScanQueueCapacityPrometheus(capacity float64)

SetScanQueueCapacityPrometheus sets scan queue capacity using global metrics

func SetScanQueueDepthPrometheus added in v0.13.0

func SetScanQueueDepthPrometheus(depth float64)

SetScanQueueDepthPrometheus sets scan queue depth using global metrics

Types

type Labels

type Labels map[string]string

type Metric

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

type MetricType

type MetricType string

MetricType and Metric kept for compatibility with existing interfaces/tests.

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 PrometheusMetrics added in v0.12.0

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

PrometheusMetrics holds all Prometheus metric collectors

func GetGlobalMetrics added in v0.12.0

func GetGlobalMetrics() *PrometheusMetrics

GetGlobalMetrics returns the global Prometheus metrics instance

func NewPrometheusMetrics added in v0.12.0

func NewPrometheusMetrics() *PrometheusMetrics

NewPrometheusMetrics creates a new Prometheus metrics instance with all collectors

func (*PrometheusMetrics) GetLastUpdate added in v0.12.0

func (pm *PrometheusMetrics) GetLastUpdate() time.Time

GetLastUpdate returns the last metrics update time

func (*PrometheusMetrics) GetRegistry added in v0.12.0

func (pm *PrometheusMetrics) GetRegistry() *prometheus.Registry

GetRegistry returns the Prometheus registry for HTTP handler

func (*PrometheusMetrics) GetUptime added in v0.12.0

func (pm *PrometheusMetrics) GetUptime() time.Duration

GetUptime returns the application uptime

func (*PrometheusMetrics) IncrementDatabaseErrors added in v0.12.0

func (pm *PrometheusMetrics) IncrementDatabaseErrors(operation, errorType string)

IncrementDatabaseErrors increments database error counter

func (*PrometheusMetrics) IncrementDatabaseQueries added in v0.12.0

func (pm *PrometheusMetrics) IncrementDatabaseQueries(operation, status string)

IncrementDatabaseQueries increments database query counter

func (*PrometheusMetrics) IncrementDiscoveryErrors added in v0.12.0

func (pm *PrometheusMetrics) IncrementDiscoveryErrors(method, errorType string)

IncrementDiscoveryErrors increments discovery error counter

func (*PrometheusMetrics) IncrementDiscoveryTotal added in v0.12.0

func (pm *PrometheusMetrics) IncrementDiscoveryTotal(method, status string)

IncrementDiscoveryTotal increments discovery counter

func (*PrometheusMetrics) IncrementHTTPErrors added in v0.12.0

func (pm *PrometheusMetrics) IncrementHTTPErrors(method, path, errorType string)

IncrementHTTPErrors increments HTTP error counter

func (*PrometheusMetrics) IncrementHTTPRequests added in v0.12.0

func (pm *PrometheusMetrics) IncrementHTTPRequests(method, path, status string)

IncrementHTTPRequests increments HTTP request counter

func (*PrometheusMetrics) IncrementHostsDiscovered added in v0.12.0

func (pm *PrometheusMetrics) IncrementHostsDiscovered(method, network string, count int)

IncrementHostsDiscovered increments hosts discovered counter

func (*PrometheusMetrics) IncrementHostsScanned added in v0.12.0

func (pm *PrometheusMetrics) IncrementHostsScanned(scanType, status string, count int)

IncrementHostsScanned increments hosts scanned counter

func (*PrometheusMetrics) IncrementPortsScanned added in v0.12.0

func (pm *PrometheusMetrics) IncrementPortsScanned(scanType, status string, count int)

IncrementPortsScanned increments ports scanned counter

func (*PrometheusMetrics) IncrementScanErrors added in v0.12.0

func (pm *PrometheusMetrics) IncrementScanErrors(scanType, errorType string)

IncrementScanErrors increments scan error counter

func (*PrometheusMetrics) IncrementScansRejected added in v0.13.0

func (pm *PrometheusMetrics) IncrementScansRejected(reason string)

IncrementScansRejected increments the count of rejected scans by reason

func (*PrometheusMetrics) IncrementScansTotal added in v0.12.0

func (pm *PrometheusMetrics) IncrementScansTotal(scanType, status string)

IncrementScansTotal increments the total scan counter

func (*PrometheusMetrics) RecordDatabaseQueryDuration added in v0.12.0

func (pm *PrometheusMetrics) RecordDatabaseQueryDuration(operation string, duration time.Duration)

RecordDatabaseQueryDuration records database query duration

func (*PrometheusMetrics) RecordDiscoveryDuration added in v0.12.0

func (pm *PrometheusMetrics) RecordDiscoveryDuration(method string, duration time.Duration)

RecordDiscoveryDuration records discovery duration

func (*PrometheusMetrics) RecordHTTPDuration added in v0.12.0

func (pm *PrometheusMetrics) RecordHTTPDuration(method, path string, duration time.Duration)

RecordHTTPDuration records HTTP request duration

func (*PrometheusMetrics) RecordScanDuration added in v0.12.0

func (pm *PrometheusMetrics) RecordScanDuration(scanType string, duration time.Duration)

RecordScanDuration records a scan duration

func (*PrometheusMetrics) SetActiveConnections added in v0.12.0

func (pm *PrometheusMetrics) SetActiveConnections(count int)

SetActiveConnections sets the number of active database connections

func (*PrometheusMetrics) SetActiveDiscovery added in v0.12.0

func (pm *PrometheusMetrics) SetActiveDiscovery(count int)

SetActiveDiscovery sets the number of active discovery operations

func (*PrometheusMetrics) SetActiveScans added in v0.12.0

func (pm *PrometheusMetrics) SetActiveScans(count int)

SetActiveScans sets the number of active scans

func (*PrometheusMetrics) SetCPUUsage added in v0.12.0

func (pm *PrometheusMetrics) SetCPUUsage(percent float64)

SetCPUUsage sets the CPU usage percentage

func (*PrometheusMetrics) SetScanQueueCapacity added in v0.13.0

func (pm *PrometheusMetrics) SetScanQueueCapacity(capacity float64)

SetScanQueueCapacity sets the maximum scan queue capacity

func (*PrometheusMetrics) SetScanQueueDepth added in v0.13.0

func (pm *PrometheusMetrics) SetScanQueueDepth(depth float64)

SetScanQueueDepth sets the current scan queue depth

func (*PrometheusMetrics) StartPeriodicUpdates added in v0.12.0

func (pm *PrometheusMetrics) StartPeriodicUpdates(ctx context.Context, interval time.Duration)

StartPeriodicUpdates starts a goroutine that periodically updates system metrics

func (*PrometheusMetrics) UpdateSystemMetrics added in v0.12.0

func (pm *PrometheusMetrics) UpdateSystemMetrics()

UpdateSystemMetrics updates all system metrics with current values

type Registry

type Registry struct{}

Registry is a no-op adapter kept for compatibility with existing call sites.

func Default

func Default() *Registry

func NewRegistry

func NewRegistry() *Registry

func (*Registry) Counter

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

func (*Registry) Gauge

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

func (*Registry) GetMetrics

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

func (*Registry) Histogram

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

func (*Registry) IsEnabled

func (r *Registry) IsEnabled() bool

func (*Registry) Reset

func (r *Registry) Reset()

func (*Registry) SetEnabled

func (r *Registry) SetEnabled(enabled bool)

type Timer

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

Timer provides a simple way to measure execution time (no-op record).

func NewTimer

func NewTimer(name string, labels Labels) *Timer

func (*Timer) Stop

func (t *Timer) Stop()

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