Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions internal/config/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ func (c *ClickhouseConfig) Options() (*clickhouse.Options, error) {

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

//nolint:unparam
Expand All @@ -74,8 +75,9 @@ func (a *AnalyticsConfig) setDefaults(v *viper.Viper) error {
"url": "",
},
"prometheus": map[string]any{
"enabled": "false",
"url": "",
"enabled": "false",
"scrapeIntervalSeconds": "15",
"url": "",
},
},
"buffer": map[string]any{
Expand Down
17 changes: 12 additions & 5 deletions internal/server/analytics/prometheus/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type prometheusClient interface {
}

type client struct {
promClient prometheusClient
logger *zap.Logger
promClient prometheusClient
minStepMinutes int
logger *zap.Logger
}

func New(logger *zap.Logger, cfg *config.Config) (*client, error) {
Expand All @@ -39,15 +40,21 @@ func New(logger *zap.Logger, cfg *config.Config) (*client, error) {
return nil, err
}
promClient := promapi.NewAPI(apiClient)
return &client{promClient: promClient, logger: logger}, nil
return &client{
promClient: promClient,
logger: logger,
minStepMinutes: cfg.Analytics.Storage.Prometheus.ScrapeIntervalSeconds * 4 / 60,
}, nil
}

func (c *client) GetFlagEvaluationsCount(ctx context.Context, req *panalytics.FlagEvaluationsCountRequest) ([]string, []float32, error) {
stepMinutes := max(req.StepMinutes, c.minStepMinutes)
query := fmt.Sprintf(
`sum(increase(flipt_evaluations_requests_total{namespace="%s", flag="%s"}[%dm])) or vector(0)`,
`sum(increase(flipt_evaluations_requests_total{namespace="%s", flag="%s"}[%dm]) / %d) or vector(0)`,
req.NamespaceKey,
req.FlagKey,
req.StepMinutes,
stepMinutes,
stepMinutes,
)
r := promapi.Range{
Start: req.From.UTC(),
Expand Down
Loading