@@ -106,6 +106,23 @@ def inc_counter(
106106 """
107107 self .counter (name , label_names = label_names ).inc (labels = labels , amount = float (amount ))
108108
109+ def initialize_counter (
110+ self ,
111+ name : str ,
112+ * ,
113+ labels : Mapping [str , str ] | None = None ,
114+ label_names : Sequence [str ] = (),
115+ ) -> None :
116+ """
117+ Materialize a Counter series at zero without treating zero as an increment.
118+
119+ Args:
120+ name: Prometheus metric name.
121+ labels: Optional label dict. Keys must exactly match `label_names`.
122+ label_names: Ordered label key tuple for this metric name.
123+ """
124+ self .counter (name , label_names = label_names ).initialize (labels = labels )
125+
109126 def set_gauge (
110127 self ,
111128 name : str ,
@@ -376,6 +393,17 @@ def inc(self, *, labels: Mapping[str, str] | None, amount: float) -> None:
376393 return
377394 self ._values [key ] = self ._values .get (key , 0.0 ) + float (amount )
378395
396+ def initialize (self , * , labels : Mapping [str , str ] | None ) -> None :
397+ """Materialize one counter series at zero while preserving any existing value."""
398+ key = self ._normalize_and_validate (labels )
399+ with self ._lock :
400+ if key in self ._values :
401+ return
402+ if len (self ._values ) >= self ._max_series :
403+ self ._on_drop (self .name )
404+ return
405+ self ._values [key ] = 0.0
406+
379407 def copy_values (self ) -> dict [tuple [tuple [str , str ], ...], float ]:
380408 """Return a detached copy of all series values in this family."""
381409 with self ._lock :
@@ -620,6 +648,10 @@ def inc(self, amount: float = 1.0, *, labels: Mapping[str, str] | None = None) -
620648 """Increment one series in the bound counter family using the public wrapper API."""
621649 self ._family .inc (labels = labels , amount = amount )
622650
651+ def initialize (self , * , labels : Mapping [str , str ] | None = None ) -> None :
652+ """Materialize one series at zero without weakening positive-increment validation."""
653+ self ._family .initialize (labels = labels )
654+
623655
624656class _Gauge :
625657 """Public lightweight handle used by callers to mutate one gauge family."""
@@ -636,6 +668,7 @@ def inc(self, amount: float = 1.0, *, labels: Mapping[str, str] | None = None) -
636668 """Increase one series in the bound gauge family by a positive delta."""
637669 self ._family .add (labels = labels , delta = amount )
638670
671+
639672class _Histogram :
640673 """Public lightweight handle used by callers to mutate one histogram family."""
641674
0 commit comments