metrics

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TestInject

func TestInject(reg *asm.Registry)

TestInject provides VictoriaMetrics writer and reader for test environments using a mock HTTP server

Types

type CPUUsage

type CPUUsage struct {
	Log    *slog.Logger
	Writer *VictoriaMetricsWriter `asm:"victoriametrics-writer"`
	Reader *VictoriaMetricsReader `asm:"victoriametrics-reader"`
	// contains filtered or unexported fields
}

func (*CPUUsage) CPUUsageDayAgo

func (m *CPUUsage) CPUUsageDayAgo(entity string, day units.Days) (float64, error)

Returns the cpu usage in cores for the given entity for a specific day in the past

func (*CPUUsage) CPUUsageLastHour

func (m *CPUUsage) CPUUsageLastHour(entity string) ([]UsageAtTime, error)

func (*CPUUsage) CPUUsageOver

func (m *CPUUsage) CPUUsageOver(entity string, interval string) (float64, error)

Returns the cpu usage in cores for the given entity over a given interval

func (*CPUUsage) CPUUsageOverDay

func (m *CPUUsage) CPUUsageOverDay(entity string) (float64, error)

func (*CPUUsage) CPUUsageOverLastHour

func (m *CPUUsage) CPUUsageOverLastHour(entity string) (float64, error)

func (*CPUUsage) CurrentCPUUsage

func (m *CPUUsage) CurrentCPUUsage(entity string) (float64, error)

Returns the cpu usage in cores for the given entity in the last minute

func (*CPUUsage) Populated

func (m *CPUUsage) Populated() error

func (*CPUUsage) RecordUsage

func (m *CPUUsage) RecordUsage(ctx context.Context, entity string, windowStart, windowEnd time.Time, cpuUsec units.Microseconds, attrs map[string]string) error

func (*CPUUsage) Setup

func (m *CPUUsage) Setup() error

type Data

type Data struct {
	ResultType string   `json:"resultType"`
	Result     []Result `json:"result"`
}

type ErrorBreakdown

type ErrorBreakdown struct {
	StatusCode int
	Count      int64
	Percentage float64
}

ErrorBreakdown represents error counts by status code

type HTTPMetrics

type HTTPMetrics struct {
	Log    *slog.Logger
	Writer *VictoriaMetricsWriter `asm:"victoriametrics-writer,optional"`
	Reader *VictoriaMetricsReader `asm:"victoriametrics-reader,optional"`
	// contains filtered or unexported fields
}

HTTPMetrics tracks HTTP request metrics for applications using VictoriaMetrics

func (*HTTPMetrics) Close

func (h *HTTPMetrics) Close() error

Close is a no-op for VictoriaMetrics (writer handles its own lifecycle)

func (*HTTPMetrics) ErrorsLastHour

func (h *HTTPMetrics) ErrorsLastHour(app string) ([]ErrorBreakdown, error)

ErrorsLastHour returns breakdown of errors by status code for the last hour

func (*HTTPMetrics) Populated

func (h *HTTPMetrics) Populated() error

func (*HTTPMetrics) RPSLastMinute

func (h *HTTPMetrics) RPSLastMinute(app string) (float64, error)

RPSLastMinute returns requests per second for the last minute

func (*HTTPMetrics) RecordRequest

func (h *HTTPMetrics) RecordRequest(ctx context.Context, req HTTPRequest) error

RecordRequest records an HTTP request as metrics in VictoriaMetrics

func (*HTTPMetrics) Setup

func (h *HTTPMetrics) Setup() error

func (*HTTPMetrics) StatsLastHour

func (h *HTTPMetrics) StatsLastHour(app string) ([]RequestStats, error)

StatsLastHour returns request statistics for the last hour in 1-minute buckets

func (*HTTPMetrics) TopPaths

func (h *HTTPMetrics) TopPaths(app string, limit int) ([]PathStats, error)

TopPaths returns the most frequently accessed paths for an app

type HTTPRequest

type HTTPRequest struct {
	Timestamp    time.Time
	App          string
	Method       string
	Path         string
	StatusCode   int
	DurationMs   int64
	ResponseSize int64
}

HTTPRequest represents a single HTTP request for metrics

type MemoryUsage

type MemoryUsage struct {
	Log    *slog.Logger
	Writer *VictoriaMetricsWriter `asm:"victoriametrics-writer,optional"`
	Reader *VictoriaMetricsReader `asm:"victoriametrics-reader,optional"`
	// contains filtered or unexported fields
}

func (*MemoryUsage) Populated

func (m *MemoryUsage) Populated() error

func (*MemoryUsage) RecordUsage

func (m *MemoryUsage) RecordUsage(
	ctx context.Context,
	entity string,
	ts time.Time,
	memory units.Bytes,
	attrs map[string]string,
) error

func (*MemoryUsage) Setup

func (m *MemoryUsage) Setup() error

func (*MemoryUsage) UsageLastHour

func (m *MemoryUsage) UsageLastHour(entity string) ([]MemoryUsageAtTime, error)

type MemoryUsageAtTime

