-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
68 lines (59 loc) · 1.59 KB
/
metrics.go
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
/*
Package metrics provides an extremely fast and lightweight API for
recording and exporting metrics in Prometheus format.
*/
package metrics
import (
"bytes"
"cmp"
)
// Metric is a single data point that can be written to the Prometheus
// text format.
type Metric interface {
marshalTo(w ExpfmtWriter, name MetricName)
}
// Collector is custom data collector that is called during [Set.WritePrometheus].
type Collector interface {
Collect(w ExpfmtWriter)
}
// namedMetric is a single data point.
type namedMetric struct {
// id is the unique hash to represent metric series.
// the hash is based on the family an tags
id metricHash
name MetricName
metric Metric
}
// NewMetricName creates a new [MetricName] with the given family and optional tags.
func NewMetricName(family string, tags ...string) MetricName {
return MetricName{
Family: MustIdent(family),
Tags: MustTags(tags...),
}
}
// MetricName represents a fully qualified name of a metric in pieces.
type MetricName struct {
// Family is the metric Ident, see [MustIdent].
Family Ident
// Tags are optional tags for the metric, see [MustTags].
Tags []Tag
}
// String returns the MetricName in fully qualified format. Prefer
// [ExpfmtWriter.WriteMetricName] over this when marshalling.
func (n MetricName) String() string {
if !n.hasTags() {
return n.Family.String()
}
var b bytes.Buffer
writeMetricName(&b, n, "")
return b.String()
}
func (n MetricName) hasTags() bool {
return len(n.Tags) > 0
}
func compareNamedMetrics(a, b *namedMetric) int {
return cmp.Compare(
a.name.Family.String(),
b.name.Family.String(),
)
}