summaryrefslogtreecommitdiff
path: root/internal/metrics/sample.go
blob: 04360f58ee26700400f2beac5316b890176ccd9d (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
package metrics

import "time"

// Sample represents a single metric sample with timestamp
type Sample struct {
	MetricName string
	Labels     map[string]string
	Value      float64
	Timestamp  time.Time
}

// NewSample creates a new Sample
func NewSample(name string, labels map[string]string, value float64, timestamp time.Time) Sample {
	if labels == nil {
		labels = make(map[string]string)
	}
	return Sample{
		MetricName: name,
		Labels:     labels,
		Value:      value,
		Timestamp:  timestamp,
	}
}

// Age returns how old the sample is
func (s Sample) Age() time.Duration {
	return time.Since(s.Timestamp)
}

// IsRecent returns true if the sample is recent enough for realtime ingestion
func (s Sample) IsRecent(threshold time.Duration) bool {
	return s.Age() < threshold
}