From efd161d60783ae50df5b91e9e22dddaa7480bd1c Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 19 Dec 2023 03:07:21 +0200 Subject: [PATCH] Expose WriteMetadataIfNeeded() function --- go_metrics.go | 8 ++++---- metrics.go | 9 ++++++--- set.go | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/go_metrics.go b/go_metrics.go index 15c5d34..d8b949d 100644 --- a/go_metrics.go +++ b/go_metrics.go @@ -82,7 +82,7 @@ func writeGoMetrics(w io.Writer) { } phis := []float64{0, 0.25, 0.5, 0.75, 1} quantiles := make([]float64, 0, len(phis)) - writeMetadataIfNeeded(w, "go_gc_duration_seconds", "summary") + WriteMetadataIfNeeded(w, "go_gc_duration_seconds", "summary") for i, q := range gcPauses.Quantiles(quantiles[:0], phis) { fmt.Fprintf(w, `go_gc_duration_seconds{quantile="%g"} %g`+"\n", phis[i], q) } @@ -97,10 +97,10 @@ func writeGoMetrics(w io.Writer) { WriteGaugeUint64(w, "go_threads", uint64(numThread)) // Export build details. - writeMetadataIfNeeded(w, "go_info", "gauge") + WriteMetadataIfNeeded(w, "go_info", "gauge") fmt.Fprintf(w, "go_info{version=%q} 1\n", runtime.Version()) - writeMetadataIfNeeded(w, "go_info_ext", "gauge") + WriteMetadataIfNeeded(w, "go_info_ext", "gauge") fmt.Fprintf(w, "go_info_ext{compiler=%q, GOARCH=%q, GOOS=%q, GOROOT=%q} 1\n", runtime.Compiler, runtime.GOARCH, runtime.GOOS, runtime.GOROOT()) } @@ -165,7 +165,7 @@ func writeRuntimeHistogramMetric(w io.Writer, name string, h *runtimemetrics.Flo totalCount := uint64(0) iNext := 0.0 - writeMetadataIfNeeded(w, name, "histogram") + WriteMetadataIfNeeded(w, name, "histogram") for i, count := range counts { totalCount += count if float64(i) >= iNext { diff --git a/metrics.go b/metrics.go index f103c61..c0efc58 100644 --- a/metrics.go +++ b/metrics.go @@ -290,16 +290,19 @@ func WriteCounterFloat64(w io.Writer, name string, value float64) { } func writeMetricUint64(w io.Writer, metricName, metricType string, value uint64) { - writeMetadataIfNeeded(w, metricName, metricType) + WriteMetadataIfNeeded(w, metricName, metricType) fmt.Fprintf(w, "%s %d\n", metricName, value) } func writeMetricFloat64(w io.Writer, metricName, metricType string, value float64) { - writeMetadataIfNeeded(w, metricName, metricType) + WriteMetadataIfNeeded(w, metricName, metricType) fmt.Fprintf(w, "%s %g\n", metricName, value) } -func writeMetadataIfNeeded(w io.Writer, metricName, metricType string) { +// WriteMetadataIfNeeded writes HELP and TYPE metadata for the given metricName and metricType if this is globally enabled via ExposeMetadata(). +// +// If the metadata exposition isn't enabled, then this function is no-op. +func WriteMetadataIfNeeded(w io.Writer, metricName, metricType string) { if !isMetadataEnabled() { return } diff --git a/set.go b/set.go index 3ad6073..9949b7c 100644 --- a/set.go +++ b/set.go @@ -53,7 +53,7 @@ func (s *Set) WritePrometheus(w io.Writer) { if metricFamily != prevMetricFamily { // write meta info only once per metric family metricType := nm.metric.metricType() - writeMetadataIfNeeded(&bb, nm.name, metricType) + WriteMetadataIfNeeded(&bb, nm.name, metricType) prevMetricFamily = metricFamily } // Call marshalTo without the global lock, since certain metric types such as Gauge