This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
exporter.go
205 lines (180 loc) · 8.28 KB
/
exporter.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/util/validation/exporter.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package exporter
import (
"context"
"flag"
"net/http"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/services"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/grafana/phlare/pkg/util"
"github.com/grafana/phlare/pkg/validation"
)
// Config holds the configuration for an overrides-exporter
type Config struct {
Ring RingConfig `yaml:"ring"`
}
// RegisterFlags configs this instance to the given FlagSet
func (c *Config) RegisterFlags(f *flag.FlagSet, logger log.Logger) {
c.Ring.RegisterFlags(f, logger)
}
// Validate validates the configuration for an overrides-exporter.
func (c *Config) Validate() error {
return c.Ring.Validate()
}
// OverridesExporter exposes per-tenant resource limit overrides as Prometheus metrics
type OverridesExporter struct {
services.Service
defaultLimits *validation.Limits
tenantLimits validation.TenantLimits
overrideDescription *prometheus.Desc
defaultsDescription *prometheus.Desc
logger log.Logger
// OverridesExporter can optionally use a ring to uniquely shard tenants to
// instances and avoid export of duplicate metrics.
ring *overridesExporterRing
}
// NewOverridesExporter creates an OverridesExporter that reads updates to per-tenant
// limits using the provided function.
func NewOverridesExporter(
config Config,
defaultLimits *validation.Limits,
tenantLimits validation.TenantLimits,
log log.Logger,
registerer prometheus.Registerer,
) (*OverridesExporter, error) {
exporter := &OverridesExporter{
defaultLimits: defaultLimits,
tenantLimits: tenantLimits,
overrideDescription: prometheus.NewDesc(
"pyroscope_limits_overrides",
"Resource limit overrides applied to tenants",
[]string{"limit_name", "tenant"},
nil,
),
defaultsDescription: prometheus.NewDesc(
"pyroscope_limits_defaults",
"Resource limit defaults for tenants without overrides",
[]string{"limit_name"},
nil,
),
logger: log,
}
var err error
exporter.ring, err = newRing(config.Ring, log, registerer)
if err != nil {
return nil, errors.Wrap(err, "failed to create ring/lifecycler")
}
exporter.Service = services.NewBasicService(exporter.starting, exporter.running, exporter.stopping)
return exporter, nil
}
func (oe *OverridesExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- oe.defaultsDescription
ch <- oe.overrideDescription
}
func (oe *OverridesExporter) Collect(ch chan<- prometheus.Metric) {
if !oe.isLeader() {
// If another replica is the leader, don't expose any metrics from this one.
return
}
// Write path limits
ch <- prometheus.MustNewConstMetric(oe.defaultsDescription, prometheus.GaugeValue, oe.defaultLimits.IngestionRateMB, "ingestion_rate_mb")
ch <- prometheus.MustNewConstMetric(oe.defaultsDescription, prometheus.GaugeValue, oe.defaultLimits.IngestionBurstSizeMB, "ingestion_burst_size_mb")
ch <- prometheus.MustNewConstMetric(oe.defaultsDescription, prometheus.GaugeValue, float64(oe.defaultLimits.MaxGlobalSeriesPerTenant), "max_global_series_per_tenant")
ch <- prometheus.MustNewConstMetric(oe.defaultsDescription, prometheus.GaugeValue, float64(oe.defaultLimits.MaxLocalSeriesPerTenant), "max_series_per_tenant")
ch <- prometheus.MustNewConstMetric(oe.defaultsDescription, prometheus.GaugeValue, float64(oe.defaultLimits.MaxLabelNameLength), "max_label_name_length")
ch <- prometheus.MustNewConstMetric(oe.defaultsDescription, prometheus.GaugeValue, float64(oe.defaultLimits.MaxLabelValueLength), "max_label_value_length")
ch <- prometheus.MustNewConstMetric(oe.defaultsDescription, prometheus.GaugeValue, float64(oe.defaultLimits.MaxLabelNamesPerSeries), "max_label_names_per_series")
// Read path limits
ch <- prometheus.MustNewConstMetric(oe.defaultsDescription, prometheus.GaugeValue, float64(oe.defaultLimits.MaxQueryLookback), "max_query_lookback")
ch <- prometheus.MustNewConstMetric(oe.defaultsDescription, prometheus.GaugeValue, float64(oe.defaultLimits.MaxQueryLength), "max_query_length")
ch <- prometheus.MustNewConstMetric(oe.defaultsDescription, prometheus.GaugeValue, float64(oe.defaultLimits.MaxQueryParallelism), "max_query_parallelism")
ch <- prometheus.MustNewConstMetric(oe.defaultsDescription, prometheus.GaugeValue, float64(oe.defaultLimits.QuerySplitDuration), "split_queries_by_interval")
// Do not export per-tenant limits if they've not been configured at all.
if oe.tenantLimits == nil {
return
}
allLimits := oe.tenantLimits.AllByTenantID()
for tenant, limits := range allLimits {
// Write path limits
ch <- prometheus.MustNewConstMetric(oe.overrideDescription, prometheus.GaugeValue, limits.IngestionRateMB, "ingestion_rate_mb", tenant)
ch <- prometheus.MustNewConstMetric(oe.overrideDescription, prometheus.GaugeValue, limits.IngestionBurstSizeMB, "ingestion_burst_size_mb", tenant)
ch <- prometheus.MustNewConstMetric(oe.overrideDescription, prometheus.GaugeValue, float64(limits.MaxGlobalSeriesPerTenant), "max_global_series_per_tenant", tenant)
ch <- prometheus.MustNewConstMetric(oe.overrideDescription, prometheus.GaugeValue, float64(limits.MaxLocalSeriesPerTenant), "max_series_per_tenant", tenant)
ch <- prometheus.MustNewConstMetric(oe.overrideDescription, prometheus.GaugeValue, float64(limits.MaxLabelNameLength), "max_label_name_length", tenant)
ch <- prometheus.MustNewConstMetric(oe.overrideDescription, prometheus.GaugeValue, float64(limits.MaxLabelValueLength), "max_label_value_length", tenant)
ch <- prometheus.MustNewConstMetric(oe.overrideDescription, prometheus.GaugeValue, float64(limits.MaxLabelNamesPerSeries), "max_label_names_per_series", tenant)
// Read path limits
ch <- prometheus.MustNewConstMetric(oe.overrideDescription, prometheus.GaugeValue, float64(limits.MaxQueryLookback), "max_query_lookback", tenant)
ch <- prometheus.MustNewConstMetric(oe.overrideDescription, prometheus.GaugeValue, float64(limits.MaxQueryLength), "max_query_length", tenant)
ch <- prometheus.MustNewConstMetric(oe.overrideDescription, prometheus.GaugeValue, float64(limits.MaxQueryParallelism), "max_query_parallelism", tenant)
ch <- prometheus.MustNewConstMetric(oe.overrideDescription, prometheus.GaugeValue, float64(limits.QuerySplitDuration), "split_queries_by_interval", tenant)
}
}
// RingHandler is an http.Handler that serves requests for the overrides-exporter ring status page
func (oe *OverridesExporter) RingHandler(w http.ResponseWriter, req *http.Request) {
if oe.ring != nil {
oe.ring.lifecycler.ServeHTTP(w, req)
return
}
ringDisabledPage := `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Overrides-exporter Status</title>
</head>
<body>
<h1>Overrides-exporter Status</h1>
<p>Overrides-exporter hash ring is disabled.</p>
</body>
</html>`
util.WriteHTMLResponse(w, ringDisabledPage)
}
// isLeader determines whether this overrides-exporter instance is the leader
// replica that exports all limit metrics. If the ring is disabled, leadership is
// assumed. If the ring is enabled, it is used to determine which ring member is
// the leader replica.
func (oe *OverridesExporter) isLeader() bool {
if oe.ring == nil {
// If the ring is not enabled, export all metrics
return true
}
if oe.Service.State() != services.Running {
// We haven't finished startup yet, likely waiting for ring stability.
return false
}
isLeaderNow, err := oe.ring.isLeader()
if err != nil {
// If there was an error establishing ownership using the ring, log a warning and
// default to not exporting metrics to keep series churn low for transient ring
// issues.
level.Warn(oe.logger).Log("msg", "overrides-exporter failed to determine ring leader", "err", err.Error())
return false
}
return isLeaderNow
}
func (oe *OverridesExporter) starting(ctx context.Context) error {
if oe.ring == nil {
return nil
}
return oe.ring.starting(ctx)
}
func (oe *OverridesExporter) running(ctx context.Context) error {
if oe.ring == nil {
<-ctx.Done()
return nil
}
return oe.ring.running(ctx)
}
func (oe *OverridesExporter) stopping(err error) error {
if oe.ring == nil {
return nil
}
return oe.ring.stopping(err)
}