Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/target-manager-registered-metric #243

Merged
merged 2 commits into from
Dec 25, 2024
Merged
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
3 changes: 2 additions & 1 deletion pkg/sparrow/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
"fmt"
"net/http"

"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/caas-team/sparrow/internal/logger"
"github.com/caas-team/sparrow/pkg/api"
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/yaml.v3"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/sparrow/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func New(cfg *config.Config) *Sparrow {
}

if cfg.HasTargetManager() {
gm := targets.NewManager(cfg.SparrowName, cfg.TargetManager)
gm := targets.NewManager(cfg.SparrowName, cfg.TargetManager, m)
sparrow.tarMan = gm
}
sparrow.loader = config.NewLoader(cfg, sparrow.cRuntime)
Expand Down
45 changes: 38 additions & 7 deletions pkg/sparrow/targets/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
"sync"
"time"

smetrics "github.com/caas-team/sparrow/pkg/sparrow/metrics"
"github.com/prometheus/client_golang/prometheus"

"github.com/caas-team/sparrow/internal/logger"
"github.com/caas-team/sparrow/pkg/checks"
"github.com/caas-team/sparrow/pkg/sparrow/targets/remote"
Expand All @@ -50,16 +53,40 @@ type manager struct {
cfg General
// interactor is the remote interactor used to interact with the remote state backend
interactor remote.Interactor
// metrics allows access to the central metrics provider
metrics metrics
// metricsProvider is the metrics provider used to register the metrics
metricsProvider smetrics.Provider
}

// metrics contains the prometheus metrics for the target manager
type metrics struct {
registered prometheus.Gauge
}

// newMetrics creates a new metrics struct
func newMetrics() metrics {
return metrics{
registered: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "sparrow_target_manager_registered",
Help: "Indicates whether the instance is registered as a global target",
}),
}
}

// NewManager creates a new target manager
func NewManager(name string, cfg TargetManagerConfig) TargetManager { //nolint:gocritic // no performance concerns yet
func NewManager(name string, cfg TargetManagerConfig, mp smetrics.Provider) TargetManager { //nolint:gocritic // no performance concerns yet
m := newMetrics()
mp.GetRegistry().MustRegister(m.registered)

return &manager{
name: name,
cfg: cfg.General,
mu: sync.RWMutex{},
done: make(chan struct{}, 1),
interactor: cfg.Type.Interactor(&cfg.Config),
name: name,
cfg: cfg.General,
mu: sync.RWMutex{},
done: make(chan struct{}, 1),
interactor: cfg.Type.Interactor(&cfg.Config),
metrics: m,
metricsProvider: mp,
}
}

Expand Down Expand Up @@ -141,6 +168,8 @@ func (t *manager) Shutdown(ctx context.Context) error {
return fmt.Errorf("failed to shutdown gracefully: %w", errors.Join(errC, err))
}
t.registered = false
t.metrics.registered.Set(0)
log.Info("Successfully unregistered as global target")
}

select {
Expand Down Expand Up @@ -178,8 +207,9 @@ func (t *manager) register(ctx context.Context) error {
log.Error("Failed to register global gitlabTargetManager", "error", err)
return err
}
log.Debug("Successfully registered")
log.Info("Successfully registered")
t.registered = true
t.metrics.registered.Set(1)

return nil
}
Expand Down Expand Up @@ -230,6 +260,7 @@ func (t *manager) refreshTargets(ctx context.Context) error {
if !t.registered && target.Url == fmt.Sprintf("%s://%s", t.cfg.Scheme, t.name) {
log.Debug("Found self as global target", "lastSeenMin", time.Since(target.LastSeen).Minutes())
t.registered = true
t.metrics.registered.Set(1)
}

if t.cfg.UnhealthyThreshold == 0 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/sparrow/targets/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func Test_gitlabTargetManager_refreshTargets(t *testing.T) {
interactor: remote,
name: "test",
cfg: General{UnhealthyThreshold: time.Hour, Scheme: "https"},
metrics: newMetrics(),
}
if err := gtm.refreshTargets(context.Background()); (err != nil) != (tt.wantErr != nil) {
t.Fatalf("refreshTargets() error = %v, wantErr %v", err, tt.wantErr)
Expand Down Expand Up @@ -193,6 +194,7 @@ func Test_gitlabTargetManager_refreshTargets_No_Threshold(t *testing.T) {
interactor: remote,
name: "test",
cfg: General{UnhealthyThreshold: 0, Scheme: "https"},
metrics: newMetrics(),
}
if err := gtm.refreshTargets(context.Background()); (err != nil) != (tt.wantErr != nil) {
t.Fatalf("refreshTargets() error = %v, wantErr %v", err, tt.wantErr)
Expand Down Expand Up @@ -304,6 +306,7 @@ func Test_gitlabTargetManager_register(t *testing.T) {
}
gtm := &manager{
interactor: glmock,
metrics: newMetrics(),
}
if err := gtm.register(context.Background()); (err != nil) != tt.wantErr {
t.Fatalf("register() error = %v, wantErr %v", err, tt.wantErr)
Expand Down Expand Up @@ -830,6 +833,7 @@ func mockGitlabTargetManager(g *remotemock.MockClient, name string) *manager { /
done: make(chan struct{}, 1),
interactor: g,
name: name,
metrics: newMetrics(),
cfg: General{
CheckInterval: 100 * time.Millisecond,
UnhealthyThreshold: 1 * time.Second,
Expand Down
Loading