Skip to content

Commit

Permalink
metrics: scrape kube-state-metrics if configured
Browse files Browse the repository at this point in the history
  • Loading branch information
apetruhin committed Sep 27, 2024
1 parent c7e9b7f commit c22497f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var (

ProfilesScrapeInterval = kingpin.Flag("profiles-scrape-interval", "").Envar("PROFILES_SCRAPE_INTERVAL").Default("60s").Duration()
ProfilesScrapeTimeout = kingpin.Flag("profiles-scrape-timeout", "").Envar("PROFILES_SCRAPE_TIMEOUT").Default("10s").Duration()

KubeStateMetricsAddress = kingpin.Flag("kube-state-metrics-address", "").Envar("KUBE_STATE_METRICS_ADDRESS").String()
)

func init() {
Expand Down
2 changes: 2 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Metrics struct {
endpoint *url.URL
apiKey string
listenAddr string
ksmAddr string
scrapeInterval time.Duration
scrapeTimeout time.Duration
walDir string
Expand All @@ -47,6 +48,7 @@ func NewMetrics() (*Metrics, error) {
endpoint: (*flags.CorootURL).JoinPath("/v1/metrics"),
apiKey: *flags.APIKey,
listenAddr: *flags.ListenAddress,
ksmAddr: *flags.KubeStateMetricsAddress,
scrapeInterval: *flags.MetricsScrapeInterval,
scrapeTimeout: *flags.MetricsScrapeTimeout,
walDir: *flags.MetricsWALDir,
Expand Down
21 changes: 14 additions & 7 deletions metrics/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,23 @@ func (ms *Metrics) runScraper() error {

tch := make(chan map[string][]*targetgroup.Group)
go scrapeManager.Run(tch)
targets := []model.LabelSet{
{
model.AddressLabel: model.LabelValue(ms.listenAddr),
model.InstanceLabel: model.LabelValue(ms.listenAddr),
},
}
if ms.ksmAddr != "" {
targets = append(targets, model.LabelSet{
model.AddressLabel: model.LabelValue(ms.ksmAddr),
model.InstanceLabel: model.LabelValue(ms.ksmAddr),
})
}
tch <- map[string][]*targetgroup.Group{
jobName: {
&targetgroup.Group{
Targets: []model.LabelSet{
{
model.AddressLabel: model.LabelValue(ms.listenAddr),
model.InstanceLabel: model.LabelValue(ms.listenAddr),
},
},
Labels: model.LabelSet{model.JobLabel: jobName},
Targets: targets,
Labels: model.LabelSet{model.JobLabel: jobName},
},
},
}
Expand Down

0 comments on commit c22497f

Please sign in to comment.