@@ -12,8 +12,9 @@ import (
1212// Metrics contains Prometheus metrics for DA verification failures
1313type Metrics struct {
1414 // track ranges of unsubmitted blocks
15- UnsubmittedRangeStart * prometheus.GaugeVec
16- UnsubmittedRangeEnd * prometheus.GaugeVec
15+ UnsubmittedRangeStart * prometheus.GaugeVec
16+ UnsubmittedRangeEnd * prometheus.GaugeVec
17+ UnsubmittedBlocksTotal * prometheus.GaugeVec
1718
1819 mu sync.Mutex
1920 ranges map [string ][]* blockRange // key: blobType -> sorted slice of ranges
@@ -49,14 +50,35 @@ func NewWithRegistry(namespace string, registerer prometheus.Registerer) *Metric
4950 },
5051 []string {"chain_id" , "blob_type" , "range_id" },
5152 ),
53+ UnsubmittedBlocksTotal : factory .NewGaugeVec (
54+ prometheus.GaugeOpts {
55+ Namespace : namespace ,
56+ Name : "unsubmitted_blocks_total" ,
57+ Help : "total number of unsubmitted blocks" ,
58+ },
59+ []string {"chain_id" , "blob_type" },
60+ ),
5261 ranges : make (map [string ][]* blockRange ),
5362 }
5463}
5564
65+ // RecordTotalMissingBlocks updates the total count of missing blocks metric.
66+ func (m * Metrics ) RecordTotalMissingBlocks (chainID , blobType string ) {
67+ ranges := m .ranges [blobType ]
68+ total := uint64 (0 )
69+ for _ , r := range ranges {
70+ total += r .end - r .start + 1 // inclusive count
71+ }
72+ m .UnsubmittedBlocksTotal .WithLabelValues (chainID , blobType ).Set (float64 (total ))
73+ }
74+
5675// RecordMissingBlock records a block that is missing from Celestia
57- func (m * Metrics ) RecordMissingBlock (chain , blobType string , blockHeight uint64 ) {
76+ func (m * Metrics ) RecordMissingBlock (chainID , blobType string , blockHeight uint64 ) {
5877 m .mu .Lock ()
59- defer m .mu .Unlock ()
78+ defer func () {
79+ m .RecordTotalMissingBlocks (chainID , blobType )
80+ m .mu .Unlock ()
81+ }()
6082
6183 ranges := m .ranges [blobType ]
6284 if ranges == nil {
@@ -81,12 +103,12 @@ func (m *Metrics) RecordMissingBlock(chain, blobType string, blockHeight uint64)
81103 leftRange := ranges [idx - 1 ]
82104 rightRange := ranges [idx ]
83105
84- m .deleteRange (chain , blobType , leftRange )
85- m .deleteRange (chain , blobType , rightRange )
106+ m .deleteRange (chainID , blobType , leftRange )
107+ m .deleteRange (chainID , blobType , rightRange )
86108
87109 // extend left range to include right range
88110 leftRange .end = rightRange .end
89- m .updateRange (chain , blobType , leftRange )
111+ m .updateRange (chainID , blobType , leftRange )
90112
91113 // remove right range from slice
92114 m .ranges [blobType ] = append (ranges [:idx ], ranges [idx + 1 :]... )
@@ -96,18 +118,18 @@ func (m *Metrics) RecordMissingBlock(chain, blobType string, blockHeight uint64)
96118 if canMergeLeft {
97119 // extend left range
98120 leftRange := ranges [idx - 1 ]
99- m .deleteRange (chain , blobType , leftRange )
121+ m .deleteRange (chainID , blobType , leftRange )
100122 leftRange .end = blockHeight
101- m .updateRange (chain , blobType , leftRange )
123+ m .updateRange (chainID , blobType , leftRange )
102124 return
103125 }
104126
105127 if canMergeRight {
106128 // extend right range
107129 rightRange := ranges [idx ]
108- m .deleteRange (chain , blobType , rightRange )
130+ m .deleteRange (chainID , blobType , rightRange )
109131 rightRange .start = blockHeight
110- m .updateRange (chain , blobType , rightRange )
132+ m .updateRange (chainID , blobType , rightRange )
111133 return
112134 }
113135
@@ -118,14 +140,17 @@ func (m *Metrics) RecordMissingBlock(chain, blobType string, blockHeight uint64)
118140 }
119141 // insert at idx
120142 ranges = append (ranges [:idx ], append ([]* blockRange {newRange }, ranges [idx :]... )... )
121- m .updateRange (chain , blobType , newRange )
143+ m .updateRange (chainID , blobType , newRange )
122144 m .ranges [blobType ] = ranges
123145}
124146
125147// RemoveVerifiedBlock removes a block from the missing ranges when it gets verified
126- func (m * Metrics ) RemoveVerifiedBlock (chain , blobType string , blockHeight uint64 ) {
148+ func (m * Metrics ) RemoveVerifiedBlock (chainID , blobType string , blockHeight uint64 ) {
127149 m .mu .Lock ()
128- defer m .mu .Unlock ()
150+ defer func () {
151+ m .RecordTotalMissingBlocks (chainID , blobType )
152+ m .mu .Unlock ()
153+ }()
129154
130155 ranges := m .ranges [blobType ]
131156 if ranges == nil {
@@ -147,7 +172,7 @@ func (m *Metrics) RemoveVerifiedBlock(chain, blobType string, blockHeight uint64
147172
148173 // range contains only this block, delete it.
149174 if r .start == r .end {
150- m .deleteRange (chain , blobType , r )
175+ m .deleteRange (chainID , blobType , r )
151176 // remove range from slice
152177 m .ranges [blobType ] = append (ranges [:idx ], ranges [idx + 1 :]... )
153178 return
@@ -156,28 +181,28 @@ func (m *Metrics) RemoveVerifiedBlock(chain, blobType string, blockHeight uint64
156181 // block is at start of range, shrink the range
157182 if blockHeight == r .start {
158183 // remove from start of range
159- m .deleteRange (chain , blobType , r )
184+ m .deleteRange (chainID , blobType , r )
160185 r .start ++ // modify existing range
161- m .updateRange (chain , blobType , r )
186+ m .updateRange (chainID , blobType , r )
162187 return
163188 }
164189
165190 // block is at end of range, shrink the range
166191 if blockHeight == r .end {
167192 // remove from end of range
168- m .deleteRange (chain , blobType , r )
193+ m .deleteRange (chainID , blobType , r )
169194 r .end -- // modify existing range
170- m .updateRange (chain , blobType , r )
195+ m .updateRange (chainID , blobType , r )
171196 return
172197 }
173198
174199 // block is in middle of range, split into two ranges
175200 oldEnd := r .end
176- m .deleteRange (chain , blobType , r )
201+ m .deleteRange (chainID , blobType , r )
177202
178203 // update first range
179204 r .end = blockHeight - 1
180- m .updateRange (chain , blobType , r )
205+ m .updateRange (chainID , blobType , r )
181206
182207 // create new range for the second part
183208 newRange := & blockRange {
@@ -186,7 +211,7 @@ func (m *Metrics) RemoveVerifiedBlock(chain, blobType string, blockHeight uint64
186211 }
187212 // insert after current range
188213 ranges = append (ranges [:idx + 1 ], append ([]* blockRange {newRange }, ranges [idx + 1 :]... )... )
189- m .updateRange (chain , blobType , newRange )
214+ m .updateRange (chainID , blobType , newRange )
190215
191216 m .ranges [blobType ] = ranges
192217}
0 commit comments