2019-03-25 10:54:22 +00:00
|
|
|
// Copyright 2019 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package refresh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-03 11:47:14 +00:00
|
|
|
"errors"
|
2024-09-10 01:41:53 +00:00
|
|
|
"log/slog"
|
2019-03-25 10:54:22 +00:00
|
|
|
"time"
|
|
|
|
|
2024-09-10 01:41:53 +00:00
|
|
|
"github.com/prometheus/common/promslog"
|
2019-03-25 10:54:22 +00:00
|
|
|
|
2023-10-23 13:55:36 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2019-03-25 10:54:22 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
|
|
|
)
|
|
|
|
|
2023-10-23 13:55:36 +00:00
|
|
|
type Options struct {
|
2024-09-10 01:41:53 +00:00
|
|
|
Logger *slog.Logger
|
2024-01-23 15:53:55 +00:00
|
|
|
Mech string
|
|
|
|
Interval time.Duration
|
|
|
|
RefreshF func(ctx context.Context) ([]*targetgroup.Group, error)
|
|
|
|
MetricsInstantiator discovery.RefreshMetricsInstantiator
|
2019-03-25 10:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Discovery implements the Discoverer interface.
|
|
|
|
type Discovery struct {
|
2024-09-10 01:41:53 +00:00
|
|
|
logger *slog.Logger
|
2019-03-25 10:54:22 +00:00
|
|
|
interval time.Duration
|
|
|
|
refreshf func(ctx context.Context) ([]*targetgroup.Group, error)
|
2024-01-23 15:53:55 +00:00
|
|
|
metrics *discovery.RefreshMetrics
|
2019-03-25 10:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDiscovery returns a Discoverer function that calls a refresh() function at every interval.
|
2023-10-23 13:55:36 +00:00
|
|
|
func NewDiscovery(opts Options) *Discovery {
|
2024-01-23 15:53:55 +00:00
|
|
|
m := opts.MetricsInstantiator.Instantiate(opts.Mech)
|
|
|
|
|
2024-09-10 01:41:53 +00:00
|
|
|
var logger *slog.Logger
|
2023-10-23 13:55:36 +00:00
|
|
|
if opts.Logger == nil {
|
2024-09-10 01:41:53 +00:00
|
|
|
logger = promslog.NewNopLogger()
|
2023-10-23 13:55:36 +00:00
|
|
|
} else {
|
|
|
|
logger = opts.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
d := Discovery{
|
|
|
|
logger: logger,
|
|
|
|
interval: opts.Interval,
|
|
|
|
refreshf: opts.RefreshF,
|
2024-01-23 15:53:55 +00:00
|
|
|
metrics: m,
|
2019-03-25 10:54:22 +00:00
|
|
|
}
|
2023-10-23 13:55:36 +00:00
|
|
|
|
|
|
|
return &d
|
2019-03-25 10:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the Discoverer interface.
|
|
|
|
func (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
|
|
|
// Get an initial set right away.
|
|
|
|
tgs, err := d.refresh(ctx)
|
|
|
|
if err != nil {
|
2022-06-03 11:47:14 +00:00
|
|
|
if !errors.Is(ctx.Err(), context.Canceled) {
|
2024-09-10 01:41:53 +00:00
|
|
|
d.logger.Error("Unable to refresh target groups", "err", err.Error())
|
2019-10-09 09:51:38 +00:00
|
|
|
}
|
2019-03-25 10:54:22 +00:00
|
|
|
} else {
|
|
|
|
select {
|
|
|
|
case ch <- tgs:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(d.interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
tgs, err := d.refresh(ctx)
|
|
|
|
if err != nil {
|
2022-06-03 11:47:14 +00:00
|
|
|
if !errors.Is(ctx.Err(), context.Canceled) {
|
2024-09-10 01:41:53 +00:00
|
|
|
d.logger.Error("Unable to refresh target groups", "err", err.Error())
|
2019-10-09 09:51:38 +00:00
|
|
|
}
|
2019-03-25 10:54:22 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ch <- tgs:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
|
|
|
|
now := time.Now()
|
2022-10-25 22:26:12 +00:00
|
|
|
defer func() {
|
2024-01-23 15:53:55 +00:00
|
|
|
d.metrics.Duration.Observe(time.Since(now).Seconds())
|
2022-10-25 22:26:12 +00:00
|
|
|
}()
|
|
|
|
|
2019-03-25 10:54:22 +00:00
|
|
|
tgs, err := d.refreshf(ctx)
|
|
|
|
if err != nil {
|
2024-01-23 15:53:55 +00:00
|
|
|
d.metrics.Failures.Inc()
|
2019-03-25 10:54:22 +00:00
|
|
|
}
|
|
|
|
return tgs, err
|
|
|
|
}
|