Expose WriteMetadataIfNeeded() function

This commit is contained in:
Aliaksandr Valialkin 2023-12-19 03:07:21 +02:00
parent 2d7f9a140e
commit efd161d607
No known key found for this signature in database
GPG Key ID: 52C003EE2BCDB9EB
3 changed files with 11 additions and 8 deletions

View File

@ -82,7 +82,7 @@ func writeGoMetrics(w io.Writer) {
} }
phis := []float64{0, 0.25, 0.5, 0.75, 1} phis := []float64{0, 0.25, 0.5, 0.75, 1}
quantiles := make([]float64, 0, len(phis)) 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) { for i, q := range gcPauses.Quantiles(quantiles[:0], phis) {
fmt.Fprintf(w, `go_gc_duration_seconds{quantile="%g"} %g`+"\n", phis[i], q) 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)) WriteGaugeUint64(w, "go_threads", uint64(numThread))
// Export build details. // Export build details.
writeMetadataIfNeeded(w, "go_info", "gauge") WriteMetadataIfNeeded(w, "go_info", "gauge")
fmt.Fprintf(w, "go_info{version=%q} 1\n", runtime.Version()) 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", fmt.Fprintf(w, "go_info_ext{compiler=%q, GOARCH=%q, GOOS=%q, GOROOT=%q} 1\n",
runtime.Compiler, runtime.GOARCH, runtime.GOOS, runtime.GOROOT()) 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) totalCount := uint64(0)
iNext := 0.0 iNext := 0.0
writeMetadataIfNeeded(w, name, "histogram") WriteMetadataIfNeeded(w, name, "histogram")
for i, count := range counts { for i, count := range counts {
totalCount += count totalCount += count
if float64(i) >= iNext { if float64(i) >= iNext {

View File

@ -290,16 +290,19 @@ func WriteCounterFloat64(w io.Writer, name string, value float64) {
} }
func writeMetricUint64(w io.Writer, metricName, metricType string, value uint64) { 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) fmt.Fprintf(w, "%s %d\n", metricName, value)
} }
func writeMetricFloat64(w io.Writer, metricName, metricType string, value float64) { 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) 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() { if !isMetadataEnabled() {
return return
} }

2
set.go
View File

@ -53,7 +53,7 @@ func (s *Set) WritePrometheus(w io.Writer) {
if metricFamily != prevMetricFamily { if metricFamily != prevMetricFamily {
// write meta info only once per metric family // write meta info only once per metric family
metricType := nm.metric.metricType() metricType := nm.metric.metricType()
writeMetadataIfNeeded(&bb, nm.name, metricType) WriteMetadataIfNeeded(&bb, nm.name, metricType)
prevMetricFamily = metricFamily prevMetricFamily = metricFamily
} }
// Call marshalTo without the global lock, since certain metric types such as Gauge // Call marshalTo without the global lock, since certain metric types such as Gauge