type MemoryUsageAtTime struct {
	Timestamp time.Time
	Memory    units.Bytes
}

type MetricPoint

type MetricPoint struct {
	Name      string
	Labels    map[string]string
	Value     float64
	Timestamp time.Time
}

MetricPoint represents a single metric data point

type PathStats

type PathStats struct {
	Path          string
	Count         int64
	AvgDurationMs float64
	ErrorRate     float64
}

PathStats represents statistics for a specific path

type QueryResult

type QueryResult struct {
	Status string `json:"status"`
	Data   Data   `json:"data"`
}

QueryResult represents the result of a MetricsQL query

type RequestStats

type RequestStats struct {
	Time          time.Time
	Count         int64
	AvgDurationMs float64
	P95DurationMs float64
	P99DurationMs float64
	ErrorRate     float64
}

RequestStats represents aggregated request statistics

type Result

type Result struct {
	Metric map[string]string `json:"metric"`
	Value  []interface{}     `json:"value,omitempty"`  // For instant queries: [timestamp, value_string]
	Values [][]interface{}   `json:"values,omitempty"` // For range queries: [[timestamp, value_string], ...]
}

type TimeSeriesPoint

type TimeSeriesPoint struct {
	Timestamp time.Time
	Value     float64
}

TimeSeriesPoint represents a single point in a time series

type UsageAtTime

type UsageAtTime struct {
	Timestamp time.Time
	Cores     float64
}

type VictoriaMetricsReader

type VictoriaMetricsReader struct {
	Log     *slog.Logger
	Address string
	Timeout time.Duration
	// contains filtered or unexported fields
}

VictoriaMetricsReader reads metrics from VictoriaMetrics using MetricsQL

func NewVictoriaMetricsReader

func NewVictoriaMetricsReader(log *slog.Logger, address string, timeout time.Duration) *VictoriaMetricsReader

NewVictoriaMetricsReader creates a new VictoriaMetrics reader

func (*VictoriaMetricsReader) GetAverage

func (r *VictoriaMetricsReader) GetAverage(ctx context.Context, metricName string, labels map[string]string, window string) (float64, error)

GetAverage calculates the average value over time

func (*VictoriaMetricsReader) GetLatestValue

func (r *VictoriaMetricsReader) GetLatestValue(ctx context.Context, metricName string, labels map[string]string) (float64, error)

GetLatestValue retrieves the latest value for a metric

func (*VictoriaMetricsReader) GetQuantile

func (r *VictoriaMetricsReader) GetQuantile(ctx context.Context, quantile float64, metricName string, labels map[string]string, window string) (float64, error)

GetQuantile calculates a quantile over time

func (*VictoriaMetricsReader) GetRate

func (r *VictoriaMetricsReader) GetRate(ctx context.Context, metricName string, labels map[string]string, window string) (float64, error)

GetRate calculates the rate of a counter metric over a time range

func (*VictoriaMetricsReader) GetTimeSeries

func (r *VictoriaMetricsReader) GetTimeSeries(ctx context.Context, metricName string, labels map[string]string, start, end time.Time, step string) ([]TimeSeriesPoint, error)

GetTimeSeries retrieves time series data for a metric over a time range

func (*VictoriaMetricsReader) InstantQuery

func (r *VictoriaMetricsReader) InstantQuery(ctx context.Context, query string, ts time.Time) (*QueryResult, error)

InstantQuery executes an instant MetricsQL query (point-in-time)

func (*VictoriaMetricsReader) RangeQuery

func (r *VictoriaMetricsReader) RangeQuery(ctx context.Context, query string, start, end time.Time, step string) (*QueryResult, error)

RangeQuery executes a range MetricsQL query (time series)

type VictoriaMetricsWriter

type VictoriaMetricsWriter struct {
	Log     *slog.Logger
	Address string // e.g., "localhost:8428"
	Timeout time.Duration
	// contains filtered or unexported fields
}

VictoriaMetricsWriter writes metrics to VictoriaMetrics using the import API

func NewVictoriaMetricsWriter

func NewVictoriaMetricsWriter(log *slog.Logger, address string, timeout time.Duration) *VictoriaMetricsWriter

NewVictoriaMetricsWriter creates a new VictoriaMetrics writer

func (*VictoriaMetricsWriter) Close

func (w *VictoriaMetricsWriter) Close() error

Close stops the background flush routine and flushes remaining data

func (*VictoriaMetricsWriter) Flush

func (w *VictoriaMetricsWriter) Flush()

Flush manually triggers a flush of buffered metrics

func (*VictoriaMetricsWriter) Start

func (w *VictoriaMetricsWriter) Start()

Start begins the background flush routine

func (*VictoriaMetricsWriter) WritePoint

func (w *VictoriaMetricsWriter) WritePoint(ctx context.Context, point MetricPoint) error

WritePoint adds a metric point to the buffer

func (*VictoriaMetricsWriter) WritePoints

func (w *VictoriaMetricsWriter) WritePoints(ctx context.Context, points []MetricPoint) error

WritePoints adds multiple metric points to the buffer

Jump to

Keyboard shortcuts

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