From 35c98cdce999f035b29d0e195fb0ed842de8f68a Mon Sep 17 00:00:00 2001
From: Mark Holt <135143369+mh0lt@users.noreply.github.com>
Date: Thu, 7 Sep 2023 17:48:01 +0100
Subject: [PATCH] Added write of vm default set to metrics (#8151)

Metrics handler was missing erigon-lib metrics - which where added to
the VM default set

This fixes that whilst avoiding duplicates from erigon registrations
---
 metrics/prometheus.go | 3 +++
 metrics/register.go   | 5 +++++
 2 files changed, 8 insertions(+)

diff --git a/metrics/prometheus.go b/metrics/prometheus.go
index e21bf025f..98d6cf5c3 100644
--- a/metrics/prometheus.go
+++ b/metrics/prometheus.go
@@ -39,6 +39,9 @@ func Handler(reg Registry) http.Handler {
 		sort.Strings(names)
 
 		w.Header().Set("Access-Control-Allow-Origin", "*")
+
+		metrics2.WritePrometheus(w, false)
+
 		contentType := expfmt.Negotiate(r.Header)
 		enc := expfmt.NewEncoder(w, contentType)
 		mf, err := prometheus.DefaultGatherer.Gather()
diff --git a/metrics/register.go b/metrics/register.go
index 9920508bc..90564d47d 100644
--- a/metrics/register.go
+++ b/metrics/register.go
@@ -7,29 +7,34 @@ import (
 func GetOrCreateCounter(s string, isGauge ...bool) *metrics2.Counter {
 	counter := metrics2.GetOrCreateCounter(s, isGauge...)
 	DefaultRegistry.Register(s, counter)
+	metrics2.GetDefaultSet().UnregisterMetric(s)
 	return counter
 }
 
 func GetOrCreateGauge(s string, f func() float64) *metrics2.Gauge {
 	gauge := metrics2.GetOrCreateGauge(s, f)
 	DefaultRegistry.Register(s, gauge)
+	metrics2.GetDefaultSet().UnregisterMetric(s)
 	return gauge
 }
 
 func GetOrCreateFloatCounter(s string) *metrics2.FloatCounter {
 	floatCounter := metrics2.GetOrCreateFloatCounter(s)
 	DefaultRegistry.Register(s, floatCounter)
+	metrics2.GetDefaultSet().UnregisterMetric(s)
 	return floatCounter
 }
 
 func GetOrCreateSummary(s string) *metrics2.Summary {
 	summary := metrics2.GetOrCreateSummary(s)
 	DefaultRegistry.Register(s, summary)
+	metrics2.GetDefaultSet().UnregisterMetric(s)
 	return summary
 }
 
 func GetOrCreateHistogram(s string) *metrics2.Histogram {
 	histogram := metrics2.GetOrCreateHistogram(s)
 	DefaultRegistry.Register(s, histogram)
+	metrics2.GetDefaultSet().UnregisterMetric(s)
 	return histogram
 }