diff --git a/metrics.go b/metrics.go index 8ef1058..f103c61 100644 --- a/metrics.go +++ b/metrics.go @@ -28,14 +28,6 @@ type namedMetric struct { isAux bool } -func (nm *namedMetric) family() string { - n := strings.IndexByte(nm.name, '{') - if n < 0 { - return nm.name - } - return nm.name[:n] -} - type metric interface { marshalTo(prefix string, w io.Writer) metricType() string @@ -306,3 +298,20 @@ func writeMetricFloat64(w io.Writer, metricName, metricType string, value float6 writeMetadataIfNeeded(w, metricName, metricType) fmt.Fprintf(w, "%s %g\n", metricName, value) } + +func writeMetadataIfNeeded(w io.Writer, metricName, metricType string) { + if !isMetadataEnabled() { + return + } + metricFamily := getMetricFamily(metricName) + fmt.Fprintf(w, "# HELP %s\n", metricFamily) + fmt.Fprintf(w, "# TYPE %s %s\n", metricFamily, metricType) +} + +func getMetricFamily(metricName string) string { + n := strings.IndexByte(metricName, '{') + if n < 0 { + return metricName + } + return metricName[:n] +} diff --git a/set.go b/set.go index 338e2c0..3ad6073 100644 --- a/set.go +++ b/set.go @@ -49,11 +49,11 @@ func (s *Set) WritePrometheus(w io.Writer) { prevMetricFamily := "" for _, nm := range sa { - metricFamily := nm.family() + metricFamily := getMetricFamily(nm.name) if metricFamily != prevMetricFamily { // write meta info only once per metric family metricType := nm.metric.metricType() - writeMetadataIfNeeded(&bb, metricFamily, metricType) + writeMetadataIfNeeded(&bb, nm.name, metricType) prevMetricFamily = metricFamily } // Call marshalTo without the global lock, since certain metric types such as Gauge @@ -63,14 +63,6 @@ func (s *Set) WritePrometheus(w io.Writer) { w.Write(bb.Bytes()) } -func writeMetadataIfNeeded(w io.Writer, metricFamily, metricType string) { - if !isMetadataEnabled() { - return - } - fmt.Fprintf(w, "# HELP %s\n", metricFamily) - fmt.Fprintf(w, "# TYPE %s %s\n", metricFamily, metricType) -} - // NewHistogram creates and returns new histogram in s with the given name. // // name must be valid Prometheus-compatible metric with possible labels.