Skip to content

Commit c1d2780

Browse files
committed
feat(prometheus): Add minimum range interval based on scrape interval
Inspired by how Grafana does `$__rate_interval`, this adds the feature that the minimum range passed to Prometheus will be 4x the configured scrape interval. This allows users of 1 minutes scrape intervals to see metrics on the 30 minute and 1 hour time range, which is currently impossible. For details of Grafana's implementation: https://grafana.com/blog/2020/09/28/new-in-grafana-7.2-__rate_interval-for-prometheus-rate-queries-that-just-work/ Signed-off-by: Chris McDonnell <[email protected]>
1 parent 303c848 commit c1d2780

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

internal/config/analytics.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ func (c *ClickhouseConfig) Options() (*clickhouse.Options, error) {
6060

6161
// PrometheusConfig defines the connection details for connecting Flipt to Prometheus.
6262
type PrometheusConfig struct {
63-
Enabled bool `json:"enabled,omitempty" mapstructure:"enabled" yaml:"enabled,omitempty"`
64-
URL string `json:"-" mapstructure:"url" yaml:"-"`
65-
Headers map[string]string `json:"-" mapstructure:"headers" yaml:"-"`
63+
Enabled bool `json:"enabled,omitempty" mapstructure:"enabled" yaml:"enabled,omitempty"`
64+
ScrapeIntervalSeconds int `json:"scrapeIntervalSeconds,omitempty" mapstructure:"scrapeIntervalSeconds" yaml:"scrapeIntervalSeconds,omitempty"`
65+
URL string `json:"-" mapstructure:"url" yaml:"-"`
66+
Headers map[string]string `json:"-" mapstructure:"headers" yaml:"-"`
6667
}
6768

6869
//nolint:unparam
@@ -74,8 +75,9 @@ func (a *AnalyticsConfig) setDefaults(v *viper.Viper) error {
7475
"url": "",
7576
},
7677
"prometheus": map[string]any{
77-
"enabled": "false",
78-
"url": "",
78+
"enabled": "false",
79+
"scrapeIntervalSeconds": "15",
80+
"url": "",
7981
},
8082
},
8183
"buffer": map[string]any{

internal/server/analytics/prometheus/client.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ type prometheusClient interface {
2121
}
2222

2323
type client struct {
24-
promClient prometheusClient
25-
logger *zap.Logger
24+
promClient prometheusClient
25+
minStepMinutes int
26+
logger *zap.Logger
2627
}
2728

2829
func New(logger *zap.Logger, cfg *config.Config) (*client, error) {
@@ -39,15 +40,21 @@ func New(logger *zap.Logger, cfg *config.Config) (*client, error) {
3940
return nil, err
4041
}
4142
promClient := promapi.NewAPI(apiClient)
42-
return &client{promClient: promClient, logger: logger}, nil
43+
return &client{
44+
promClient: promClient,
45+
logger: logger,
46+
minStepMinutes: cfg.Analytics.Storage.Prometheus.ScrapeIntervalSeconds * 4 / 60,
47+
}, nil
4348
}
4449

4550
func (c *client) GetFlagEvaluationsCount(ctx context.Context, req *panalytics.FlagEvaluationsCountRequest) ([]string, []float32, error) {
51+
stepMinutes := max(req.StepMinutes, c.minStepMinutes)
4652
query := fmt.Sprintf(
47-
`sum(increase(flipt_evaluations_requests_total{namespace="%s", flag="%s"}[%dm])) or vector(0)`,
53+
`sum(increase(flipt_evaluations_requests_total{namespace="%s", flag="%s"}[%dm]) / %d) or vector(0)`,
4854
req.NamespaceKey,
4955
req.FlagKey,
50-
req.StepMinutes,
56+
stepMinutes,
57+
stepMinutes,
5158
)
5259
r := promapi.Range{
5360
Start: req.From.UTC(),

0 commit comments

Comments
 (0)