/
metrics-v3-handler.go
254 lines (221 loc) · 7.46 KB
/
metrics-v3-handler.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (c) 2015-2024 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program 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.
//
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"encoding/json"
"fmt"
"net/http"
"slices"
"strings"
"github.com/minio/minio/internal/logger"
"github.com/minio/minio/internal/mcontext"
"github.com/minio/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type promLogger struct{}
func (p promLogger) Println(v ...interface{}) {
s := make([]string, 0, len(v))
for _, val := range v {
s = append(s, fmt.Sprintf("%v", val))
}
err := fmt.Errorf("metrics handler error: %v", strings.Join(s, " "))
logger.LogIf(GlobalContext, err)
}
type metricsV3Server struct {
registry *prometheus.Registry
opts promhttp.HandlerOpts
authFn func(http.Handler) http.Handler
metricsData *metricsV3Collection
}
func newMetricsV3Server(authType prometheusAuthType) *metricsV3Server {
registry := prometheus.NewRegistry()
authFn := AuthMiddleware
if authType == prometheusPublic {
authFn = NoAuthMiddleware
}
metricGroups := newMetricGroups(registry)
return &metricsV3Server{
registry: registry,
opts: promhttp.HandlerOpts{
ErrorLog: promLogger{},
ErrorHandling: promhttp.HTTPErrorOnError,
Registry: registry,
MaxRequestsInFlight: 2,
},
authFn: authFn,
metricsData: metricGroups,
}
}
// metricDisplay - contains info on a metric for display purposes.
type metricDisplay struct {
Name string `json:"name"`
Help string `json:"help"`
Type string `json:"type"`
Labels []string `json:"labels"`
}
func (md metricDisplay) String() string {
return fmt.Sprintf("Name: %s\nType: %s\nHelp: %s\nLabels: {%s}\n", md.Name, md.Type, md.Help, strings.Join(md.Labels, ","))
}
func (md metricDisplay) TableRow() string {
labels := strings.Join(md.Labels, ",")
if labels == "" {
labels = ""
} else {
labels = "`" + labels + "`"
}
return fmt.Sprintf("| `%s` | `%s` | %s | %s |\n", md.Name, md.Type, md.Help, labels)
}
// listMetrics - returns a handler that lists all the metrics that could be
// returned for the requested path.
//
// FIXME: It currently only lists `minio_` prefixed metrics.
func (h *metricsV3Server) listMetrics(path string) http.Handler {
// First collect all matching MetricsGroup's
matchingMG := make(map[collectorPath]*MetricsGroup)
for _, collPath := range h.metricsData.collectorPaths {
if collPath.isDescendantOf(path) {
if v, ok := h.metricsData.mgMap[collPath]; ok {
matchingMG[collPath] = v
} else {
matchingMG[collPath] = h.metricsData.bucketMGMap[collPath]
}
}
}
if len(matchingMG) == 0 {
return nil
}
var metrics []metricDisplay
for _, collectorPath := range h.metricsData.collectorPaths {
if mg, ok := matchingMG[collectorPath]; ok {
var commonLabels []string
for k := range mg.ExtraLabels {
commonLabels = append(commonLabels, k)
}
for _, d := range mg.Descriptors {
labels := slices.Clone(d.VariableLabels)
labels = append(labels, commonLabels...)
metric := metricDisplay{
Name: mg.MetricFQN(d.Name),
Help: d.Help,
Type: d.Type.String(),
Labels: labels,
}
metrics = append(metrics, metric)
}
}
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
if contentType == "application/json" {
w.Header().Set("Content-Type", "application/json")
jsonEncoder := json.NewEncoder(w)
jsonEncoder.Encode(metrics)
return
}
// If not JSON, return plain text. We format it as a markdown table for
// readability.
w.Header().Set("Content-Type", "text/plain")
var b strings.Builder
b.WriteString("| Name | Type | Help | Labels |\n")
b.WriteString("| ---- | ---- | ---- | ------ |\n")
for _, metric := range metrics {
b.WriteString(metric.TableRow())
}
w.Write([]byte(b.String()))
})
}
func (h *metricsV3Server) handle(path string, isListingRequest bool, buckets []string) http.Handler {
var notFoundHandler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Metrics Resource Not found", http.StatusNotFound)
})
// Require that metrics path has at least component.
if path == "/" {
return notFoundHandler
}
if isListingRequest {
handler := h.listMetrics(path)
if handler == nil {
return notFoundHandler
}
return handler
}
// In each of the following cases, we check if the collect path is a
// descendant of `path`, and if so, we add the corresponding gatherer to
// the list of gatherers. This way, /api/a will return all metrics returned
// by /api/a/b and /api/a/c (and any other matching descendant collector
// paths).
var gatherers []prometheus.Gatherer
for _, collectorPath := range h.metricsData.collectorPaths {
if collectorPath.isDescendantOf(path) {
gatherer := h.metricsData.mgGatherers[collectorPath]
// For Bucket metrics we need to set the buckets argument inside the
// metric group, so that it will affect collection. If no buckets
// are provided, we will not return bucket metrics.
if bmg, ok := h.metricsData.bucketMGMap[collectorPath]; ok {
if len(buckets) == 0 {
continue
}
unLocker := bmg.LockAndSetBuckets(buckets)
defer unLocker()
}
gatherers = append(gatherers, gatherer)
}
}
if len(gatherers) == 0 {
return notFoundHandler
}
return promhttp.HandlerFor(prometheus.Gatherers(gatherers), h.opts)
}
// ServeHTTP - implements http.Handler interface.
//
// When the `list` query parameter is provided (its value is ignored), the
// server lists all metrics that could be returned for the requested path.
//
// The (repeatable) `buckets` query parameter is a list of bucket names (or it
// could be a comma separated value) to return metrics with a bucket label.
// Bucket metrics will be returned only for the provided buckets. If no buckets
// parameter is provided, no bucket metrics are returned.
func (h *metricsV3Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pathComponents := mux.Vars(r)["pathComps"]
isListingRequest := r.Form.Has("list")
// Parse optional buckets query parameter.
bucketsParam := r.Form["buckets"]
buckets := make([]string, 0, len(bucketsParam))
for _, bp := range bucketsParam {
bp = strings.TrimSpace(bp)
if bp == "" {
continue
}
splits := strings.Split(bp, ",")
for _, split := range splits {
buckets = append(buckets, strings.TrimSpace(split))
}
}
innerHandler := h.handle(pathComponents, isListingRequest, buckets)
// Add tracing to the prom. handler
tracedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tc, ok := r.Context().Value(mcontext.ContextTraceKey).(*mcontext.TraceCtxt)
if ok {
tc.FuncName = "handler.MetricsV3"
tc.ResponseRecorder.LogErrBody = true
}
innerHandler.ServeHTTP(w, r)
})
// Add authentication
h.authFn(tracedHandler).ServeHTTP(w, r)
}