summaryrefslogtreecommitdiff
path: root/internal/metrics/generator.go
blob: c85906a0324aa507058f49f3ee501b7ac5458c87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package metrics

import (
	"math/rand"

	"github.com/prometheus/client_golang/prometheus"
)

const (
	minTemperature = 15.0
	maxTemperature = 35.0
	maxConnections = 100
	maxRequests    = 10
)

var (
	jobTypes = []string{"email", "report", "backup"}
	statuses = []string{"success", "failed"}
)

// Collectors holds Prometheus metric collectors for realtime mode
type Collectors struct {
	RequestsTotal      prometheus.Counter
	ActiveConnections  prometheus.Gauge
	TemperatureCelsius prometheus.Gauge
	RequestDuration    prometheus.Histogram
	JobsProcessed      *prometheus.CounterVec
}

// NewCollectors creates new Prometheus metric collectors for testing.
// All metrics are prefixed with "epimetheus_test_" to clearly indicate
// they are generated by the prometheus-pusher test/demo functionality.
func NewCollectors() Collectors {
	return Collectors{
		RequestsTotal: prometheus.NewCounter(
			prometheus.CounterOpts{
				Name: "epimetheus_test_requests_total",
				Help: "Total number of requests processed (test metric)",
			},
		),
		ActiveConnections: prometheus.NewGauge(
			prometheus.GaugeOpts{
				Name: "epimetheus_test_active_connections",
				Help: "Number of currently active connections (test metric)",
			},
		),
		TemperatureCelsius: prometheus.NewGauge(
			prometheus.GaugeOpts{
				Name: "epimetheus_test_temperature_celsius",
				Help: "Current temperature in Celsius (test metric)",
			},
		),
		RequestDuration: prometheus.NewHistogram(
			prometheus.HistogramOpts{
				Name:    "epimetheus_test_request_duration_seconds",
				Help:    "Histogram of request duration in seconds (test metric)",
				Buckets: prometheus.DefBuckets,
			},
		),
		JobsProcessed: prometheus.NewCounterVec(
			prometheus.CounterOpts{
				Name: "epimetheus_test_jobs_processed_total",
				Help: "Total number of jobs processed by type (test metric)",
			},
			[]string{"job_type", "status"},
		),
	}
}

// Simulate generates random metric values for the collectors
func (c Collectors) Simulate() {
	c.RequestsTotal.Add(float64(rand.Intn(maxRequests) + 1))
	c.ActiveConnections.Set(float64(rand.Intn(maxConnections)))
	c.TemperatureCelsius.Set(minTemperature + rand.Float64()*(maxTemperature-minTemperature))

	for i := 0; i < rand.Intn(5)+1; i++ {
		duration := rand.Float64() * 2
		c.RequestDuration.Observe(duration)
	}

	for _, jobType := range jobTypes {
		status := statuses[rand.Intn(len(statuses))]
		c.JobsProcessed.WithLabelValues(jobType, status).Add(float64(rand.Intn(5)))
	}
}