Skip to content

Commit

Permalink
Fix exporter's observe run (#288)
Browse files Browse the repository at this point in the history
Some time after the start exporter stopped to collect metrics because of
timer leak

Signed-off-by: Igor Shishkin <[email protected]>
  • Loading branch information
teran authored Dec 15, 2024
1 parent 929278c commit ce514ea
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions exporter/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package service
import (
"context"
"strconv"
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"

"github.com/teran/archived/repositories/metadata"
)

Expand All @@ -22,13 +25,23 @@ type service struct {
blobsSize *prometheus.GaugeVec
blobsTotalRawSize *prometheus.GaugeVec

repo metadata.Repository
repo metadata.Repository
mutex *sync.Mutex
}

func New(repo metadata.Repository) (Service, error) {
svc := &service{
repo: repo,

namespacesTotal: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "archived",
Name: "namespaces_amount",
Help: "Total amount of namespaces",
},
[]string{},
),

containersTotal: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "archived",
Expand Down Expand Up @@ -80,9 +93,12 @@ func New(repo metadata.Repository) (Service, error) {
Help: "Total effective size of blobs (i.e. after deduplication)",
}, []string{},
),

mutex: &sync.Mutex{},
}

for _, m := range []*prometheus.GaugeVec{
svc.namespacesTotal,
svc.containersTotal,
svc.versionsTotal,
svc.objectsTotal,
Expand All @@ -99,11 +115,18 @@ func New(repo metadata.Repository) (Service, error) {
}

func (s *service) observe(ctx context.Context) error {
log.Trace("running observe() to gather metrics ...")

stats, err := s.repo.CountStats(ctx)
if err != nil {
return err
}

log.WithFields(log.Fields{
"namespaces": stats.NamespacesCount,
"containers": stats.ContainersCount,
}).Trace("publishing metrics ...")

s.namespacesTotal.WithLabelValues().Set(float64(stats.NamespacesCount))
s.containersTotal.WithLabelValues().Set(float64(stats.ContainersCount))

Expand Down Expand Up @@ -133,14 +156,24 @@ func (s *service) observe(ctx context.Context) error {
}

func (s *service) Run(ctx context.Context) error {
ticker := time.NewTicker(60 * time.Second)
for {
select {
case <-ctx.Done():
ticker.Stop()
return ctx.Err()
case <-time.After(30 * time.Second):
if err := s.observe(ctx); err != nil {
return err
}
case <-ticker.C:
go func() {
if !s.mutex.TryLock() {
log.Warn("lock is already taken. Skipping the run ...")
return
}
defer s.mutex.Unlock()

if err := s.observe(ctx); err != nil {
log.Warnf("error running observe(): %s", err)
}
}()
}
}
}

0 comments on commit ce514ea

Please sign in to comment.