Skip to content

Commit

Permalink
feat(analytics): allow to set Authorization header for prometheus (#3656
Browse files Browse the repository at this point in the history
)
  • Loading branch information
erka authored Nov 26, 2024
1 parent 75ec5c4 commit a2d194a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/flipt.schema.cue
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ import "strings"
prometheus?: {
enabled?: bool | *false
url?: string | *""
headers?: [string]: string
}
}
buffer?: {
Expand Down
4 changes: 4 additions & 0 deletions config/flipt.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,10 @@
"url": {
"type": "string",
"default": ""
},
"headers": {
"type": ["object", "null"],
"additionalProperties": { "type": "string" }
}
},
"title": "Prometheus"
Expand Down
7 changes: 7 additions & 0 deletions examples/analytics/prometheus/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ services:
image: prom/prometheus:latest
volumes:
- "./prometheus.yml:/etc/prometheus/prometheus.yml"
- "./web-config.yml:/etc/prometheus/web-config.yml"
ports:
- "9090:9090"
command:
- --config.file=/etc/prometheus/prometheus.yml
- --web.config.file=/etc/prometheus/web-config.yml
flipt:
image: flipt/flipt:latest
volumes:
- "./flipt-config.yml:/etc/flipt/config/default.yml"
depends_on:
prometheus:
condition: service_healthy
Expand All @@ -21,6 +27,7 @@ services:
- FLIPT_LOG_LEVEL=info
- FLIPT_ANALYTICS_STORAGE_PROMETHEUS_ENABLED=true
- FLIPT_ANALYTICS_STORAGE_PROMETHEUS_URL=http://prometheus:9090
- PROMETHEUS_AUTH_TOKEN='Basic YWRtaW46dGVzdA=='
- FLIPT_META_TELEMETRY_ENABLED=false
command: ["/flipt", "--force-migrate"]

Expand Down
7 changes: 7 additions & 0 deletions examples/analytics/prometheus/flipt-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
analytics:
storage:
prometheus:
enabled: true
url: http://localhost:9090
headers:
Authorization: ${PROMETHEUS_AUTH_TOKEN}
3 changes: 3 additions & 0 deletions examples/analytics/prometheus/web-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
basic_auth_users:
admin: $2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay
# password is `test`
5 changes: 3 additions & 2 deletions internal/config/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ 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:"url,omitempty" mapstructure:"url" yaml:"url,omitempty"`
Enabled bool `json:"enabled,omitempty" mapstructure:"enabled" yaml:"enabled,omitempty"`
URL string `json:"-" mapstructure:"url" yaml:"-"`
Headers map[string]string `json:"-" mapstructure:"headers" yaml:"-"`
}

//nolint:unparam
Expand Down
13 changes: 13 additions & 0 deletions internal/server/analytics/prometheus/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math"
"net/http"
"time"

"github.com/prometheus/client_golang/api"
Expand All @@ -26,6 +27,12 @@ type client struct {
func New(logger *zap.Logger, cfg *config.Config) (*client, error) {
apiClient, err := api.NewClient(api.Config{
Address: cfg.Analytics.Storage.Prometheus.URL,
RoundTripper: roundTripFunc(func(r *http.Request) (*http.Response, error) {
for k, v := range cfg.Analytics.Storage.Prometheus.Headers {
r.Header.Set(k, v)
}
return api.DefaultRoundTripper.RoundTrip(r)
}),
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -76,3 +83,9 @@ func (c *client) GetFlagEvaluationsCount(ctx context.Context, req *panalytics.Fl
func (c *client) String() string {
return "prometheus"
}

type roundTripFunc func(r *http.Request) (*http.Response, error)

func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return f(r)
}

0 comments on commit a2d194a

Please sign in to comment.