summaryrefslogtreecommitdiff
path: root/util/metrics/runtime.go
blob: 4624abb25040915aa0c431fb331780c5170ccdef (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package metrics

import (
	"runtime/metrics"
	"strconv"
	"strings"

	"github.com/algorand/go-deadlock"
)

// defaultRuntimeMetrics contains all the Go runtime metrics, except histograms.
var defaultRuntimeMetrics = []string{
	"/gc/cycles/automatic:gc-cycles",
	"/gc/cycles/forced:gc-cycles",
	"/gc/cycles/total:gc-cycles",
	"/gc/heap/allocs:bytes",
	"/gc/heap/allocs:objects",
	"/gc/heap/frees:bytes",
	"/gc/heap/frees:objects",
	"/gc/heap/goal:bytes",
	"/gc/heap/objects:objects",
	"/gc/heap/tiny/allocs:objects",
	"/memory/classes/heap/free:bytes",
	"/memory/classes/heap/objects:bytes",
	"/memory/classes/heap/released:bytes",
	"/memory/classes/heap/stacks:bytes",
	"/memory/classes/heap/unused:bytes",
	"/memory/classes/metadata/mcache/free:bytes",
	"/memory/classes/metadata/mcache/inuse:bytes",
	"/memory/classes/metadata/mspan/free:bytes",
	"/memory/classes/metadata/mspan/inuse:bytes",
	"/memory/classes/metadata/other:bytes",
	"/memory/classes/os-stacks:bytes",
	"/memory/classes/other:bytes",
	"/memory/classes/profiling/buckets:bytes",
	"/memory/classes/total:bytes",
	"/sched/goroutines:goroutines",
}

// RuntimeMetrics gathers selected metrics from Go's builtin runtime.metrics package
// and makes them available as Prometheus metrics.
type RuntimeMetrics struct {
	descriptions []metrics.Description
	samples      []metrics.Sample
	deadlock.Mutex
}

// NewRuntimeMetrics creates a RuntimeMetrics object, provided a list of metric names matching
// names in Go's metrics.All(). Otherwise, a default list of runtime metrics will be used.
func NewRuntimeMetrics(enabledMetrics ...string) *RuntimeMetrics {
	enabled := make(map[string]bool)
	if len(enabledMetrics) == 0 {
		enabledMetrics = defaultRuntimeMetrics
	}
	for _, name := range enabledMetrics {
		enabled[name] = true
	}

	// create []metrics.Sample and get metric descriptions
	rm := &RuntimeMetrics{}
	descs := metrics.All()
	for _, desc := range descs {
		if enabled[desc.Name] {
			rm.descriptions = append(rm.descriptions, desc)
			rm.samples = append(rm.samples, metrics.Sample{Name: desc.Name})
		}
	}

	return rm
}

// WriteMetric writes runtime metrics to the output stream in prometheus exposition format.
func (rm *RuntimeMetrics) WriteMetric(buf *strings.Builder, parentLabels string) {
	rm.Lock()
	defer rm.Unlock()

	metrics.Read(rm.samples)
	for i, s := range rm.samples {
		name := "algod_go" + sanitizePrometheusName(s.Name)
		desc := rm.descriptions[i]

		buf.WriteString("# HELP " + name + " " + desc.Description + "\n")
		if desc.Cumulative {
			buf.WriteString("# TYPE " + name + " counter\n")
		} else {
			buf.WriteString("# TYPE " + name + " gauge\n")
		}
		buf.WriteString(name)
		if len(parentLabels) > 0 {
			buf.WriteString("{" + parentLabels + "}")
		}
		buf.WriteRune(' ')
		switch s.Value.Kind() {
		case metrics.KindUint64:
			buf.WriteString(strconv.FormatUint(s.Value.Uint64(), 10))
		case metrics.KindFloat64:
			buf.WriteString(strconv.FormatFloat(s.Value.Float64(), 'f', -1, 64))
		default:
		}
		buf.WriteRune('\n')
	}
}

// AddMetric adds runtime metrics to the map used for heartbeat metrics.
func (rm *RuntimeMetrics) AddMetric(m map[string]float64) {
	rm.Lock()
	defer rm.Unlock()

	metrics.Read(rm.samples)
	for _, s := range rm.samples {
		name := "go" + sanitizeTelemetryName(s.Name)

		switch s.Value.Kind() {
		case metrics.KindUint64:
			m[name] = float64(s.Value.Uint64())
		case metrics.KindFloat64:
			m[name] = s.Value.Float64()
		default:
		}
	}
}