detached e7393e2

fix: initialize ManagedContainers metric with correct count on startup

SyncContainers now reports the delta between actual container count and
the UpDownCounter value, so the metric reflects reality after startup
and reconnect syncs. Deploy/Remove also track managedCount to keep the
counter consistent across the full lifecycle.
commit
e7393e2298fa57bd1ed4b05a07f22c92fd32cf27
author
brice · · 2026-02-10 08:50 UTC
parent
c00708a
signature
unsigned
1 file · +13 -0
M internal/usecase/container/service.go +13 -0
@@ -76,6 +76,7 @@ type Service struct {
7676 metrics *telemetry.Metrics
7777 containers map[string]*domain.Container
7878 attachments map[string][]string // ownerDomain → []containerIDs
79+ managedCount int64 // tracks UpDownCounter value for delta computation
7980 mu sync.RWMutex
8081 deployMu sync.Map // per-domain deploy locks (domain → *domainDeployLock)
8182 monitor *Monitor
@@ -456,6 +457,9 @@ func (s *Service) activateDeployedContainer(ctx context.Context, domainName stri
456457 s.mu.Lock()
457458 _, wasTracked := s.containers[domainName]
458459 s.containers[domainName] = container
460+ if !wasTracked {
461+ s.managedCount++
462+ }
459463 s.mu.Unlock()
460464
461465 // Track managed container count (only increment for new domains, not replacements)
@@ -679,6 +683,7 @@ func (s *Service) Remove(ctx context.Context, containerID string, force bool) er
679683 if c.ID == containerID {
680684 delete(s.containers, d)
681685 removedDomain = d
686+ s.managedCount--
682687 log.Info().Str("domain", d).Msg("container removed")
683688 break
684689 }
@@ -912,8 +917,16 @@ func (s *Service) SyncContainers(ctx context.Context) error {
912917
913918 s.mu.Lock()
914919 s.containers = managed
920+ newCount := int64(len(managed))
921+ delta := newCount - s.managedCount
922+ s.managedCount = newCount
915923 s.mu.Unlock()
916924
925+ // Report initial/delta managed container count to OTel UpDownCounter.
926+ if delta != 0 && s.metrics != nil {
927+ s.metrics.ManagedContainers.Add(ctx, delta)
928+ }
929+
917930 log.Info().Int(zerowrap.FieldCount, len(managed)).Msg("container state synchronized")
918931 return nil
919932 }
@@ -76,6 +76,7 @@ type Service struct {
76 metrics *telemetry.Metrics76 metrics *telemetry.Metrics
77 containers map[string]*domain.Container77 containers map[string]*domain.Container
78 attachments map[string][]string // ownerDomain → []containerIDs78 attachments map[string][]string // ownerDomain → []containerIDs
79+ managedCount int64 // tracks UpDownCounter value for delta computation
79 mu sync.RWMutex80 mu sync.RWMutex
80 deployMu sync.Map // per-domain deploy locks (domain → *domainDeployLock)81 deployMu sync.Map // per-domain deploy locks (domain → *domainDeployLock)
81 monitor *Monitor82 monitor *Monitor
@@ -456,6 +457,9 @@ func (s *Service) activateDeployedContainer(ctx context.Context, domainName stri
456 s.mu.Lock()457 s.mu.Lock()
457 _, wasTracked := s.containers[domainName]458 _, wasTracked := s.containers[domainName]
458 s.containers[domainName] = container459 s.containers[domainName] = container
460+ if !wasTracked {
461+ s.managedCount++
462+ }
459 s.mu.Unlock()463 s.mu.Unlock()
460 464
461 // Track managed container count (only increment for new domains, not replacements)465 // Track managed container count (only increment for new domains, not replacements)
@@ -679,6 +683,7 @@ func (s *Service) Remove(ctx context.Context, containerID string, force bool) er
679 if c.ID == containerID {683 if c.ID == containerID {
680 delete(s.containers, d)684 delete(s.containers, d)
681 removedDomain = d685 removedDomain = d
686+ s.managedCount--
682 log.Info().Str("domain", d).Msg("container removed")687 log.Info().Str("domain", d).Msg("container removed")
683 break688 break
684 }689 }
@@ -912,8 +917,16 @@ func (s *Service) SyncContainers(ctx context.Context) error {
912 917
913 s.mu.Lock()918 s.mu.Lock()
914 s.containers = managed919 s.containers = managed
920+ newCount := int64(len(managed))
921+ delta := newCount - s.managedCount
922+ s.managedCount = newCount
915 s.mu.Unlock()923 s.mu.Unlock()
916 924
925+ // Report initial/delta managed container count to OTel UpDownCounter.
926+ if delta != 0 && s.metrics != nil {
927+ s.metrics.ManagedContainers.Add(ctx, delta)
928+ }
929+
917 log.Info().Int(zerowrap.FieldCount, len(managed)).Msg("container state synchronized")930 log.Info().Int(zerowrap.FieldCount, len(managed)).Msg("container state synchronized")
918 return nil931 return nil
919 }932 }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close