@@ -163,6 +163,9 @@ func Run() error {
163163 if err := initStats (); err != nil {
164164 log .Printf ("initializing stats: %v" , err )
165165 }
166+ if err := initMetricsData (); err != nil {
167+ log .Printf ("initializing metrics data: %v" , err )
168+ }
166169
167170 // if link specified on command line, resolve and exit
168171 if flag .NArg () > 0 {
@@ -357,13 +360,26 @@ func newTemplate(files ...string) *template.Template {
357360 return template .Must (t .ParseFS (embeddedFS , tf ... ))
358361}
359362
360- // initMetrics initialize Prometheus Metrics
363+ // initMetrics initializes Prometheus Metrics
361364func initMetrics () {
362365 prometheus .MustRegister (clickCounter )
363366 prometheus .MustRegister (clickNotFound )
364367 prometheus .MustRegister (totalLinkCount )
365368}
366369
370+ // initMetricsData set metrics to what is represented in the DB
371+ func initMetricsData () error {
372+ // Set the totalLinkCount metric to what is saved in the DB
373+ var count float64
374+ err := db .db .QueryRow ("SELECT COUNT(DISTINCT id) FROM Links" ).Scan (& count )
375+ if err != nil {
376+ return err
377+ }
378+ totalLinkCount .Set (count )
379+
380+ return nil
381+ }
382+
367383// initStats initializes the in-memory stats counter with counts from db.
368384func initStats () error {
369385 stats .mu .Lock ()
@@ -377,14 +393,6 @@ func initStats() error {
377393 stats .clicks = clicks
378394 stats .dirty = make (ClickStats )
379395
380- // Set the totalLinkCount metric to what is saved in the DB
381- var count float64
382- err = db .db .QueryRow ("SELECT COUNT(DISTINCT id) FROM Links" ).Scan (& count )
383- if err != nil {
384- return err
385- }
386- totalLinkCount .Set (count )
387-
388396 return nil
389397}
390398
0 commit comments